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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 PLY: | 47 PLY: |
48 http://www.dabeaz.com/ply/ | 48 http://www.dabeaz.com/ply/ |
49 """ | 49 """ |
50 | 50 |
51 # Disable attribute validation, as lint can't import parent class to check | 51 # Disable attribute validation, as lint can't import parent class to check |
52 # pylint: disable=E1101 | 52 # pylint: disable=E1101 |
53 | 53 |
54 import os.path | 54 import os.path |
55 import sys | 55 import sys |
56 | 56 |
| 57 # PLY is in Chromium src/third_party/ply |
| 58 module_path, module_name = os.path.split(__file__) |
| 59 third_party = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pard
ir) |
| 60 # Insert at front to override system libraries, and after path[0] == script dir |
| 61 sys.path.insert(1, third_party) |
| 62 from ply import lex |
| 63 |
57 # Base lexer is in Chromium src/tools/idl_parser | 64 # Base lexer is in Chromium src/tools/idl_parser |
58 module_path, module_name = os.path.split(__file__) | 65 tools_dir = os.path.join(third_party, os.pardir, 'tools') |
59 tools_dir = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pardir
, os.pardir, 'tools') | |
60 sys.path.append(tools_dir) | 66 sys.path.append(tools_dir) |
61 | |
62 from idl_parser.idl_lexer import IDLLexer | 67 from idl_parser.idl_lexer import IDLLexer |
63 | 68 |
64 REMOVE_TOKENS = ['COMMENT'] | 69 REMOVE_TOKENS = ['COMMENT'] |
65 | 70 |
66 | 71 |
67 class BlinkIDLLexer(IDLLexer): | 72 class BlinkIDLLexer(IDLLexer): |
68 # ignore comments | 73 # ignore comments |
69 def t_COMMENT(self, t): | 74 def t_COMMENT(self, t): |
70 r'(/\*(.|\n)*?\*/)|(//.*(\n[ \t]*//.*)*)' | 75 r'(/\*(.|\n)*?\*/)|(//.*(\n[ \t]*//.*)*)' |
71 self.AddLines(t.value.count('\n')) | 76 self.AddLines(t.value.count('\n')) |
72 | 77 |
73 # Analogs to _AddToken/_AddTokens in base lexer | 78 # Analogs to _AddToken/_AddTokens in base lexer |
74 # Needed to remove COMMENT token, since comments ignored | 79 # Needed to remove COMMENT token, since comments ignored |
75 def _RemoveToken(self, token): | 80 def _RemoveToken(self, token): |
76 if token in self.tokens: | 81 if token in self.tokens: |
77 self.tokens.remove(token) | 82 self.tokens.remove(token) |
78 | 83 |
79 def _RemoveTokens(self, tokens): | 84 def _RemoveTokens(self, tokens): |
80 for token in tokens: | 85 for token in tokens: |
81 self._RemoveToken(token) | 86 self._RemoveToken(token) |
82 | 87 |
83 def __init__(self): | 88 def __init__(self, debug=False, optimize=True, outputdir=None): |
| 89 if debug: |
| 90 # Turn off optimization and caching to help debugging |
| 91 optimize = False |
| 92 outputdir = None |
| 93 if outputdir: |
| 94 # Need outputdir in path because lex imports the cached lex table |
| 95 # as a Python module |
| 96 sys.path.append(outputdir) |
| 97 |
84 IDLLexer.__init__(self) | 98 IDLLexer.__init__(self) |
| 99 # Overrides to parent class |
85 self._RemoveTokens(REMOVE_TOKENS) | 100 self._RemoveTokens(REMOVE_TOKENS) |
| 101 # Optimized mode substantially decreases startup time (by disabling |
| 102 # error checking), and also allows use of Python's optimized mode. |
| 103 # See: Optimized Mode |
| 104 # http://www.dabeaz.com/ply/ply.html#ply_nn15 |
| 105 self._lexobj = lex.lex(object=self, |
| 106 debug=debug, |
| 107 optimize=optimize, |
| 108 outputdir=outputdir) |
86 | 109 |
87 | 110 |
88 # If run by itself, attempt to build the lexer | 111 ################################################################################ |
| 112 |
| 113 def main(argv): |
| 114 # If file itself executed, build and cache lex table |
| 115 try: |
| 116 outputdir = argv[1] |
| 117 except IndexError as err: |
| 118 print 'Usage: %s OUTPUT_DIR' % argv[0] |
| 119 return 1 |
| 120 lexer = BlinkIDLLexer(outputdir=outputdir) |
| 121 |
| 122 |
89 if __name__ == '__main__': | 123 if __name__ == '__main__': |
90 lexer = BlinkIDLLexer() | 124 sys.exit(main(sys.argv)) |
OLD | NEW |