| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Generates a syntax tree from a Mojo IDL file.""" | 5 """Generates a syntax tree from a Mojo IDL file.""" |
| 6 | 6 |
| 7 import imp | 7 import imp |
| 8 import os.path | 8 import os.path |
| 9 import sys | 9 import sys |
| 10 | 10 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 # then we name the functions |p_foo_bar_N| (for left-hand-side |foo_bar|), | 79 # then we name the functions |p_foo_bar_N| (for left-hand-side |foo_bar|), |
| 80 # where N is a number (numbered starting from 1). Note that using multiple | 80 # where N is a number (numbered starting from 1). Note that using multiple |
| 81 # functions is actually more efficient than having single functions handle | 81 # functions is actually more efficient than having single functions handle |
| 82 # multiple rules (and, e.g., distinguishing them by examining |len(p)|). | 82 # multiple rules (and, e.g., distinguishing them by examining |len(p)|). |
| 83 # | 83 # |
| 84 # It's also possible to have a function handling multiple rules with different | 84 # It's also possible to have a function handling multiple rules with different |
| 85 # left-hand-sides. We do not do this. | 85 # left-hand-sides. We do not do this. |
| 86 # | 86 # |
| 87 # See http://www.dabeaz.com/ply/ply.html#ply_nn25 for more details. | 87 # See http://www.dabeaz.com/ply/ply.html#ply_nn25 for more details. |
| 88 | 88 |
| 89 # TODO(vtl): Get rid of this ('MODULE', ...) stuff and replace it with an | |
| 90 # ast.Mojom node. (The ('IMPORT', ...) stuff is a hack until I do this and | |
| 91 # update translate.py.) | |
| 92 # TODO(vtl): Get rid of the braces in the module "statement". (Consider | 89 # TODO(vtl): Get rid of the braces in the module "statement". (Consider |
| 93 # renaming "module" -> "package".) | 90 # renaming "module" -> "package".) Then we'll be able to have a single rule |
| 94 def p_root(self, p): | 91 # for root (by making module "optional"). |
| 95 """root : import_list module LBRACE definition_list RBRACE | 92 def p_root_1(self, p): |
| 96 | import_list definition_list""" | 93 """root : import_list module LBRACE definition_list RBRACE""" |
| 97 p[0] = [('IMPORT', import_statement.import_filename) \ | 94 p[0] = ast.Mojom(p[2], p[1], p[4]) |
| 98 for import_statement in p[1]] | 95 |
| 99 if len(p) == 6: | 96 def p_root_2(self, p): |
| 100 p[0].append(('MODULE', p[2].name, p[2].attribute_list, p[4])) | 97 """root : import_list definition_list""" |
| 101 else: | 98 p[0] = ast.Mojom(None, p[1], p[2]) |
| 102 p[0].append(('MODULE', None, None, p[2])) | |
| 103 | 99 |
| 104 def p_import_list_1(self, p): | 100 def p_import_list_1(self, p): |
| 105 """import_list : """ | 101 """import_list : """ |
| 106 p[0] = ast.ImportList() | 102 p[0] = ast.ImportList() |
| 107 | 103 |
| 108 def p_import_list_2(self, p): | 104 def p_import_list_2(self, p): |
| 109 """import_list : import_list import""" | 105 """import_list : import_list import""" |
| 110 p[0] = p[1] | 106 p[0] = p[1] |
| 111 p[0].Append(p[2]) | 107 p[0].Append(p[2]) |
| 112 | 108 |
| 113 def p_import(self, p): | 109 def p_import(self, p): |
| 114 """import : IMPORT STRING_LITERAL""" | 110 """import : IMPORT STRING_LITERAL""" |
| 115 # 'eval' the literal to strip the quotes. | 111 # 'eval' the literal to strip the quotes. |
| 116 # TODO(vtl): This eval is dubious. We should unquote/unescape ourselves. | 112 # TODO(vtl): This eval is dubious. We should unquote/unescape ourselves. |
| 117 p[0] = ast.Import(eval(p[2])) | 113 p[0] = ast.Import(eval(p[2])) |
| 118 | 114 |
| 119 def p_module(self, p): | 115 def p_module(self, p): |
| 120 """module : attribute_section MODULE identifier_wrapped """ | 116 """module : attribute_section MODULE identifier_wrapped """ |
| 121 p[0] = ast.Module(p[3], p[1], filename=self.filename, lineno=p.lineno(2)) | 117 p[0] = ast.Module(p[3], p[1], filename=self.filename, lineno=p.lineno(2)) |
| 122 | 118 |
| 123 def p_definition_list(self, p): | 119 def p_definition_list(self, p): |
| 124 """definition_list : definition definition_list | 120 """definition_list : definition definition_list |
| 125 | """ | 121 | """ |
| 126 if len(p) > 1: | 122 if len(p) > 1: |
| 127 p[0] = _ListFromConcat(p[1], p[2]) | 123 p[0] = p[2] |
| 124 p[0].insert(0, p[1]) |
| 125 else: |
| 126 p[0] = [] |
| 128 | 127 |
| 129 def p_definition(self, p): | 128 def p_definition(self, p): |
| 130 """definition : struct | 129 """definition : struct |
| 131 | interface | 130 | interface |
| 132 | enum | 131 | enum |
| 133 | const""" | 132 | const""" |
| 134 p[0] = p[1] | 133 p[0] = p[1] |
| 135 | 134 |
| 136 def p_attribute_section_1(self, p): | 135 def p_attribute_section_1(self, p): |
| 137 """attribute_section : """ | 136 """attribute_section : """ |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 | 374 |
| 376 def Parse(source, filename): | 375 def Parse(source, filename): |
| 377 lexer = Lexer(filename) | 376 lexer = Lexer(filename) |
| 378 parser = Parser(lexer, source, filename) | 377 parser = Parser(lexer, source, filename) |
| 379 | 378 |
| 380 lex.lex(object=lexer) | 379 lex.lex(object=lexer) |
| 381 yacc.yacc(module=parser, debug=0, write_tables=0) | 380 yacc.yacc(module=parser, debug=0, write_tables=0) |
| 382 | 381 |
| 383 tree = yacc.parse(source) | 382 tree = yacc.parse(source) |
| 384 return tree | 383 return tree |
| OLD | NEW |