OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 third_party = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pard
ir) | 62 third_party = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pard
ir) |
63 # Insert at front to override system libraries, and after path[0] == script dir | 63 # Insert at front to override system libraries, and after path[0] == script dir |
64 sys.path.insert(1, third_party) | 64 sys.path.insert(1, third_party) |
65 from ply import lex | 65 from ply import lex |
66 | 66 |
67 # Base lexer is in Chromium src/tools/idl_parser | 67 # Base lexer is in Chromium src/tools/idl_parser |
68 tools_dir = os.path.join(third_party, os.pardir, 'tools') | 68 tools_dir = os.path.join(third_party, os.pardir, 'tools') |
69 sys.path.append(tools_dir) | 69 sys.path.append(tools_dir) |
70 from idl_parser.idl_lexer import IDLLexer | 70 from idl_parser.idl_lexer import IDLLexer |
71 | 71 |
| 72 LEXTAB = 'lextab' |
72 REMOVE_TOKENS = ['COMMENT'] | 73 REMOVE_TOKENS = ['COMMENT'] |
73 | 74 |
74 | 75 |
75 class BlinkIDLLexer(IDLLexer): | 76 class BlinkIDLLexer(IDLLexer): |
76 # ignore comments | 77 # ignore comments |
77 def t_COMMENT(self, t): | 78 def t_COMMENT(self, t): |
78 r'(/\*(.|\n)*?\*/)|(//.*(\n[ \t]*//.*)*)' | 79 r'(/\*(.|\n)*?\*/)|(//.*(\n[ \t]*//.*)*)' |
79 self.AddLines(t.value.count('\n')) | 80 self.AddLines(t.value.count('\n')) |
80 | 81 |
81 # Analogs to _AddToken/_AddTokens in base lexer | 82 # Analogs to _AddToken/_AddTokens in base lexer |
(...skipping 11 matching lines...) Expand all Loading... |
93 if debug: | 94 if debug: |
94 # Turn off optimization and caching to help debugging | 95 # Turn off optimization and caching to help debugging |
95 optimize = False | 96 optimize = False |
96 outputdir = None | 97 outputdir = None |
97 if outputdir: | 98 if outputdir: |
98 # Need outputdir in path because lex imports the cached lex table | 99 # Need outputdir in path because lex imports the cached lex table |
99 # as a Python module | 100 # as a Python module |
100 sys.path.append(outputdir) | 101 sys.path.append(outputdir) |
101 | 102 |
102 if rewrite_tables: | 103 if rewrite_tables: |
103 tablefile = os.path.join(outputdir, 'lextab.py') | 104 tablefile_root = os.path.join(outputdir, LEXTAB) |
104 | 105 # Also remove the .pyc/.pyo files, or they'll be used even if |
105 def unlink(filename): | 106 # the .py file doesn't exist. |
| 107 for ext in ('.py', '.pyc', '.pyo'): |
106 try: | 108 try: |
107 os.unlink(filename) | 109 os.unlink(tablefile_root + ext) |
108 except OSError: | 110 except OSError: |
109 pass | 111 pass |
110 | 112 |
111 unlink(tablefile) | |
112 # Also remove the .pyc/.pyo files, or they'll be used even if | |
113 # the .py file doesn't exist. | |
114 unlink(tablefile + 'c') | |
115 unlink(tablefile + 'o') | |
116 | |
117 IDLLexer.__init__(self) | 113 IDLLexer.__init__(self) |
118 # Overrides to parent class | 114 # Overrides to parent class |
119 self._RemoveTokens(REMOVE_TOKENS) | 115 self._RemoveTokens(REMOVE_TOKENS) |
120 # Optimized mode substantially decreases startup time (by disabling | 116 # Optimized mode substantially decreases startup time (by disabling |
121 # error checking), and also allows use of Python's optimized mode. | 117 # error checking), and also allows use of Python's optimized mode. |
122 # See: Optimized Mode | 118 # See: Optimized Mode |
123 # http://www.dabeaz.com/ply/ply.html#ply_nn15 | 119 # http://www.dabeaz.com/ply/ply.html#ply_nn15 |
124 self._lexobj = lex.lex(object=self, | 120 self._lexobj = lex.lex(object=self, |
125 debug=debug, | 121 debug=debug, |
126 optimize=optimize, | 122 optimize=optimize, |
127 lextab='lextab', | 123 lextab=LEXTAB, |
128 outputdir=outputdir) | 124 outputdir=outputdir) |
129 | 125 |
130 | 126 |
131 ################################################################################ | 127 ################################################################################ |
132 | 128 |
133 def main(argv): | 129 def main(argv): |
134 # If file itself executed, build and cache lex table | 130 # If file itself executed, build and cache lex table |
135 try: | 131 try: |
136 outputdir = argv[1] | 132 outputdir = argv[1] |
137 except IndexError as err: | 133 except IndexError as err: |
138 print 'Usage: %s OUTPUT_DIR' % argv[0] | 134 print 'Usage: %s OUTPUT_DIR' % argv[0] |
139 return 1 | 135 return 1 |
140 # Important: rewrite_tables=True causes the cache file to be deleted if it | 136 # Important: rewrite_tables=True causes the cache file to be deleted if it |
141 # exists, thus making sure that PLY doesn't load it instead of regenerating | 137 # exists, thus making sure that PLY doesn't load it instead of regenerating |
142 # the parse table. | 138 # the parse table. |
143 lexer = BlinkIDLLexer(outputdir=outputdir, rewrite_tables=True) | 139 lexer = BlinkIDLLexer(outputdir=outputdir, rewrite_tables=True) |
144 | 140 |
145 | 141 |
146 if __name__ == '__main__': | 142 if __name__ == '__main__': |
147 sys.exit(main(sys.argv)) | 143 sys.exit(main(sys.argv)) |
OLD | NEW |