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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 class Parser(object): | 66 class Parser(object): |
67 | 67 |
68 def __init__(self, lexer, source, filename): | 68 def __init__(self, lexer, source, filename): |
69 self.tokens = lexer.tokens | 69 self.tokens = lexer.tokens |
70 self.source = source | 70 self.source = source |
71 self.filename = filename | 71 self.filename = filename |
72 | 72 |
73 def p_root(self, p): | 73 def p_root(self, p): |
74 """root : import root | 74 """root : import root |
75 | module | 75 | module |
76 | definitions""" | 76 | definition_list""" |
77 if len(p) > 2: | 77 if len(p) > 2: |
78 p[0] = _ListFromConcat(p[1], p[2]) | 78 p[0] = _ListFromConcat(p[1], p[2]) |
79 else: | 79 else: |
80 # Generator expects a module. If one wasn't specified insert one with an | 80 # Generator expects a module. If one wasn't specified insert one with an |
81 # empty name. | 81 # empty name. |
82 if p[1][0] != 'MODULE': | 82 if p[1][0] != 'MODULE': |
83 p[0] = [('MODULE', '', None, p[1])] | 83 p[0] = [('MODULE', '', None, p[1])] |
84 else: | 84 else: |
85 p[0] = [p[1]] | 85 p[0] = [p[1]] |
86 | 86 |
87 def p_import(self, p): | 87 def p_import(self, p): |
88 """import : IMPORT STRING_LITERAL""" | 88 """import : IMPORT STRING_LITERAL""" |
89 # 'eval' the literal to strip the quotes. | 89 # 'eval' the literal to strip the quotes. |
90 p[0] = ('IMPORT', eval(p[2])) | 90 p[0] = ('IMPORT', eval(p[2])) |
91 | 91 |
92 def p_module(self, p): | 92 def p_module(self, p): |
93 """module : attribute_section MODULE identifier LBRACE definitions RBRACE""" | 93 """module : attribute_section MODULE identifier LBRACE definition_list \ |
| 94 RBRACE""" |
94 p[0] = ('MODULE', p[3], p[1], p[5]) | 95 p[0] = ('MODULE', p[3], p[1], p[5]) |
95 | 96 |
96 def p_definitions(self, p): | 97 def p_definition_list(self, p): |
97 """definitions : definition definitions | 98 """definition_list : definition definition_list |
98 | """ | 99 | """ |
99 if len(p) > 1: | 100 if len(p) > 1: |
100 p[0] = _ListFromConcat(p[1], p[2]) | 101 p[0] = _ListFromConcat(p[1], p[2]) |
101 | 102 |
102 def p_definition(self, p): | 103 def p_definition(self, p): |
103 """definition : struct | 104 """definition : struct |
104 | interface | 105 | interface |
105 | enum | 106 | enum |
106 | const""" | 107 | const""" |
107 p[0] = p[1] | 108 p[0] = p[1] |
108 | 109 |
109 def p_attribute_section(self, p): | 110 def p_attribute_section(self, p): |
110 """attribute_section : LBRACKET attributes RBRACKET | 111 """attribute_section : LBRACKET attribute_list RBRACKET |
111 | """ | 112 | """ |
112 if len(p) > 3: | 113 if len(p) > 3: |
113 p[0] = p[2] | 114 p[0] = p[2] |
114 | 115 |
115 def p_attributes(self, p): | 116 def p_attribute_list(self, p): |
116 """attributes : attribute | 117 """attribute_list : attribute |
117 | attribute COMMA attributes | 118 | attribute COMMA attribute_list |
118 | """ | 119 | """ |
119 if len(p) == 2: | 120 if len(p) == 2: |
120 p[0] = _ListFromConcat(p[1]) | 121 p[0] = _ListFromConcat(p[1]) |
121 elif len(p) > 3: | 122 elif len(p) > 3: |
122 p[0] = _ListFromConcat(p[1], p[3]) | 123 p[0] = _ListFromConcat(p[1], p[3]) |
123 | 124 |
124 def p_attribute(self, p): | 125 def p_attribute(self, p): |
125 """attribute : NAME EQUALS evaled_literal | 126 """attribute : NAME EQUALS evaled_literal |
126 | NAME EQUALS NAME""" | 127 | NAME EQUALS NAME""" |
127 p[0] = ('ATTRIBUTE', p[1], p[3]) | 128 p[0] = ('ATTRIBUTE', p[1], p[3]) |
128 | 129 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 161 |
161 def p_interface_body(self, p): | 162 def p_interface_body(self, p): |
162 """interface_body : method interface_body | 163 """interface_body : method interface_body |
163 | enum interface_body | 164 | enum interface_body |
164 | const interface_body | 165 | const interface_body |
165 | """ | 166 | """ |
166 if len(p) > 1: | 167 if len(p) > 1: |
167 p[0] = _ListFromConcat(p[1], p[2]) | 168 p[0] = _ListFromConcat(p[1], p[2]) |
168 | 169 |
169 def p_response(self, p): | 170 def p_response(self, p): |
170 """response : RESPONSE LPAREN parameters RPAREN | 171 """response : RESPONSE LPAREN parameter_list RPAREN |
171 | """ | 172 | """ |
172 if len(p) > 3: | 173 if len(p) > 3: |
173 p[0] = p[3] | 174 p[0] = p[3] |
174 | 175 |
175 def p_method(self, p): | 176 def p_method(self, p): |
176 """method : NAME ordinal LPAREN parameters RPAREN response SEMI""" | 177 """method : NAME ordinal LPAREN parameter_list RPAREN response SEMI""" |
177 p[0] = ('METHOD', p[1], p[4], p[2], p[6]) | 178 p[0] = ('METHOD', p[1], p[4], p[2], p[6]) |
178 | 179 |
179 def p_parameters(self, p): | 180 def p_parameter_list(self, p): |
180 """parameters : parameter | 181 """parameter_list : parameter |
181 | parameter COMMA parameters | 182 | parameter COMMA parameter_list |
182 | """ | 183 | """ |
183 if len(p) == 1: | 184 if len(p) == 1: |
184 p[0] = [] | 185 p[0] = [] |
185 elif len(p) == 2: | 186 elif len(p) == 2: |
186 p[0] = [p[1]] | 187 p[0] = [p[1]] |
187 else: | 188 else: |
188 p[0] = [p[1]] + p[3] | 189 p[0] = [p[1]] + p[3] |
189 | 190 |
190 def p_parameter(self, p): | 191 def p_parameter(self, p): |
191 """parameter : typename NAME ordinal""" | 192 """parameter : typename NAME ordinal""" |
192 p[0] = ast.Parameter(p[1], p[2], p[3], | 193 p[0] = ast.Parameter(p[1], p[2], p[3], |
(...skipping 30 matching lines...) Expand all Loading... |
223 | 224 |
224 def p_array(self, p): | 225 def p_array(self, p): |
225 """array : typename LBRACKET RBRACKET""" | 226 """array : typename LBRACKET RBRACKET""" |
226 p[0] = p[1] + "[]" | 227 p[0] = p[1] + "[]" |
227 | 228 |
228 def p_fixed_array(self, p): | 229 def p_fixed_array(self, p): |
229 """fixed_array : typename LBRACKET INT_CONST_DEC RBRACKET""" | 230 """fixed_array : typename LBRACKET INT_CONST_DEC RBRACKET""" |
230 value = int(p[3]) | 231 value = int(p[3]) |
231 if value == 0 or value > _MAX_ARRAY_SIZE: | 232 if value == 0 or value > _MAX_ARRAY_SIZE: |
232 raise ParseError(self.filename, "Fixed array size %d invalid" % value, | 233 raise ParseError(self.filename, "Fixed array size %d invalid" % value, |
233 lineno=p.lineno(1), | 234 lineno=p.lineno(3), |
234 snippet=self._GetSnippet(p.lineno(1))) | 235 snippet=self._GetSnippet(p.lineno(3))) |
235 p[0] = p[1] + "[" + p[3] + "]" | 236 p[0] = p[1] + "[" + p[3] + "]" |
236 | 237 |
237 def p_interfacerequest(self, p): | 238 def p_interfacerequest(self, p): |
238 """interfacerequest : identifier AMP""" | 239 """interfacerequest : identifier AMP""" |
239 p[0] = p[1] + "&" | 240 p[0] = p[1] + "&" |
240 | 241 |
241 def p_ordinal(self, p): | 242 def p_ordinal(self, p): |
242 """ordinal : ORDINAL | 243 """ordinal : ORDINAL |
243 | """ | 244 | """ |
244 if len(p) > 1: | 245 if len(p) > 1: |
245 value = int(p[1][1:]) | 246 value = int(p[1][1:]) |
246 if value > _MAX_ORDINAL_VALUE: | 247 if value > _MAX_ORDINAL_VALUE: |
247 raise ParseError(self.filename, "Ordinal value %d too large:" % value, | 248 raise ParseError(self.filename, "Ordinal value %d too large:" % value, |
248 lineno=p.lineno(1), | 249 lineno=p.lineno(1), |
249 snippet=self._GetSnippet(p.lineno(1))) | 250 snippet=self._GetSnippet(p.lineno(1))) |
250 p[0] = ast.Ordinal(value, filename=self.filename, lineno=p.lineno(1)) | 251 p[0] = ast.Ordinal(value, filename=self.filename, lineno=p.lineno(1)) |
251 else: | 252 else: |
252 p[0] = ast.Ordinal(None) | 253 p[0] = ast.Ordinal(None) |
253 | 254 |
254 def p_enum(self, p): | 255 def p_enum(self, p): |
255 """enum : ENUM NAME LBRACE enum_fields RBRACE SEMI""" | 256 """enum : ENUM NAME LBRACE enum_field_list RBRACE SEMI""" |
256 p[0] = ('ENUM', p[2], p[4]) | 257 p[0] = ('ENUM', p[2], p[4]) |
257 | 258 |
258 def p_enum_fields(self, p): | 259 def p_enum_field_list(self, p): |
259 """enum_fields : enum_field | 260 """enum_field_list : enum_field |
260 | enum_field COMMA enum_fields | 261 | enum_field COMMA enum_field_list |
261 | """ | 262 | """ |
262 if len(p) == 2: | 263 if len(p) == 2: |
263 p[0] = _ListFromConcat(p[1]) | 264 p[0] = _ListFromConcat(p[1]) |
264 elif len(p) > 3: | 265 elif len(p) > 3: |
265 p[0] = _ListFromConcat(p[1], p[3]) | 266 p[0] = _ListFromConcat(p[1], p[3]) |
266 | 267 |
267 def p_enum_field(self, p): | 268 def p_enum_field(self, p): |
268 """enum_field : NAME | 269 """enum_field : NAME |
269 | NAME EQUALS constant""" | 270 | NAME EQUALS constant""" |
270 if len(p) == 2: | 271 if len(p) == 2: |
271 p[0] = ('ENUM_FIELD', p[1], None) | 272 p[0] = ('ENUM_FIELD', p[1], None) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 | 326 |
326 def Parse(source, filename): | 327 def Parse(source, filename): |
327 lexer = Lexer(filename) | 328 lexer = Lexer(filename) |
328 parser = Parser(lexer, source, filename) | 329 parser = Parser(lexer, source, filename) |
329 | 330 |
330 lex.lex(object=lexer) | 331 lex.lex(object=lexer) |
331 yacc.yacc(module=parser, debug=0, write_tables=0) | 332 yacc.yacc(module=parser, debug=0, write_tables=0) |
332 | 333 |
333 tree = yacc.parse(source) | 334 tree = yacc.parse(source) |
334 return tree | 335 return tree |
OLD | NEW |