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

Side by Side Diff: tools/lexer_generator/nfa.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 | « no previous file | tools/lexer_generator/nfa_builder.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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 def action(self): 48 def action(self):
49 assert self.is_closed() 49 assert self.is_closed()
50 return self.__action 50 return self.__action
51 51
52 def set_action(self, action): 52 def set_action(self, action):
53 assert not self.is_closed() 53 assert not self.is_closed()
54 assert not self.__action 54 assert not self.__action
55 assert isinstance(action, Action) 55 assert isinstance(action, Action)
56 self.__action = action 56 self.__action = action
57 57
58 def transitions(self): 58 def state_iter_for_key(self, key):
59 assert self.is_closed() 59 assert self.is_closed()
60 return self.__transitions 60 if not key in self.__transitions:
61 return iter([])
62 return iter(self.__transitions[key])
63
64 def swap_key(self, old_key, new_key):
65 'this is one of the few mutations allowed after closing'
66 assert self.is_closed()
67 assert not new_key == TransitionKey.epsilon(), "changes epsilon closure"
68 value = self.__transitions[old_key]
69 del self.__transitions[old_key]
70 self.__transitions[new_key] = value
61 71
62 def __add_transition(self, key, next_state): 72 def __add_transition(self, key, next_state):
63 assert key != None 73 assert key != None
64 if next_state == None: 74 if next_state == None:
65 assert not self.is_closed(), "already closed" 75 assert not self.is_closed(), "already closed"
66 self.__unclosed.add(key) 76 self.__unclosed.add(key)
67 return 77 return
68 if not key in self.__transitions: 78 if not key in self.__transitions:
69 self.__transitions[key] = set() 79 self.__transitions[key] = set()
70 self.__transitions[key].add(next_state) 80 self.__transitions[key].add(next_state)
(...skipping 19 matching lines...) Expand all
90 self.__add_transition(key, end) 100 self.__add_transition(key, end)
91 if not unclosed: 101 if not unclosed:
92 self.__add_transition(TransitionKey.epsilon(), end) 102 self.__add_transition(TransitionKey.epsilon(), end)
93 103
94 def key_state_iter( 104 def key_state_iter(
95 self, 105 self,
96 key_filter = lambda x: True, 106 key_filter = lambda x: True,
97 state_filter = lambda x: True, 107 state_filter = lambda x: True,
98 match_func = lambda x, y: True, 108 match_func = lambda x, y: True,
99 yield_func = lambda x, y: (x, y)): 109 yield_func = lambda x, y: (x, y)):
110 assert self.is_closed()
100 for key, states in self.__transitions.items(): 111 for key, states in self.__transitions.items():
101 if key_filter(key): 112 if key_filter(key):
102 for state in states: 113 for state in states:
103 if state_filter(state) and match_func(key, state): 114 if state_filter(state) and match_func(key, state):
104 yield yield_func(key, state) 115 yield yield_func(key, state)
105 116
106 class Nfa(Automaton): 117 class Nfa(Automaton):
107 118
108 def __init__(self, encoding, start, end, nodes_created): 119 def __init__(self, encoding, start, end, nodes_created):
109 super(Nfa, self).__init__(encoding) 120 super(Nfa, self).__init__(encoding)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 f = lambda state: state.transition_state_iter_for_key(key) 165 f = lambda state: state.transition_state_iter_for_key(key)
155 match_states = set(chain(*map(f, nfa_state_set))) 166 match_states = set(chain(*map(f, nfa_state_set)))
156 transition_state = self.__to_dfa(match_states, dfa_nodes) 167 transition_state = self.__to_dfa(match_states, dfa_nodes)
157 dfa_nodes[name]['transitions'][key] = transition_state 168 dfa_nodes[name]['transitions'][key] = transition_state
158 return name 169 return name
159 170
160 def compute_dfa(self): 171 def compute_dfa(self):
161 dfa_nodes = {} 172 dfa_nodes = {}
162 start_name = self.__to_dfa(set([self.__start]), dfa_nodes) 173 start_name = self.__to_dfa(set([self.__start]), dfa_nodes)
163 return (start_name, dfa_nodes) 174 return (start_name, dfa_nodes)
OLDNEW
« no previous file with comments | « no previous file | tools/lexer_generator/nfa_builder.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698