| OLD | NEW |
| 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 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 class NfaState(AutomatonState): | 31 class NfaState(AutomatonState): |
| 32 | 32 |
| 33 def __init__(self): | 33 def __init__(self): |
| 34 super(NfaState, self).__init__() | 34 super(NfaState, self).__init__() |
| 35 self.__transitions = {} | 35 self.__transitions = {} |
| 36 self.__unclosed = set() | 36 self.__unclosed = set() |
| 37 self.__epsilon_closure = None | 37 self.__epsilon_closure = None |
| 38 self.__action = Action.empty_action() | 38 self.__action = Action.empty_action() |
| 39 | 39 |
| 40 def transitions_to_multiple_states(self): | |
| 41 return True | |
| 42 | |
| 43 def epsilon_closure_iter(self): | 40 def epsilon_closure_iter(self): |
| 44 return iter(self.__epsilon_closure) | 41 return iter(self.__epsilon_closure) |
| 45 | 42 |
| 46 def set_epsilon_closure(self, closure): | 43 def set_epsilon_closure(self, closure): |
| 47 assert self.is_closed() | 44 assert self.is_closed() |
| 48 assert self.__epsilon_closure == None | 45 assert self.__epsilon_closure == None |
| 49 self.__epsilon_closure = frozenset(closure) | 46 self.__epsilon_closure = frozenset(closure) |
| 50 | 47 |
| 51 def action(self): | 48 def action(self): |
| 52 assert self.is_closed() | 49 assert self.is_closed() |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 assert not self.is_closed() | 84 assert not self.is_closed() |
| 88 unclosed, self.__unclosed = self.__unclosed, None | 85 unclosed, self.__unclosed = self.__unclosed, None |
| 89 if end == None: | 86 if end == None: |
| 90 assert not unclosed | 87 assert not unclosed |
| 91 return | 88 return |
| 92 for key in unclosed: | 89 for key in unclosed: |
| 93 self.__add_transition(key, end) | 90 self.__add_transition(key, end) |
| 94 if not unclosed: | 91 if not unclosed: |
| 95 self.__add_transition(TransitionKey.epsilon(), end) | 92 self.__add_transition(TransitionKey.epsilon(), end) |
| 96 | 93 |
| 97 def __matches(self, match_func, value): | 94 def key_state_iter( |
| 98 # f collects states whose corresponding TransitionKey matches 'value'. | 95 self, |
| 99 items = self.__transitions.items() | 96 key_filter = lambda x: True, |
| 100 iters = [iter(states) for (key, states) in items if match_func(key, value)] | 97 state_filter = lambda x: True, |
| 101 return chain(*iters) | 98 match_func = lambda x, y: True, |
| 102 | 99 yield_func = lambda x, y: (x, y)): |
| 103 def transition_state_iter_for_char(self, value): | 100 for key, states in self.__transitions.items(): |
| 104 return self.__matches(lambda k, v : k.matches_char(v), value) | 101 if key_filter(key): |
| 105 | 102 for state in states: |
| 106 def transition_state_iter_for_key(self, value): | 103 if state_filter(state) and match_func(key, state): |
| 107 return self.__matches(lambda k, v : k.is_superset_of_key(v), value) | 104 yield yield_func(key, state) |
| 108 | 105 |
| 109 class Nfa(Automaton): | 106 class Nfa(Automaton): |
| 110 | 107 |
| 111 def __init__(self, encoding, start, end, nodes_created): | 108 def __init__(self, encoding, start, end, nodes_created): |
| 112 super(Nfa, self).__init__(encoding) | 109 super(Nfa, self).__init__(encoding) |
| 113 self.__start = start | 110 self.__start = start |
| 114 self.__end = end | 111 self.__end = end |
| 115 self.__verify(nodes_created) | 112 self.__verify(nodes_created) |
| 116 | 113 |
| 117 def start_state(self): | 114 def start_state(self): |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 f = lambda state: state.transition_state_iter_for_key(key) | 154 f = lambda state: state.transition_state_iter_for_key(key) |
| 158 match_states = set(chain(*map(f, nfa_state_set))) | 155 match_states = set(chain(*map(f, nfa_state_set))) |
| 159 transition_state = self.__to_dfa(match_states, dfa_nodes, end_node) | 156 transition_state = self.__to_dfa(match_states, dfa_nodes, end_node) |
| 160 dfa_nodes[name]['transitions'][key] = transition_state | 157 dfa_nodes[name]['transitions'][key] = transition_state |
| 161 return name | 158 return name |
| 162 | 159 |
| 163 def compute_dfa(self): | 160 def compute_dfa(self): |
| 164 dfa_nodes = {} | 161 dfa_nodes = {} |
| 165 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end) | 162 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end) |
| 166 return (start_name, dfa_nodes) | 163 return (start_name, dfa_nodes) |
| OLD | NEW |