OLD | NEW |
(Empty) | |
| 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, i.e., both Web IDL and Blink IDL |
| 33 tokens. |
| 34 |
| 35 Web IDL: |
| 36 http://www.w3.org/TR/WebIDL/ |
| 37 Web IDL Grammar: |
| 38 http://www.w3.org/TR/WebIDL/#idl-grammar |
| 39 PLY: |
| 40 http://www.dabeaz.com/ply/ |
| 41 """ |
| 42 |
| 43 # Disable attribute validation, as lint can't import parent class to check |
| 44 # pylint: disable=E1101 |
| 45 |
| 46 import os.path |
| 47 import sys |
| 48 |
| 49 # Base lexer is in Chromium src/tools/idl_parser |
| 50 module_path, module_name = os.path.split(__file__) |
| 51 tools_dir = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pardir
, os.pardir, 'tools') |
| 52 sys.path.append(tools_dir) |
| 53 |
| 54 from idl_parser.idl_lexer import IDLLexer |
| 55 |
| 56 NEW_TOKENS = ['SCOPE_RESOLUTION'] |
| 57 REMOVE_TOKENS = ['COMMENT'] |
| 58 NEW_KEYWORDS = ['getraises', 'inherits', 'iterator', 'raises', 'serializer', 'se
traises'] |
| 59 |
| 60 |
| 61 class BlinkIDLLexer(IDLLexer): |
| 62 # Special multi-character operators |
| 63 def t_SCOPE_RESOLUTION(self, t): |
| 64 r'::' |
| 65 return t |
| 66 |
| 67 # ignore comments |
| 68 # FIXME: Upstream; needed because base doesn't ignore comments |
| 69 def t_COMMENT(self, t): |
| 70 r'(/\*(.|\n)*?\*/)|(//.*(\n[ \t]*//.*)*)' |
| 71 self.AddLines(t.value.count('\n')) |
| 72 |
| 73 # Analogs to _AddToken/_AddTokens in base lexer |
| 74 def _RemoveToken(self, token): |
| 75 try: |
| 76 self.tokens.remove(token) |
| 77 except ValueError: |
| 78 raise RuntimeError('Token "%s" not present, so cannot be removed ' %
token) |
| 79 |
| 80 def _RemoveTokens(self, tokens): |
| 81 for token in tokens: |
| 82 self._RemoveToken(token) |
| 83 |
| 84 def __init__(self): |
| 85 IDLLexer.__init__(self) |
| 86 self._AddTokens(NEW_TOKENS) |
| 87 self._RemoveTokens(REMOVE_TOKENS) |
| 88 self._AddKeywords(NEW_KEYWORDS) |
| 89 |
| 90 |
| 91 # If run by itself, attempt to build the lexer |
| 92 if __name__ == '__main__': |
| 93 lexer = BlinkIDLLexer() |
OLD | NEW |