| 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 unclosed, self.__unclosed = self.__unclosed, None | 91 unclosed, self.__unclosed = self.__unclosed, None |
| 92 if end == None: | 92 if end == None: |
| 93 assert not unclosed | 93 assert not unclosed |
| 94 return | 94 return |
| 95 for key in unclosed: | 95 for key in unclosed: |
| 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 = lambda acc, (k, vs): acc | vs if match_func(k, value) else acc | 101 # f collects states whose corresponding TransitionKey matches 'value'. |
| 102 f = (lambda acc, (key, states): |
| 103 acc | states if match_func(key, value) else acc) |
| 102 return reduce(f, self.__transitions.items(), set()) | 104 return reduce(f, self.__transitions.items(), set()) |
| 103 | 105 |
| 104 def char_matches(self, value): | 106 def next_states_with_char(self, value): |
| 105 return self.__matches(lambda k, v : k.matches_char(v), value) | 107 return self.__matches(lambda k, v : k.matches_char(v), value) |
| 106 | 108 |
| 107 def key_matches(self, value): | 109 def key_matches(self, value): |
| 108 return self.__matches(lambda k, v : k.is_superset_of_key(v), value) | 110 return self.__matches(lambda k, v : k.is_superset_of_key(v), value) |
| 109 | 111 |
| 110 class Nfa(Automaton): | 112 class Nfa(Automaton): |
| 111 | 113 |
| 112 def __init__(self, start, end, nodes_created): | 114 def __init__(self, start, end, nodes_created): |
| 113 super(Nfa, self).__init__() | 115 super(Nfa, self).__init__() |
| 114 self.__start = start | 116 self.__start = start |
| (...skipping 14 matching lines...) Expand all Loading... |
| 129 assert count == nodes_created | 131 assert count == nodes_created |
| 130 | 132 |
| 131 @staticmethod | 133 @staticmethod |
| 132 def __close(states): | 134 def __close(states): |
| 133 f = lambda acc, node: acc | node.epsilon_closure() | 135 f = lambda acc, node: acc | node.epsilon_closure() |
| 134 return reduce(f, states, set(states)) | 136 return reduce(f, states, set(states)) |
| 135 | 137 |
| 136 def matches(self, string): | 138 def matches(self, string): |
| 137 valid_states = Nfa.__close(set([self.__start])) | 139 valid_states = Nfa.__close(set([self.__start])) |
| 138 for c in string: | 140 for c in string: |
| 139 f = lambda acc, state: acc | state.char_matches(c) | 141 f = lambda acc, state: acc | state.next_states_with_char(c) |
| 140 transitions = reduce(f, valid_states, set()) | 142 transitions = reduce(f, valid_states, set()) |
| 141 if not transitions: | 143 if not transitions: |
| 142 return False | 144 return False |
| 143 valid_states = Nfa.__close(transitions) | 145 valid_states = Nfa.__close(transitions) |
| 144 return self.__end in valid_states | 146 return self.__end in valid_states |
| 145 | 147 |
| 146 @staticmethod | 148 @staticmethod |
| 147 def __gather_transition_keys(state_set): | 149 def __gather_transition_keys(state_set): |
| 148 keys = set(chain(*map(lambda state: state.key_iter(), state_set))) | 150 keys = set(chain(*map(lambda state: state.key_iter(), state_set))) |
| 149 keys.discard(TransitionKey.epsilon()) | 151 keys.discard(TransitionKey.epsilon()) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 for state in reduce(f, nfa_state_set, set()): | 183 for state in reduce(f, nfa_state_set, set()): |
| 182 match_states.add(state) | 184 match_states.add(state) |
| 183 transition_state = Nfa.__to_dfa(match_states, dfa_nodes, end_node) | 185 transition_state = Nfa.__to_dfa(match_states, dfa_nodes, end_node) |
| 184 dfa_nodes[name]['transitions'][key] = transition_state | 186 dfa_nodes[name]['transitions'][key] = transition_state |
| 185 return name | 187 return name |
| 186 | 188 |
| 187 def compute_dfa(self): | 189 def compute_dfa(self): |
| 188 dfa_nodes = {} | 190 dfa_nodes = {} |
| 189 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end) | 191 start_name = self.__to_dfa(set([self.__start]), dfa_nodes, self.__end) |
| 190 return (start_name, dfa_nodes) | 192 return (start_name, dfa_nodes) |
| OLD | NEW |