1 # Copyright (C) 2013 Google Inc. All rights reserved. 2 # 3 # Redistribution and use in source and binary forms, with or without 4 # modification, are permitted provided that the following conditions are 5 # met: 6 # 7 # * Redistributions of source code must retain the above copyright 8 # notice, this list of conditions and the following disclaimer. 9 # * Redistributions in binary form must reproduce the above 10 # copyright notice, this list of conditions and the following disclaimer 11 # in the documentation and/or other materials provided with the 12 # distribution. 13 # * Neither the name of Google Inc. nor the names of its 14 # contributors may be used to endorse or promote products derived from 15 # this software without specific prior written permission. 16 # 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 """Lexer for Blink IDL. 30 31 The lexer uses the PLY (Python Lex-Yacc) library to build a tokenizer which 32 understands the Blink dialect of Web IDL and produces a token stream suitable 33 for the Blink IDL parser. 34 35 Blink IDL is identical to Web IDL at the token level, but the base lexer 36 does not discard comments. We need to override (and not include comments in 37 the token stream), as otherwise comments must be explicitly included in the 38 phrase grammar of the parser. 39 40 FIXME: Change base lexer to discard comments, and simply used the base 41 lexer, eliminating this separate lexer. 42 43 Web IDL: 44 http://www.w3.org/TR/WebIDL/ 45 Web IDL Grammar: 46 http://www.w3.org/TR/WebIDL/#idl-grammar 47 PLY: 48 http://www.dabeaz.com/ply/ 49 50 Design doc: 51 http://www.chromium.org/developers/design-documents/idl-compiler#TOC-Front-end 52 """ 53 54 # Disable attribute validation, as lint can't import parent class to check 55 # pylint: disable=E1101 56 57 import os.path 58 import sys 59 60 # PLY is in Chromium src/third_party/ply 61 module_path, module_name = os.path.split(__file__) 62 third_party = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pardir) 63 # Insert at front to override system libraries, and after path[0] == script dir 64 sys.path.insert(1, third_party) 65 from ply import lex 66 67 # Base lexer is in Chromium src/tools/idl_parser 68 tools_dir = os.path.join(third_party, os.pardir, 'tools') 69 sys.path.append(tools_dir) 70 from idl_parser.idl_lexer import IDLLexer 71 72 REMOVE_TOKENS = ['COMMENT'] 73 74 75 class BlinkIDLLexer(IDLLexer): 76 # ignore comments 77 def t_COMMENT(self, t): 78 r'(/\*(.|\n)*?\*/)|(//.*(\n[ \t]*//.*)*)' 79 self.AddLines(t.value.count('\n')) 80 81 # Analogs to _AddToken/_AddTokens in base lexer 82 # Needed to remove COMMENT token, since comments ignored 83 def _RemoveToken(self, token): 84 if token in self.tokens: 85 self.tokens.remove(token) 86 87 def _RemoveTokens(self, tokens): 88 for token in tokens: 89 self._RemoveToken(token) 90 91 def __init__(self, debug=False, optimize=True, outputdir=None): 92 if debug: 93 # Turn off optimization and caching to help debugging 94 optimize = False 95 outputdir = None 96 if outputdir: 97 # Need outputdir in path because lex imports the cached lex table 98 # as a Python module 99 sys.path.append(outputdir) 100 101 IDLLexer.__init__(self) 102 # Overrides to parent class 103 self._RemoveTokens(REMOVE_TOKENS) 104 # Optimized mode substantially decreases startup time (by disabling 105 # error checking), and also allows use of Python's optimized mode. 106 # See: Optimized Mode 107 # http://www.dabeaz.com/ply/ply.html#ply_nn15 108 self._lexobj = lex.lex(object=self, 109 debug=debug, 110 optimize=optimize, 111 outputdir=outputdir) 112 113 114 ################################################################################ 115 116 def main(argv): 117 # If file itself executed, build and cache lex table 118 try: 119 outputdir = argv[1] 120 except IndexError as err: 121 print 'Usage: %s OUTPUT_DIR' % argv[0] 122 return 1 123 lexer = BlinkIDLLexer(outputdir=outputdir) 124 125 126 if __name__ == '__main__': 127 sys.exit(main(sys.argv)) 128