| 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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 self.__add_transition(key, end) | 96 self.__add_transition(key, end) |
| 97 if not unclosed: | 97 if not unclosed: |
| 98 self.__add_transition(TransitionKey.epsilon(), end) | 98 self.__add_transition(TransitionKey.epsilon(), end) |
| 99 | 99 |
| 100 def __matches(self, match_func, value): | 100 def __matches(self, match_func, value): |
| 101 # f collects states whose corresponding TransitionKey matches 'value'. | 101 # f collects states whose corresponding TransitionKey matches 'value'. |
| 102 f = (lambda acc, (key, states): | 102 f = (lambda acc, (key, states): |
| 103 acc | states if match_func(key, value) else acc) | 103 acc | states if match_func(key, value) else acc) |
| 104 return reduce(f, self.__transitions.items(), set()) | 104 return reduce(f, self.__transitions.items(), set()) |
| 105 | 105 |
| 106 def next_states_with_char(self, value): | 106 def transition_state_iter_for_char(self, value): |
| 107 return self.__matches(lambda k, v : k.matches_char(v), value) | 107 return iter(self.__matches(lambda k, v : k.matches_char(v), value)) |
| 108 | 108 |
| 109 def key_matches(self, value): | 109 def transition_state_iter_for_key(self, value): |
| 110 return self.__matches(lambda k, v : k.is_superset_of_key(v), value) | 110 return iter(self.__matches(lambda k, v : k.is_superset_of_key(v), value)) |
| 111 | 111 |
| 112 class Nfa(Automaton): | 112 class Nfa(Automaton): |
| 113 | 113 |
| 114 def __init__(self, start, end, nodes_created): | 114 def __init__(self, start, end, nodes_created): |
| 115 super(Nfa, self).__init__() | 115 super(Nfa, self).__init__() |
| 116 self.__start = start | 116 self.__start = start |
| 117 self.__end = end | 117 self.__end = end |
| 118 self.__verify(nodes_created) | 118 self.__verify(nodes_created) |
| 119 | 119 |
| 120 def start_set(self): | 120 def start_state(self): |
| 121 return set([self.__start]) | 121 return self.__start |
| 122 | 122 |
| 123 def terminal_set(self): | 123 def terminal_set(self): |
| 124 return set([self.__end]) | 124 return set([self.__end]) |
| 125 | 125 |
| 126 def __verify(self, nodes_created): | 126 def __verify(self, nodes_created): |
| 127 def f(node, count): | 127 def f(node, count): |
| 128 assert node.is_closed() | 128 assert node.is_closed() |
| 129 return count + 1 | 129 return count + 1 |
| 130 count = self.visit_all_states(f, 0) | 130 count = self.visit_all_states(f, 0) |
| 131 assert count == nodes_created | 131 assert count == nodes_created |
| 132 | 132 |
| 133 @staticmethod | 133 @staticmethod |
| 134 def __close(states): | |
| 135 f = lambda acc, node: acc | node.epsilon_closure() | |
| 136 return reduce(f, states, set(states)) | |
| 137 | |
| 138 def matches(self, string): | |
| 139 valid_states = Nfa.__close(set([self.__start])) | |
| 140 for c in string: | |
| 141 f = lambda acc, state: acc | state.next_states_with_char(c) | |
| 142 transitions = reduce(f, valid_states, set()) | |
| 143 if not transitions: | |
| 144 return False | |
| 145 valid_states = Nfa.__close(transitions) | |
| 146 return self.__end in valid_states | |
| 147 | |
| 148 @staticmethod | |
| 149 def __gather_transition_keys(state_set): | 134 def __gather_transition_keys(state_set): |
| 150 keys = set(chain(*map(lambda state: state.key_iter(), state_set))) | 135 keys = set(chain(*map(lambda state: state.key_iter(), state_set))) |
| 151 keys.discard(TransitionKey.epsilon()) | 136 keys.discard(TransitionKey.epsilon()) |
| 152 return TransitionKey.disjoint_keys(keys) | 137 return TransitionKey.disjoint_keys(keys) |
| 153 | 138 |
| 154 @staticmethod | 139 @staticmethod |
| 155 def __gather_actions(states): | |
| 156 action = None | |
| 157 for state in states: | |
| 158 if not state.action(): | |
| 159 continue | |
| 160 if not action: | |
| 161 action = state.action() | |
| 162 continue | |
| 163 if state.action().precedence() == action.precedence(): | |
| 164 assert state.action() == action | |
| 165 elif state.action().precedence() < action.precedence(): | |
| 166 action = state.action() | |
| 167 return action | |
| 168 | |
| 169 @staticmethod | |
| 170 def __to_dfa(nfa_state_set, dfa_nodes, end_node): | 140 def __to_dfa(nfa_state_set, dfa_nodes, end_node): |
| 171 nfa_state_set = Nfa.__close(nfa_state_set) | 141 nfa_state_set = Automaton.epsilon_closure(nfa_state_set) |
| 172 assert nfa_state_set | 142 assert nfa_state_set |
| 173 name = ".".join(str(x.node_number()) for x in sorted(nfa_state_set)) | 143 name = ".".join(str(x.node_number()) for x in sorted(nfa_state_set)) |
| 174 if name in dfa_nodes: | 144 if name in dfa_nodes: |
| 175 return name | 145 return name |
| 176 dfa_nodes[name] = { | 146 dfa_nodes[name] = { |
| 177 'transitions': {}, | 147 'transitions': {}, |
| 178 'terminal': end_node in nfa_state_set, | 148 'terminal': end_node in nfa_state_set, |
| 179 'action' : Nfa.__gather_actions(nfa_state_set)} | 149 'action' : Action.dominant_action(nfa_state_set)} |
| 180 for key in Nfa.__gather_transition_keys(nfa_state_set): | 150 for key in Nfa.__gather_transition_keys(nfa_state_set): |
| 181 match_states = set() | 151 match_states = set() |
| 182 f = lambda acc, state: acc | state.key_matches(key) | 152 f = lambda state: state.transition_state_iter_for_key(key) |
| 183 for state in reduce(f, nfa_state_set, set()): | 153 match_states |= set(chain(*map(f, nfa_state_set))) |
| 184 match_states.add(state) | |
| 185 transition_state = Nfa.__to_dfa(match_states, dfa_nodes, end_node) | 154 transition_state = Nfa.__to_dfa(match_states, dfa_nodes, end_node) |
| 186 dfa_nodes[name]['transitions'][key] = transition_state | 155 dfa_nodes[name]['transitions'][key] = transition_state |
| 187 return name | 156 return name |
| 188 | 157 |
| 189 def compute_dfa(self): | 158 def compute_dfa(self): |
| 190 dfa_nodes = {} | 159 dfa_nodes = {} |
| 191 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end) | 160 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end) |
| 192 return (start_name, dfa_nodes) | 161 return (start_name, dfa_nodes) |
| OLD | NEW |