| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 63 |
| 64 # We have methods which look like they could be functions: | 64 # We have methods which look like they could be functions: |
| 65 # pylint: disable=R0201 | 65 # pylint: disable=R0201 |
| 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 # Names of functions |
| 74 # |
| 75 # In general, we name functions after the left-hand-side of the rule(s) that |
| 76 # they handle. E.g., |p_foo_bar| for a rule |foo_bar : ...|. |
| 77 # |
| 78 # There may be multiple functions handling rules for the same left-hand-side; |
| 79 # then we name the functions |p_foo_bar_N| (for left-hand-side |foo_bar|), |
| 80 # where N is a number (numbered starting from 1). Note that using multiple |
| 81 # functions is actually more efficient than having single functions handle |
| 82 # multiple rules (and, e.g., distinguishing them by examining |len(p)|). |
| 83 # |
| 84 # It's also possible to have a function handling multiple rules with different |
| 85 # left-hand-sides. We do not do this. |
| 86 # |
| 87 # See http://www.dabeaz.com/ply/ply.html#ply_nn25 for more details. |
| 88 |
| 73 def p_root(self, p): | 89 def p_root(self, p): |
| 74 """root : import root | 90 """root : import root |
| 75 | module | 91 | module |
| 76 | definition_list""" | 92 | definition_list""" |
| 77 if len(p) > 2: | 93 if len(p) > 2: |
| 78 p[0] = _ListFromConcat(p[1], p[2]) | 94 p[0] = _ListFromConcat(p[1], p[2]) |
| 79 else: | 95 else: |
| 80 # Generator expects a module. If one wasn't specified insert one with an | 96 # Generator expects a module. If one wasn't specified insert one with an |
| 81 # empty name. | 97 # empty name. |
| 82 if p[1][0] != 'MODULE': | 98 if p[1][0] != 'MODULE': |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 def p_response(self, p): | 186 def p_response(self, p): |
| 171 """response : RESPONSE LPAREN parameter_list RPAREN | 187 """response : RESPONSE LPAREN parameter_list RPAREN |
| 172 | """ | 188 | """ |
| 173 if len(p) > 3: | 189 if len(p) > 3: |
| 174 p[0] = p[3] | 190 p[0] = p[3] |
| 175 | 191 |
| 176 def p_method(self, p): | 192 def p_method(self, p): |
| 177 """method : NAME ordinal LPAREN parameter_list RPAREN response SEMI""" | 193 """method : NAME ordinal LPAREN parameter_list RPAREN response SEMI""" |
| 178 p[0] = ('METHOD', p[1], p[4], p[2], p[6]) | 194 p[0] = ('METHOD', p[1], p[4], p[2], p[6]) |
| 179 | 195 |
| 180 def p_parameter_list(self, p): | 196 def p_parameter_list_1(self, p): |
| 181 """parameter_list : parameter | 197 """parameter_list : """ |
| 182 | parameter COMMA parameter_list | 198 p[0] = [] |
| 183 | """ | 199 |
| 184 if len(p) == 1: | 200 def p_parameter_list_2(self, p): |
| 185 p[0] = [] | 201 """parameter_list : nonempty_parameter_list""" |
| 186 elif len(p) == 2: | 202 p[0] = p[1] |
| 187 p[0] = [p[1]] | 203 |
| 188 else: | 204 def p_nonempty_parameter_list_1(self, p): |
| 189 p[0] = [p[1]] + p[3] | 205 """nonempty_parameter_list : parameter""" |
| 206 p[0] = [p[1]] |
| 207 |
| 208 def p_nonempty_parameter_list_2(self, p): |
| 209 """nonempty_parameter_list : nonempty_parameter_list COMMA parameter""" |
| 210 p[0] = p[1] |
| 211 p[0].append(p[3]) |
| 190 | 212 |
| 191 def p_parameter(self, p): | 213 def p_parameter(self, p): |
| 192 """parameter : typename NAME ordinal""" | 214 """parameter : typename NAME ordinal""" |
| 193 p[0] = ast.Parameter(p[1], p[2], p[3], | 215 p[0] = ast.Parameter(p[1], p[2], p[3], |
| 194 filename=self.filename, lineno=p.lineno(1)) | 216 filename=self.filename, lineno=p.lineno(1)) |
| 195 | 217 |
| 196 def p_typename(self, p): | 218 def p_typename(self, p): |
| 197 """typename : basictypename | 219 """typename : basictypename |
| 198 | array | 220 | array |
| 199 | fixed_array | 221 | fixed_array |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 | 348 |
| 327 def Parse(source, filename): | 349 def Parse(source, filename): |
| 328 lexer = Lexer(filename) | 350 lexer = Lexer(filename) |
| 329 parser = Parser(lexer, source, filename) | 351 parser = Parser(lexer, source, filename) |
| 330 | 352 |
| 331 lex.lex(object=lexer) | 353 lex.lex(object=lexer) |
| 332 yacc.yacc(module=parser, debug=0, write_tables=0) | 354 yacc.yacc(module=parser, debug=0, write_tables=0) |
| 333 | 355 |
| 334 tree = yacc.parse(source) | 356 tree = yacc.parse(source) |
| 335 return tree | 357 return tree |
| OLD | NEW |