| 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 state = self.__peek_state() | 139 state = self.__peek_state() |
| 140 if not state['start_node']: | 140 if not state['start_node']: |
| 141 state['start_node'] = self.__new_state() | 141 state['start_node'] = self.__new_state() |
| 142 self.__patch_ends(ends, state['start_node']) | 142 self.__patch_ends(ends, state['start_node']) |
| 143 return (start, []) | 143 return (start, []) |
| 144 | 144 |
| 145 def __catch_all(self, graph): | 145 def __catch_all(self, graph): |
| 146 return self.__key_state(TransitionKey.unique('catch_all')) | 146 return self.__key_state(TransitionKey.unique('catch_all')) |
| 147 | 147 |
| 148 def __join(self, graph): | 148 def __join(self, graph): |
| 149 (graph, name, subgraph, modifier) = graph[1:] | 149 (graph, name, subgraph) = graph[1:] |
| 150 subgraphs = self.__peek_state()['subgraphs'] | 150 subgraphs = self.__peek_state()['subgraphs'] |
| 151 if not name in subgraphs: | 151 if not name in subgraphs: |
| 152 subgraphs[name] = self.__nfa(subgraph) | 152 subgraphs[name] = self.__nfa(subgraph) |
| 153 (subgraph_start, subgraph_end, nodes_in_subgraph) = subgraphs[name] | 153 (subgraph_start, subgraph_end, nodes_in_subgraph) = subgraphs[name] |
| 154 (start, ends) = self.__process(graph) | 154 (start, ends) = self.__process(graph) |
| 155 if modifier: | |
| 156 assert modifier == 'ZERO_OR_MORE' | |
| 157 for end in ends: | |
| 158 end.add_epsilon_transition(subgraph_end) | |
| 159 self.__patch_ends(ends, subgraph_start) | 155 self.__patch_ends(ends, subgraph_start) |
| 160 end = self.__new_state() | 156 end = self.__new_state() |
| 161 subgraph_end.add_epsilon_transition(end) | 157 subgraph_end.add_epsilon_transition(end) |
| 162 return (start, [end]) | 158 return (start, [end]) |
| 163 | 159 |
| 164 def __process(self, graph): | 160 def __process(self, graph): |
| 165 assert type(graph) == TupleType | 161 assert type(graph) == TupleType |
| 166 method = "_NfaBuilder__" + graph[0].lower() | 162 method = "_NfaBuilder__" + graph[0].lower() |
| 167 if not method in self.__operation_map: | 163 if not method in self.__operation_map: |
| 168 matches = filter(lambda (name, func): name == method, self.__members) | 164 matches = filter(lambda (name, func): name == method, self.__members) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 | 203 |
| 208 @staticmethod | 204 @staticmethod |
| 209 def __compute_epsilon_closures(start_state): | 205 def __compute_epsilon_closures(start_state): |
| 210 def outer(node, state): | 206 def outer(node, state): |
| 211 def inner(node, closure): | 207 def inner(node, closure): |
| 212 closure.add(node) | 208 closure.add(node) |
| 213 return closure | 209 return closure |
| 214 is_epsilon = lambda k: k == TransitionKey.epsilon() | 210 is_epsilon = lambda k: k == TransitionKey.epsilon() |
| 215 state_iter = lambda node : node.state_iter(key_filter = is_epsilon) | 211 state_iter = lambda node : node.state_iter(key_filter = is_epsilon) |
| 216 edge = set(state_iter(node)) | 212 edge = set(state_iter(node)) |
| 217 closure = Automaton.visit_states(edge, inner, state_iter=state_iter, visit
_state=set()) | 213 closure = Automaton.visit_states( |
| 214 edge, inner, state_iter=state_iter, visit_state=set()) |
| 218 node.set_epsilon_closure(closure) | 215 node.set_epsilon_closure(closure) |
| 219 Automaton.visit_states(set([start_state]), outer) | 216 Automaton.visit_states(set([start_state]), outer) |
| 220 | 217 |
| 221 @staticmethod | 218 @staticmethod |
| 222 def __replace_catch_all(state): | 219 def __replace_catch_all(state): |
| 223 catch_all = TransitionKey.unique('catch_all') | 220 catch_all = TransitionKey.unique('catch_all') |
| 224 transitions = state.transitions() | 221 transitions = state.transitions() |
| 225 if not catch_all in transitions: | 222 if not catch_all in transitions: |
| 226 return | 223 return |
| 227 f = lambda acc, state: acc | set(state.epsilon_closure_iter()) | 224 f = lambda acc, state: acc | set(state.epsilon_closure_iter()) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 253 | 250 |
| 254 @staticmethod | 251 @staticmethod |
| 255 def catch_all(): | 252 def catch_all(): |
| 256 return ('CATCH_ALL',) | 253 return ('CATCH_ALL',) |
| 257 | 254 |
| 258 @staticmethod | 255 @staticmethod |
| 259 def epsilon(): | 256 def epsilon(): |
| 260 return ('EPSILON',) | 257 return ('EPSILON',) |
| 261 | 258 |
| 262 @staticmethod | 259 @staticmethod |
| 263 def join_subgraph(graph, name, subgraph, modifier): | 260 def join_subgraph(graph, name, subgraph): |
| 264 if modifier: | 261 return ('JOIN', graph, name, subgraph) |
| 265 modifier = NfaBuilder.__modifer_map[modifier] | |
| 266 return ('JOIN', graph, name, subgraph, modifier) | |
| 267 | 262 |
| 268 @staticmethod | 263 @staticmethod |
| 269 def or_graphs(graphs): | 264 def or_graphs(graphs): |
| 270 return reduce(lambda acc, g: ('OR', acc, g), graphs) | 265 return reduce(lambda acc, g: ('OR', acc, g), graphs) |
| 271 | 266 |
| 272 @staticmethod | 267 @staticmethod |
| 273 def cat_graphs(graphs): | 268 def cat_graphs(graphs): |
| 274 return reduce(lambda acc, g: ('CAT', acc, g), graphs) | 269 return reduce(lambda acc, g: ('CAT', acc, g), graphs) |
| 275 | 270 |
| 276 __modifer_map = { | 271 __modifer_map = { |
| 277 '+': 'ONE_OR_MORE', | 272 '+': 'ONE_OR_MORE', |
| 278 '?': 'ZERO_OR_ONE', | 273 '?': 'ZERO_OR_ONE', |
| 279 '*': 'ZERO_OR_MORE', | 274 '*': 'ZERO_OR_MORE', |
| 280 } | 275 } |
| 281 | 276 |
| 282 @staticmethod | 277 @staticmethod |
| 283 def apply_modifier(modifier, graph): | 278 def apply_modifier(modifier, graph): |
| 284 return (NfaBuilder.__modifer_map[modifier], graph) | 279 return (NfaBuilder.__modifer_map[modifier], graph) |
| OLD | NEW |