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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 | 94 |
95 def p_definitions(self, p): | 95 def p_definitions(self, p): |
96 """definitions : definition definitions | 96 """definitions : definition definitions |
97 | """ | 97 | """ |
98 if len(p) > 1: | 98 if len(p) > 1: |
99 p[0] = _ListFromConcat(p[1], p[2]) | 99 p[0] = _ListFromConcat(p[1], p[2]) |
100 | 100 |
101 def p_definition(self, p): | 101 def p_definition(self, p): |
102 """definition : struct | 102 """definition : struct |
103 | interface | 103 | interface |
104 | enum""" | 104 | enum |
| 105 | const""" |
105 p[0] = p[1] | 106 p[0] = p[1] |
106 | 107 |
107 def p_attribute_section(self, p): | 108 def p_attribute_section(self, p): |
108 """attribute_section : LBRACKET attributes RBRACKET | 109 """attribute_section : LBRACKET attributes RBRACKET |
109 | """ | 110 | """ |
110 if len(p) > 3: | 111 if len(p) > 3: |
111 p[0] = p[2] | 112 p[0] = p[2] |
112 | 113 |
113 def p_attributes(self, p): | 114 def p_attributes(self, p): |
114 """attributes : attribute | 115 """attributes : attribute |
115 | attribute COMMA attributes | 116 | attribute COMMA attributes |
116 | """ | 117 | """ |
117 if len(p) == 2: | 118 if len(p) == 2: |
118 p[0] = _ListFromConcat(p[1]) | 119 p[0] = _ListFromConcat(p[1]) |
119 elif len(p) > 3: | 120 elif len(p) > 3: |
120 p[0] = _ListFromConcat(p[1], p[3]) | 121 p[0] = _ListFromConcat(p[1], p[3]) |
121 | 122 |
122 def p_attribute(self, p): | 123 def p_attribute(self, p): |
123 """attribute : NAME EQUALS expression | 124 """attribute : NAME EQUALS expression |
124 | NAME EQUALS NAME""" | 125 | NAME EQUALS NAME""" |
125 p[0] = ('ATTRIBUTE', p[1], p[3]) | 126 p[0] = ('ATTRIBUTE', p[1], p[3]) |
126 | 127 |
127 def p_struct(self, p): | 128 def p_struct(self, p): |
128 """struct : attribute_section STRUCT NAME LBRACE struct_body RBRACE SEMI""" | 129 """struct : attribute_section STRUCT NAME LBRACE struct_body RBRACE SEMI""" |
129 p[0] = ('STRUCT', p[3], p[1], p[5]) | 130 p[0] = ('STRUCT', p[3], p[1], p[5]) |
130 | 131 |
131 def p_struct_body(self, p): | 132 def p_struct_body(self, p): |
132 """struct_body : field struct_body | 133 """struct_body : field struct_body |
133 | enum struct_body | 134 | enum struct_body |
| 135 | const struct_body |
134 | """ | 136 | """ |
135 if len(p) > 1: | 137 if len(p) > 1: |
136 p[0] = _ListFromConcat(p[1], p[2]) | 138 p[0] = _ListFromConcat(p[1], p[2]) |
137 | 139 |
138 def p_field(self, p): | 140 def p_field(self, p): |
139 """field : typename NAME default ordinal SEMI""" | 141 """field : typename NAME default ordinal SEMI""" |
140 p[0] = ('FIELD', p[1], p[2], p[4], p[3]) | 142 p[0] = ('FIELD', p[1], p[2], p[4], p[3]) |
141 | 143 |
142 def p_default(self, p): | 144 def p_default(self, p): |
143 """default : EQUALS expression | 145 """default : EQUALS expression |
144 | EQUALS expression_object | 146 | EQUALS expression_object |
145 | """ | 147 | """ |
146 if len(p) > 2: | 148 if len(p) > 2: |
147 p[0] = p[2] | 149 p[0] = p[2] |
148 | 150 |
149 def p_interface(self, p): | 151 def p_interface(self, p): |
150 """interface : attribute_section INTERFACE NAME LBRACE interface_body \ | 152 """interface : attribute_section INTERFACE NAME LBRACE interface_body \ |
151 RBRACE SEMI""" | 153 RBRACE SEMI""" |
152 p[0] = ('INTERFACE', p[3], p[1], p[5]) | 154 p[0] = ('INTERFACE', p[3], p[1], p[5]) |
153 | 155 |
154 def p_interface_body(self, p): | 156 def p_interface_body(self, p): |
155 """interface_body : method interface_body | 157 """interface_body : method interface_body |
156 | enum interface_body | 158 | enum interface_body |
| 159 | const interface_body |
157 | """ | 160 | """ |
158 if len(p) > 1: | 161 if len(p) > 1: |
159 p[0] = _ListFromConcat(p[1], p[2]) | 162 p[0] = _ListFromConcat(p[1], p[2]) |
160 | 163 |
161 def p_response(self, p): | 164 def p_response(self, p): |
162 """response : RESPONSE LPAREN parameters RPAREN | 165 """response : RESPONSE LPAREN parameters RPAREN |
163 | """ | 166 | """ |
164 if len(p) > 3: | 167 if len(p) > 3: |
165 p[0] = p[3] | 168 p[0] = p[3] |
166 | 169 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 p[0] = _ListFromConcat(p[1], p[3]) | 239 p[0] = _ListFromConcat(p[1], p[3]) |
237 | 240 |
238 def p_enum_field(self, p): | 241 def p_enum_field(self, p): |
239 """enum_field : NAME | 242 """enum_field : NAME |
240 | NAME EQUALS expression""" | 243 | NAME EQUALS expression""" |
241 if len(p) == 2: | 244 if len(p) == 2: |
242 p[0] = ('ENUM_FIELD', p[1], None) | 245 p[0] = ('ENUM_FIELD', p[1], None) |
243 else: | 246 else: |
244 p[0] = ('ENUM_FIELD', p[1], p[3]) | 247 p[0] = ('ENUM_FIELD', p[1], p[3]) |
245 | 248 |
| 249 def p_const(self, p): |
| 250 """const : CONST typename NAME EQUALS expression SEMI""" |
| 251 p[0] = ('CONST', p[2], p[3], p[5]) |
| 252 |
246 ### Expressions ### | 253 ### Expressions ### |
247 | 254 |
248 def p_expression_object(self, p): | 255 def p_expression_object(self, p): |
249 """expression_object : expression_array | 256 """expression_object : expression_array |
250 | LBRACE expression_object_elements RBRACE """ | 257 | LBRACE expression_object_elements RBRACE """ |
251 if len(p) < 3: | 258 if len(p) < 3: |
252 p[0] = p[1] | 259 p[0] = p[1] |
253 else: | 260 else: |
254 p[0] = ('OBJECT', p[2]) | 261 p[0] = ('OBJECT', p[2]) |
255 | 262 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 | 359 |
353 def Parse(source, filename): | 360 def Parse(source, filename): |
354 lexer = Lexer(filename) | 361 lexer = Lexer(filename) |
355 parser = Parser(lexer, source, filename) | 362 parser = Parser(lexer, source, filename) |
356 | 363 |
357 lex.lex(object=lexer) | 364 lex.lex(object=lexer) |
358 yacc.yacc(module=parser, debug=0, write_tables=0) | 365 yacc.yacc(module=parser, debug=0, write_tables=0) |
359 | 366 |
360 tree = yacc.parse(source) | 367 tree = yacc.parse(source) |
361 return tree | 368 return tree |
OLD | NEW |