| Index: tools/lexer_generator/nfa.py
|
| diff --git a/tools/lexer_generator/nfa.py b/tools/lexer_generator/nfa.py
|
| index 8aa2b69088fd8f7c2a17092a888db16359b167f8..eb711b26c6318dfd953f99a9780a610a6f4e81a2 100644
|
| --- a/tools/lexer_generator/nfa.py
|
| +++ b/tools/lexer_generator/nfa.py
|
| @@ -37,61 +37,45 @@ class NfaState(AutomatonState):
|
| self.__epsilon_closure = None
|
| self.__action = Action.empty_action()
|
|
|
| - def epsilon_closure_iter(self):
|
| - return iter(self.__epsilon_closure)
|
| -
|
| - def set_epsilon_closure(self, closure):
|
| - assert self.is_closed()
|
| - assert self.__epsilon_closure == None
|
| - self.__epsilon_closure = frozenset(closure)
|
| -
|
| def action(self):
|
| - assert self.is_closed()
|
| + assert self.__is_closed()
|
| return self.__action
|
|
|
| def set_action(self, action):
|
| - assert not self.is_closed()
|
| + assert not self.__is_closed()
|
| assert not self.__action
|
| assert isinstance(action, Action)
|
| self.__action = action
|
|
|
| - def state_iter_for_key(self, key):
|
| - assert self.is_closed()
|
| - if not key in self.__transitions:
|
| - return iter([])
|
| - return iter(self.__transitions[key])
|
| -
|
| - def swap_key(self, old_key, new_key):
|
| - 'this is one of the few mutations allowed after closing'
|
| - assert self.is_closed()
|
| - assert not new_key == TransitionKey.epsilon(), "changes epsilon closure"
|
| - value = self.__transitions[old_key]
|
| - del self.__transitions[old_key]
|
| - self.__transitions[new_key] = value
|
| -
|
| def __add_transition(self, key, next_state):
|
| assert key != None
|
| if next_state == None:
|
| - assert not self.is_closed(), "already closed"
|
| + assert not self.__is_closed(), "already closed"
|
| self.__unclosed.add(key)
|
| return
|
| if not key in self.__transitions:
|
| self.__transitions[key] = set()
|
| + else:
|
| + assert key == TransitionKey.epsilon()
|
| self.__transitions[key].add(next_state)
|
|
|
| def add_unclosed_transition(self, key):
|
| assert key != TransitionKey.epsilon()
|
| self.__add_transition(key, None)
|
|
|
| - def add_epsilon_transition(self, state):
|
| + def add_transition(self, key, state):
|
| + assert not self.__is_closed(), "already closed"
|
| assert state != None
|
| - self.__add_transition(TransitionKey.epsilon(), state)
|
| + self.__add_transition(key, state)
|
| +
|
| + def add_epsilon_transition(self, state):
|
| + self.add_transition(TransitionKey.epsilon(), state)
|
|
|
| - def is_closed(self):
|
| + def __is_closed(self):
|
| return self.__unclosed == None
|
|
|
| def close(self, end):
|
| - assert not self.is_closed()
|
| + assert not self.__is_closed()
|
| unclosed, self.__unclosed = self.__unclosed, None
|
| if end == None:
|
| assert not unclosed
|
| @@ -101,19 +85,46 @@ class NfaState(AutomatonState):
|
| if not unclosed:
|
| self.__add_transition(TransitionKey.epsilon(), end)
|
|
|
| + def post_creation_verify(self):
|
| + assert self.__is_closed()
|
| + assert self.__epsilon_closure != None
|
| +
|
| + def state_iter_for_key(self, key):
|
| + assert self.__is_closed()
|
| + if not key in self.__transitions:
|
| + return iter([])
|
| + return iter(self.__transitions[key])
|
| +
|
| def key_state_iter(
|
| self,
|
| key_filter = lambda x: True,
|
| state_filter = lambda x: True,
|
| match_func = lambda x, y: True,
|
| yield_func = lambda x, y: (x, y)):
|
| - assert self.is_closed()
|
| + assert self.__is_closed()
|
| for key, states in self.__transitions.items():
|
| if key_filter(key):
|
| for state in states:
|
| if state_filter(state) and match_func(key, state):
|
| yield yield_func(key, state)
|
|
|
| + def epsilon_closure_iter(self):
|
| + return iter(self.__epsilon_closure)
|
| +
|
| + def set_epsilon_closure(self, closure):
|
| + assert self.__is_closed()
|
| + assert self.__epsilon_closure == None
|
| + self.__epsilon_closure = frozenset(closure)
|
| +
|
| + def swap_key(self, old_key, new_key):
|
| + 'this is one of the few mutations allowed after closing'
|
| + self.post_creation_verify()
|
| + assert not old_key == TransitionKey.epsilon(), "changes epsilon closure"
|
| + assert not new_key == TransitionKey.epsilon(), "changes epsilon closure"
|
| + value = self.__transitions[old_key]
|
| + del self.__transitions[old_key]
|
| + self.__transitions[new_key] = value
|
| +
|
| class Nfa(Automaton):
|
|
|
| def __init__(self, encoding, start, end, nodes_created):
|
| @@ -130,7 +141,7 @@ class Nfa(Automaton):
|
|
|
| def __verify(self, nodes_created):
|
| def f(node, count):
|
| - assert node.is_closed()
|
| + node.post_creation_verify()
|
| return count + 1
|
| count = self.visit_all_states(f, 0)
|
| assert count == nodes_created
|
| @@ -151,7 +162,7 @@ class Nfa(Automaton):
|
| dfa_nodes[name] = {
|
| 'transitions': {},
|
| 'terminal': self.__end in nfa_state_set,
|
| - 'action' : Action.dominant_action(nfa_state_set)}
|
| + 'action' : self.dominant_action(nfa_state_set)}
|
| # Gather the set of transition keys for which the dfa state will have
|
| # transitions (the disjoint set of all the transition keys from all the
|
| # states combined). For example, if a state in the state set has a
|
|
|