| 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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 value = self.__transitions[old_key] | 124 value = self.__transitions[old_key] |
| 125 del self.__transitions[old_key] | 125 del self.__transitions[old_key] |
| 126 self.__transitions[new_key] = value | 126 self.__transitions[new_key] = value |
| 127 | 127 |
| 128 class Nfa(Automaton): | 128 class Nfa(Automaton): |
| 129 | 129 |
| 130 def __init__(self, encoding, start, end, nodes_created): | 130 def __init__(self, encoding, start, end, nodes_created): |
| 131 super(Nfa, self).__init__(encoding) | 131 super(Nfa, self).__init__(encoding) |
| 132 self.__start = start | 132 self.__start = start |
| 133 self.__end = end | 133 self.__end = end |
| 134 self.__verify(nodes_created) | 134 self.__node_count = nodes_created |
| 135 self.__verify() |
| 136 |
| 137 def node_count(self): |
| 138 return self.__node_count |
| 135 | 139 |
| 136 def start_state(self): | 140 def start_state(self): |
| 137 return self.__start | 141 return self.__start |
| 138 | 142 |
| 139 def terminal_set(self): | 143 def terminal_set(self): |
| 140 return set([self.__end]) | 144 return set([self.__end]) |
| 141 | 145 |
| 142 def __verify(self, nodes_created): | 146 def __verify(self): |
| 143 def f(node, count): | 147 def f(node, count): |
| 144 node.post_creation_verify() | 148 node.post_creation_verify() |
| 145 return count + 1 | 149 return count + 1 |
| 146 count = self.visit_all_states(f, 0) | 150 count = self.visit_all_states(f, 0) |
| 147 assert count == nodes_created | 151 assert count == self.__node_count |
| 148 | 152 |
| 149 @staticmethod | 153 @staticmethod |
| 150 def __gather_transition_keys(encoding, state_set): | 154 def __gather_transition_keys(encoding, state_set): |
| 151 keys = set(chain(*map(lambda state: state.key_iter(), state_set))) | 155 keys = set(chain(*map(lambda state: state.key_iter(), state_set))) |
| 152 keys.discard(TransitionKey.epsilon()) | 156 keys.discard(TransitionKey.epsilon()) |
| 153 return TransitionKey.disjoint_keys(encoding, keys) | 157 return TransitionKey.disjoint_keys(encoding, keys) |
| 154 | 158 |
| 155 def __to_dfa(self, nfa_state_set, dfa_nodes): | 159 def __to_dfa(self, nfa_state_set, dfa_nodes): |
| 156 nfa_state_set = Automaton.epsilon_closure(nfa_state_set) | 160 nfa_state_set = Automaton.epsilon_closure(nfa_state_set) |
| 157 # nfa_state_set will be a state in the dfa. | 161 # nfa_state_set will be a state in the dfa. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 176 f = lambda state: state.transition_state_iter_for_key(key) | 180 f = lambda state: state.transition_state_iter_for_key(key) |
| 177 match_states = set(chain(*map(f, nfa_state_set))) | 181 match_states = set(chain(*map(f, nfa_state_set))) |
| 178 transition_state = self.__to_dfa(match_states, dfa_nodes) | 182 transition_state = self.__to_dfa(match_states, dfa_nodes) |
| 179 dfa_nodes[name]['transitions'][key] = transition_state | 183 dfa_nodes[name]['transitions'][key] = transition_state |
| 180 return name | 184 return name |
| 181 | 185 |
| 182 def compute_dfa(self): | 186 def compute_dfa(self): |
| 183 dfa_nodes = {} | 187 dfa_nodes = {} |
| 184 start_name = self.__to_dfa(set([self.__start]), dfa_nodes) | 188 start_name = self.__to_dfa(set([self.__start]), dfa_nodes) |
| 185 return (start_name, dfa_nodes) | 189 return (start_name, dfa_nodes) |
| OLD | NEW |