1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
#!/usr/bin/env python
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Generates a syntax tree from a Mojo IDL file."""
import sys
import os.path
# Try to load the ply module, if not, then assume it is in the third_party
# directory.
try:
# Disable lint check which fails to find the ply module.
# pylint: disable=F0401
from ply import lex
from ply import yacc
except ImportError:
module_path, module_name = os.path.split(__file__)
third_party = os.path.join(module_path, os.pardir, os.pardir, os.pardir,
os.pardir, os.pardir, 'third_party')
sys.path.append(third_party)
# pylint: disable=F0401
from ply import lex
from ply import yacc
from mojo_lexer import Lexer
def _ListFromConcat(*items):
"""Generate list by concatenating inputs (note: only concatenates lists, not
tuples or other iterables)."""
itemsout = []
for item in items:
if item is None:
continue
if type(item) is not type([]):
itemsout.append(item)
else:
itemsout.extend(item)
return itemsout
class ParseError(Exception):
def __init__(self, filename, lineno=None, snippet=None, bad_char=None,
eof=False):
self.filename = filename
self.lineno = lineno
self.snippet = snippet
self.bad_char = bad_char
self.eof = eof
def __str__(self):
return "%s: error: unexpected end of file" % self.filename if self.eof \
else "%s:%d: error: unexpected %r:\n%s" % (
self.filename, self.lineno + 1, self.bad_char, self.snippet)
def __repr__(self):
return str(self)
class Parser(object):
def __init__(self, lexer, source, filename):
self.tokens = lexer.tokens
self.source = source
self.filename = filename
def p_root(self, p):
"""root : import root
| module"""
if len(p) > 2:
p[0] = _ListFromConcat(p[1], p[2])
else:
p[0] = [p[1]]
def p_import(self, p):
"""import : IMPORT STRING_LITERAL"""
# 'eval' the literal to strip the quotes.
p[0] = ('IMPORT', eval(p[2]))
def p_module(self, p):
"""module : MODULE NAME LBRACE definitions RBRACE"""
p[0] = ('MODULE', p[2], p[4])
def p_definitions(self, p):
"""definitions : definition definitions
| """
if len(p) > 1:
p[0] = _ListFromConcat(p[1], p[2])
def p_definition(self, p):
"""definition : struct
| interface
| enum"""
p[0] = p[1]
def p_attribute_section(self, p):
"""attribute_section : LBRACKET attributes RBRACKET
| """
if len(p) > 3:
p[0] = p[2]
def p_attributes(self, p):
"""attributes : attribute
| attribute COMMA attributes
| """
if len(p) == 2:
p[0] = _ListFromConcat(p[1])
elif len(p) > 3:
p[0] = _ListFromConcat(p[1], p[3])
def p_attribute(self, p):
"""attribute : NAME EQUALS expression
| NAME EQUALS NAME"""
p[0] = ('ATTRIBUTE', p[1], p[3])
def p_struct(self, p):
"""struct : attribute_section STRUCT NAME LBRACE struct_body RBRACE SEMI"""
p[0] = ('STRUCT', p[3], p[1], p[5])
def p_struct_body(self, p):
"""struct_body : field struct_body
| enum struct_body
| """
if len(p) > 1:
p[0] = _ListFromConcat(p[1], p[2])
def p_field(self, p):
"""field : typename NAME default ordinal SEMI"""
p[0] = ('FIELD', p[1], p[2], p[4], p[3])
def p_default(self, p):
"""default : EQUALS expression
| EQUALS expression_object
| """
if len(p) > 2:
p[0] = p[2]
def p_interface(self, p):
"""interface : attribute_section INTERFACE NAME LBRACE interface_body \
RBRACE SEMI"""
p[0] = ('INTERFACE', p[3], p[1], p[5])
def p_interface_body(self, p):
"""interface_body : method interface_body
| enum interface_body
| """
if len(p) > 1:
p[0] = _ListFromConcat(p[1], p[2])
def p_response(self, p):
"""response : RESPONSE LPAREN parameters RPAREN
| """
if len(p) > 3:
p[0] = p[3]
def p_method(self, p):
"""method : NAME ordinal LPAREN parameters RPAREN response SEMI"""
p[0] = ('METHOD', p[1], p[4], p[2], p[6])
def p_parameters(self, p):
"""parameters : parameter
| parameter COMMA parameters
| """
if len(p) == 1:
p[0] = []
elif len(p) == 2:
p[0] = _ListFromConcat(p[1])
elif len(p) > 3:
p[0] = _ListFromConcat(p[1], p[3])
def p_parameter(self, p):
"""parameter : typename NAME ordinal"""
p[0] = ('PARAM', p[1], p[2], p[3])
def p_typename(self, p):
"""typename : basictypename
| array"""
p[0] = p[1]
def p_basictypename(self, p):
"""basictypename : identifier
| HANDLE
| specializedhandle"""
p[0] = p[1]
def p_specializedhandle(self, p):
"""specializedhandle : HANDLE LT specializedhandlename GT"""
p[0] = "handle<" + p[3] + ">"
def p_specializedhandlename(self, p):
"""specializedhandlename : DATA_PIPE_CONSUMER
| DATA_PIPE_PRODUCER
| MESSAGE_PIPE"""
p[0] = p[1]
def p_array(self, p):
"""array : basictypename LBRACKET RBRACKET"""
p[0] = p[1] + "[]"
def p_ordinal(self, p):
"""ordinal : ORDINAL
| """
if len(p) > 1:
p[0] = p[1]
def p_enum(self, p):
"""enum : ENUM NAME LBRACE enum_fields RBRACE SEMI"""
p[0] = ('ENUM', p[2], p[4])
def p_enum_fields(self, p):
"""enum_fields : enum_field
| enum_field COMMA enum_fields
| """
if len(p) == 2:
p[0] = _ListFromConcat(p[1])
elif len(p) > 3:
p[0] = _ListFromConcat(p[1], p[3])
def p_enum_field(self, p):
"""enum_field : NAME
| NAME EQUALS expression"""
if len(p) == 2:
p[0] = ('ENUM_FIELD', p[1], None)
else:
p[0] = ('ENUM_FIELD', p[1], p[3])
### Expressions ###
def p_expression_object(self, p):
"""expression_object : expression_array
| LBRACE expression_object_elements RBRACE """
if len(p) < 3:
p[0] = p[1]
else:
p[0] = ('OBJECT', p[2])
def p_expression_object_elements(self, p):
"""expression_object_elements : expression_object
| expression_object COMMA expression_object_elements
| """
if len(p) == 2:
p[0] = _ListFromConcat(p[1])
elif len(p) > 3:
p[0] = _ListFromConcat(p[1], p[3])
def p_expression_array(self, p):
"""expression_array : expression
| LBRACKET expression_array_elements RBRACKET """
if len(p) < 3:
p[0] = p[1]
else:
p[0] = ('ARRAY', p[2])
def p_expression_array_elements(self, p):
"""expression_array_elements : expression_object
| expression_object COMMA expression_array_elements
| """
if len(p) == 2:
p[0] = _ListFromConcat(p[1])
elif len(p) > 3:
p[0] = _ListFromConcat(p[1], p[3])
def p_expression(self, p):
"""expression : conditional_expression"""
p[0] = ('EXPRESSION', p[1])
def p_conditional_expression(self, p):
"""conditional_expression : binary_expression
| binary_expression CONDOP expression COLON \
conditional_expression"""
# Just pass the arguments through. I don't think it's possible to preserve
# the spaces of the original, so just put a single space between them.
p[0] = _ListFromConcat(*p[1:])
# PLY lets us specify precedence of operators, but since we don't actually
# evaluate them, we don't need that here.
def p_binary_expression(self, p):
"""binary_expression : unary_expression
| binary_expression binary_operator \
binary_expression"""
p[0] = _ListFromConcat(*p[1:])
def p_binary_operator(self, p):
"""binary_operator : TIMES
| DIVIDE
| MOD
| PLUS
| MINUS
| RSHIFT
| LSHIFT
| LT
| LE
| GE
| GT
| EQ
| NE
| AND
| OR
| XOR
| LAND
| LOR"""
p[0] = p[1]
def p_unary_expression(self, p):
"""unary_expression : primary_expression
| unary_operator expression"""
p[0] = _ListFromConcat(*p[1:])
def p_unary_operator(self, p):
"""unary_operator : PLUS
| MINUS
| NOT
| LNOT"""
p[0] = p[1]
def p_primary_expression(self, p):
"""primary_expression : constant
| identifier
| LPAREN expression RPAREN"""
p[0] = _ListFromConcat(*p[1:])
def p_identifier(self, p):
"""identifier : NAME
| NAME DOT identifier"""
p[0] = ''.join(p[1:])
def p_constant(self, p):
"""constant : INT_CONST_DEC
| INT_CONST_OCT
| INT_CONST_HEX
| FLOAT_CONST
| HEX_FLOAT_CONST
| CHAR_CONST
| WCHAR_CONST
| STRING_LITERAL
| WSTRING_LITERAL"""
p[0] = _ListFromConcat(*p[1:])
def p_error(self, e):
if e is None:
# Unexpected EOF.
# TODO(vtl): Can we figure out what's missing?
raise ParseError(self.filename, eof=True)
lineno = e.lineno + 1
snippet = self.source.split('\n')[lineno]
raise ParseError(self.filename, lineno=lineno, snippet=snippet,
bad_char=e.value)
def Parse(filename):
source = open(filename).read()
lexer = Lexer()
parser = Parser(lexer, source, filename)
lex.lex(object=lexer)
yacc.yacc(module=parser, debug=0, write_tables=0)
tree = yacc.parse(source)
return tree
def main(argv):
if len(argv) < 2:
print "usage: %s filename" % argv[0]
return 0
for filename in argv[1:]:
print "%s:" % filename
try:
print Parse(filename)
except ParseError, e:
print e
return 1
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))
|