OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 # |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 # | 206 # |
207 # We force all input files to start with two comments. The first comment is a | 207 # We force all input files to start with two comments. The first comment is a |
208 # Copyright notice followed by a set of file wide Extended Attributes, followed | 208 # Copyright notice followed by a set of file wide Extended Attributes, followed |
209 # by the file comment and finally by file level patterns. | 209 # by the file comment and finally by file level patterns. |
210 # | 210 # |
211 # Find the Copyright, File comment, and optional file wide attributes. We | 211 # Find the Copyright, File comment, and optional file wide attributes. We |
212 # use a match with COMMENT instead of comments to force the token to be | 212 # use a match with COMMENT instead of comments to force the token to be |
213 # present. The extended attributes and the top_list become siblings which | 213 # present. The extended attributes and the top_list become siblings which |
214 # in turn are children of the file object created from the results of top. | 214 # in turn are children of the file object created from the results of top. |
215 def p_top(self, p): | 215 def p_top(self, p): |
216 """top : COMMENT COMMENT ext_attr_block top_list""" | 216 """top : top_list |
217 | namespace""" | |
noelallen1
2012/02/28 22:31:33
I would encourage us to use the copyright notice c
asargent_no_longer_on_chrome
2012/03/03 01:12:02
I fell back to leaving this as-is for now, and wil
| |
218 # TODO(noelallen) - push the expectation of copyright/filedoc comments out | |
219 # of the parser and into the PPAPI-specific codegen. | |
220 if len(p[1]) > 0 and p[1][0].cls != 'Namespace': | |
221 # Hack to pull out the first two comments from the children of |top_list| | |
noelallen1
2012/02/28 22:31:33
Lets find a better solution for this hack.
asargent_no_longer_on_chrome
2012/03/03 01:12:02
Removed it per above comment.
| |
222 # and insert them into the top-level list we return in p[0] | |
223 assert len(p[1][0].children) >= 2 | |
224 for i in (0, 1): | |
225 assert p[1][0].children[i].cls == 'Comment' | |
226 copyright_comment = p[1][0].children.pop(0) | |
227 copyright_comment.cls = 'Copyright' | |
228 filedoc_comment = p[1][0].children.pop(0) | |
229 p[0] = ListFromConcat(copyright_comment, filedoc_comment, p[1]) | |
230 else: | |
231 p[0] = ListFromConcat(p[1]) | |
232 if self.parse_debug: DumpReduction('top', p) | |
217 | 233 |
218 Copyright = self.BuildComment('Copyright', p, 1) | 234 def p_namespace(self, p): |
219 Filedoc = self.BuildComment('Comment', p, 2) | 235 """namespace : modifiers NAMESPACE namespace_name '{' top_list '}' ';'""" |
236 children = ListFromConcat(p[1], p[5]) | |
237 p[0] = self.BuildNamed('Namespace', p, 3, children) | |
220 | 238 |
221 p[0] = ListFromConcat(Copyright, Filedoc, p[3], p[4]) | 239 # We allow namespace names of the form foo.bar.baz. |
222 if self.parse_debug: DumpReduction('top', p) | 240 def p_namespace_name(self, p): |
241 """namespace_name : SYMBOL | |
242 | SYMBOL '.' namespace_name""" | |
243 p[0] = "".join(p[1:]) | |
244 | |
245 def p_dictionary_block(self, p): | |
246 """dictionary_block : modifiers DICTIONARY SYMBOL '{' struct_list '}' ';'""" | |
247 p[0] = self.BuildNamed('Dictionary', p, 3, ListFromConcat(p[5])) | |
248 | |
249 def p_callback_decl(self, p): | |
250 """callback_decl : modifiers CALLBACK SYMBOL '=' SYMBOL param_list ';'""" | |
251 children = ListFromConcat(p[1], p[6]) | |
252 p[0] = self.BuildNamed('Callback', p, 3, children) | |
223 | 253 |
224 # Build a list of top level items. | 254 # Build a list of top level items. |
225 def p_top_list(self, p): | 255 def p_top_list(self, p): |
226 """top_list : describe_block top_list | 256 """top_list : dictionary_block top_list |
257 | describe_block top_list | |
227 | enum_block top_list | 258 | enum_block top_list |
228 | inline top_list | 259 | inline top_list |
229 | interface_block top_list | 260 | interface_block top_list |
230 | label_block top_list | 261 | label_block top_list |
231 | struct_block top_list | 262 | struct_block top_list |
232 | typedef_decl top_list | 263 | typedef_decl top_list |
264 | callback_decl top_list | |
233 | """ | 265 | """ |
234 if len(p) > 2: | 266 if len(p) > 2: |
235 p[0] = ListFromConcat(p[1], p[2]) | 267 p[0] = ListFromConcat(p[1], p[2]) |
236 if self.parse_debug: DumpReduction('top_list', p) | 268 if self.parse_debug: DumpReduction('top_list', p) |
237 | 269 |
238 # Recover from error and continue parsing at the next top match. | 270 # Recover from error and continue parsing at the next top match. |
239 def p_top_error(self, p): | 271 def p_top_error(self, p): |
240 """top_list : error top_list""" | 272 """top_list : error top_list""" |
241 p[0] = p[2] | 273 p[0] = p[2] |
242 | 274 |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
475 p[0] = ListFromConcat(array, p[3]) | 507 p[0] = ListFromConcat(array, p[3]) |
476 # If there are 4 tokens plus a return slot it is a fixed array | 508 # If there are 4 tokens plus a return slot it is a fixed array |
477 elif len(p) == 5: | 509 elif len(p) == 5: |
478 count = self.BuildAttribute('FIXED', p[2]) | 510 count = self.BuildAttribute('FIXED', p[2]) |
479 array = self.BuildProduction('Array', p, 2, [count]) | 511 array = self.BuildProduction('Array', p, 2, [count]) |
480 p[0] = ListFromConcat(array, p[4]) | 512 p[0] = ListFromConcat(array, p[4]) |
481 # If there is only a return slot, do not fill it for this terminator. | 513 # If there is only a return slot, do not fill it for this terminator. |
482 elif len(p) == 1: return | 514 elif len(p) == 1: return |
483 if self.parse_debug: DumpReduction('arrays', p) | 515 if self.parse_debug: DumpReduction('arrays', p) |
484 | 516 |
517 | |
518 # An identifier is a legal value for a parameter or attribute name. | |
519 def p_identifier(self, p): | |
520 """identifier : SYMBOL | |
521 | CALLBACK""" | |
522 p[0] = p[1] | |
523 | |
485 # | 524 # |
486 # Parameter List | 525 # Parameter List |
487 # | 526 # |
488 # A parameter list is a collection of arguments which are passed to a | 527 # A parameter list is a collection of arguments which are passed to a |
489 # function. | 528 # function. |
490 # | 529 # |
491 def p_param_list(self, p): | 530 def p_param_list(self, p): |
492 """param_list : '(' param_item param_cont ')' | 531 """param_list : '(' param_item param_cont ')' |
493 | '(' ')' """ | 532 | '(' ')' """ |
494 if len(p) > 3: | 533 if len(p) > 3: |
495 args = ListFromConcat(p[2], p[3]) | 534 args = ListFromConcat(p[2], p[3]) |
496 else: | 535 else: |
497 args = [] | 536 args = [] |
498 p[0] = self.BuildProduction('Callspec', p, 1, args) | 537 p[0] = self.BuildProduction('Callspec', p, 1, args) |
499 if self.parse_debug: DumpReduction('param_list', p) | 538 if self.parse_debug: DumpReduction('param_list', p) |
500 | 539 |
501 def p_param_item(self, p): | 540 def p_param_item(self, p): |
502 """param_item : modifiers SYMBOL arrays SYMBOL""" | 541 """param_item : modifiers optional SYMBOL arrays identifier""" |
503 typeref = self.BuildAttribute('TYPEREF', p[2]) | 542 typeref = self.BuildAttribute('TYPEREF', p[3]) |
504 children = ListFromConcat(p[1],typeref, p[3]) | 543 children = ListFromConcat(p[1],p[2], typeref, p[4]) |
505 p[0] = self.BuildNamed('Param', p, 4, children) | 544 p[0] = self.BuildNamed('Param', p, 5, children) |
506 if self.parse_debug: DumpReduction('param_item', p) | 545 if self.parse_debug: DumpReduction('param_item', p) |
507 | 546 |
547 def p_optional(self, p): | |
548 """optional : OPTIONAL | |
549 | """ | |
550 if len(p) == 2: | |
551 p[0] = self.BuildAttribute('OPTIONAL', True) | |
552 | |
553 | |
508 def p_param_cont(self, p): | 554 def p_param_cont(self, p): |
509 """param_cont : ',' param_item param_cont | 555 """param_cont : ',' param_item param_cont |
510 | """ | 556 | """ |
511 if len(p) > 1: | 557 if len(p) > 1: |
512 p[0] = ListFromConcat(p[2], p[3]) | 558 p[0] = ListFromConcat(p[2], p[3]) |
513 if self.parse_debug: DumpReduction('param_cont', p) | 559 if self.parse_debug: DumpReduction('param_cont', p) |
514 | 560 |
515 def p_param_error(self, p): | 561 def p_param_error(self, p): |
516 """param_cont : error param_cont""" | 562 """param_cont : error param_cont""" |
517 p[0] = p[2] | 563 p[0] = p[2] |
518 | 564 |
519 | 565 |
520 # | 566 # |
521 # Typedef | 567 # Typedef |
522 # | 568 # |
523 # A typedef creates a new referencable type. The tyepdef can specify an array | 569 # A typedef creates a new referencable type. The typedef can specify an array |
524 # definition as well as a function declaration. | 570 # definition as well as a function declaration. |
525 # | 571 # |
526 def p_typedef_data(self, p): | 572 def p_typedef_data(self, p): |
527 """typedef_decl : modifiers TYPEDEF SYMBOL SYMBOL ';' """ | 573 """typedef_decl : modifiers TYPEDEF SYMBOL SYMBOL ';' """ |
528 typeref = self.BuildAttribute('TYPEREF', p[3]) | 574 typeref = self.BuildAttribute('TYPEREF', p[3]) |
529 children = ListFromConcat(p[1], typeref) | 575 children = ListFromConcat(p[1], typeref) |
530 p[0] = self.BuildNamed('Typedef', p, 4, children) | 576 p[0] = self.BuildNamed('Typedef', p, 4, children) |
531 if self.parse_debug: DumpReduction('typedef_data', p) | 577 if self.parse_debug: DumpReduction('typedef_data', p) |
532 | 578 |
533 def p_typedef_array(self, p): | 579 def p_typedef_array(self, p): |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
608 p[0] = p[2] | 654 p[0] = p[2] |
609 if self.parse_debug: DumpReduction('label_error', p) | 655 if self.parse_debug: DumpReduction('label_error', p) |
610 | 656 |
611 | 657 |
612 # | 658 # |
613 # Members | 659 # Members |
614 # | 660 # |
615 # A member attribute or function of a struct or interface. | 661 # A member attribute or function of a struct or interface. |
616 # | 662 # |
617 def p_member_attribute(self, p): | 663 def p_member_attribute(self, p): |
618 """member_attribute : modifiers SYMBOL SYMBOL """ | 664 """member_attribute : modifiers SYMBOL arrays questionmark identifier""" |
619 typeref = self.BuildAttribute('TYPEREF', p[2]) | 665 typeref = self.BuildAttribute('TYPEREF', p[2]) |
620 children = ListFromConcat(p[1], typeref) | 666 children = ListFromConcat(p[1], typeref, p[3], p[4]) |
621 p[0] = self.BuildNamed('Member', p, 3, children) | 667 p[0] = self.BuildNamed('Member', p, 5, children) |
622 if self.parse_debug: DumpReduction('attribute', p) | |
623 | |
624 def p_member_attribute_array(self, p): | |
625 """member_attribute : modifiers SYMBOL arrays SYMBOL """ | |
626 typeref = self.BuildAttribute('TYPEREF', p[2]) | |
627 children = ListFromConcat(p[1], typeref, p[3]) | |
628 p[0] = self.BuildNamed('Member', p, 4, children) | |
629 if self.parse_debug: DumpReduction('attribute', p) | 668 if self.parse_debug: DumpReduction('attribute', p) |
630 | 669 |
631 def p_member_function(self, p): | 670 def p_member_function(self, p): |
632 """member_function : modifiers SYMBOL SYMBOL param_list""" | 671 """member_function : modifiers static SYMBOL SYMBOL param_list""" |
633 typeref = self.BuildAttribute('TYPEREF', p[2]) | 672 typeref = self.BuildAttribute('TYPEREF', p[3]) |
634 children = ListFromConcat(p[1], typeref, p[4]) | 673 children = ListFromConcat(p[1], p[2], typeref, p[5]) |
635 p[0] = self.BuildNamed('Member', p, 3, children) | 674 p[0] = self.BuildNamed('Member', p, 4, children) |
636 if self.parse_debug: DumpReduction('function', p) | 675 if self.parse_debug: DumpReduction('function', p) |
637 | 676 |
677 def p_static(self, p): | |
678 """static : STATIC | |
679 | """ | |
680 if len(p) == 2: | |
681 p[0] = self.BuildAttribute('STATIC', True) | |
682 | |
683 def p_questionmark(self, p): | |
684 """questionmark : '?' | |
685 | """ | |
686 if len(p) == 2: | |
687 p[0] = self.BuildAttribute('OPTIONAL', True) | |
688 | |
638 # | 689 # |
639 # Interface | 690 # Interface |
640 # | 691 # |
641 # An interface is a named collection of functions. | 692 # An interface is a named collection of functions. |
642 # | 693 # |
643 def p_interface_block(self, p): | 694 def p_interface_block(self, p): |
644 """interface_block : modifiers INTERFACE SYMBOL '{' interface_list '}' ';'"" " | 695 """interface_block : modifiers INTERFACE SYMBOL '{' interface_list '}' ';'"" " |
645 p[0] = self.BuildNamed('Interface', p, 3, ListFromConcat(p[1], p[5])) | 696 p[0] = self.BuildNamed('Interface', p, 3, ListFromConcat(p[1], p[5])) |
646 if self.parse_debug: DumpReduction('interface_block', p) | 697 if self.parse_debug: DumpReduction('interface_block', p) |
647 | 698 |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1048 ast = ParseFiles(filenames) | 1099 ast = ParseFiles(filenames) |
1049 errs = ast.GetProperty('ERRORS') | 1100 errs = ast.GetProperty('ERRORS') |
1050 if errs: | 1101 if errs: |
1051 ErrOut.Log('Found %d error(s).' % errs); | 1102 ErrOut.Log('Found %d error(s).' % errs); |
1052 InfoOut.Log("%d files processed." % len(filenames)) | 1103 InfoOut.Log("%d files processed." % len(filenames)) |
1053 return errs | 1104 return errs |
1054 | 1105 |
1055 | 1106 |
1056 if __name__ == '__main__': | 1107 if __name__ == '__main__': |
1057 sys.exit(Main(sys.argv[1:])) | 1108 sys.exit(Main(sys.argv[1:])) |
OLD | NEW |