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

Side by Side Diff: tools/idl_parser/idl_parser.py

Issue 329853005: IDL parser: align with current Web IDL specification (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: extend commentary Created 6 years, 6 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
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """ Parser for PPAPI IDL """ 6 """ Parser for PPAPI IDL """
7 7
8 # 8 #
9 # IDL Parser 9 # IDL Parser
10 # 10 #
11 # The parser is uses the PLY yacc library to build a set of parsing rules based 11 # The parser is uses the PLY yacc library to build a set of parsing rules based
12 # on WebIDL. 12 # on WebIDL.
13 # 13 #
14 # WebIDL, and WebIDL grammar can be found at: 14 # WebIDL, and WebIDL grammar can be found at:
15 # http://dev.w3.org/2006/webapi/WebIDL/ 15 # http://heycam.github.io/webidl/
16 # PLY can be found at: 16 # PLY can be found at:
17 # http://www.dabeaz.com/ply/ 17 # http://www.dabeaz.com/ply/
18 # 18 #
19 # The parser generates a tree by recursively matching sets of items against 19 # The parser generates a tree by recursively matching sets of items against
20 # defined patterns. When a match is made, that set of items is reduced 20 # defined patterns. When a match is made, that set of items is reduced
21 # to a new item. The new item can provide a match for parent patterns. 21 # to a new item. The new item can provide a match for parent patterns.
22 # In this way an AST is built (reduced) depth first. 22 # In this way an AST is built (reduced) depth first.
23 # 23 #
24 24
25 # 25 #
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 # separated by the "|". 137 # separated by the "|".
138 # 138 #
139 # The function is called with an object 'p' where p[0] is the output object 139 # The function is called with an object 'p' where p[0] is the output object
140 # and p[n] is the set of inputs for positive values of 'n'. Len(p) can be 140 # and p[n] is the set of inputs for positive values of 'n'. Len(p) can be
141 # used to distinguish between multiple item sets in the pattern. 141 # used to distinguish between multiple item sets in the pattern.
142 # 142 #
143 # For more details on parsing refer to the PLY documentation at 143 # For more details on parsing refer to the PLY documentation at
144 # http://www.dabeaz.com/ply/ 144 # http://www.dabeaz.com/ply/
145 # 145 #
146 # The parser is based on the WebIDL standard. See: 146 # The parser is based on the WebIDL standard. See:
147 # http://www.w3.org/TR/WebIDL/#idl-grammar 147 # http://heycam.github.io/webidl/#idl-grammar
148 # 148 #
149 # The various productions are annotated so that the WHOLE number greater than 149 # The various productions are annotated so that the WHOLE number greater than
150 # zero in the comment denotes the matching WebIDL grammar definition. 150 # zero in the comment denotes the matching WebIDL grammar definition.
151 # 151 #
152 # Productions with a fractional component in the comment denote additions to 152 # Productions with a fractional component in the comment denote additions to
153 # the WebIDL spec, such as comments. 153 # the WebIDL spec, such as comments.
154 # 154 #
155 155
156 156
157 class IDLParser(object): 157 class IDLParser(object):
(...skipping 18 matching lines...) Expand all
176 # [0.2] Produce a COMMENT and aggregate sibling comments 176 # [0.2] Produce a COMMENT and aggregate sibling comments
177 def p_CommentsRest(self, p): 177 def p_CommentsRest(self, p):
178 """CommentsRest : COMMENT CommentsRest 178 """CommentsRest : COMMENT CommentsRest
179 | """ 179 | """
180 if len(p) > 1: 180 if len(p) > 1:
181 p[0] = ListFromConcat(self.BuildComment('Comment', p, 1), p[2]) 181 p[0] = ListFromConcat(self.BuildComment('Comment', p, 1), p[2])
182 182
183 183
184 # 184 #
185 #The parser is based on the WebIDL standard. See: 185 #The parser is based on the WebIDL standard. See:
186 # http://www.w3.org/TR/WebIDL/#idl-grammar 186 # http://heycam.github.io/webidl/#idl-grammar
187 # 187 #
188 # [1] 188 # [1]
189 def p_Definitions(self, p): 189 def p_Definitions(self, p):
190 """Definitions : ExtendedAttributeList Definition Definitions 190 """Definitions : ExtendedAttributeList Definition Definitions
191 | """ 191 | """
192 if len(p) > 1: 192 if len(p) > 1:
193 p[2].AddChildren(p[1]) 193 p[2].AddChildren(p[1])
194 p[0] = ListFromConcat(p[2], p[3]) 194 p[0] = ListFromConcat(p[2], p[3])
195 195
196 # [2] Add INLINE definition 196 # [2]
197 def p_Definition(self, p): 197 def p_Definition(self, p):
198 """Definition : CallbackOrInterface 198 """Definition : CallbackOrInterface
199 | Partial 199 | Partial
200 | Dictionary 200 | Dictionary
201 | Exception 201 | Exception
202 | Enum 202 | Enum
203 | Typedef 203 | Typedef
204 | ImplementsStatement""" 204 | ImplementsStatement"""
205 p[0] = p[1] 205 p[0] = p[1]
206 206
(...skipping 15 matching lines...) Expand all
222 def p_CallbackRestOrInterface(self, p): 222 def p_CallbackRestOrInterface(self, p):
223 """CallbackRestOrInterface : CallbackRest 223 """CallbackRestOrInterface : CallbackRest
224 | Interface""" 224 | Interface"""
225 p[0] = p[1] 225 p[0] = p[1]
226 226
227 # [5] 227 # [5]
228 def p_Interface(self, p): 228 def p_Interface(self, p):
229 """Interface : INTERFACE identifier Inheritance '{' InterfaceMembers '}' ';' """ 229 """Interface : INTERFACE identifier Inheritance '{' InterfaceMembers '}' ';' """
230 p[0] = self.BuildNamed('Interface', p, 2, ListFromConcat(p[3], p[5])) 230 p[0] = self.BuildNamed('Interface', p, 2, ListFromConcat(p[3], p[5]))
231 231
232 # [6] Error recovery for PARTIAL 232 # [6]
233 def p_Partial(self, p): 233 def p_Partial(self, p):
234 """Partial : PARTIAL PartialDefinition""" 234 """Partial : PARTIAL PartialDefinition"""
235 p[2].AddChildren(self.BuildTrue('Partial')) 235 p[2].AddChildren(self.BuildTrue('Partial'))
236 p[0] = p[2] 236 p[0] = p[2]
237 237
238 # [6.1] Error recovery for Enums 238 # [6.1] Error recovery for Partial
239 def p_PartialError(self, p): 239 def p_PartialError(self, p):
240 """Partial : PARTIAL error""" 240 """Partial : PARTIAL error"""
241 p[0] = self.BuildError(p, 'Partial') 241 p[0] = self.BuildError(p, 'Partial')
242 242
243 # [7] 243 # [7]
244 def p_PartialDefinition(self, p): 244 def p_PartialDefinition(self, p):
245 """PartialDefinition : PartialDictionary 245 """PartialDefinition : PartialDictionary
246 | PartialInterface""" 246 | PartialInterface"""
247 p[0] = p[1] 247 p[0] = p[1]
248 248
249 # [8] 249 # [8]
250 def p_PartialInterface(self, p): 250 def p_PartialInterface(self, p):
251 """PartialInterface : INTERFACE identifier '{' InterfaceMembers '}' ';'""" 251 """PartialInterface : INTERFACE identifier '{' InterfaceMembers '}' ';'"""
252 p[0] = self.BuildNamed('Interface', p, 2, p[4]) 252 p[0] = self.BuildNamed('Interface', p, 2, p[4])
253 253
254 # [9] 254 # [9]
255 def p_InterfaceMembers(self, p): 255 def p_InterfaceMembers(self, p):
256 """InterfaceMembers : ExtendedAttributeList InterfaceMember InterfaceMembers 256 """InterfaceMembers : ExtendedAttributeList InterfaceMember InterfaceMembers
257 |""" 257 |"""
258 if len(p) > 1: 258 if len(p) > 1:
259 p[2].AddChildren(p[1]) 259 p[2].AddChildren(p[1])
260 p[0] = ListFromConcat(p[2], p[3]) 260 p[0] = ListFromConcat(p[2], p[3])
261 261
262 # [10] 262 # [10]
263 def p_InterfaceMember(self, p): 263 def p_InterfaceMember(self, p):
264 """InterfaceMember : Const 264 """InterfaceMember : Const
265 | AttributeOrOperation""" 265 | AttributeOrOperationOrIterator"""
266 p[0] = p[1] 266 p[0] = p[1]
267 267
268 # [11] 268 # [11]
269 def p_Dictionary(self, p): 269 def p_Dictionary(self, p):
270 """Dictionary : DICTIONARY identifier Inheritance '{' DictionaryMembers '}' ';'""" 270 """Dictionary : DICTIONARY identifier Inheritance '{' DictionaryMembers '}' ';'"""
271 p[0] = self.BuildNamed('Dictionary', p, 2, ListFromConcat(p[3], p[5])) 271 p[0] = self.BuildNamed('Dictionary', p, 2, ListFromConcat(p[3], p[5]))
272 272
273 # [11.1] Error recovery for regular Dictionary 273 # [11.1] Error recovery for regular Dictionary
274 def p_DictionaryError(self, p): 274 def p_DictionaryError(self, p):
275 """Dictionary : DICTIONARY error ';'""" 275 """Dictionary : DICTIONARY error ';'"""
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 """Enum : ENUM identifier '{' EnumValueList '}' ';'""" 346 """Enum : ENUM identifier '{' EnumValueList '}' ';'"""
347 p[0] = self.BuildNamed('Enum', p, 2, p[4]) 347 p[0] = self.BuildNamed('Enum', p, 2, p[4])
348 348
349 # [20.1] Error recovery for Enums 349 # [20.1] Error recovery for Enums
350 def p_EnumError(self, p): 350 def p_EnumError(self, p):
351 """Enum : ENUM error ';'""" 351 """Enum : ENUM error ';'"""
352 p[0] = self.BuildError(p, 'Enum') 352 p[0] = self.BuildError(p, 'Enum')
353 353
354 # [21] 354 # [21]
355 def p_EnumValueList(self, p): 355 def p_EnumValueList(self, p):
356 """EnumValueList : ExtendedAttributeList string EnumValues""" 356 """EnumValueList : ExtendedAttributeList string EnumValueListComma"""
357 enum = self.BuildNamed('EnumItem', p, 2, p[1]) 357 enum = self.BuildNamed('EnumItem', p, 2, p[1])
358 p[0] = ListFromConcat(enum, p[3]) 358 p[0] = ListFromConcat(enum, p[3])
359 359
360 # [22] 360 # [22]
361 def p_EnumValues(self, p): 361 def p_EnumValueListComma(self, p):
362 """EnumValues : ',' ExtendedAttributeList string EnumValues 362 """EnumValueListComma : ',' EnumValueListString
363 |""" 363 |"""
364 if len(p) > 1: 364 if len(p) > 1:
365 enum = self.BuildNamed('EnumItem', p, 3, p[2]) 365 p[0] = p[2]
366 p[0] = ListFromConcat(enum, p[4])
367 366
368 # [23] 367 # [23]
368 def p_EnumValueListString(self, p):
369 """EnumValueListString : ExtendedAttributeList string EnumValueListComma
370 |"""
371 if len(p) > 1:
372 enum = self.BuildNamed('EnumItem', p, 2, p[1])
373 p[0] = ListFromConcat(enum, p[3])
374
375 # [24]
369 def p_CallbackRest(self, p): 376 def p_CallbackRest(self, p):
370 """CallbackRest : identifier '=' ReturnType '(' ArgumentList ')' ';'""" 377 """CallbackRest : identifier '=' ReturnType '(' ArgumentList ')' ';'"""
371 arguments = self.BuildProduction('Arguments', p, 4, p[5]) 378 arguments = self.BuildProduction('Arguments', p, 4, p[5])
372 p[0] = self.BuildNamed('Callback', p, 1, ListFromConcat(p[3], arguments)) 379 p[0] = self.BuildNamed('Callback', p, 1, ListFromConcat(p[3], arguments))
373 380
374 # [24] 381 # [25]
375 def p_Typedef(self, p): 382 def p_Typedef(self, p):
376 """Typedef : TYPEDEF ExtendedAttributeListNoComments Type identifier ';'""" 383 """Typedef : TYPEDEF ExtendedAttributeListNoComments Type identifier ';'"""
377 p[0] = self.BuildNamed('Typedef', p, 4, ListFromConcat(p[2], p[3])) 384 p[0] = self.BuildNamed('Typedef', p, 4, ListFromConcat(p[2], p[3]))
378 385
379 # [24.1] Error recovery for Typedefs 386 # [25.1] Error recovery for Typedefs
380 def p_TypedefError(self, p): 387 def p_TypedefError(self, p):
381 """Typedef : TYPEDEF error ';'""" 388 """Typedef : TYPEDEF error ';'"""
382 p[0] = self.BuildError(p, 'Typedef') 389 p[0] = self.BuildError(p, 'Typedef')
383 390
384 # [25] 391 # [26]
385 def p_ImplementsStatement(self, p): 392 def p_ImplementsStatement(self, p):
386 """ImplementsStatement : identifier IMPLEMENTS identifier ';'""" 393 """ImplementsStatement : identifier IMPLEMENTS identifier ';'"""
387 name = self.BuildAttribute('REFERENCE', p[3]) 394 name = self.BuildAttribute('REFERENCE', p[3])
388 p[0] = self.BuildNamed('Implements', p, 1, name) 395 p[0] = self.BuildNamed('Implements', p, 1, name)
389 396
390 # [26] 397 # [27]
391 def p_Const(self, p): 398 def p_Const(self, p):
392 """Const : CONST ConstType identifier '=' ConstValue ';'""" 399 """Const : CONST ConstType identifier '=' ConstValue ';'"""
393 value = self.BuildProduction('Value', p, 5, p[5]) 400 value = self.BuildProduction('Value', p, 5, p[5])
394 p[0] = self.BuildNamed('Const', p, 3, ListFromConcat(p[2], value)) 401 p[0] = self.BuildNamed('Const', p, 3, ListFromConcat(p[2], value))
395 402
396 # [27] 403 # [28]
397 def p_ConstValue(self, p): 404 def p_ConstValue(self, p):
398 """ConstValue : BooleanLiteral 405 """ConstValue : BooleanLiteral
399 | FloatLiteral 406 | FloatLiteral
400 | integer 407 | integer
401 | null""" 408 | null"""
402 if type(p[1]) == str: 409 if type(p[1]) == str:
403 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'integer'), 410 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'integer'),
404 self.BuildAttribute('NAME', p[1])) 411 self.BuildAttribute('NAME', p[1]))
405 else: 412 else:
406 p[0] = p[1] 413 p[0] = p[1]
407 414
408 # [27.1] Add definition for NULL 415 # [28.1] Add definition for NULL
409 def p_null(self, p): 416 def p_null(self, p):
410 """null : NULL""" 417 """null : NULL"""
411 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'NULL'), 418 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'NULL'),
412 self.BuildAttribute('NAME', 'NULL')) 419 self.BuildAttribute('NAME', 'NULL'))
413 420
414 # [28] 421 # [29]
415 def p_BooleanLiteral(self, p): 422 def p_BooleanLiteral(self, p):
416 """BooleanLiteral : TRUE 423 """BooleanLiteral : TRUE
417 | FALSE""" 424 | FALSE"""
418 value = self.BuildAttribute('VALUE', Boolean(p[1] == 'true')) 425 value = self.BuildAttribute('VALUE', Boolean(p[1] == 'true'))
419 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'boolean'), value) 426 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'boolean'), value)
420 427
421 # [29] 428 # [30]
422 def p_FloatLiteral(self, p): 429 def p_FloatLiteral(self, p):
423 """FloatLiteral : float 430 """FloatLiteral : float
424 | '-' INFINITY 431 | '-' INFINITY
425 | INFINITY 432 | INFINITY
426 | NAN """ 433 | NAN """
427 if len(p) > 2: 434 if len(p) > 2:
428 val = '-Infinity' 435 val = '-Infinity'
429 else: 436 else:
430 val = p[1] 437 val = p[1]
431 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'float'), 438 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'float'),
432 self.BuildAttribute('VALUE', val)) 439 self.BuildAttribute('VALUE', val))
433 440
434 # [30] 441 # [31] Removed unsupported: Serializer, Stringifier
435 def p_AttributeOrOperation(self, p): 442 def p_AttributeOrOperationOrIterator(self, p):
436 """AttributeOrOperation : STRINGIFIER StringifierAttributeOrOperation 443 """AttributeOrOperationOrIterator : StaticMember
437 | Attribute 444 | Attribute
438 | Operation""" 445 | OperationOrIterator"""
439 if len(p) > 2: 446 p[0] = p[1]
447
448 # [32-37] NOT IMPLEMENTED (Serializer)
449 # [38-39] FIXME: NOT IMPLEMENTED (Stringifier) http://crbug.com/306606
450
451 # [40]
452 def p_StaticMember(self, p):
453 """StaticMember : STATIC StaticMemberRest"""
454 p[2].AddChildren(self.BuildTrue('STATIC'))
455 p[0] = p[2]
456
457 # [41]
458 def p_StaticMemberRest(self, p):
459 """StaticMemberRest : AttributeRest
460 | ReturnType OperationRest"""
461 if len(p) == 2:
462 p[0] = p[1]
463 else:
464 p[2].AddChildren(p[1])
440 p[0] = p[2] 465 p[0] = p[2]
441 else:
442 p[0] = p[1]
443 466
444 # [31] 467 # [42]
445 def p_StringifierAttributeOrOperation(self, p): 468 def p_Attribute(self, p):
446 """StringifierAttributeOrOperation : Attribute 469 """Attribute : Inherit AttributeRest"""
447 | OperationRest 470 p[2].AddChildren(ListFromConcat(p[1]))
448 | ';'""" 471 p[0] = p[2]
449 if p[1] == ';':
450 p[0] = self.BuildAttribute('STRINGIFIER', Boolean(True))
451 else:
452 p[0] = ListFromConcat(self.BuildAttribute('STRINGIFIER', p[1]), p[1])
453 472
454 # [32] 473 # [43]
455 def p_Attribute(self, p): 474 def p_AttributeRest(self, p):
456 """Attribute : Inherit ReadOnly ATTRIBUTE Type identifier ';'""" 475 """AttributeRest : ReadOnly ATTRIBUTE Type identifier ';'"""
457 p[0] = self.BuildNamed('Attribute', p, 5, 476 p[0] = self.BuildNamed('Attribute', p, 4,
458 ListFromConcat(p[1], p[2], p[4])) 477 ListFromConcat(p[1], p[3]))
459 478
460 # [33] 479 # [44]
461 def p_Inherit(self, p): 480 def p_Inherit(self, p):
462 """Inherit : INHERIT 481 """Inherit : INHERIT
463 |""" 482 |"""
464 if len(p) > 1: 483 if len(p) > 1:
465 p[0] = self.BuildTrue('INHERIT') 484 p[0] = self.BuildTrue('INHERIT')
466 485
467 # [34] 486 # [45]
468 def p_ReadOnly(self, p): 487 def p_ReadOnly(self, p):
469 """ReadOnly : READONLY 488 """ReadOnly : READONLY
470 |""" 489 |"""
471 if len(p) > 1: 490 if len(p) > 1:
472 p[0] = self.BuildTrue('READONLY') 491 p[0] = self.BuildTrue('READONLY')
473 492
474 # [35] 493 # [46]
475 def p_Operation(self, p): 494 def p_OperationOrIterator(self, p):
476 """Operation : Qualifiers OperationRest""" 495 """OperationOrIterator : ReturnType OperationOrIteratorRest
477 p[2].AddChildren(p[1]) 496 | SpecialOperation"""
478 p[0] = p[2] 497 if len(p) == 3:
479 498 p[2].AddChildren(p[1])
480 # [36] 499 p[0] = p[2]
481 def p_Qualifiers(self, p):
482 """Qualifiers : STATIC
483 | Specials"""
484 if p[1] == 'static':
485 p[0] = self.BuildTrue('STATIC')
486 else: 500 else:
487 p[0] = p[1] 501 p[0] = p[1]
488 502
489 # [37] 503 # [47]
504 def p_SpecialOperation(self, p):
505 """SpecialOperation : Special Specials ReturnType OperationRest"""
506 p[4].AddChildren(ListFromConcat(p[1], p[2], p[3]))
507 p[0] = p[4]
508
509 # [48]
490 def p_Specials(self, p): 510 def p_Specials(self, p):
491 """Specials : Special Specials 511 """Specials : Special Specials
492 | """ 512 | """
493 if len(p) > 1: 513 if len(p) > 1:
494 p[0] = ListFromConcat(p[1], p[2]) 514 p[0] = ListFromConcat(p[1], p[2])
495 515
496 # [38] 516 # [49]
497 def p_Special(self, p): 517 def p_Special(self, p):
498 """Special : GETTER 518 """Special : GETTER
499 | SETTER 519 | SETTER
500 | CREATOR 520 | CREATOR
501 | DELETER 521 | DELETER
502 | LEGACYCALLER""" 522 | LEGACYCALLER"""
503 p[0] = self.BuildTrue(p[1].upper()) 523 p[0] = self.BuildTrue(p[1].upper())
504 524
525 # [50] Removed unsupported: IteratorRest
526 def p_OperationOrIteratorRest(self, p):
527 """OperationOrIteratorRest : OperationRest"""
528 p[0] = p[1]
505 529
506 # [39] 530 # [51-53] NOT IMPLEMENTED (IteratorRest)
531
532 # [54]
507 def p_OperationRest(self, p): 533 def p_OperationRest(self, p):
508 """OperationRest : ReturnType OptionalIdentifier '(' ArgumentList ')' ';'""" 534 """OperationRest : OptionalIdentifier '(' ArgumentList ')' ';'"""
509 arguments = self.BuildProduction('Arguments', p, 3, p[4]) 535 arguments = self.BuildProduction('Arguments', p, 2, p[3])
510 p[0] = self.BuildNamed('Operation', p, 2, ListFromConcat(p[1], arguments)) 536 p[0] = self.BuildNamed('Operation', p, 1, arguments)
511 537
512 # [40] 538 # [55]
513 def p_OptionalIdentifier(self, p): 539 def p_OptionalIdentifier(self, p):
514 """OptionalIdentifier : identifier 540 """OptionalIdentifier : identifier
515 |""" 541 |"""
516 if len(p) > 1: 542 if len(p) > 1:
517 p[0] = p[1] 543 p[0] = p[1]
518 else: 544 else:
519 p[0] = '_unnamed_' 545 p[0] = '_unnamed_'
520 546
521 # [41] 547 # [56]
522 def p_ArgumentList(self, p): 548 def p_ArgumentList(self, p):
523 """ArgumentList : Argument Arguments 549 """ArgumentList : Argument Arguments
524 |""" 550 |"""
525 if len(p) > 1: 551 if len(p) > 1:
526 p[0] = ListFromConcat(p[1], p[2]) 552 p[0] = ListFromConcat(p[1], p[2])
527 553
528 # [41.1] ArgumentList error recovery 554 # [56.1] ArgumentList error recovery
529 def p_ArgumentListError(self, p): 555 def p_ArgumentListError(self, p):
530 """ArgumentList : error """ 556 """ArgumentList : error """
531 p[0] = self.BuildError(p, 'ArgumentList') 557 p[0] = self.BuildError(p, 'ArgumentList')
532 558
533 # [42] 559 # [57]
534 def p_Arguments(self, p): 560 def p_Arguments(self, p):
535 """Arguments : ',' Argument Arguments 561 """Arguments : ',' Argument Arguments
536 |""" 562 |"""
537 if len(p) > 1: 563 if len(p) > 1:
538 p[0] = ListFromConcat(p[2], p[3]) 564 p[0] = ListFromConcat(p[2], p[3])
539 565
540 # [43] 566 # [58]
541 def p_Argument(self, p): 567 def p_Argument(self, p):
542 """Argument : ExtendedAttributeList OptionalOrRequiredArgument""" 568 """Argument : ExtendedAttributeList OptionalOrRequiredArgument"""
543 p[2].AddChildren(p[1]) 569 p[2].AddChildren(p[1])
544 p[0] = p[2] 570 p[0] = p[2]
545 571
546 572 # [59]
547 # [44]
548 def p_OptionalOrRequiredArgument(self, p): 573 def p_OptionalOrRequiredArgument(self, p):
549 """OptionalOrRequiredArgument : OPTIONAL Type ArgumentName Default 574 """OptionalOrRequiredArgument : OPTIONAL Type ArgumentName Default
550 | Type Ellipsis ArgumentName""" 575 | Type Ellipsis ArgumentName"""
551 if len(p) > 4: 576 if len(p) > 4:
552 arg = self.BuildNamed('Argument', p, 3, ListFromConcat(p[2], p[4])) 577 arg = self.BuildNamed('Argument', p, 3, ListFromConcat(p[2], p[4]))
553 arg.AddChildren(self.BuildTrue('OPTIONAL')) 578 arg.AddChildren(self.BuildTrue('OPTIONAL'))
554 else: 579 else:
555 arg = self.BuildNamed('Argument', p, 3, ListFromConcat(p[1], p[2])) 580 arg = self.BuildNamed('Argument', p, 3, ListFromConcat(p[1], p[2]))
556 p[0] = arg 581 p[0] = arg
557 582
558 # [45] 583 # [60]
559 def p_ArgumentName(self, p): 584 def p_ArgumentName(self, p):
560 """ArgumentName : ArgumentNameKeyword 585 """ArgumentName : ArgumentNameKeyword
561 | identifier""" 586 | identifier"""
562 p[0] = p[1] 587 p[0] = p[1]
563 588
564 # [46] 589 # [61]
565 def p_Ellipsis(self, p): 590 def p_Ellipsis(self, p):
566 """Ellipsis : ELLIPSIS 591 """Ellipsis : ELLIPSIS
567 |""" 592 |"""
568 if len(p) > 1: 593 if len(p) > 1:
569 p[0] = self.BuildNamed('Argument', p, 1) 594 p[0] = self.BuildNamed('Argument', p, 1)
570 p[0].AddChildren(self.BuildTrue('ELLIPSIS')) 595 p[0].AddChildren(self.BuildTrue('ELLIPSIS'))
571 596
572 # [47] 597 # [62]
573 def p_ExceptionMember(self, p): 598 def p_ExceptionMember(self, p):
574 """ExceptionMember : Const 599 """ExceptionMember : Const
575 | ExceptionField""" 600 | ExceptionField"""
576 p[0] = p[1] 601 p[0] = p[1]
577 602
578 # [48] 603 # [63]
579 def p_ExceptionField(self, p): 604 def p_ExceptionField(self, p):
580 """ExceptionField : Type identifier ';'""" 605 """ExceptionField : Type identifier ';'"""
581 p[0] = self.BuildNamed('ExceptionField', p, 2, p[1]) 606 p[0] = self.BuildNamed('ExceptionField', p, 2, p[1])
582 607
583 # [48.1] Error recovery for ExceptionMembers 608 # [63.1] Error recovery for ExceptionMembers
584 def p_ExceptionFieldError(self, p): 609 def p_ExceptionFieldError(self, p):
585 """ExceptionField : error""" 610 """ExceptionField : error"""
586 p[0] = self.BuildError(p, 'ExceptionField') 611 p[0] = self.BuildError(p, 'ExceptionField')
587 612
588 # [49] No comment version for mid statement attributes. 613 # [64] No comment version for mid statement attributes.
589 def p_ExtendedAttributeListNoComments(self, p): 614 def p_ExtendedAttributeListNoComments(self, p):
590 """ExtendedAttributeListNoComments : '[' ExtendedAttribute ExtendedAttribute s ']' 615 """ExtendedAttributeListNoComments : '[' ExtendedAttribute ExtendedAttribute s ']'
591 | """ 616 | """
592 if len(p) > 2: 617 if len(p) > 2:
593 items = ListFromConcat(p[2], p[3]) 618 items = ListFromConcat(p[2], p[3])
594 p[0] = self.BuildProduction('ExtAttributes', p, 1, items) 619 p[0] = self.BuildProduction('ExtAttributes', p, 1, items)
595 620
596 # [49.1] Add optional comment field for start of statements. 621 # [64.1] Add optional comment field for start of statements.
597 def p_ExtendedAttributeList(self, p): 622 def p_ExtendedAttributeList(self, p):
598 """ExtendedAttributeList : Comments '[' ExtendedAttribute ExtendedAttributes ']' 623 """ExtendedAttributeList : Comments '[' ExtendedAttribute ExtendedAttributes ']'
599 | Comments """ 624 | Comments """
600 if len(p) > 2: 625 if len(p) > 2:
601 items = ListFromConcat(p[3], p[4]) 626 items = ListFromConcat(p[3], p[4])
602 attribs = self.BuildProduction('ExtAttributes', p, 2, items) 627 attribs = self.BuildProduction('ExtAttributes', p, 2, items)
603 p[0] = ListFromConcat(p[1], attribs) 628 p[0] = ListFromConcat(p[1], attribs)
604 else: 629 else:
605 p[0] = p[1] 630 p[0] = p[1]
606 631
607 # [50] 632 # [65]
608 def p_ExtendedAttributes(self, p): 633 def p_ExtendedAttributes(self, p):
609 """ExtendedAttributes : ',' ExtendedAttribute ExtendedAttributes 634 """ExtendedAttributes : ',' ExtendedAttribute ExtendedAttributes
610 |""" 635 |"""
611 if len(p) > 1: 636 if len(p) > 1:
612 p[0] = ListFromConcat(p[2], p[3]) 637 p[0] = ListFromConcat(p[2], p[3])
613 638
614 # We only support: 639 # We only support:
615 # [ identifier ] 640 # [ identifier ]
616 # [ identifier = identifier ] 641 # [ identifier = identifier ]
617 # [ identifier ( ArgumentList )] 642 # [ identifier ( ArgumentList )]
618 # [ identifier = identifier ( ArgumentList )] 643 # [ identifier = identifier ( ArgumentList )]
619 # [51] map directly to 74-77 644 # [66] map directly to [91-93, 95]
620 # [52-54, 56] are unsupported 645 # [67-69, 71] are unsupported
621 def p_ExtendedAttribute(self, p): 646 def p_ExtendedAttribute(self, p):
622 """ExtendedAttribute : ExtendedAttributeNoArgs 647 """ExtendedAttribute : ExtendedAttributeNoArgs
623 | ExtendedAttributeArgList 648 | ExtendedAttributeArgList
624 | ExtendedAttributeIdent 649 | ExtendedAttributeIdent
625 | ExtendedAttributeNamedArgList""" 650 | ExtendedAttributeNamedArgList"""
626 p[0] = p[1] 651 p[0] = p[1]
627 652
628 # [55] 653 # [70]
629 def p_ArgumentNameKeyword(self, p): 654 def p_ArgumentNameKeyword(self, p):
630 """ArgumentNameKeyword : ATTRIBUTE 655 """ArgumentNameKeyword : ATTRIBUTE
631 | CALLBACK 656 | CALLBACK
632 | CONST 657 | CONST
633 | CREATOR 658 | CREATOR
634 | DELETER 659 | DELETER
635 | DICTIONARY 660 | DICTIONARY
636 | ENUM 661 | ENUM
637 | EXCEPTION 662 | EXCEPTION
638 | GETTER 663 | GETTER
639 | IMPLEMENTS 664 | IMPLEMENTS
640 | INHERIT 665 | INHERIT
641 | LEGACYCALLER 666 | LEGACYCALLER
642 | PARTIAL 667 | PARTIAL
668 | SERIALIZER
643 | SETTER 669 | SETTER
644 | STATIC 670 | STATIC
645 | STRINGIFIER 671 | STRINGIFIER
646 | TYPEDEF 672 | TYPEDEF
647 | UNRESTRICTED""" 673 | UNRESTRICTED"""
648 p[0] = p[1] 674 p[0] = p[1]
649 675
650 # [57] 676 # [72]
651 def p_Type(self, p): 677 def p_Type(self, p):
652 """Type : SingleType 678 """Type : SingleType
653 | UnionType TypeSuffix""" 679 | UnionType TypeSuffix"""
654 if len(p) == 2: 680 if len(p) == 2:
655 p[0] = self.BuildProduction('Type', p, 1, p[1]) 681 p[0] = self.BuildProduction('Type', p, 1, p[1])
656 else: 682 else:
657 p[0] = self.BuildProduction('Type', p, 1, ListFromConcat(p[1], p[2])) 683 p[0] = self.BuildProduction('Type', p, 1, ListFromConcat(p[1], p[2]))
658 684
659 # [58] 685 # [73]
660 def p_SingleType(self, p): 686 def p_SingleType(self, p):
661 """SingleType : NonAnyType 687 """SingleType : NonAnyType
662 | ANY TypeSuffixStartingWithArray""" 688 | ANY TypeSuffixStartingWithArray"""
663 if len(p) == 2: 689 if len(p) == 2:
664 p[0] = p[1] 690 p[0] = p[1]
665 else: 691 else:
666 p[0] = ListFromConcat(self.BuildProduction('Any', p, 1), p[2]) 692 p[0] = ListFromConcat(self.BuildProduction('Any', p, 1), p[2])
667 693
668 # [59] 694 # [74]
669 def p_UnionType(self, p): 695 def p_UnionType(self, p):
670 """UnionType : '(' UnionMemberType OR UnionMemberType UnionMemberTypes ')'"" " 696 """UnionType : '(' UnionMemberType OR UnionMemberType UnionMemberTypes ')'"" "
671 697
672 # [60] 698 # [75]
673 def p_UnionMemberType(self, p): 699 def p_UnionMemberType(self, p):
674 """UnionMemberType : NonAnyType 700 """UnionMemberType : NonAnyType
675 | UnionType TypeSuffix 701 | UnionType TypeSuffix
676 | ANY '[' ']' TypeSuffix""" 702 | ANY '[' ']' TypeSuffix"""
677 # [61] 703 # [76]
678 def p_UnionMemberTypes(self, p): 704 def p_UnionMemberTypes(self, p):
679 """UnionMemberTypes : OR UnionMemberType UnionMemberTypes 705 """UnionMemberTypes : OR UnionMemberType UnionMemberTypes
680 |""" 706 |"""
681 707
682 # [62] Moved DATE, DOMSTRING, OBJECT to PrimitiveType 708 # [77] Moved BYTESTRING, DOMSTRING, OBJECT, DATE, REGEXP to PrimitiveType
709 # Moving all built-in types into PrimitiveType makes it easier to
710 # differentiate between them and 'identifier', since p[1] would be a string in
711 # both cases.
683 def p_NonAnyType(self, p): 712 def p_NonAnyType(self, p):
684 """NonAnyType : PrimitiveType TypeSuffix 713 """NonAnyType : PrimitiveType TypeSuffix
685 | identifier TypeSuffix 714 | identifier TypeSuffix
686 | SEQUENCE '<' Type '>' Null""" 715 | SEQUENCE '<' Type '>' Null"""
687 if len(p) == 3: 716 if len(p) == 3:
688 if type(p[1]) == str: 717 if type(p[1]) == str:
689 typeref = self.BuildNamed('Typeref', p, 1) 718 typeref = self.BuildNamed('Typeref', p, 1)
690 else: 719 else:
691 typeref = p[1] 720 typeref = p[1]
692 p[0] = ListFromConcat(typeref, p[2]) 721 p[0] = ListFromConcat(typeref, p[2])
693 722
694 if len(p) == 6: 723 if len(p) == 6:
695 p[0] = self.BuildProduction('Sequence', p, 1, ListFromConcat(p[3], p[5])) 724 p[0] = self.BuildProduction('Sequence', p, 1, ListFromConcat(p[3], p[5]))
696 725
697 726
698 # [63] 727 # [78]
699 def p_ConstType(self, p): 728 def p_ConstType(self, p):
700 """ConstType : PrimitiveType Null 729 """ConstType : PrimitiveType Null
701 | identifier Null""" 730 | identifier Null"""
702 if type(p[1]) == str: 731 if type(p[1]) == str:
703 p[0] = self.BuildNamed('Typeref', p, 1, p[2]) 732 p[0] = self.BuildNamed('Typeref', p, 1, p[2])
704 else: 733 else:
705 p[1].AddChildren(p[2]) 734 p[1].AddChildren(p[2])
706 p[0] = p[1] 735 p[0] = p[1]
707 736
708 737
709 # [64] 738 # [79] Added BYTESTRING, DOMSTRING, OBJECT, DATE, REGEXP
710 def p_PrimitiveType(self, p): 739 def p_PrimitiveType(self, p):
711 """PrimitiveType : UnsignedIntegerType 740 """PrimitiveType : UnsignedIntegerType
712 | UnrestrictedFloatType 741 | UnrestrictedFloatType
713 | BOOLEAN 742 | BOOLEAN
714 | BYTE 743 | BYTE
715 | OCTET 744 | OCTET
745 | BYTESTRING
716 | DOMSTRING 746 | DOMSTRING
747 | OBJECT
717 | DATE 748 | DATE
718 | OBJECT""" 749 | REGEXP"""
719 if type(p[1]) == str: 750 if type(p[1]) == str:
720 p[0] = self.BuildNamed('PrimitiveType', p, 1) 751 p[0] = self.BuildNamed('PrimitiveType', p, 1)
721 else: 752 else:
722 p[0] = p[1] 753 p[0] = p[1]
723 754
724 755
725 # [65] 756 # [80]
726 def p_UnrestrictedFloatType(self, p): 757 def p_UnrestrictedFloatType(self, p):
727 """UnrestrictedFloatType : UNRESTRICTED FloatType 758 """UnrestrictedFloatType : UNRESTRICTED FloatType
728 | FloatType""" 759 | FloatType"""
729 if len(p) == 2: 760 if len(p) == 2:
730 typeref = self.BuildNamed('PrimitiveType', p, 1) 761 typeref = self.BuildNamed('PrimitiveType', p, 1)
731 else: 762 else:
732 typeref = self.BuildNamed('PrimitiveType', p, 2) 763 typeref = self.BuildNamed('PrimitiveType', p, 2)
733 typeref.AddChildren(self.BuildTrue('UNRESTRICTED')) 764 typeref.AddChildren(self.BuildTrue('UNRESTRICTED'))
734 p[0] = typeref 765 p[0] = typeref
735 766
736 767
737 # [66] 768 # [81]
738 def p_FloatType(self, p): 769 def p_FloatType(self, p):
739 """FloatType : FLOAT 770 """FloatType : FLOAT
740 | DOUBLE""" 771 | DOUBLE"""
741 p[0] = p[1] 772 p[0] = p[1]
742 773
743 # [67] 774 # [82]
744 def p_UnsignedIntegerType(self, p): 775 def p_UnsignedIntegerType(self, p):
745 """UnsignedIntegerType : UNSIGNED IntegerType 776 """UnsignedIntegerType : UNSIGNED IntegerType
746 | IntegerType""" 777 | IntegerType"""
747 if len(p) == 2: 778 if len(p) == 2:
748 p[0] = p[1] 779 p[0] = p[1]
749 else: 780 else:
750 p[0] = 'unsigned ' + p[2] 781 p[0] = 'unsigned ' + p[2]
751 782
752 # [68] 783 # [83]
753 def p_IntegerType(self, p): 784 def p_IntegerType(self, p):
754 """IntegerType : SHORT 785 """IntegerType : SHORT
755 | LONG OptionalLong""" 786 | LONG OptionalLong"""
756 if len(p) == 2: 787 if len(p) == 2:
757 p[0] = p[1] 788 p[0] = p[1]
758 else: 789 else:
759 p[0] = p[1] + p[2] 790 p[0] = p[1] + p[2]
760 791
761 # [69] 792 # [84]
762 def p_OptionalLong(self, p): 793 def p_OptionalLong(self, p):
763 """OptionalLong : LONG 794 """OptionalLong : LONG
764 | """ 795 | """
765 if len(p) > 1: 796 if len(p) > 1:
766 p[0] = ' ' + p[1] 797 p[0] = ' ' + p[1]
767 else: 798 else:
768 p[0] = '' 799 p[0] = ''
769 800
770 801
771 # [70] Add support for sized array 802 # [85] Add support for sized array
772 def p_TypeSuffix(self, p): 803 def p_TypeSuffix(self, p):
773 """TypeSuffix : '[' integer ']' TypeSuffix 804 """TypeSuffix : '[' integer ']' TypeSuffix
774 | '[' ']' TypeSuffix 805 | '[' ']' TypeSuffix
775 | '?' TypeSuffixStartingWithArray 806 | '?' TypeSuffixStartingWithArray
776 | """ 807 | """
777 if len(p) == 5: 808 if len(p) == 5:
778 p[0] = self.BuildNamed('Array', p, 2, p[4]) 809 p[0] = self.BuildNamed('Array', p, 2, p[4])
779 810
780 if len(p) == 4: 811 if len(p) == 4:
781 p[0] = self.BuildProduction('Array', p, 1, p[3]) 812 p[0] = self.BuildProduction('Array', p, 1, p[3])
782 813
783 if len(p) == 3: 814 if len(p) == 3:
784 p[0] = ListFromConcat(self.BuildTrue('NULLABLE'), p[2]) 815 p[0] = ListFromConcat(self.BuildTrue('NULLABLE'), p[2])
785 816
786 817
787 # [71] 818 # [86]
788 def p_TypeSuffixStartingWithArray(self, p): 819 def p_TypeSuffixStartingWithArray(self, p):
789 """TypeSuffixStartingWithArray : '[' ']' TypeSuffix 820 """TypeSuffixStartingWithArray : '[' ']' TypeSuffix
790 | """ 821 | """
791 if len(p) > 1: 822 if len(p) > 1:
792 p[0] = self.BuildProduction('Array', p, 0, p[3]) 823 p[0] = self.BuildProduction('Array', p, 0, p[3])
793 824
794 # [72] 825 # [87]
795 def p_Null(self, p): 826 def p_Null(self, p):
796 """Null : '?' 827 """Null : '?'
797 |""" 828 |"""
798 if len(p) > 1: 829 if len(p) > 1:
799 p[0] = self.BuildTrue('NULLABLE') 830 p[0] = self.BuildTrue('NULLABLE')
800 831
801 # [73] 832 # [88]
802 def p_ReturnType(self, p): 833 def p_ReturnType(self, p):
803 """ReturnType : Type 834 """ReturnType : Type
804 | VOID""" 835 | VOID"""
805 if p[1] == 'void': 836 if p[1] == 'void':
806 p[0] = self.BuildProduction('Type', p, 1) 837 p[0] = self.BuildProduction('Type', p, 1)
807 p[0].AddChildren(self.BuildNamed('PrimitiveType', p, 1)) 838 p[0].AddChildren(self.BuildNamed('PrimitiveType', p, 1))
808 else: 839 else:
809 p[0] = p[1] 840 p[0] = p[1]
810 841
811 # [74] 842 # [89-90] NOT IMPLEMENTED (IdentifierList)
843
844 # [91]
812 def p_ExtendedAttributeNoArgs(self, p): 845 def p_ExtendedAttributeNoArgs(self, p):
813 """ExtendedAttributeNoArgs : identifier""" 846 """ExtendedAttributeNoArgs : identifier"""
814 p[0] = self.BuildNamed('ExtAttribute', p, 1) 847 p[0] = self.BuildNamed('ExtAttribute', p, 1)
815 848
816 # [75] 849 # [92]
817 def p_ExtendedAttributeArgList(self, p): 850 def p_ExtendedAttributeArgList(self, p):
818 """ExtendedAttributeArgList : identifier '(' ArgumentList ')'""" 851 """ExtendedAttributeArgList : identifier '(' ArgumentList ')'"""
819 arguments = self.BuildProduction('Arguments', p, 2, p[3]) 852 arguments = self.BuildProduction('Arguments', p, 2, p[3])
820 p[0] = self.BuildNamed('ExtAttribute', p, 1, arguments) 853 p[0] = self.BuildNamed('ExtAttribute', p, 1, arguments)
821 854
822 # [76] 855 # [93]
823 def p_ExtendedAttributeIdent(self, p): 856 def p_ExtendedAttributeIdent(self, p):
824 """ExtendedAttributeIdent : identifier '=' identifier""" 857 """ExtendedAttributeIdent : identifier '=' identifier"""
825 value = self.BuildAttribute('VALUE', p[3]) 858 value = self.BuildAttribute('VALUE', p[3])
826 p[0] = self.BuildNamed('ExtAttribute', p, 1, value) 859 p[0] = self.BuildNamed('ExtAttribute', p, 1, value)
827 860
828 # [77] 861 # [94] NOT IMPLEMENTED (ExtendedAttributeIdentList)
862
863 # [95]
829 def p_ExtendedAttributeNamedArgList(self, p): 864 def p_ExtendedAttributeNamedArgList(self, p):
830 """ExtendedAttributeNamedArgList : identifier '=' identifier '(' ArgumentLis t ')'""" 865 """ExtendedAttributeNamedArgList : identifier '=' identifier '(' ArgumentLis t ')'"""
831 args = self.BuildProduction('Arguments', p, 4, p[5]) 866 args = self.BuildProduction('Arguments', p, 4, p[5])
832 value = self.BuildNamed('Call', p, 3, args) 867 value = self.BuildNamed('Call', p, 3, args)
833 p[0] = self.BuildNamed('ExtAttribute', p, 1, value) 868 p[0] = self.BuildNamed('ExtAttribute', p, 1, value)
834 869
870 # [96] NOT IMPLEMENTED (ExtendedAttributeTypePair)
871
835 # 872 #
836 # Parser Errors 873 # Parser Errors
837 # 874 #
838 # p_error is called whenever the parser can not find a pattern match for 875 # p_error is called whenever the parser can not find a pattern match for
839 # a set of items from the current state. The p_error function defined here 876 # a set of items from the current state. The p_error function defined here
840 # is triggered logging an error, and parsing recovery happens as the 877 # is triggered logging an error, and parsing recovery happens as the
841 # p_<type>_error functions defined above are called. This allows the parser 878 # p_<type>_error functions defined above are called. This allows the parser
842 # to continue so as to capture more than one error per file. 879 # to continue so as to capture more than one error per file.
843 # 880 #
844 def p_error(self, t): 881 def p_error(self, t):
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
1044 1081
1045 print '\n'.join(ast.Tree(accept_props=['PROD'])) 1082 print '\n'.join(ast.Tree(accept_props=['PROD']))
1046 if errors: 1083 if errors:
1047 print '\nFound %d errors.\n' % errors 1084 print '\nFound %d errors.\n' % errors
1048 1085
1049 return errors 1086 return errors
1050 1087
1051 1088
1052 if __name__ == '__main__': 1089 if __name__ == '__main__':
1053 sys.exit(main(sys.argv[1:])) 1090 sys.exit(main(sys.argv[1:]))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698