Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/env python
      2 
      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.
      6 
      7 import in_generator
      8 import sys
      9 import os
     10 
     11 module_basename = os.path.basename(__file__)
     12 module_pyname = os.path.splitext(module_basename)[0] + '.py'
     13 
     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.
     18 
     19 // Auto-generated by {module_pyname}
     20 
     21 const MediaQueryTokenizer::CodePoint MediaQueryTokenizer::codePoints[{array_size}] = {{
     22 {token_lines}
     23 }};
     24 const unsigned codePointsNumber = {array_size};
     25 """
     26 
     27 
     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                   ',': 'comma',
     40                   '/': 'solidus',
     41                   '\\': 'reverseSolidus',
     42                   ':': 'colon',
     43                   ';': 'semiColon',
     44                   }
     45     whitespace = '\n\r\t\f '
     46     quotes = '"\''
     47     c = chr(i)
     48     if c in whitespace:
     49         return 'whiteSpace'
     50     if c.isdigit():
     51         return 'asciiDigit'
     52     if c.isalpha() or c == '_':
     53         return 'nameStart'
     54     if c in quotes:
     55         return 'stringStart'
     56     if i == 0:
     57         return 'endOfFile'
     58     return codepoints.get(c)
     59 
     60 
     61 class MakeMediaQueryTokenizerCodePointsWriter(in_generator.Writer):
     62     defaults = {
     63         'Conditional': None,
     64         'RuntimeEnabled': None,
     65         'ImplementedAs': None,
     66     }
     67     filters = {
     68     }
     69     default_parameters = {
     70         'namespace': '',
     71         'export': '',
     72     }
     73 
     74     def __init__(self, in_file_path):
     75         super(MakeMediaQueryTokenizerCodePointsWriter, self).__init__(in_file_path)
     76 
     77         self._outputs = {
     78             ('MediaQueryTokenizerCodepoints.cpp'): self.generate,
     79         }
     80         self._template_context = {
     81             'namespace': '',
     82             'export': '',
     83         }
     84 
     85     def generate(self):
     86         array_size = 128  # SCHAR_MAX + 1
     87         token_lines = ['    &MediaQueryTokenizer::%s,' % token_type(i)
     88                         if token_type(i) else '    0,'
     89                         for i in range(array_size)]
     90         return CPP_TEMPLATE.format(array_size=array_size, token_lines='\n'.join(token_lines), module_pyname=module_pyname)
     91 
     92 if __name__ == '__main__':
     93     in_generator.Maker(MakeMediaQueryTokenizerCodePointsWriter).main(sys.argv)
     94