ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / tools / idl_parser / idl_parser.py
blobbfe6698771ff5400ea36ad4bd21b56f9c293adba
1 #!/usr/bin/env python
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
4 # found in the LICENSE file.
6 """ Parser for PPAPI IDL """
9 # IDL Parser
11 # The parser is uses the PLY yacc library to build a set of parsing rules based
12 # on WebIDL.
14 # WebIDL, and WebIDL grammar can be found at:
15 # http://heycam.github.io/webidl/
16 # PLY can be found at:
17 # http://www.dabeaz.com/ply/
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
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.
26 # Disable check for line length and Member as Function due to how grammar rules
27 # are defined with PLY
29 # pylint: disable=R0201
30 # pylint: disable=C0301
32 import os.path
33 import sys
34 import time
36 from idl_lexer import IDLLexer
37 from idl_node import IDLAttribute, IDLNode
40 # Try to load the ply module, if not, then assume it is in the third_party
41 # directory.
43 try:
44 # Disable lint check which fails to find the ply module.
45 # pylint: disable=F0401
46 from ply import lex
47 from ply import yacc
48 except ImportError:
49 module_path, module_name = os.path.split(__file__)
50 third_party = os.path.join(module_path, os.par, os.par, 'third_party')
51 sys.path.append(third_party)
52 # pylint: disable=F0401
53 from ply import lex
54 from ply import yacc
57 # ERROR_REMAP
59 # Maps the standard error formula into a more friendly error message.
61 ERROR_REMAP = {
62 'Unexpected ")" after "(".' : 'Empty argument list.',
63 'Unexpected ")" after ",".' : 'Missing argument.',
64 'Unexpected "}" after ",".' : 'Trailing comma in block.',
65 'Unexpected "}" after "{".' : 'Unexpected empty block.',
66 'Unexpected comment after "}".' : 'Unexpected trailing comment.',
67 'Unexpected "{" after keyword "enum".' : 'Enum missing name.',
68 'Unexpected "{" after keyword "struct".' : 'Struct missing name.',
69 'Unexpected "{" after keyword "interface".' : 'Interface missing name.',
73 def Boolean(val):
74 """Convert to strict boolean type."""
75 if val:
76 return True
77 return False
80 def ListFromConcat(*items):
81 """Generate list by concatenating inputs"""
82 itemsout = []
83 for item in items:
84 if item is None:
85 continue
86 if type(item) is not type([]):
87 itemsout.append(item)
88 else:
89 itemsout.extend(item)
91 return itemsout
93 def ExpandProduction(p):
94 if type(p) == list:
95 return '[' + ', '.join([ExpandProduction(x) for x in p]) + ']'
96 if type(p) == IDLNode:
97 return 'Node:' + str(p)
98 if type(p) == IDLAttribute:
99 return 'Attr:' + str(p)
100 if type(p) == str:
101 return 'str:' + p
102 return '%s:%s' % (p.__class__.__name__, str(p))
104 # TokenTypeName
106 # Generate a string which has the type and value of the token.
108 def TokenTypeName(t):
109 if t.type == 'SYMBOL':
110 return 'symbol %s' % t.value
111 if t.type in ['HEX', 'INT', 'OCT', 'FLOAT']:
112 return 'value %s' % t.value
113 if t.type == 'string' :
114 return 'string "%s"' % t.value
115 if t.type == 'COMMENT' :
116 return 'comment'
117 if t.type == t.value:
118 return '"%s"' % t.value
119 if t.type == ',':
120 return 'Comma'
121 if t.type == 'identifier':
122 return 'identifier "%s"' % t.value
123 return 'keyword "%s"' % t.value
127 # IDL Parser
129 # The Parser inherits the from the Lexer to provide PLY with the tokenizing
130 # definitions. Parsing patterns are encoded as functions where p_<name> is
131 # is called any time a patern matching the function documentation is found.
132 # Paterns are expressed in the form of:
133 # """ <new item> : <item> ....
134 # | <item> ...."""
136 # Where new item is the result of a match against one or more sets of items
137 # separated by the "|".
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
141 # used to distinguish between multiple item sets in the pattern.
143 # For more details on parsing refer to the PLY documentation at
144 # http://www.dabeaz.com/ply/
146 # The parser is based on the WebIDL standard. See:
147 # http://heycam.github.io/webidl/#idl-grammar
149 # The various productions are annotated so that the WHOLE number greater than
150 # zero in the comment denotes the matching WebIDL grammar definition.
152 # Productions with a fractional component in the comment denote additions to
153 # the WebIDL spec, such as comments.
157 class IDLParser(object):
159 # We force all input files to start with two comments. The first comment is a
160 # Copyright notice followed by a file comment and finally by file level
161 # productions.
163 # [0] Insert a TOP definition for Copyright and Comments
164 def p_Top(self, p):
165 """Top : COMMENT COMMENT Definitions"""
166 Copyright = self.BuildComment('Copyright', p, 1)
167 Filedoc = self.BuildComment('Comment', p, 2)
168 p[0] = ListFromConcat(Copyright, Filedoc, p[3])
170 # [0.1] Add support for Multiple COMMENTS
171 def p_Comments(self, p):
172 """Comments : CommentsRest"""
173 if len(p) > 1:
174 p[0] = p[1]
176 # [0.2] Produce a COMMENT and aggregate sibling comments
177 def p_CommentsRest(self, p):
178 """CommentsRest : COMMENT CommentsRest
179 | """
180 if len(p) > 1:
181 p[0] = ListFromConcat(self.BuildComment('Comment', p, 1), p[2])
185 #The parser is based on the WebIDL standard. See:
186 # http://heycam.github.io/webidl/#idl-grammar
188 # [1]
189 def p_Definitions(self, p):
190 """Definitions : ExtendedAttributeList Definition Definitions
191 | """
192 if len(p) > 1:
193 p[2].AddChildren(p[1])
194 p[0] = ListFromConcat(p[2], p[3])
196 # [2]
197 def p_Definition(self, p):
198 """Definition : CallbackOrInterface
199 | Partial
200 | Dictionary
201 | Exception
202 | Enum
203 | Typedef
204 | ImplementsStatement"""
205 p[0] = p[1]
207 # [2.1] Error recovery for definition
208 def p_DefinitionError(self, p):
209 """Definition : error ';'"""
210 p[0] = self.BuildError(p, 'Definition')
212 # [3]
213 def p_CallbackOrInterface(self, p):
214 """CallbackOrInterface : CALLBACK CallbackRestOrInterface
215 | Interface"""
216 if len(p) > 2:
217 p[0] = p[2]
218 else:
219 p[0] = p[1]
221 # [4]
222 def p_CallbackRestOrInterface(self, p):
223 """CallbackRestOrInterface : CallbackRest
224 | Interface"""
225 p[0] = p[1]
227 # [5]
228 def p_Interface(self, p):
229 """Interface : INTERFACE identifier Inheritance '{' InterfaceMembers '}' ';'"""
230 p[0] = self.BuildNamed('Interface', p, 2, ListFromConcat(p[3], p[5]))
232 # [5.1] Error recovery for interface.
233 def p_InterfaceError(self, p):
234 """Interface : INTERFACE identifier Inheritance '{' error"""
235 p[0] = self.BuildError(p, 'Interface')
237 # [6]
238 def p_Partial(self, p):
239 """Partial : PARTIAL PartialDefinition"""
240 p[2].AddChildren(self.BuildTrue('Partial'))
241 p[0] = p[2]
243 # [6.1] Error recovery for Partial
244 def p_PartialError(self, p):
245 """Partial : PARTIAL error"""
246 p[0] = self.BuildError(p, 'Partial')
248 # [7]
249 def p_PartialDefinition(self, p):
250 """PartialDefinition : PartialDictionary
251 | PartialInterface"""
252 p[0] = p[1]
254 # [8]
255 def p_PartialInterface(self, p):
256 """PartialInterface : INTERFACE identifier '{' InterfaceMembers '}' ';'"""
257 p[0] = self.BuildNamed('Interface', p, 2, p[4])
259 # [9]
260 def p_InterfaceMembers(self, p):
261 """InterfaceMembers : ExtendedAttributeList InterfaceMember InterfaceMembers
262 |"""
263 if len(p) > 1:
264 p[2].AddChildren(p[1])
265 p[0] = ListFromConcat(p[2], p[3])
267 # [10] Removed unsupported: Serializer
268 def p_InterfaceMember(self, p):
269 """InterfaceMember : Const
270 | Operation
271 | Stringifier
272 | StaticMember
273 | Iterable
274 | ReadonlyMember
275 | ReadWriteAttribute
276 | ReadWriteMaplike
277 | ReadWriteSetlike"""
278 p[0] = p[1]
280 # [11]
281 def p_Dictionary(self, p):
282 """Dictionary : DICTIONARY identifier Inheritance '{' DictionaryMembers '}' ';'"""
283 p[0] = self.BuildNamed('Dictionary', p, 2, ListFromConcat(p[3], p[5]))
285 # [11.1] Error recovery for regular Dictionary
286 def p_DictionaryError(self, p):
287 """Dictionary : DICTIONARY error ';'"""
288 p[0] = self.BuildError(p, 'Dictionary')
290 # [11.2] Error recovery for regular Dictionary
291 # (for errors inside dictionary definition)
292 def p_DictionaryError2(self, p):
293 """Dictionary : DICTIONARY identifier Inheritance '{' error"""
294 p[0] = self.BuildError(p, 'Dictionary')
296 # [12]
297 def p_DictionaryMembers(self, p):
298 """DictionaryMembers : ExtendedAttributeList DictionaryMember DictionaryMembers
299 |"""
300 if len(p) > 1:
301 p[2].AddChildren(p[1])
302 p[0] = ListFromConcat(p[2], p[3])
304 # [13]
305 def p_DictionaryMember(self, p):
306 """DictionaryMember : Type identifier Default ';'"""
307 p[0] = self.BuildNamed('Key', p, 2, ListFromConcat(p[1], p[3]))
309 # [14] NOT IMPLEMENTED (Required)
311 # [15]
312 def p_PartialDictionary(self, p):
313 """PartialDictionary : DICTIONARY identifier '{' DictionaryMembers '}' ';'"""
314 partial = self.BuildTrue('Partial')
315 p[0] = self.BuildNamed('Dictionary', p, 2, ListFromConcat(p[4], partial))
317 # [15.1] Error recovery for Partial Dictionary
318 def p_PartialDictionaryError(self, p):
319 """PartialDictionary : DICTIONARY error ';'"""
320 p[0] = self.BuildError(p, 'PartialDictionary')
322 # [16]
323 def p_Default(self, p):
324 """Default : '=' DefaultValue
325 |"""
326 if len(p) > 1:
327 p[0] = self.BuildProduction('Default', p, 2, p[2])
329 # [17]
330 def p_DefaultValue(self, p):
331 """DefaultValue : ConstValue
332 | string
333 | '[' ']'"""
334 if len(p) == 3:
335 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'sequence'),
336 self.BuildAttribute('VALUE', '[]'))
337 elif type(p[1]) == str:
338 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'DOMString'),
339 self.BuildAttribute('NAME', p[1]))
340 else:
341 p[0] = p[1]
343 # [] - Not specified
344 def p_Exception(self, p):
345 """Exception : EXCEPTION identifier Inheritance '{' ExceptionMembers '}' ';'"""
346 p[0] = self.BuildNamed('Exception', p, 2, ListFromConcat(p[3], p[5]))
348 # [] - Not specified
349 def p_ExceptionMembers(self, p):
350 """ExceptionMembers : ExtendedAttributeList ExceptionMember ExceptionMembers
351 |"""
352 if len(p) > 1:
353 p[2].AddChildren(p[1])
354 p[0] = ListFromConcat(p[2], p[3])
356 # [.1] Error recovery for ExceptionMembers - Not specified
357 def p_ExceptionMembersError(self, p):
358 """ExceptionMembers : error"""
359 p[0] = self.BuildError(p, 'ExceptionMembers')
361 # [18]
362 def p_Inheritance(self, p):
363 """Inheritance : ':' identifier
364 |"""
365 if len(p) > 1:
366 p[0] = self.BuildNamed('Inherit', p, 2)
368 # [19]
369 def p_Enum(self, p):
370 """Enum : ENUM identifier '{' EnumValueList '}' ';'"""
371 p[0] = self.BuildNamed('Enum', p, 2, p[4])
373 # [19.1] Error recovery for Enums
374 def p_EnumError(self, p):
375 """Enum : ENUM error ';'"""
376 p[0] = self.BuildError(p, 'Enum')
378 # [20]
379 def p_EnumValueList(self, p):
380 """EnumValueList : ExtendedAttributeList string EnumValueListComma"""
381 enum = self.BuildNamed('EnumItem', p, 2, p[1])
382 p[0] = ListFromConcat(enum, p[3])
384 # [21]
385 def p_EnumValueListComma(self, p):
386 """EnumValueListComma : ',' EnumValueListString
387 |"""
388 if len(p) > 1:
389 p[0] = p[2]
391 # [22]
392 def p_EnumValueListString(self, p):
393 """EnumValueListString : ExtendedAttributeList string EnumValueListComma
394 |"""
395 if len(p) > 1:
396 enum = self.BuildNamed('EnumItem', p, 2, p[1])
397 p[0] = ListFromConcat(enum, p[3])
399 # [23]
400 def p_CallbackRest(self, p):
401 """CallbackRest : identifier '=' ReturnType '(' ArgumentList ')' ';'"""
402 arguments = self.BuildProduction('Arguments', p, 4, p[5])
403 p[0] = self.BuildNamed('Callback', p, 1, ListFromConcat(p[3], arguments))
405 # [24]
406 def p_Typedef(self, p):
407 """Typedef : TYPEDEF ExtendedAttributeListNoComments Type identifier ';'"""
408 p[0] = self.BuildNamed('Typedef', p, 4, ListFromConcat(p[2], p[3]))
410 # [24.1] Error recovery for Typedefs
411 def p_TypedefError(self, p):
412 """Typedef : TYPEDEF error ';'"""
413 p[0] = self.BuildError(p, 'Typedef')
415 # [25]
416 def p_ImplementsStatement(self, p):
417 """ImplementsStatement : identifier IMPLEMENTS identifier ';'"""
418 name = self.BuildAttribute('REFERENCE', p[3])
419 p[0] = self.BuildNamed('Implements', p, 1, name)
421 # [26]
422 def p_Const(self, p):
423 """Const : CONST ConstType identifier '=' ConstValue ';'"""
424 value = self.BuildProduction('Value', p, 5, p[5])
425 p[0] = self.BuildNamed('Const', p, 3, ListFromConcat(p[2], value))
427 # [27]
428 def p_ConstValue(self, p):
429 """ConstValue : BooleanLiteral
430 | FloatLiteral
431 | integer
432 | null"""
433 if type(p[1]) == str:
434 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'integer'),
435 self.BuildAttribute('NAME', p[1]))
436 else:
437 p[0] = p[1]
439 # [27.1] Add definition for NULL
440 def p_null(self, p):
441 """null : NULL"""
442 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'NULL'),
443 self.BuildAttribute('NAME', 'NULL'))
445 # [28]
446 def p_BooleanLiteral(self, p):
447 """BooleanLiteral : TRUE
448 | FALSE"""
449 value = self.BuildAttribute('VALUE', Boolean(p[1] == 'true'))
450 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'boolean'), value)
452 # [29]
453 def p_FloatLiteral(self, p):
454 """FloatLiteral : float
455 | '-' INFINITY
456 | INFINITY
457 | NAN """
458 if len(p) > 2:
459 val = '-Infinity'
460 else:
461 val = p[1]
462 p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'float'),
463 self.BuildAttribute('VALUE', val))
465 # [30-34] NOT IMPLEMENTED (Serializer)
467 # [35]
468 def p_Stringifier(self, p):
469 """Stringifier : STRINGIFIER StringifierRest"""
470 p[0] = self.BuildProduction('Stringifier', p, 1, p[2])
472 # [36]
473 def p_StringifierRest(self, p):
474 """StringifierRest : AttributeRest
475 | ReturnType OperationRest
476 | ';'"""
477 if len(p) == 3:
478 p[2].AddChildren(p[1])
479 p[0] = p[2]
480 elif p[1] != ';':
481 p[0] = p[1]
483 # [37]
484 def p_StaticMember(self, p):
485 """StaticMember : STATIC StaticMemberRest"""
486 p[2].AddChildren(self.BuildTrue('STATIC'))
487 p[0] = p[2]
489 # [38]
490 def p_StaticMemberRest(self, p):
491 """StaticMemberRest : ReadOnly AttributeRest
492 | ReturnType OperationRest"""
493 if len(p) == 2:
494 p[0] = p[1]
495 else:
496 p[2].AddChildren(p[1])
497 p[0] = p[2]
499 # [39]
500 def p_ReadonlyMember(self, p):
501 """ReadonlyMember : READONLY ReadonlyMemberRest"""
502 p[2].AddChildren(self.BuildTrue('READONLY'))
503 p[0] = p[2]
505 # [40]
506 def p_ReadonlyMemberRest(self, p):
507 """ReadonlyMemberRest : AttributeRest
508 | MaplikeRest
509 | SetlikeRest"""
510 p[0] = p[1]
512 # [41]
513 def p_ReadWriteAttribute(self, p):
514 """ReadWriteAttribute : INHERIT ReadOnly AttributeRest
515 | AttributeRest"""
516 if len(p) > 2:
517 inherit = self.BuildTrue('INHERIT')
518 p[3].AddChildren(ListFromConcat(inherit, p[2]))
519 p[0] = p[3]
520 else:
521 p[0] = p[1]
523 # [42]
524 def p_AttributeRest(self, p):
525 """AttributeRest : ATTRIBUTE Type AttributeName ';'"""
526 p[0] = self.BuildNamed('Attribute', p, 3, p[2])
528 # [43]
529 def p_AttributeName(self, p):
530 """AttributeName : AttributeNameKeyword
531 | identifier"""
532 p[0] = p[1]
534 # [44]
535 def p_AttributeNameKeyword(self, p):
536 """AttributeNameKeyword : REQUIRED"""
537 p[0] = p[1]
539 # [45] Unreferenced in the specification
541 # [46]
542 def p_ReadOnly(self, p):
543 """ReadOnly : READONLY
544 |"""
545 if len(p) > 1:
546 p[0] = self.BuildTrue('READONLY')
548 # [47]
549 def p_Operation(self, p):
550 """Operation : ReturnType OperationRest
551 | SpecialOperation"""
552 if len(p) == 3:
553 p[2].AddChildren(p[1])
554 p[0] = p[2]
555 else:
556 p[0] = p[1]
558 # [48]
559 def p_SpecialOperation(self, p):
560 """SpecialOperation : Special Specials ReturnType OperationRest"""
561 p[4].AddChildren(ListFromConcat(p[1], p[2], p[3]))
562 p[0] = p[4]
564 # [49]
565 def p_Specials(self, p):
566 """Specials : Special Specials
567 | """
568 if len(p) > 1:
569 p[0] = ListFromConcat(p[1], p[2])
571 # [50]
572 def p_Special(self, p):
573 """Special : GETTER
574 | SETTER
575 | CREATOR
576 | DELETER
577 | LEGACYCALLER"""
578 p[0] = self.BuildTrue(p[1].upper())
580 # [51]
581 def p_OperationRest(self, p):
582 """OperationRest : OptionalIdentifier '(' ArgumentList ')' ';'"""
583 arguments = self.BuildProduction('Arguments', p, 2, p[3])
584 p[0] = self.BuildNamed('Operation', p, 1, arguments)
586 # [52]
587 def p_OptionalIdentifier(self, p):
588 """OptionalIdentifier : identifier
589 |"""
590 if len(p) > 1:
591 p[0] = p[1]
592 else:
593 p[0] = '_unnamed_'
595 # [53]
596 def p_ArgumentList(self, p):
597 """ArgumentList : Argument Arguments
598 |"""
599 if len(p) > 1:
600 p[0] = ListFromConcat(p[1], p[2])
602 # [53.1] ArgumentList error recovery
603 def p_ArgumentListError(self, p):
604 """ArgumentList : error """
605 p[0] = self.BuildError(p, 'ArgumentList')
607 # [54]
608 def p_Arguments(self, p):
609 """Arguments : ',' Argument Arguments
610 |"""
611 if len(p) > 1:
612 p[0] = ListFromConcat(p[2], p[3])
614 # [54.1] Arguments error recovery
615 def p_ArgumentsError(self, p):
616 """Arguments : ',' error"""
617 p[0] = self.BuildError(p, 'Arguments')
619 # [55]
620 def p_Argument(self, p):
621 """Argument : ExtendedAttributeList OptionalOrRequiredArgument"""
622 p[2].AddChildren(p[1])
623 p[0] = p[2]
625 # [56]
626 def p_OptionalOrRequiredArgument(self, p):
627 """OptionalOrRequiredArgument : OPTIONAL Type ArgumentName Default
628 | Type Ellipsis ArgumentName"""
629 if len(p) > 4:
630 arg = self.BuildNamed('Argument', p, 3, ListFromConcat(p[2], p[4]))
631 arg.AddChildren(self.BuildTrue('OPTIONAL'))
632 else:
633 arg = self.BuildNamed('Argument', p, 3, ListFromConcat(p[1], p[2]))
634 p[0] = arg
636 # [57]
637 def p_ArgumentName(self, p):
638 """ArgumentName : ArgumentNameKeyword
639 | identifier"""
640 p[0] = p[1]
642 # [58]
643 def p_Ellipsis(self, p):
644 """Ellipsis : ELLIPSIS
645 |"""
646 if len(p) > 1:
647 p[0] = self.BuildNamed('Argument', p, 1)
648 p[0].AddChildren(self.BuildTrue('ELLIPSIS'))
650 # [] Unspecified
651 def p_ExceptionMember(self, p):
652 """ExceptionMember : Const
653 | ExceptionField"""
654 p[0] = p[1]
656 # [] Unspecified
657 def p_ExceptionField(self, p):
658 """ExceptionField : Type identifier ';'"""
659 p[0] = self.BuildNamed('ExceptionField', p, 2, p[1])
661 # [] Error recovery for ExceptionMembers - Unspecified
662 def p_ExceptionFieldError(self, p):
663 """ExceptionField : error"""
664 p[0] = self.BuildError(p, 'ExceptionField')
666 # [59]
667 def p_Iterable(self, p):
668 """Iterable : ITERABLE '<' Type OptionalType '>' ';'
669 | LEGACYITERABLE '<' Type '>' ';'"""
670 if len(p) > 6:
671 childlist = ListFromConcat(p[3], p[4])
672 p[0] = self.BuildProduction('Iterable', p, 2, childlist)
673 else:
674 p[0] = self.BuildProduction('LegacyIterable', p, 2, p[3])
676 # [60]
677 def p_OptionalType(self, p):
678 """OptionalType : ',' Type
679 |"""
680 if len(p) > 1:
681 p[0] = p[2]
683 # [61]
684 def p_ReadWriteMaplike(self, p):
685 """ReadWriteMaplike : MaplikeRest"""
686 p[0] = p[1]
688 # [62]
689 def p_ReadWriteSetlike(self, p):
690 """ReadWriteSetlike : SetlikeRest"""
691 p[0] = p[1]
693 # [63]
694 def p_MaplikeRest(self, p):
695 """MaplikeRest : MAPLIKE '<' Type ',' Type '>' ';'"""
696 childlist = ListFromConcat(p[3], p[5])
697 p[0] = self.BuildProduction('Maplike', p, 2, childlist)
699 # [64]
700 def p_SetlikeRest(self, p):
701 """SetlikeRest : SETLIKE '<' Type '>' ';'"""
702 p[0] = self.BuildProduction('Setlike', p, 2, p[3])
704 # [65] No comment version for mid statement attributes.
705 def p_ExtendedAttributeListNoComments(self, p):
706 """ExtendedAttributeListNoComments : '[' ExtendedAttribute ExtendedAttributes ']'
707 | """
708 if len(p) > 2:
709 items = ListFromConcat(p[2], p[3])
710 p[0] = self.BuildProduction('ExtAttributes', p, 1, items)
712 # [65.1] Add optional comment field for start of statements.
713 def p_ExtendedAttributeList(self, p):
714 """ExtendedAttributeList : Comments '[' ExtendedAttribute ExtendedAttributes ']'
715 | Comments """
716 if len(p) > 2:
717 items = ListFromConcat(p[3], p[4])
718 attribs = self.BuildProduction('ExtAttributes', p, 2, items)
719 p[0] = ListFromConcat(p[1], attribs)
720 else:
721 p[0] = p[1]
723 # [66]
724 def p_ExtendedAttributes(self, p):
725 """ExtendedAttributes : ',' ExtendedAttribute ExtendedAttributes
726 |"""
727 if len(p) > 1:
728 p[0] = ListFromConcat(p[2], p[3])
730 # We only support:
731 # [ identifier ]
732 # [ identifier ( ArgumentList ) ]
733 # [ identifier = identifier ]
734 # [ identifier = ( IdentifierList ) ]
735 # [ identifier = identifier ( ArgumentList ) ]
736 # [66] map directly to [91-93, 95]
737 # [67-69, 71] are unsupported
738 def p_ExtendedAttribute(self, p):
739 """ExtendedAttribute : ExtendedAttributeNoArgs
740 | ExtendedAttributeArgList
741 | ExtendedAttributeIdent
742 | ExtendedAttributeIdentList
743 | ExtendedAttributeNamedArgList"""
744 p[0] = p[1]
746 # [71]
747 def p_ArgumentNameKeyword(self, p):
748 """ArgumentNameKeyword : ATTRIBUTE
749 | CALLBACK
750 | CONST
751 | CREATOR
752 | DELETER
753 | DICTIONARY
754 | ENUM
755 | EXCEPTION
756 | GETTER
757 | IMPLEMENTS
758 | INHERIT
759 | LEGACYCALLER
760 | PARTIAL
761 | SERIALIZER
762 | SETTER
763 | STATIC
764 | STRINGIFIER
765 | TYPEDEF
766 | UNRESTRICTED"""
767 p[0] = p[1]
769 # [72] NOT IMPLEMENTED (OtherOrComma)
771 # [73]
772 def p_Type(self, p):
773 """Type : SingleType
774 | UnionType TypeSuffix"""
775 if len(p) == 2:
776 p[0] = self.BuildProduction('Type', p, 1, p[1])
777 else:
778 p[0] = self.BuildProduction('Type', p, 1, ListFromConcat(p[1], p[2]))
780 # [74]
781 def p_SingleType(self, p):
782 """SingleType : NonAnyType
783 | ANY TypeSuffixStartingWithArray"""
784 if len(p) == 2:
785 p[0] = p[1]
786 else:
787 p[0] = ListFromConcat(self.BuildProduction('Any', p, 1), p[2])
789 # [75]
790 def p_UnionType(self, p):
791 """UnionType : '(' UnionMemberType OR UnionMemberType UnionMemberTypes ')'"""
793 # [76]
794 def p_UnionMemberType(self, p):
795 """UnionMemberType : NonAnyType
796 | UnionType TypeSuffix
797 | ANY '[' ']' TypeSuffix"""
798 # [77]
799 def p_UnionMemberTypes(self, p):
800 """UnionMemberTypes : OR UnionMemberType UnionMemberTypes
801 |"""
803 # [78] Moved BYTESTRING, DOMSTRING, OBJECT, DATE, REGEXP to PrimitiveType
804 # Moving all built-in types into PrimitiveType makes it easier to
805 # differentiate between them and 'identifier', since p[1] would be a string in
806 # both cases.
807 def p_NonAnyType(self, p):
808 """NonAnyType : PrimitiveType TypeSuffix
809 | PromiseType Null
810 | identifier TypeSuffix
811 | SEQUENCE '<' Type '>' Null"""
812 if len(p) == 3:
813 if type(p[1]) == str:
814 typeref = self.BuildNamed('Typeref', p, 1)
815 else:
816 typeref = p[1]
817 p[0] = ListFromConcat(typeref, p[2])
819 if len(p) == 6:
820 p[0] = self.BuildProduction('Sequence', p, 1, ListFromConcat(p[3], p[5]))
822 # [79] NOT IMPLEMENTED (BufferRelatedType)
824 # [80]
825 def p_ConstType(self, p):
826 """ConstType : PrimitiveType Null
827 | identifier Null"""
828 if type(p[1]) == str:
829 p[0] = self.BuildNamed('Typeref', p, 1, p[2])
830 else:
831 p[1].AddChildren(p[2])
832 p[0] = p[1]
835 # [81] Added BYTESTRING, DOMSTRING, OBJECT, DATE, REGEXP
836 def p_PrimitiveType(self, p):
837 """PrimitiveType : UnsignedIntegerType
838 | UnrestrictedFloatType
839 | BOOLEAN
840 | BYTE
841 | OCTET
842 | BYTESTRING
843 | DOMSTRING
844 | OBJECT
845 | DATE
846 | REGEXP"""
847 if type(p[1]) == str:
848 p[0] = self.BuildNamed('PrimitiveType', p, 1)
849 else:
850 p[0] = p[1]
853 # [82]
854 def p_UnrestrictedFloatType(self, p):
855 """UnrestrictedFloatType : UNRESTRICTED FloatType
856 | FloatType"""
857 if len(p) == 2:
858 typeref = self.BuildNamed('PrimitiveType', p, 1)
859 else:
860 typeref = self.BuildNamed('PrimitiveType', p, 2)
861 typeref.AddChildren(self.BuildTrue('UNRESTRICTED'))
862 p[0] = typeref
865 # [83]
866 def p_FloatType(self, p):
867 """FloatType : FLOAT
868 | DOUBLE"""
869 p[0] = p[1]
871 # [84]
872 def p_UnsignedIntegerType(self, p):
873 """UnsignedIntegerType : UNSIGNED IntegerType
874 | IntegerType"""
875 if len(p) == 2:
876 p[0] = p[1]
877 else:
878 p[0] = 'unsigned ' + p[2]
880 # [85]
881 def p_IntegerType(self, p):
882 """IntegerType : SHORT
883 | LONG OptionalLong"""
884 if len(p) == 2:
885 p[0] = p[1]
886 else:
887 p[0] = p[1] + p[2]
889 # [86]
890 def p_OptionalLong(self, p):
891 """OptionalLong : LONG
892 | """
893 if len(p) > 1:
894 p[0] = ' ' + p[1]
895 else:
896 p[0] = ''
898 # [87] Add unqualified Promise
899 def p_PromiseType(self, p):
900 """PromiseType : PROMISE '<' ReturnType '>'
901 | PROMISE"""
902 if len(p) == 2:
903 # Promise without resolution type is not specified in the Web IDL spec.
904 # As it is used in some specs and in the blink implementation,
905 # we allow that here.
906 resolution_type = self.BuildProduction('Type', p, 1,
907 self.BuildProduction('Any', p, 1))
908 p[0] = self.BuildNamed('Promise', p, 1, resolution_type)
909 else:
910 p[0] = self.BuildNamed('Promise', p, 1, p[3])
912 # [88] Add support for sized array
913 def p_TypeSuffix(self, p):
914 """TypeSuffix : '[' integer ']' TypeSuffix
915 | '[' ']' TypeSuffix
916 | '?' TypeSuffixStartingWithArray
917 | """
918 if len(p) == 5:
919 p[0] = self.BuildNamed('Array', p, 2, p[4])
921 if len(p) == 4:
922 p[0] = self.BuildProduction('Array', p, 1, p[3])
924 if len(p) == 3:
925 p[0] = ListFromConcat(self.BuildTrue('NULLABLE'), p[2])
928 # [89]
929 def p_TypeSuffixStartingWithArray(self, p):
930 """TypeSuffixStartingWithArray : '[' ']' TypeSuffix
931 | """
932 if len(p) > 1:
933 p[0] = self.BuildProduction('Array', p, 0, p[3])
935 # [90]
936 def p_Null(self, p):
937 """Null : '?'
938 |"""
939 if len(p) > 1:
940 p[0] = self.BuildTrue('NULLABLE')
942 # [91]
943 def p_ReturnType(self, p):
944 """ReturnType : Type
945 | VOID"""
946 if p[1] == 'void':
947 p[0] = self.BuildProduction('Type', p, 1)
948 p[0].AddChildren(self.BuildNamed('PrimitiveType', p, 1))
949 else:
950 p[0] = p[1]
952 # [92]
953 def p_IdentifierList(self, p):
954 """IdentifierList : identifier Identifiers"""
955 p[0] = ListFromConcat(p[1], p[2])
957 # [93]
958 def p_Identifiers(self, p):
959 """Identifiers : ',' identifier Identifiers
960 |"""
961 if len(p) > 1:
962 p[0] = ListFromConcat(p[2], p[3])
964 # [94]
965 def p_ExtendedAttributeNoArgs(self, p):
966 """ExtendedAttributeNoArgs : identifier"""
967 p[0] = self.BuildNamed('ExtAttribute', p, 1)
969 # [95]
970 def p_ExtendedAttributeArgList(self, p):
971 """ExtendedAttributeArgList : identifier '(' ArgumentList ')'"""
972 arguments = self.BuildProduction('Arguments', p, 2, p[3])
973 p[0] = self.BuildNamed('ExtAttribute', p, 1, arguments)
975 # [96]
976 def p_ExtendedAttributeIdent(self, p):
977 """ExtendedAttributeIdent : identifier '=' identifier"""
978 value = self.BuildAttribute('VALUE', p[3])
979 p[0] = self.BuildNamed('ExtAttribute', p, 1, value)
981 # [97]
982 def p_ExtendedAttributeIdentList(self, p):
983 """ExtendedAttributeIdentList : identifier '=' '(' IdentifierList ')'"""
984 value = self.BuildAttribute('VALUE', p[4])
985 p[0] = self.BuildNamed('ExtAttribute', p, 1, value)
987 # [98]
988 def p_ExtendedAttributeNamedArgList(self, p):
989 """ExtendedAttributeNamedArgList : identifier '=' identifier '(' ArgumentList ')'"""
990 args = self.BuildProduction('Arguments', p, 4, p[5])
991 value = self.BuildNamed('Call', p, 3, args)
992 p[0] = self.BuildNamed('ExtAttribute', p, 1, value)
995 # Parser Errors
997 # p_error is called whenever the parser can not find a pattern match for
998 # a set of items from the current state. The p_error function defined here
999 # is triggered logging an error, and parsing recovery happens as the
1000 # p_<type>_error functions defined above are called. This allows the parser
1001 # to continue so as to capture more than one error per file.
1003 def p_error(self, t):
1004 if t:
1005 lineno = t.lineno
1006 pos = t.lexpos
1007 prev = self.yaccobj.symstack[-1]
1008 if type(prev) == lex.LexToken:
1009 msg = "Unexpected %s after %s." % (
1010 TokenTypeName(t), TokenTypeName(prev))
1011 else:
1012 msg = "Unexpected %s." % (t.value)
1013 else:
1014 last = self.LastToken()
1015 lineno = last.lineno
1016 pos = last.lexpos
1017 msg = "Unexpected end of file after %s." % TokenTypeName(last)
1018 self.yaccobj.restart()
1020 # Attempt to remap the error to a friendlier form
1021 if msg in ERROR_REMAP:
1022 msg = ERROR_REMAP[msg]
1024 self._last_error_msg = msg
1025 self._last_error_lineno = lineno
1026 self._last_error_pos = pos
1028 def Warn(self, node, msg):
1029 sys.stdout.write(node.GetLogLine(msg))
1030 self.parse_warnings += 1
1032 def LastToken(self):
1033 return self.lexer.last
1035 def __init__(self, lexer, verbose=False, debug=False, mute_error=False):
1036 self.lexer = lexer
1037 self.tokens = lexer.KnownTokens()
1038 self.yaccobj = yacc.yacc(module=self, tabmodule=None, debug=debug,
1039 optimize=0, write_tables=0)
1040 self.parse_debug = debug
1041 self.verbose = verbose
1042 self.mute_error = mute_error
1043 self._parse_errors = 0
1044 self._parse_warnings = 0
1045 self._last_error_msg = None
1046 self._last_error_lineno = 0
1047 self._last_error_pos = 0
1051 # BuildProduction
1053 # Production is the set of items sent to a grammar rule resulting in a new
1054 # item being returned.
1056 # p - Is the Yacc production object containing the stack of items
1057 # index - Index into the production of the name for the item being produced.
1058 # cls - The type of item being producted
1059 # childlist - The children of the new item
1060 def BuildProduction(self, cls, p, index, childlist=None):
1061 try:
1062 if not childlist:
1063 childlist = []
1065 filename = self.lexer.Lexer().filename
1066 lineno = p.lineno(index)
1067 pos = p.lexpos(index)
1068 out = IDLNode(cls, filename, lineno, pos, childlist)
1069 return out
1070 except:
1071 print 'Exception while parsing:'
1072 for num, item in enumerate(p):
1073 print ' [%d] %s' % (num, ExpandProduction(item))
1074 if self.LastToken():
1075 print 'Last token: %s' % str(self.LastToken())
1076 raise
1078 def BuildNamed(self, cls, p, index, childlist=None):
1079 childlist = ListFromConcat(childlist)
1080 childlist.append(self.BuildAttribute('NAME', p[index]))
1081 return self.BuildProduction(cls, p, index, childlist)
1083 def BuildComment(self, cls, p, index):
1084 name = p[index]
1086 # Remove comment markers
1087 lines = []
1088 if name[:2] == '//':
1089 # For C++ style, remove any leading whitespace and the '//' marker from
1090 # each line.
1091 form = 'cc'
1092 for line in name.split('\n'):
1093 start = line.find('//')
1094 lines.append(line[start+2:])
1095 else:
1096 # For C style, remove ending '*/''
1097 form = 'c'
1098 for line in name[:-2].split('\n'):
1099 # Remove characters until start marker for this line '*' if found
1100 # otherwise it should be blank.
1101 offs = line.find('*')
1102 if offs >= 0:
1103 line = line[offs + 1:].rstrip()
1104 else:
1105 line = ''
1106 lines.append(line)
1107 name = '\n'.join(lines)
1108 childlist = [self.BuildAttribute('NAME', name),
1109 self.BuildAttribute('FORM', form)]
1110 return self.BuildProduction(cls, p, index, childlist)
1113 # BuildError
1115 # Build and Errror node as part of the recovery process.
1118 def BuildError(self, p, prod):
1119 self._parse_errors += 1
1120 name = self.BuildAttribute('NAME', self._last_error_msg)
1121 line = self.BuildAttribute('LINE', self._last_error_lineno)
1122 pos = self.BuildAttribute('POS', self._last_error_pos)
1123 prod = self.BuildAttribute('PROD', prod)
1125 node = self.BuildProduction('Error', p, 1,
1126 ListFromConcat(name, line, pos, prod))
1127 if not self.mute_error:
1128 node.Error(self._last_error_msg)
1130 return node
1133 # BuildAttribute
1135 # An ExtendedAttribute is a special production that results in a property
1136 # which is applied to the adjacent item. Attributes have no children and
1137 # instead represent key/value pairs.
1139 def BuildAttribute(self, key, val):
1140 return IDLAttribute(key, val)
1142 def BuildFalse(self, key):
1143 return IDLAttribute(key, Boolean(False))
1145 def BuildTrue(self, key):
1146 return IDLAttribute(key, Boolean(True))
1148 def GetErrors(self):
1149 # Access lexer errors, despite being private
1150 # pylint: disable=W0212
1151 return self._parse_errors + self.lexer._lex_errors
1154 # ParseData
1156 # Attempts to parse the current data loaded in the lexer.
1158 def ParseText(self, filename, data):
1159 self._parse_errors = 0
1160 self._parse_warnings = 0
1161 self._last_error_msg = None
1162 self._last_error_lineno = 0
1163 self._last_error_pos = 0
1165 try:
1166 self.lexer.Tokenize(data, filename)
1167 nodes = self.yaccobj.parse(lexer=self.lexer) or []
1168 name = self.BuildAttribute('NAME', filename)
1169 return IDLNode('File', filename, 0, 0, nodes + [name])
1171 except lex.LexError as lexError:
1172 sys.stderr.write('Error in token: %s\n' % str(lexError))
1173 return None
1177 def ParseFile(parser, filename):
1178 """Parse a file and return a File type of node."""
1179 with open(filename) as fileobject:
1180 try:
1181 out = parser.ParseText(filename, fileobject.read())
1182 out.SetProperty('DATETIME', time.ctime(os.path.getmtime(filename)))
1183 out.SetProperty('ERRORS', parser.GetErrors())
1184 return out
1186 except Exception as e:
1187 last = parser.LastToken()
1188 sys.stderr.write('%s(%d) : Internal parsing error\n\t%s.\n' % (
1189 filename, last.lineno, str(e)))
1192 def main(argv):
1193 nodes = []
1194 parser = IDLParser(IDLLexer())
1195 errors = 0
1196 for filename in argv:
1197 filenode = ParseFile(parser, filename)
1198 if (filenode):
1199 errors += filenode.GetProperty('ERRORS')
1200 nodes.append(filenode)
1202 ast = IDLNode('AST', '__AST__', 0, 0, nodes)
1204 print '\n'.join(ast.Tree(accept_props=['PROD']))
1205 if errors:
1206 print '\nFound %d errors.\n' % errors
1208 return errors
1211 if __name__ == '__main__':
1212 sys.exit(main(sys.argv[1:]))