Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: Source/bindings/scripts/blink_idl_lexer.py

Issue 15801003: IDL parser rewrite in Python (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Ready for review! (cleaner) Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 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
haraken 2013/07/16 14:17:51 Let's add "FIXME:".
Nils Barth (inactive) 2013/07/17 12:05:09 Ok, consolidated FIXME at top.
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 If the base lexer is changed to discard comments, we can simply used the base
40 lexer and eliminate this separate lexer.
41
42 Web IDL:
43 http://www.w3.org/TR/WebIDL/
44 Web IDL Grammar:
45 http://www.w3.org/TR/WebIDL/#idl-grammar
46 PLY:
47 http://www.dabeaz.com/ply/
48 """
49
50 # Disable attribute validation, as lint can't import parent class to check
51 # pylint: disable=E1101
haraken 2013/07/16 14:17:51 Is this comment helpful?
Nils Barth (inactive) 2013/07/17 12:05:09 Yup, otherwise I get a pylint error, I think on pr
52
53 import os.path
54 import sys
55
56 # Base lexer is in Chromium src/tools/idl_parser
57 module_path, module_name = os.path.split(__file__)
58 tools_dir = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pardir , os.pardir, 'tools')
59 sys.path.append(tools_dir)
haraken 2013/07/16 14:17:51 Instead of writing the relative path here, can you
Nils Barth (inactive) 2013/07/17 12:05:09 (See long reply.)
Nils Barth (inactive) 2013/07/17 12:09:41 I don’t think there’s an easy way to specify the P
haraken 2013/07/21 14:31:50 Given that this is a limitation of Python, I'm OK
60
61 from idl_parser.idl_lexer import IDLLexer
62
63 REMOVE_TOKENS = ['COMMENT']
64
65
66 class BlinkIDLLexer(IDLLexer):
67 # ignore comments
68 # FIXME: Upstream; needed because base doesn't ignore comments
haraken 2013/07/16 14:17:51 You can remove this comment, because you already s
Nils Barth (inactive) 2013/07/17 12:05:09 Got it, consolidate FIXMEs at top.
69 def t_COMMENT(self, t):
70 r'(/\*(.|\n)*?\*/)|(//.*(\n[ \t]*//.*)*)'
71 self.AddLines(t.value.count('\n'))
haraken 2013/07/16 14:17:51 I'm just curious: What is this doing?
Nils Barth (inactive) 2013/07/17 12:05:09 This tracks the current line number, which is used
72
73 # Analogs to _AddToken/_AddTokens in base lexer
74 # Needed to remove COMMENT token, since comments ignored
75 # FIXME: Upstream
haraken 2013/07/16 14:17:51 I'd remove this comment.
Nils Barth (inactive) 2013/07/17 12:05:09 Got it, consolidated FIXME.
76 def _RemoveToken(self, token):
77 try:
78 self.tokens.remove(token)
79 except ValueError:
80 raise RuntimeError('Token "%s" not present, so cannot be removed ' % token)
haraken 2013/07/16 14:17:51 Does this mean that we raise an exception when par
Nils Barth (inactive) 2013/07/17 12:05:09 No no, this is used when initializing the lexer, n
81
82 def _RemoveTokens(self, tokens):
83 for token in tokens:
84 self._RemoveToken(token)
85
86 def __init__(self):
87 IDLLexer.__init__(self)
88 self._RemoveTokens(REMOVE_TOKENS)
89
90
91 # If run by itself, attempt to build the lexer
92 if __name__ == '__main__':
93 lexer = BlinkIDLLexer()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698