Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: tools/lexer_generator/rule_parser.py

Issue 135053003: Experimental parser: remove inline code from lexer rules (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/lexer_generator/rule_lexer.py ('k') | tools/lexer_generator/rule_parser_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2013 the V8 project authors. All rights reserved. 1 # Copyright 2013 the V8 project authors. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without 2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are 3 # modification, are permitted provided that the following conditions are
4 # met: 4 # met:
5 # 5 #
6 # * Redistributions of source code must retain the above copyright 6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer. 7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above 8 # * Redistributions in binary form must reproduce the above
9 # copyright notice, this list of conditions and the following 9 # copyright notice, this list of conditions and the following
10 # disclaimer in the documentation and/or other materials provided 10 # disclaimer in the documentation and/or other materials provided
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 assert not rules['default_action'] 113 assert not rules['default_action']
114 rules['default_action'] = action 114 rules['default_action'] = action
115 elif p[1] == 'catch_all': 115 elif p[1] == 'catch_all':
116 assert not rules['catch_all'] 116 assert not rules['catch_all']
117 rules['catch_all'] = (precedence, action) 117 rules['catch_all'] = (precedence, action)
118 else: 118 else:
119 regex = p[1] 119 regex = p[1]
120 rules['regex'].append((regex, precedence, action)) 120 rules['regex'].append((regex, precedence, action))
121 121
122 def p_action(self, p): 122 def p_action(self, p):
123 '''action : ACTION_OPEN maybe_action_part OR maybe_action_part OR maybe_tran sition ACTION_CLOSE''' 123 '''action : ACTION_OPEN maybe_identifier_action OR maybe_identifier_action O R maybe_transition ACTION_CLOSE'''
124 p[0] = (p[2], p[4], p[6]) 124 p[0] = (p[2], p[4], p[6])
125 125
126 def p_default_action(self, p): 126 def p_default_action(self, p):
127 'default_action : ACTION_OPEN action_part ACTION_CLOSE' 127 'default_action : ACTION_OPEN identifier_action ACTION_CLOSE'
128 p[0] = (None, p[2], None) 128 p[0] = (None, p[2], None)
129 129
130 def p_maybe_action_part(self, p): 130 def p_maybe_identifier_action(self, p):
131 '''maybe_action_part : action_part 131 '''maybe_identifier_action : identifier_action
132 | empty''' 132 | empty'''
133 p[0] = p[1] 133 p[0] = p[1]
134 134
135 def p_action_part(self, p):
136 '''action_part : code
137 | identifier_action'''
138 p[0] = p[1]
139
140 def p_maybe_transition(self, p): 135 def p_maybe_transition(self, p):
141 '''maybe_transition : IDENTIFIER 136 '''maybe_transition : IDENTIFIER
142 | empty''' 137 | empty'''
143 p[0] = p[1] 138 p[0] = p[1]
144 139
145 def p_identifier_action(self, p): 140 def p_identifier_action(self, p):
146 '''identifier_action : IDENTIFIER 141 '''identifier_action : IDENTIFIER
147 | IDENTIFIER LEFT_PARENTHESIS RIGHT_PARENTHESIS 142 | IDENTIFIER LEFT_PARENTHESIS RIGHT_PARENTHESIS
148 | IDENTIFIER LEFT_PARENTHESIS action_params RIGHT_PAREN THESIS''' 143 | IDENTIFIER LEFT_PARENTHESIS action_params RIGHT_PAREN THESIS'''
149 assert p[1] != 'code'
150 if len(p) == 2 or len(p) == 4: 144 if len(p) == 2 or len(p) == 4:
151 p[0] = (p[1], None) 145 p[0] = (p[1], None)
152 elif len(p) == 5: 146 elif len(p) == 5:
153 if len(p[3]) == 1: 147 if len(p[3]) == 1:
154 p[0] = (p[1], p[3][0]) 148 p[0] = (p[1], p[3][0])
155 else: 149 else:
156 p[0] = (p[1], p[3]) 150 p[0] = (p[1], p[3])
157 else: 151 else:
158 raise Exception() 152 raise Exception()
159 153
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 'regex_alias : IDENTIFIER' 207 'regex_alias : IDENTIFIER'
214 p[0] = self.__state.aliases[p[1]] 208 p[0] = self.__state.aliases[p[1]]
215 209
216 def p_modifier(self, p): 210 def p_modifier(self, p):
217 '''modifier : PLUS 211 '''modifier : PLUS
218 | QUESTION_MARK 212 | QUESTION_MARK
219 | STAR 213 | STAR
220 | empty''' 214 | empty'''
221 p[0] = p[1] 215 p[0] = p[1]
222 216
223 def p_code(self, p):
224 'code : LEFT_BRACKET code_fragments RIGHT_BRACKET'
225 p[0] = ('code', p[2].strip())
226
227 def p_code_fragments(self, p):
228 '''code_fragments : CODE_FRAGMENT code_fragments
229 | empty'''
230 p[0] = p[1]
231 if len(p) == 3 and p[2]:
232 p[0] = p[1] + p[2]
233
234 def p_empty(self, p): 217 def p_empty(self, p):
235 'empty :' 218 'empty :'
236 219
237 def p_error(self, p): 220 def p_error(self, p):
238 raise Exception("Syntax error in input '%s'" % str(p)) 221 raise Exception("Syntax error in input '%s'" % str(p))
239 222
240 def build(self, **kwargs): 223 def build(self, **kwargs):
241 self.parser = yacc.yacc(module=self, debug=0, write_tables=0, **kwargs) 224 self.parser = yacc.yacc(module=self, debug=0, write_tables=0, **kwargs)
242 self.lexer = RuleLexer() 225 self.lexer = RuleLexer()
243 self.lexer.build(**kwargs) 226 self.lexer.build(**kwargs)
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 for k, v in parser_state.rules.items(): 324 for k, v in parser_state.rules.items():
342 if k == 'default': continue 325 if k == 'default': continue
343 process(k, v) 326 process(k, v)
344 process('default', parser_state.rules['default']) 327 process('default', parser_state.rules['default'])
345 # build the automata 328 # build the automata
346 for rule_name, graph in rule_map.items(): 329 for rule_name, graph in rule_map.items():
347 self.__automata[rule_name] = RuleProcessor.Automata(builder, graph) 330 self.__automata[rule_name] = RuleProcessor.Automata(builder, graph)
348 # process default_action 331 # process default_action
349 default_action = parser_state.rules['default']['default_action'] 332 default_action = parser_state.rules['default']['default_action']
350 self.default_action = Action(None, default_action[1]) if default_action else None 333 self.default_action = Action(None, default_action[1]) if default_action else None
OLDNEW
« no previous file with comments | « tools/lexer_generator/rule_lexer.py ('k') | tools/lexer_generator/rule_parser_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698