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 """Parser for Blink IDL. |
| 30 |
| 31 The parser uses the PLY (Python Lex-Yacc) library to build a set of parsing |
| 32 rules which understand the Blink dialect of Web IDL. |
| 33 It derives from a standard Web IDL parser, overriding rules where Blink IDL |
| 34 differs syntactically or semantically from the base parser, or where the base |
| 35 parser diverges from the Web IDL standard. |
| 36 |
| 37 Web IDL: |
| 38 http://www.w3.org/TR/WebIDL/ |
| 39 Web IDL Grammar: |
| 40 http://www.w3.org/TR/WebIDL/#idl-grammar |
| 41 PLY: |
| 42 http://www.dabeaz.com/ply/ |
| 43 """ |
| 44 |
| 45 # Disable check for line length and Member as Function due to how grammar rules |
| 46 # are defined with PLY |
| 47 # |
| 48 # pylint: disable=R0201 |
| 49 # pylint: disable=C0301 |
| 50 # |
| 51 # Disable attribute validation, as lint can't import parent class to check |
| 52 # pylint: disable=E1101 |
| 53 |
| 54 import os.path |
| 55 import sys |
| 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 sys.path.append(third_party) |
| 61 from ply import yacc |
| 62 |
| 63 # Base parser is in Chromium src/tools/idl_parser |
| 64 tools_dir = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pardir
, os.pardir, 'tools') |
| 65 sys.path.append(tools_dir) |
| 66 from idl_parser.idl_parser import IDLParser, ListFromConcat |
| 67 from idl_parser.idl_parser import ParseFile as parse_file |
| 68 |
| 69 from blink_idl_lexer import BlinkIDLLexer |
| 70 |
| 71 # We ignore comments, but base parser preserves them |
| 72 # FIXME: Upstream: comments should be removed in base parser |
| 73 REMOVED_RULES = ['Comments', # [0.1] |
| 74 'CommentsRest', # [0.2] |
| 75 ] |
| 76 |
| 77 |
| 78 class BlinkIDLParser(IDLParser): |
| 79 # Below are grammar rules used by yacc, given by functions named p_<RULE>. |
| 80 # * The docstring is the production rule in BNF (grammar). |
| 81 # * The body is the yacc action (semantics). |
| 82 # |
| 83 # The PLY framework builds the actual low-level parser by introspecting this |
| 84 # parser object, selecting all attributes named p_<RULE> as grammar rules. |
| 85 # It extracts the docstrings and uses them as the production rules, building |
| 86 # the table of a LALR parser, and uses the body of the functions as actions. |
| 87 # |
| 88 # Reference: |
| 89 # http://www.dabeaz.com/ply/ply.html#ply_nn23 |
| 90 # |
| 91 # Review of yacc: |
| 92 # Yacc parses a token stream, internally producing a Concrete Syntax Tree |
| 93 # (CST), where each node corresponds to a production rule in the grammar. |
| 94 # At each node, it runs an action, which is usually "produce a node in the |
| 95 # Abstract Syntax Tree (AST)" or "ignore this node" (for nodes in the CST |
| 96 # that aren't included in the AST, since only needed for parsing). |
| 97 # |
| 98 # The rules use pseudo-variables; in PLY syntax: |
| 99 # p[0] is the left side: assign return value to p[0] instead of returning, |
| 100 # p[1] ... p[n] are the right side: the values can be accessed, and they |
| 101 # can be modified. |
| 102 # (In yacc these are $$ and $1 ... $n.) |
| 103 # |
| 104 # The rules can look cryptic at first, but there are a few standard |
| 105 # transforms from the CST to AST. With these in mind, the actions should |
| 106 # be reasonably legible. |
| 107 # |
| 108 # * Ignore production |
| 109 # Discard this branch. Primarily used when one alternative is empty. |
| 110 # |
| 111 # Sample code: |
| 112 # if len(p) > 1: |
| 113 # p[0] = ... |
| 114 # # Note no assignment if len(p) == 1 |
| 115 # |
| 116 # * Eliminate singleton production |
| 117 # Discard this node in the CST, pass the next level down up the tree. |
| 118 # Used to ignore productions only necessary for parsing, but not needed |
| 119 # in the AST. |
| 120 # |
| 121 # Sample code: |
| 122 # p[0] = p[1] |
| 123 # |
| 124 # * Build node |
| 125 # The key type of rule. In this parser, produces object of class IDLNode. |
| 126 # There are several helper functions: |
| 127 # * BuildProduction: actually builds an IDLNode, based on a production. |
| 128 # * BuildAttribute: builds an IDLAttribute, which is a temporary |
| 129 # object to hold a name-value pair, which is then |
| 130 # set as a Property of the IDLNode when the IDLNode |
| 131 # is built. |
| 132 # * BuildNamed: Same as BuildProduction, and sets the 'NAME' property. |
| 133 # * BuildTrue: BuildAttribute with value True, for flags. |
| 134 # See base idl_parser.py for definitions and more examples of use. |
| 135 # |
| 136 # Sample code: |
| 137 # # Build node of type NodeType, with value p[1], and children. |
| 138 # p[0] = self.BuildProduction('NodeType', p, 1, children) |
| 139 # |
| 140 # # Build named node of type NodeType, with name and value p[1]. |
| 141 # # (children optional) |
| 142 # p[0] = self.BuildNamed('NodeType', p, 1) |
| 143 # |
| 144 # # Make a list |
| 145 # # Used if one node has several children. |
| 146 # children = ListFromConcat(p[2], p[3]) |
| 147 # p[0] = self.BuildProduction('NodeType', p, 1, children) |
| 148 # |
| 149 # # Also used to collapse the right-associative tree |
| 150 # # produced by parsing a list back into a single list. |
| 151 # """Foos : Foo Foos |
| 152 # |""" |
| 153 # if len(p) > 1: |
| 154 # p[0] = ListFromConcat(p[1], p[2]) |
| 155 # |
| 156 # # Add children. |
| 157 # # Primarily used to add attributes, produced via BuildTrue. |
| 158 # # p_StaticAttribute |
| 159 # """StaticAttribute : STATIC Attribute""" |
| 160 # p[2].AddChildren(self.BuildTrue('STATIC')) |
| 161 # p[0] = p[2] |
| 162 # |
| 163 # Numbering scheme for the rules is: |
| 164 # [1] for Web IDL spec (or additions in base parser) |
| 165 # These should all be upstreamed to the base parser. |
| 166 # [b1] for Blink IDL changes (overrides Web IDL) |
| 167 # [b1.1] for Blink IDL additions, auxiliary rules for [b1] |
| 168 # Numbers are as per Candidate Recommendation 19 April 2012: |
| 169 # http://www.w3.org/TR/2012/CR-WebIDL-20120419/ |
| 170 |
| 171 # [0] Override grammar, since we strip comments |
| 172 # (not in Web IDL) |
| 173 # FIXME: Upstream |
| 174 def p_Top(self, p): |
| 175 """Top : Definitions""" |
| 176 p[0] = p[1] |
| 177 |
| 178 # [3] Override action, since we distinguish callbacks |
| 179 # FIXME: Upstream |
| 180 def p_CallbackOrInterface(self, p): |
| 181 """CallbackOrInterface : CALLBACK CallbackRestOrInterface |
| 182 | Interface""" |
| 183 if len(p) > 2: |
| 184 p[2].AddChildren(self.BuildTrue('CALLBACK')) |
| 185 p[0] = p[2] |
| 186 else: |
| 187 p[0] = p[1] |
| 188 |
| 189 # [b27] Add strings, more 'Literal' productions |
| 190 # 'Literal's needed because integers and strings are both internally strings |
| 191 def p_ConstValue(self, p): |
| 192 """ConstValue : BooleanLiteral |
| 193 | FloatLiteral |
| 194 | IntegerLiteral |
| 195 | StringLiteral |
| 196 | null""" |
| 197 # Standard is (no 'string', fewer 'Literal's): |
| 198 # ConstValue : BooleanLiteral |
| 199 # | FloatLiteral |
| 200 # | integer |
| 201 # | NULL |
| 202 p[0] = p[1] |
| 203 |
| 204 # [b27.1] |
| 205 def p_IntegerLiteral(self, p): |
| 206 """IntegerLiteral : integer""" |
| 207 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'integer'), |
| 208 self.BuildAttribute('NAME', p[1])) |
| 209 |
| 210 # [b27.2] |
| 211 def p_StringLiteral(self, p): |
| 212 """StringLiteral : string""" |
| 213 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'DOMString'), |
| 214 self.BuildAttribute('NAME', p[1])) |
| 215 |
| 216 # [b30] Add StaticAttribute |
| 217 def p_AttributeOrOperation(self, p): |
| 218 """AttributeOrOperation : STRINGIFIER StringifierAttributeOrOperation |
| 219 | Attribute |
| 220 | StaticAttribute |
| 221 | Operation""" |
| 222 # Standard is (no StaticAttribute): |
| 223 # AttributeOrOperation : STRINGIFIER StringifierAttributeOrOperation |
| 224 # | Attribute |
| 225 # | Operation |
| 226 if len(p) > 2: |
| 227 # FIXME: Clearer to add stringifier property here, as: |
| 228 # p[2].AddChildren(self.BuildTrue('STRINGIFIER')) |
| 229 # Fix when actually implementing stringifiers. |
| 230 p[0] = p[2] |
| 231 else: |
| 232 p[0] = p[1] |
| 233 |
| 234 # [b30.1] |
| 235 def p_StaticAttribute(self, p): |
| 236 """StaticAttribute : STATIC Attribute""" |
| 237 p[2].AddChildren(self.BuildTrue('STATIC')) |
| 238 p[0] = p[2] |
| 239 |
| 240 # [b47] |
| 241 def p_ExceptionMember(self, p): |
| 242 """ExceptionMember : Const |
| 243 | ExceptionField |
| 244 | Attribute |
| 245 | ExceptionOperation""" |
| 246 # Standard is (no Attribute, no ExceptionOperation): |
| 247 # ExceptionMember : Const |
| 248 # | ExceptionField |
| 249 # FIXME: In DOMException.idl, Attributes should be changed to |
| 250 # ExceptionFields, and Attribute removed from this rule. |
| 251 p[0] = p[1] |
| 252 |
| 253 # [b47.1] |
| 254 def p_ExceptionOperation(self, p): |
| 255 """ExceptionOperation : Type identifier '(' ')' ';'""" |
| 256 # Needed to handle one case in DOMException.idl: |
| 257 # // Override in a Mozilla compatible format |
| 258 # [NotEnumerable] DOMString toString(); |
| 259 # Limited form of Operation to prevent others from being added. |
| 260 # FIXME: Should be a stringifier instead. |
| 261 p[0] = self.BuildNamed('ExceptionOperation', p, 2, p[1]) |
| 262 |
| 263 # Extended attributes |
| 264 # [b49] Override base parser: remove comment field, since comments stripped |
| 265 # FIXME: Upstream |
| 266 def p_ExtendedAttributeList(self, p): |
| 267 """ExtendedAttributeList : '[' ExtendedAttribute ExtendedAttributes ']' |
| 268 | '[' ']' |
| 269 | """ |
| 270 if len(p) > 3: |
| 271 items = ListFromConcat(p[2], p[3]) |
| 272 p[0] = self.BuildProduction('ExtAttributes', p, 1, items) |
| 273 |
| 274 # [b50] Allow optional trailing comma |
| 275 # FIXME: Blink-only, but bug filed to change spec: |
| 276 # https://www.w3.org/Bugs/Public/show_bug.cgi?id=22156 |
| 277 def p_ExtendedAttributes(self, p): |
| 278 """ExtendedAttributes : ',' ExtendedAttribute ExtendedAttributes |
| 279 | ',' |
| 280 |""" |
| 281 if len(p) > 3: |
| 282 p[0] = ListFromConcat(p[2], p[3]) |
| 283 |
| 284 # [b51] Add ExtendedAttributeIdentAndOrIdent |
| 285 def p_ExtendedAttribute(self, p): |
| 286 """ExtendedAttribute : ExtendedAttributeNoArgs |
| 287 | ExtendedAttributeArgList |
| 288 | ExtendedAttributeIdent |
| 289 | ExtendedAttributeIdentAndOrIdent |
| 290 | ExtendedAttributeNamedArgList""" |
| 291 p[0] = p[1] |
| 292 |
| 293 # [59] |
| 294 # FIXME: Upstream UnionType |
| 295 def p_UnionType(self, p): |
| 296 """UnionType : '(' UnionMemberType OR UnionMemberType UnionMemberTypes '
)'""" |
| 297 members = ListFromConcat(p[2], p[4], p[5]) |
| 298 p[0] = self.BuildProduction('UnionType', p, 1, members) |
| 299 |
| 300 # [60] |
| 301 def p_UnionMemberType(self, p): |
| 302 """UnionMemberType : NonAnyType |
| 303 | UnionType TypeSuffix |
| 304 | ANY '[' ']' TypeSuffix""" |
| 305 if len(p) == 2: |
| 306 p[0] = self.BuildProduction('Type', p, 1, p[1]) |
| 307 elif len(p) == 3: |
| 308 p[0] = self.BuildProduction('Type', p, 1, ListFromConcat(p[1], p[2])
) |
| 309 else: |
| 310 any_node = ListFromConcat(self.BuildProduction('Any', p, 1), p[4]) |
| 311 p[0] = self.BuildProduction('Type', p, 1, any_node) |
| 312 |
| 313 # [61] |
| 314 def p_UnionMemberTypes(self, p): |
| 315 """UnionMemberTypes : OR UnionMemberType UnionMemberTypes |
| 316 |""" |
| 317 if len(p) > 2: |
| 318 p[0] = ListFromConcat(p[2], p[3]) |
| 319 |
| 320 # [70] Override base parser to remove non-standard sized array |
| 321 # FIXME: Upstream |
| 322 def p_TypeSuffix(self, p): |
| 323 """TypeSuffix : '[' ']' TypeSuffix |
| 324 | '?' TypeSuffixStartingWithArray |
| 325 |""" |
| 326 if len(p) == 4: |
| 327 p[0] = self.BuildProduction('Array', p, 1, p[3]) |
| 328 elif len(p) == 3: |
| 329 p[0] = ListFromConcat(self.BuildTrue('NULLABLE'), p[2]) |
| 330 |
| 331 # [b76.1] |
| 332 def p_ExtendedAttributeIdentAndOrIdent(self, p): |
| 333 """ExtendedAttributeIdentAndOrIdent : identifier '=' identifier '&' iden
tifier |
| 334 | identifier '=' identifier '|' iden
tifier""" |
| 335 # FIXME: support arbitrary number of values, not just two. |
| 336 value = self.BuildAttribute('VALUE', p[3] + p[4] + p[5]) |
| 337 p[0] = self.BuildNamed('ExtAttribute', p, 1, value) |
| 338 |
| 339 def __dir__(self): |
| 340 # Remove REMOVED_RULES from listing so yacc doesn't parse them |
| 341 # FIXME: Upstream |
| 342 keys = set(self.__dict__.keys() + dir(self.__class__)) |
| 343 for rule in REMOVED_RULES: |
| 344 keys.remove('p_' + rule) |
| 345 return list(keys) |
| 346 |
| 347 def __init__(self, lexer=None, verbose=False, debug=False, mute_error=False)
: |
| 348 lexer = lexer or BlinkIDLLexer() |
| 349 self.lexer = lexer |
| 350 self.tokens = lexer.KnownTokens() |
| 351 # Using SLR (instead of LALR) generates the table faster, |
| 352 # but produces the same output. This is ok b/c Web IDL (and Blink IDL) |
| 353 # is an LL(1) grammar, so SLR can parse it. |
| 354 self.yaccobj = yacc.yacc(module=self, debug=debug, method='SLR') |
| 355 self.parse_debug = debug |
| 356 self.verbose = verbose |
| 357 self.mute_error = mute_error |
| 358 self._parse_errors = 0 |
| 359 self._parse_warnings = 0 |
| 360 self._last_error_msg = None |
| 361 self._last_error_lineno = 0 |
| 362 self._last_error_pos = 0 |
| 363 |
| 364 |
| 365 # If run by itself, attempt to build the parser |
| 366 if __name__ == '__main__': |
| 367 parser = BlinkIDLParser() |
OLD | NEW |