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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 | 89 # TODO(vtl): Get rid of this ('MODULE', ...) stuff and replace it with an |
90 # ast.Mojom node. This will require putting the imports into a list (say | 90 # ast.Mojom node. (The ('IMPORT', ...) stuff is a hack until I do this and |
91 # ast.ImportList). | 91 # update translate.py.) |
92 # TODO(vtl): Get rid of the braces in the module "statement". (Consider | 92 # TODO(vtl): Get rid of the braces in the module "statement". (Consider |
93 # renaming "module" -> "package".) | 93 # renaming "module" -> "package".) |
94 def p_root(self, p): | 94 def p_root(self, p): |
95 """root : import root | 95 """root : import_list module LBRACE definition_list RBRACE |
96 | module LBRACE definition_list RBRACE | 96 | import_list definition_list""" |
97 | definition_list""" | 97 p[0] = [('IMPORT', import_statement.import_filename) \ |
98 if len(p) == 3: | 98 for import_statement in p[1]] |
99 p[0] = p[2] | 99 if len(p) == 6: |
100 p[0].insert(0, p[1]) | 100 p[0].append(('MODULE', p[2].name, p[2].attribute_list, p[4])) |
101 elif len(p) == 5: | |
102 p[0] = [('MODULE', p[1].name, p[1].attribute_list, p[3])] | |
103 else: | 101 else: |
104 p[0] = [('MODULE', None, None, p[1])] | 102 p[0].append(('MODULE', None, None, p[2])) |
| 103 |
| 104 def p_import_list_1(self, p): |
| 105 """import_list : """ |
| 106 p[0] = ast.ImportList() |
| 107 |
| 108 def p_import_list_2(self, p): |
| 109 """import_list : import_list import""" |
| 110 p[0] = p[1] |
| 111 p[0].Append(p[2]) |
105 | 112 |
106 def p_import(self, p): | 113 def p_import(self, p): |
107 """import : IMPORT STRING_LITERAL""" | 114 """import : IMPORT STRING_LITERAL""" |
108 # 'eval' the literal to strip the quotes. | 115 # 'eval' the literal to strip the quotes. |
109 p[0] = ('IMPORT', eval(p[2])) | 116 # TODO(vtl): This eval is dubious. We should unquote/unescape ourselves. |
| 117 p[0] = ast.Import(eval(p[2])) |
110 | 118 |
111 def p_module(self, p): | 119 def p_module(self, p): |
112 """module : attribute_section MODULE identifier_wrapped """ | 120 """module : attribute_section MODULE identifier_wrapped """ |
113 p[0] = ast.Module(p[3], p[1], filename=self.filename, lineno=p.lineno(2)) | 121 p[0] = ast.Module(p[3], p[1], filename=self.filename, lineno=p.lineno(2)) |
114 | 122 |
115 def p_definition_list(self, p): | 123 def p_definition_list(self, p): |
116 """definition_list : definition definition_list | 124 """definition_list : definition definition_list |
117 | """ | 125 | """ |
118 if len(p) > 1: | 126 if len(p) > 1: |
119 p[0] = _ListFromConcat(p[1], p[2]) | 127 p[0] = _ListFromConcat(p[1], p[2]) |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 | 375 |
368 def Parse(source, filename): | 376 def Parse(source, filename): |
369 lexer = Lexer(filename) | 377 lexer = Lexer(filename) |
370 parser = Parser(lexer, source, filename) | 378 parser = Parser(lexer, source, filename) |
371 | 379 |
372 lex.lex(object=lexer) | 380 lex.lex(object=lexer) |
373 yacc.yacc(module=parser, debug=0, write_tables=0) | 381 yacc.yacc(module=parser, debug=0, write_tables=0) |
374 | 382 |
375 tree = yacc.parse(source) | 383 tree = yacc.parse(source) |
376 return tree | 384 return tree |
OLD | NEW |