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

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

Issue 61003003: Experimental parser: cleanup rule processing result (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years, 1 month 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/generator.py ('k') | no next file » | 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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 RuleParser.parse(string, parser_state) 227 RuleParser.parse(string, parser_state)
228 return RuleProcessor(parser_state) 228 return RuleProcessor(parser_state)
229 229
230 def automata_iter(self): 230 def automata_iter(self):
231 return iter(self.__automata.items()) 231 return iter(self.__automata.items())
232 232
233 def default_automata(self): 233 def default_automata(self):
234 return self.__automata['default'] 234 return self.__automata['default']
235 235
236 def lex(self, string): 236 def lex(self, string):
237 (nfa, dfa) = self.default_automata() 237 dfa = self.default_automata().dfa()
238 return dfa.lex(string) 238 return dfa.lex(string)
239 239
240 class Automata(object):
241
242 def __init__(self, nfa):
243 (start, dfa_nodes) = nfa.compute_dfa()
244 self.__nfa = nfa
245 self.__dfa = Dfa(start, dfa_nodes)
246 self.__minimial_dfa = self.__dfa.minimize()
247
248 def nfa(self):
249 return self.__dfa
250
251 def dfa(self):
252 return self.__dfa
253
254 def minimal_dfa(self):
255 return self.__minimial_dfa
256
240 def __process_parser_state(self, parser_state): 257 def __process_parser_state(self, parser_state):
241 rule_map = {} 258 rule_map = {}
242 builder = NfaBuilder() 259 builder = NfaBuilder()
243 builder.set_character_classes(parser_state.character_classes) 260 builder.set_character_classes(parser_state.character_classes)
244 assert 'default' in parser_state.rules 261 assert 'default' in parser_state.rules
245 def process(k, v): 262 def process(k, v):
246 graphs = [] 263 graphs = []
247 continues = 0 264 continues = 0
248 for (graph, precedence, code, transition) in v['regex']: 265 for (graph, precedence, code, transition) in v['regex']:
249 default_code = v['default_action'] 266 default_code = v['default_action']
(...skipping 24 matching lines...) Expand all
274 graph = NfaBuilder.or_graphs(graphs) 291 graph = NfaBuilder.or_graphs(graphs)
275 rule_map[k] = graph 292 rule_map[k] = graph
276 # process first the subgraphs, then the default graph 293 # process first the subgraphs, then the default graph
277 for k, v in parser_state.rules.items(): 294 for k, v in parser_state.rules.items():
278 if k == 'default': continue 295 if k == 'default': continue
279 process(k, v) 296 process(k, v)
280 process('default', parser_state.rules['default']) 297 process('default', parser_state.rules['default'])
281 # build the automata 298 # build the automata
282 for rule_name, graph in rule_map.items(): 299 for rule_name, graph in rule_map.items():
283 nfa = builder.nfa(graph) 300 nfa = builder.nfa(graph)
284 (start, dfa_nodes) = nfa.compute_dfa() 301 self.__automata[rule_name] = RuleProcessor.Automata(nfa)
285 dfa = Dfa(start, dfa_nodes)
286 self.__automata[rule_name] = (nfa, dfa)
OLDNEW
« no previous file with comments | « tools/lexer_generator/generator.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698