Chromium Code Reviews| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 # Analogs to _AddToken/_AddTokens in base lexer | 81 # Analogs to _AddToken/_AddTokens in base lexer |
| 82 # Needed to remove COMMENT token, since comments ignored | 82 # Needed to remove COMMENT token, since comments ignored |
| 83 def _RemoveToken(self, token): | 83 def _RemoveToken(self, token): |
| 84 if token in self.tokens: | 84 if token in self.tokens: |
| 85 self.tokens.remove(token) | 85 self.tokens.remove(token) |
| 86 | 86 |
| 87 def _RemoveTokens(self, tokens): | 87 def _RemoveTokens(self, tokens): |
| 88 for token in tokens: | 88 for token in tokens: |
| 89 self._RemoveToken(token) | 89 self._RemoveToken(token) |
| 90 | 90 |
| 91 def __init__(self, debug=False, optimize=True, outputdir=None): | 91 def __init__(self, debug=False, optimize=True, outputdir=None, |
| 92 rewrite_tables=False): | |
| 92 if debug: | 93 if debug: |
| 93 # Turn off optimization and caching to help debugging | 94 # Turn off optimization and caching to help debugging |
| 94 optimize = False | 95 optimize = False |
| 95 outputdir = None | 96 outputdir = None |
| 96 if outputdir: | 97 if outputdir: |
| 97 # Need outputdir in path because lex imports the cached lex table | 98 # Need outputdir in path because lex imports the cached lex table |
| 98 # as a Python module | 99 # as a Python module |
| 99 sys.path.append(outputdir) | 100 sys.path.append(outputdir) |
| 100 | 101 |
| 102 if rewrite_tables: | |
| 103 tablefile = os.path.join(outputdir, 'lextab.py') | |
| 104 | |
| 105 def unlink(filename): | |
| 106 try: | |
| 107 os.unlink(filename) | |
| 108 except OSError: | |
| 109 pass | |
| 110 | |
| 111 unlink(tablefile) | |
|
Nils Barth (inactive)
2014/07/29 16:00:25
FWIW, cleaner would be a loop through suffixes, wh
| |
| 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 | |
| 101 IDLLexer.__init__(self) | 117 IDLLexer.__init__(self) |
| 102 # Overrides to parent class | 118 # Overrides to parent class |
| 103 self._RemoveTokens(REMOVE_TOKENS) | 119 self._RemoveTokens(REMOVE_TOKENS) |
| 104 # Optimized mode substantially decreases startup time (by disabling | 120 # Optimized mode substantially decreases startup time (by disabling |
| 105 # error checking), and also allows use of Python's optimized mode. | 121 # error checking), and also allows use of Python's optimized mode. |
| 106 # See: Optimized Mode | 122 # See: Optimized Mode |
| 107 # http://www.dabeaz.com/ply/ply.html#ply_nn15 | 123 # http://www.dabeaz.com/ply/ply.html#ply_nn15 |
| 108 self._lexobj = lex.lex(object=self, | 124 self._lexobj = lex.lex(object=self, |
| 109 debug=debug, | 125 debug=debug, |
| 110 optimize=optimize, | 126 optimize=optimize, |
| 127 lextab='lextab', | |
| 111 outputdir=outputdir) | 128 outputdir=outputdir) |
| 112 | 129 |
| 113 | 130 |
| 114 ################################################################################ | 131 ################################################################################ |
| 115 | 132 |
| 116 def main(argv): | 133 def main(argv): |
| 117 # If file itself executed, build and cache lex table | 134 # If file itself executed, build and cache lex table |
| 118 try: | 135 try: |
| 119 outputdir = argv[1] | 136 outputdir = argv[1] |
| 120 except IndexError as err: | 137 except IndexError as err: |
| 121 print 'Usage: %s OUTPUT_DIR' % argv[0] | 138 print 'Usage: %s OUTPUT_DIR' % argv[0] |
| 122 return 1 | 139 return 1 |
| 123 lexer = BlinkIDLLexer(outputdir=outputdir) | 140 # 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 | |
| 142 # the parse table. | |
| 143 lexer = BlinkIDLLexer(outputdir=outputdir, rewrite_tables=True) | |
| 124 | 144 |
| 125 | 145 |
| 126 if __name__ == '__main__': | 146 if __name__ == '__main__': |
| 127 sys.exit(main(sys.argv)) | 147 sys.exit(main(sys.argv)) |
| OLD | NEW |