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

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

Issue 141083011: Experimental parser: always add all subtrees (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: fix Created 6 years, 10 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/nfa_builder.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 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 return self.__nfa 288 return self.__nfa
289 289
290 def dfa(self): 290 def dfa(self):
291 if not self.__dfa: 291 if not self.__dfa:
292 (start, dfa_nodes) = self.nfa().compute_dfa() 292 (start, dfa_nodes) = self.nfa().compute_dfa()
293 self.__dfa = Dfa(self.nfa().encoding(), start, dfa_nodes) 293 self.__dfa = Dfa(self.nfa().encoding(), start, dfa_nodes)
294 return self.__dfa 294 return self.__dfa
295 295
296 def optimize_dfa(self, log = False): 296 def optimize_dfa(self, log = False):
297 assert not self.__dfa 297 assert not self.__dfa
298 self.__dfa = DfaOptimizer.optimize(self.dfa(), log) 298 assert not self.__minimial_dfa
299 self.__dfa = DfaOptimizer.optimize(self.minimal_dfa(), log)
300 self.__minimial_dfa = None
299 301
300 def minimal_dfa(self): 302 def minimal_dfa(self):
301 if not self.__minimial_dfa: 303 if not self.__minimial_dfa:
302 self.__minimial_dfa = self.dfa().minimize() 304 self.__minimial_dfa = self.dfa().minimize()
303 return self.__minimial_dfa 305 return self.__minimial_dfa
304 306
305 def __process_parser_state(self, parser_state): 307 def __process_parser_state(self, parser_state):
306 rule_map = {} 308 rule_map = {}
307 assert 'default' in parser_state.rules 309 assert 'default' in parser_state.rules
308 def process(subgraph, v): 310 def process(subgraph, v):
309 graphs = [] 311 graphs = []
310 for graph, precedence, action in v['regex']: 312 for graph, precedence, action in v['regex']:
311 (entry_action, match_action, transition) = action 313 (entry_action, match_action, transition) = action
312 if entry_action or match_action: 314 if entry_action or match_action:
313 graph = NfaBuilder.add_action( 315 graph = NfaBuilder.add_action(
314 graph, Action(entry_action, match_action, precedence)) 316 graph, Action(entry_action, match_action, precedence))
315 if not transition: 317 if not transition:
316 pass 318 pass
317 elif transition == 'continue': 319 elif transition == 'continue':
318 assert not subgraph == 'default', 'unimplemented' 320 assert not subgraph == 'default', 'unimplemented'
319 graph = NfaBuilder.add_continue(graph) 321 graph = NfaBuilder.add_continue(graph)
320 else: 322 else:
321 assert subgraph == 'default', 'unimplemented' 323 assert subgraph == 'default', 'unimplemented'
322 graph = NfaBuilder.join_subgraph( 324 graph = NfaBuilder.join_subgraph(
323 graph, transition, rule_map[transition]) 325 graph, rule_map[transition])
324 graphs.append(graph) 326 graphs.append(graph)
325 graph = NfaBuilder.or_terms(graphs) 327 graph = NfaBuilder.or_terms(graphs)
326 rule_map[subgraph] = graph 328 rule_map[subgraph] = graph
327 # process first the subgraphs, then the default graph 329 # process first the subgraphs, then the default graph
328 for k, v in parser_state.rules.items(): 330 for k, v in parser_state.rules.items():
329 if k == 'default': continue 331 if k == 'default': continue
330 process(k, v) 332 process(k, v)
331 process('default', parser_state.rules['default']) 333 process('default', parser_state.rules['default'])
332 # build the automata 334 # build the automata
333 for rule_name, graph in rule_map.items(): 335 for rule_name, graph in rule_map.items():
334 self.__automata[rule_name] = RuleProcessor.Automata( 336 self.__automata[rule_name] = RuleProcessor.Automata(
335 parser_state.encoding, parser_state.character_classes, graph) 337 parser_state.encoding, parser_state.character_classes, graph)
336 self.__rule_trees[rule_name] = graph 338 self.__rule_trees[rule_name] = graph
337 # process default_action 339 # process default_action
338 default_action = parser_state.rules['default']['default_action'] 340 default_action = parser_state.rules['default']['default_action']
339 self.__default_action = Action(Term.empty_term(), default_action) 341 self.__default_action = Action(Term.empty_term(), default_action)
OLDNEW
« no previous file with comments | « tools/lexer_generator/nfa_builder.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698