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 |
11 def _GetDirAbove(dirname): | 11 def _GetDirAbove(dirname): |
12 """Returns the directory "above" this file containing |dirname| (which must | 12 """Returns the directory "above" this file containing |dirname| (which must |
13 also be "above" this file).""" | 13 also be "above" this file).""" |
14 path = os.path.abspath(__file__) | 14 path = os.path.abspath(__file__) |
15 while True: | 15 while True: |
16 path, tail = os.path.split(path) | 16 path, tail = os.path.split(path) |
17 assert tail | 17 assert tail |
18 if tail == dirname: | 18 if tail == dirname: |
19 return path | 19 return path |
20 | 20 |
21 try: | 21 try: |
22 imp.find_module("ply") | 22 imp.find_module("ply") |
23 except ImportError: | 23 except ImportError: |
24 sys.path.append(os.path.join(_GetDirAbove("mojo"), "third_party")) | 24 sys.path.append(os.path.join(_GetDirAbove("public"), "public/third_party")) |
25 from ply import lex | 25 from ply import lex |
26 from ply import yacc | 26 from ply import yacc |
27 | 27 |
28 from ..error import Error | 28 from ..error import Error |
29 from . import ast | 29 from . import ast |
30 from .lexer import Lexer | 30 from .lexer import Lexer |
31 | 31 |
32 | 32 |
33 _MAX_ORDINAL_VALUE = 0xffffffff | 33 _MAX_ORDINAL_VALUE = 0xffffffff |
34 _MAX_ARRAY_SIZE = 0xffffffff | 34 _MAX_ARRAY_SIZE = 0xffffffff |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 # 'eval' the literal to strip the quotes. | 107 # 'eval' the literal to strip the quotes. |
108 # TODO(vtl): This eval is dubious. We should unquote/unescape ourselves. | 108 # TODO(vtl): This eval is dubious. We should unquote/unescape ourselves. |
109 p[0] = ast.Import(eval(p[2]), filename=self.filename, lineno=p.lineno(2)) | 109 p[0] = ast.Import(eval(p[2]), filename=self.filename, lineno=p.lineno(2)) |
110 | 110 |
111 def p_module(self, p): | 111 def p_module(self, p): |
112 """module : attribute_section MODULE identifier_wrapped SEMI""" | 112 """module : attribute_section MODULE identifier_wrapped SEMI""" |
113 p[0] = ast.Module(p[3], p[1], filename=self.filename, lineno=p.lineno(2)) | 113 p[0] = ast.Module(p[3], p[1], filename=self.filename, lineno=p.lineno(2)) |
114 | 114 |
115 def p_definition(self, p): | 115 def p_definition(self, p): |
116 """definition : struct | 116 """definition : struct |
| 117 | union |
117 | interface | 118 | interface |
118 | enum | 119 | enum |
119 | const""" | 120 | const""" |
120 p[0] = p[1] | 121 p[0] = p[1] |
121 | 122 |
122 def p_attribute_section_1(self, p): | 123 def p_attribute_section_1(self, p): |
123 """attribute_section : """ | 124 """attribute_section : """ |
124 p[0] = None | 125 p[0] = None |
125 | 126 |
126 def p_attribute_section_2(self, p): | 127 def p_attribute_section_2(self, p): |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 """struct_body : struct_body const | 167 """struct_body : struct_body const |
167 | struct_body enum | 168 | struct_body enum |
168 | struct_body struct_field""" | 169 | struct_body struct_field""" |
169 p[0] = p[1] | 170 p[0] = p[1] |
170 p[0].Append(p[2]) | 171 p[0].Append(p[2]) |
171 | 172 |
172 def p_struct_field(self, p): | 173 def p_struct_field(self, p): |
173 """struct_field : typename NAME ordinal default SEMI""" | 174 """struct_field : typename NAME ordinal default SEMI""" |
174 p[0] = ast.StructField(p[2], p[3], p[1], p[4]) | 175 p[0] = ast.StructField(p[2], p[3], p[1], p[4]) |
175 | 176 |
| 177 def p_union(self, p): |
| 178 """union : UNION NAME LBRACE union_body RBRACE SEMI""" |
| 179 p[0] = ast.Union(p[2], p[4]) |
| 180 |
| 181 def p_union_body_1(self, p): |
| 182 """union_body : """ |
| 183 p[0] = ast.UnionBody() |
| 184 |
| 185 def p_union_body_2(self, p): |
| 186 """union_body : union_body union_field""" |
| 187 p[0] = p[1] |
| 188 p[1].Append(p[2]) |
| 189 |
| 190 def p_union_field(self, p): |
| 191 """union_field : typename NAME ordinal SEMI""" |
| 192 p[0] = ast.UnionField(p[2], p[3], p[1]) |
| 193 |
176 def p_default_1(self, p): | 194 def p_default_1(self, p): |
177 """default : """ | 195 """default : """ |
178 p[0] = None | 196 p[0] = None |
179 | 197 |
180 def p_default_2(self, p): | 198 def p_default_2(self, p): |
181 """default : EQUALS constant""" | 199 """default : EQUALS constant""" |
182 p[0] = p[2] | 200 p[0] = p[2] |
183 | 201 |
184 def p_interface(self, p): | 202 def p_interface(self, p): |
185 """interface : attribute_section INTERFACE NAME LBRACE interface_body \ | 203 """interface : attribute_section INTERFACE NAME LBRACE interface_body \ |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 | 403 |
386 def Parse(source, filename): | 404 def Parse(source, filename): |
387 lexer = Lexer(filename) | 405 lexer = Lexer(filename) |
388 parser = Parser(lexer, source, filename) | 406 parser = Parser(lexer, source, filename) |
389 | 407 |
390 lex.lex(object=lexer) | 408 lex.lex(object=lexer) |
391 yacc.yacc(module=parser, debug=0, write_tables=0) | 409 yacc.yacc(module=parser, debug=0, write_tables=0) |
392 | 410 |
393 tree = yacc.parse(source) | 411 tree = yacc.parse(source) |
394 return tree | 412 return tree |
OLD | NEW |