Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / build / scripts / make_css_tokenizer_codepoints.py
blobd7e81d50e0128e8238a9878ebc50893fb9ecb430
1 #!/usr/bin/env python
3 # Copyright 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 import in_generator
8 import sys
9 import os
11 module_basename = os.path.basename(__file__)
12 module_pyname = os.path.splitext(module_basename)[0] + '.py'
14 CPP_TEMPLATE = """
15 // Copyright 2014 The Chromium Authors. All rights reserved.
16 // Use of this source code is governed by a BSD-style license that can be
17 // found in the LICENSE file.
19 // Auto-generated by {module_pyname}
21 const CSSTokenizer::CodePoint CSSTokenizer::codePoints[{array_size}] = {{
22 {token_lines}
23 }};
24 const unsigned codePointsNumber = {array_size};
25 """
28 def token_type(i):
29 codepoints = {'(': 'leftParenthesis',
30 ')': 'rightParenthesis',
31 '[': 'leftBracket',
32 ']': 'rightBracket',
33 '{': 'leftBrace',
34 '}': 'rightBrace',
35 '+': 'plusOrFullStop',
36 '.': 'plusOrFullStop',
37 '-': 'hyphenMinus',
38 '*': 'asterisk',
39 '<': 'lessThan',
40 ',': 'comma',
41 '/': 'solidus',
42 '\\': 'reverseSolidus',
43 ':': 'colon',
44 ';': 'semiColon',
45 '#': 'hash',
46 '^': 'circumflexAccent',
47 '$': 'dollarSign',
48 '|': 'verticalLine',
49 '~': 'tilde',
50 '@': 'commercialAt',
51 'u': 'letterU',
52 'U': 'letterU',
54 c = chr(i)
55 if c in codepoints:
56 return codepoints[c]
57 whitespace = '\n\r\t\f '
58 quotes = '"\''
59 if c in whitespace:
60 return 'whiteSpace'
61 if c.isdigit():
62 return 'asciiDigit'
63 if c.isalpha() or c == '_':
64 return 'nameStart'
65 if c in quotes:
66 return 'stringStart'
67 if i == 0:
68 return 'endOfFile'
71 class MakeCSSTokenizerCodePointsWriter(in_generator.Writer):
72 def __init__(self, in_file_path):
73 super(MakeCSSTokenizerCodePointsWriter, self).__init__(in_file_path)
75 self._outputs = {
76 ('CSSTokenizerCodepoints.cpp'): self.generate,
79 def generate(self):
80 array_size = 128 # SCHAR_MAX + 1
81 token_lines = [' &CSSTokenizer::%s,' % token_type(i)
82 if token_type(i) else ' 0,'
83 for i in range(array_size)]
84 return CPP_TEMPLATE.format(array_size=array_size, token_lines='\n'.join(token_lines), module_pyname=module_pyname)
86 if __name__ == '__main__':
87 in_generator.Maker(MakeCSSTokenizerCodePointsWriter).main(sys.argv)