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

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

Issue 148293022: Experimental parser: make nfa nodes immutable (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: 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.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 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 state_iter = lambda node : node.state_iter(key_filter = is_epsilon) 241 state_iter = lambda node : node.state_iter(key_filter = is_epsilon)
242 edge = set(state_iter(node)) 242 edge = set(state_iter(node))
243 closure = Automaton.visit_states( 243 closure = Automaton.visit_states(
244 edge, inner, state_iter=state_iter, visit_state=set()) 244 edge, inner, state_iter=state_iter, visit_state=set())
245 node.set_epsilon_closure(closure) 245 node.set_epsilon_closure(closure)
246 Automaton.visit_states(set([start_state]), outer) 246 Automaton.visit_states(set([start_state]), outer)
247 247
248 @staticmethod 248 @staticmethod
249 def __replace_catch_all(encoding, state): 249 def __replace_catch_all(encoding, state):
250 catch_all = TransitionKey.unique('catch_all') 250 catch_all = TransitionKey.unique('catch_all')
251 transitions = state.transitions() 251 states = list(state.state_iter_for_key(catch_all))
252 if not catch_all in transitions: 252 if not states:
253 return 253 return
254 f = lambda acc, state: acc | set(state.epsilon_closure_iter()) 254 f = lambda acc, state: acc | set(state.epsilon_closure_iter())
255 reachable_states = reduce(f, transitions[catch_all], set()) 255 reachable_states = reduce(f, states, set())
256 f = lambda acc, state: acc | set(state.transitions().keys()) 256 f = lambda acc, state: acc | set(state.key_iter())
257 keys = reduce(f, reachable_states, set()) 257 keys = reduce(f, reachable_states, set())
258 keys.discard(TransitionKey.epsilon()) 258 keys.discard(TransitionKey.epsilon())
259 keys.discard(catch_all) 259 keys.discard(catch_all)
260 keys.discard(TransitionKey.unique('eos')) 260 keys.discard(TransitionKey.unique('eos'))
261 inverse_key = TransitionKey.inverse_key(encoding, keys) 261 inverse_key = TransitionKey.inverse_key(encoding, keys)
262 if inverse_key: 262 if not inverse_key:
263 transitions[inverse_key] = transitions[catch_all] 263 inverse_key = TransitionKey.unique('no_match')
264 del transitions[catch_all] 264 state.swap_key(catch_all, inverse_key)
265 265
266 @staticmethod 266 @staticmethod
267 def nfa(encoding, character_classes, subtree_map, name): 267 def nfa(encoding, character_classes, subtree_map, name):
268 self = NfaBuilder(encoding, character_classes, subtree_map) 268 self = NfaBuilder(encoding, character_classes, subtree_map)
269 (start, end, nodes_created) = self.__nfa(name) 269 (start, end, nodes_created) = self.__nfa(name)
270 # close all ends 270 # close all ends
271 global_end_to_return = self.__global_end_node or end 271 global_end_to_return = self.__global_end_node or end
272 assert global_end_to_return, "no terminal nodes, default tree continues" 272 assert global_end_to_return, "no terminal nodes, default tree continues"
273 if end and self.__global_end_node: 273 if end and self.__global_end_node:
274 end.close(self.__global_end_node) 274 end.close(self.__global_end_node)
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 terms = list(NfaBuilder.__flatten_terms(terms, 'OR')) 341 terms = list(NfaBuilder.__flatten_terms(terms, 'OR'))
342 assert terms 342 assert terms
343 return terms[0] if len(terms) == 1 else Term('OR', *terms) 343 return terms[0] if len(terms) == 1 else Term('OR', *terms)
344 344
345 @staticmethod 345 @staticmethod
346 def cat_terms(terms): 346 def cat_terms(terms):
347 terms = NfaBuilder.__flatten_terms(terms, 'CAT') 347 terms = NfaBuilder.__flatten_terms(terms, 'CAT')
348 terms = list(NfaBuilder.__flatten_literals(terms)) 348 terms = list(NfaBuilder.__flatten_literals(terms))
349 assert terms 349 assert terms
350 return terms[0] if len(terms) == 1 else Term('CAT', *terms) 350 return terms[0] if len(terms) == 1 else Term('CAT', *terms)
OLDNEW
« no previous file with comments | « tools/lexer_generator/nfa.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698