| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 collects states whose corresponding TransitionKey matches 'value'. | 101 # f collects states whose corresponding TransitionKey matches 'value'. |
| 102 f = (lambda acc, (key, states): | 102 items = self.__transitions.items() |
| 103 acc | states if match_func(key, value) else acc) | 103 iters = [iter(states) for (key, states) in items if match_func(key, value)] |
| 104 return reduce(f, self.__transitions.items(), set()) | 104 return chain(*iters) |
| 105 | 105 |
| 106 def transition_state_iter_for_char(self, value): | 106 def transition_state_iter_for_char(self, value): |
| 107 return iter(self.__matches(lambda k, v : k.matches_char(v), value)) | 107 return self.__matches(lambda k, v : k.matches_char(v), value) |
| 108 | 108 |
| 109 def transition_state_iter_for_key(self, value): | 109 def transition_state_iter_for_key(self, value): |
| 110 return iter(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) |
| 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_state(self): | 120 def start_state(self): |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 f = lambda state: state.transition_state_iter_for_key(key) | 152 f = lambda state: state.transition_state_iter_for_key(key) |
| 153 match_states |= set(chain(*map(f, nfa_state_set))) | 153 match_states |= set(chain(*map(f, nfa_state_set))) |
| 154 transition_state = Nfa.__to_dfa(match_states, dfa_nodes, end_node) | 154 transition_state = Nfa.__to_dfa(match_states, dfa_nodes, end_node) |
| 155 dfa_nodes[name]['transitions'][key] = transition_state | 155 dfa_nodes[name]['transitions'][key] = transition_state |
| 156 return name | 156 return name |
| 157 | 157 |
| 158 def compute_dfa(self): | 158 def compute_dfa(self): |
| 159 dfa_nodes = {} | 159 dfa_nodes = {} |
| 160 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) |
| 161 return (start_name, dfa_nodes) | 161 return (start_name, dfa_nodes) |
| OLD | NEW |