| 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 return chain(*iters) | 99 return chain(*iters) |
| 100 | 100 |
| 101 def transition_state_iter_for_char(self, value): | 101 def transition_state_iter_for_char(self, value): |
| 102 return self.__matches(lambda k, v : k.matches_char(v), value) | 102 return self.__matches(lambda k, v : k.matches_char(v), value) |
| 103 | 103 |
| 104 def transition_state_iter_for_key(self, value): | 104 def transition_state_iter_for_key(self, value): |
| 105 return self.__matches(lambda k, v : k.is_superset_of_key(v), value) | 105 return self.__matches(lambda k, v : k.is_superset_of_key(v), value) |
| 106 | 106 |
| 107 class Nfa(Automaton): | 107 class Nfa(Automaton): |
| 108 | 108 |
| 109 def __init__(self, start, end, nodes_created): | 109 def __init__(self, encoding, start, end, nodes_created): |
| 110 super(Nfa, self).__init__() | 110 super(Nfa, self).__init__(encoding) |
| 111 self.__start = start | 111 self.__start = start |
| 112 self.__end = end | 112 self.__end = end |
| 113 self.__verify(nodes_created) | 113 self.__verify(nodes_created) |
| 114 | 114 |
| 115 def start_state(self): | 115 def start_state(self): |
| 116 return self.__start | 116 return self.__start |
| 117 | 117 |
| 118 def terminal_set(self): | 118 def terminal_set(self): |
| 119 return set([self.__end]) | 119 return set([self.__end]) |
| 120 | 120 |
| 121 def __verify(self, nodes_created): | 121 def __verify(self, nodes_created): |
| 122 def f(node, count): | 122 def f(node, count): |
| 123 assert node.is_closed() | 123 assert node.is_closed() |
| 124 return count + 1 | 124 return count + 1 |
| 125 count = self.visit_all_states(f, 0) | 125 count = self.visit_all_states(f, 0) |
| 126 assert count == nodes_created | 126 assert count == nodes_created |
| 127 | 127 |
| 128 @staticmethod | 128 @staticmethod |
| 129 def __gather_transition_keys(state_set): | 129 def __gather_transition_keys(encoding, state_set): |
| 130 keys = set(chain(*map(lambda state: state.key_iter(), state_set))) | 130 keys = set(chain(*map(lambda state: state.key_iter(), state_set))) |
| 131 keys.discard(TransitionKey.epsilon()) | 131 keys.discard(TransitionKey.epsilon()) |
| 132 return TransitionKey.disjoint_keys(keys) | 132 return TransitionKey.disjoint_keys(encoding, keys) |
| 133 | 133 |
| 134 @staticmethod | 134 def __to_dfa(self, nfa_state_set, dfa_nodes, end_node): |
| 135 def __to_dfa(nfa_state_set, dfa_nodes, end_node): | |
| 136 nfa_state_set = Automaton.epsilon_closure(nfa_state_set) | 135 nfa_state_set = Automaton.epsilon_closure(nfa_state_set) |
| 137 assert nfa_state_set | 136 assert nfa_state_set |
| 138 name = ".".join(str(x.node_number()) for x in sorted(nfa_state_set)) | 137 name = ".".join(str(x.node_number()) for x in sorted(nfa_state_set)) |
| 139 if name in dfa_nodes: | 138 if name in dfa_nodes: |
| 140 return name | 139 return name |
| 141 dfa_nodes[name] = { | 140 dfa_nodes[name] = { |
| 142 'transitions': {}, | 141 'transitions': {}, |
| 143 'terminal': end_node in nfa_state_set, | 142 'terminal': end_node in nfa_state_set, |
| 144 'action' : Action.dominant_action(nfa_state_set)} | 143 'action' : Action.dominant_action(nfa_state_set)} |
| 145 for key in Nfa.__gather_transition_keys(nfa_state_set): | 144 for key in Nfa.__gather_transition_keys(self.encoding(), nfa_state_set): |
| 146 match_states = set() | 145 match_states = set() |
| 147 f = lambda state: state.transition_state_iter_for_key(key) | 146 f = lambda state: state.transition_state_iter_for_key(key) |
| 148 match_states |= set(chain(*map(f, nfa_state_set))) | 147 match_states |= set(chain(*map(f, nfa_state_set))) |
| 149 transition_state = Nfa.__to_dfa(match_states, dfa_nodes, end_node) | 148 transition_state = self.__to_dfa(match_states, dfa_nodes, end_node) |
| 150 dfa_nodes[name]['transitions'][key] = transition_state | 149 dfa_nodes[name]['transitions'][key] = transition_state |
| 151 return name | 150 return name |
| 152 | 151 |
| 153 def compute_dfa(self): | 152 def compute_dfa(self): |
| 154 dfa_nodes = {} | 153 dfa_nodes = {} |
| 155 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end) | 154 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end) |
| 156 return (start_name, dfa_nodes) | 155 return (start_name, dfa_nodes) |
| OLD | NEW |