OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 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 """Generates a syntax tree from a Mojo IDL file.""" | 6 """Generates a syntax tree from a Mojo IDL file.""" |
7 | 7 |
8 | 8 |
9 import sys | 9 import sys |
10 import os.path | 10 import os.path |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 """expression_array_elements : expression_object | 236 """expression_array_elements : expression_object |
237 | expression_object COMMA expression_array_elem
ents | 237 | expression_object COMMA expression_array_elem
ents |
238 | """ | 238 | """ |
239 if len(p) == 2: | 239 if len(p) == 2: |
240 p[0] = ListFromConcat(p[1]) | 240 p[0] = ListFromConcat(p[1]) |
241 elif len(p) > 3: | 241 elif len(p) > 3: |
242 p[0] = ListFromConcat(p[1], p[3]) | 242 p[0] = ListFromConcat(p[1], p[3]) |
243 | 243 |
244 def p_expression(self, p): | 244 def p_expression(self, p): |
245 """expression : conditional_expression""" | 245 """expression : conditional_expression""" |
246 p[0] = p[1] | 246 p[0] = ('EXPRESSION', p[1]) |
247 | 247 |
248 def p_conditional_expression(self, p): | 248 def p_conditional_expression(self, p): |
249 """conditional_expression : binary_expression | 249 """conditional_expression : binary_expression |
250 | binary_expression CONDOP expression COLON \ | 250 | binary_expression CONDOP expression COLON \ |
251 conditional_expression""" | 251 conditional_expression""" |
252 # Just pass the arguments through. I don't think it's possible to preserve | 252 # Just pass the arguments through. I don't think it's possible to preserve |
253 # the spaces of the original, so just put a single space between them. | 253 # the spaces of the original, so just put a single space between them. |
254 p[0] = ' '.join(p[1:]) | 254 p[0] = ListFromConcat(*p[1:]) |
255 | 255 |
256 # PLY lets us specify precedence of operators, but since we don't actually | 256 # PLY lets us specify precedence of operators, but since we don't actually |
257 # evaluate them, we don't need that here. | 257 # evaluate them, we don't need that here. |
258 def p_binary_expression(self, p): | 258 def p_binary_expression(self, p): |
259 """binary_expression : unary_expression | 259 """binary_expression : unary_expression |
260 | binary_expression binary_operator \ | 260 | binary_expression binary_operator \ |
261 binary_expression""" | 261 binary_expression""" |
262 p[0] = ' '.join(p[1:]) | 262 p[0] = ListFromConcat(*p[1:]) |
263 | 263 |
264 def p_binary_operator(self, p): | 264 def p_binary_operator(self, p): |
265 """binary_operator : TIMES | 265 """binary_operator : TIMES |
266 | DIVIDE | 266 | DIVIDE |
267 | MOD | 267 | MOD |
268 | PLUS | 268 | PLUS |
269 | MINUS | 269 | MINUS |
270 | RSHIFT | 270 | RSHIFT |
271 | LSHIFT | 271 | LSHIFT |
272 | LT | 272 | LT |
273 | LE | 273 | LE |
274 | GE | 274 | GE |
275 | GT | 275 | GT |
276 | EQ | 276 | EQ |
277 | NE | 277 | NE |
278 | AND | 278 | AND |
279 | OR | 279 | OR |
280 | XOR | 280 | XOR |
281 | LAND | 281 | LAND |
282 | LOR""" | 282 | LOR""" |
283 p[0] = p[1] | 283 p[0] = p[1] |
284 | 284 |
285 def p_unary_expression(self, p): | 285 def p_unary_expression(self, p): |
286 """unary_expression : primary_expression | 286 """unary_expression : primary_expression |
287 | unary_operator expression""" | 287 | unary_operator expression""" |
288 p[0] = ''.join(p[1:]) | 288 p[0] = ListFromConcat(*p[1:]) |
289 | 289 |
290 def p_unary_operator(self, p): | 290 def p_unary_operator(self, p): |
291 """unary_operator : PLUS | 291 """unary_operator : PLUS |
292 | MINUS | 292 | MINUS |
293 | NOT | 293 | NOT |
294 | LNOT""" | 294 | LNOT""" |
295 p[0] = p[1] | 295 p[0] = p[1] |
296 | 296 |
297 def p_primary_expression(self, p): | 297 def p_primary_expression(self, p): |
298 """primary_expression : constant | 298 """primary_expression : constant |
299 | NAME | 299 | identifier |
300 | NAME DOT NAME | |
301 | LPAREN expression RPAREN""" | 300 | LPAREN expression RPAREN""" |
| 301 p[0] = ListFromConcat(*p[1:]) |
| 302 |
| 303 def p_identifier(self, p): |
| 304 """identifier : NAME |
| 305 | NAME DOT NAME""" |
302 p[0] = ''.join(p[1:]) | 306 p[0] = ''.join(p[1:]) |
303 | 307 |
304 def p_constant(self, p): | 308 def p_constant(self, p): |
305 """constant : INT_CONST_DEC | 309 """constant : INT_CONST_DEC |
306 | INT_CONST_OCT | 310 | INT_CONST_OCT |
307 | INT_CONST_HEX | 311 | INT_CONST_HEX |
308 | FLOAT_CONST | 312 | FLOAT_CONST |
309 | HEX_FLOAT_CONST | 313 | HEX_FLOAT_CONST |
310 | CHAR_CONST | 314 | CHAR_CONST |
311 | WCHAR_CONST | 315 | WCHAR_CONST |
312 | STRING_LITERAL | 316 | STRING_LITERAL |
313 | WSTRING_LITERAL""" | 317 | WSTRING_LITERAL""" |
314 p[0] = ''.join(p[1:]) | 318 p[0] = ListFromConcat(*p[1:]) |
315 | 319 |
316 def p_error(self, e): | 320 def p_error(self, e): |
317 print('error: %s'%e) | 321 print('error: %s'%e) |
318 | 322 |
319 | 323 |
320 def Parse(filename): | 324 def Parse(filename): |
321 lexer = Lexer() | 325 lexer = Lexer() |
322 parser = Parser(lexer) | 326 parser = Parser(lexer) |
323 | 327 |
324 lex.lex(object=lexer) | 328 lex.lex(object=lexer) |
325 yacc.yacc(module=parser, debug=0, write_tables=0) | 329 yacc.yacc(module=parser, debug=0, write_tables=0) |
326 | 330 |
327 tree = yacc.parse(open(filename).read()) | 331 tree = yacc.parse(open(filename).read()) |
328 return tree | 332 return tree |
329 | 333 |
330 | 334 |
331 def Main(): | 335 def Main(): |
332 if len(sys.argv) < 2: | 336 if len(sys.argv) < 2: |
333 print("usage: %s filename" % (sys.argv[0])) | 337 print("usage: %s filename" % (sys.argv[0])) |
334 sys.exit(1) | 338 sys.exit(1) |
335 tree = Parse(filename=sys.argv[1]) | 339 tree = Parse(filename=sys.argv[1]) |
336 print(tree) | 340 print(tree) |
337 | 341 |
338 | 342 |
339 if __name__ == '__main__': | 343 if __name__ == '__main__': |
340 Main() | 344 Main() |
OLD | NEW |