| 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 |
| 11 # with the distribution. | 11 # with the distribution. |
| 12 # * Neither the name of Google Inc. nor the names of its | 12 # * Neither the name of Google Inc. nor the names of its |
| 13 # contributors may be used to endorse or promote products derived | 13 # contributors may be used to endorse or promote products derived |
| 14 # from this software without specific prior written permission. | 14 # from this software without specific prior written permission. |
| 15 # | 15 # |
| 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 from automaton import * |
| 28 from nfa import Nfa | 29 from nfa import Nfa |
| 29 from transition_keys import TransitionKey | 30 from transition_keys import TransitionKey |
| 30 | 31 |
| 31 class DfaState: | 32 class DfaState(AutomatonState): |
| 32 | 33 |
| 33 def __init__(self, name, node_number): | 34 def __init__(self, name, node_number): |
| 35 super(DfaState, self).__init__(node_number) |
| 34 self.__name = name | 36 self.__name = name |
| 35 self.__node_number = node_number | |
| 36 self.__transitions = {} | 37 self.__transitions = {} |
| 37 | 38 |
| 38 def name(self): | 39 def name(self): |
| 39 return self.__name | 40 return self.__name |
| 40 | 41 |
| 41 def node_number(self): | |
| 42 return self.__node_number | |
| 43 | |
| 44 def add_transition(self, key, action, state): | 42 def add_transition(self, key, action, state): |
| 45 assert not self.__transitions.has_key(key) | 43 assert not self.__transitions.has_key(key) |
| 46 self.__transitions[key] = (state, action) | 44 self.__transitions[key] = (state, action) |
| 47 | 45 |
| 48 def transitions(self): | 46 def transitions(self): |
| 49 return self.__transitions | 47 return self.__transitions |
| 50 | 48 |
| 51 class Dfa: | 49 class Dfa(Automaton): |
| 52 | 50 |
| 53 def __init__(self, start_name, mapping): | 51 def __init__(self, start_name, mapping): |
| 52 super(Dfa, self).__init__() |
| 54 self.__terminal_set = set() | 53 self.__terminal_set = set() |
| 55 name_map = {} | 54 name_map = {} |
| 56 action_map = {} | 55 action_map = {} |
| 57 for i, name in enumerate(mapping.keys()): | 56 for i, name in enumerate(mapping.keys()): |
| 58 name_map[name] = DfaState(name, i) | 57 name_map[name] = DfaState(name, i) |
| 59 for name, node_data in mapping.items(): | 58 for name, node_data in mapping.items(): |
| 60 node = name_map[name] | 59 node = name_map[name] |
| 61 if node_data['terminal']: | 60 if node_data['terminal']: |
| 62 self.__terminal_set.add(node) | 61 self.__terminal_set.add(node) |
| 63 inversion = {} | 62 inversion = {} |
| 64 for key, (state, action) in node_data['transitions'].items(): | 63 for key, (state, action) in node_data['transitions'].items(): |
| 65 if not state in inversion: | 64 if not state in inversion: |
| 66 inversion[state] = {} | 65 inversion[state] = {} |
| 67 # TODO fix this | 66 # TODO fix this |
| 68 action_key = str(action) | 67 action_key = str(action) |
| 69 if not action_key in action_map: | 68 if not action_key in action_map: |
| 70 action_map[action_key] = action | 69 action_map[action_key] = action |
| 71 if not action_key in inversion[state]: | 70 if not action_key in inversion[state]: |
| 72 inversion[state][action_key] = [] | 71 inversion[state][action_key] = [] |
| 73 inversion[state][action_key].append(key) | 72 inversion[state][action_key].append(key) |
| 74 for state, values in inversion.items(): | 73 for state, values in inversion.items(): |
| 75 for action_key, keys in values.items(): | 74 for action_key, keys in values.items(): |
| 76 merged_key = TransitionKey.merged_key(keys) | 75 merged_key = TransitionKey.merged_key(keys) |
| 77 action = action_map[action_key] | 76 action = action_map[action_key] |
| 78 node.add_transition(merged_key, action, name_map[state]) | 77 node.add_transition(merged_key, action, name_map[state]) |
| 79 self.__start = name_map[start_name] | 78 self.__start = name_map[start_name] |
| 80 assert self.__terminal_set | 79 assert self.__terminal_set |
| 81 | 80 |
| 82 @staticmethod | |
| 83 def __visit_edges(start, visitor, state): | |
| 84 edge = set([start]) | |
| 85 visited = set() | |
| 86 first = lambda v: [x[0] for x in v] | |
| 87 while edge: | |
| 88 f = lambda (next_edge, state), node: ( | |
| 89 next_edge | set(first(node.transitions().values())), | |
| 90 visitor(node, state)) | |
| 91 (next_edge, state) = reduce(f, edge, (set(), state)) | |
| 92 visited |= edge | |
| 93 edge = next_edge - visited | |
| 94 return state | |
| 95 | |
| 96 def collect_actions(self, string): | 81 def collect_actions(self, string): |
| 97 state = self.__start | 82 state = self.__start |
| 98 for c in string: | 83 for c in string: |
| 99 next = [s for k, s in state.transitions().items() if k.matches_char(c)] | 84 next = [s for k, s in state.transitions().items() if k.matches_char(c)] |
| 100 if not next: | 85 if not next: |
| 101 yield ('MISS',) | 86 yield ('MISS',) |
| 102 return | 87 return |
| 103 assert len(next) == 1 | 88 assert len(next) == 1 |
| 104 (state, action) = next[0] | 89 (state, action) = next[0] |
| 105 if action: | 90 if action: |
| 106 yield action | 91 yield action |
| 107 if state in self.__terminal_set: | 92 if state in self.__terminal_set: |
| 108 yield ('TERMINATE', ) | 93 yield ('TERMINATE', ) |
| 109 else: | 94 else: |
| 110 yield ('MISS',) | 95 yield ('MISS',) |
| 111 | 96 |
| 112 def matches(self, string): | 97 def matches(self, string): |
| 113 actions = list(self.collect_actions(string)) | 98 actions = list(self.collect_actions(string)) |
| 114 return actions and actions[-1][0] == 'TERMINATE' | 99 return actions and actions[-1][0] == 'TERMINATE' |
| 115 | 100 |
| 101 def __visit_all_edges(self, visitor, state): |
| 102 edge = set([self.__start]) |
| 103 first = lambda v: set([x[0] for x in v]) |
| 104 next_edge = lambda node: first(node.transitions().values()) |
| 105 return self.visit_edges(edge, next_edge, visitor, state) |
| 106 |
| 116 def to_dot(self): | 107 def to_dot(self): |
| 117 | 108 iterator = lambda visitor, state: self.__visit_all_edges(visitor, state) |
| 118 def f(node, node_content): | 109 return self.generate_dot(self.__start, self.__terminal_set, iterator) |
| 119 for key, (state, action) in node.transitions().items(): | |
| 120 key = str(key).replace('\\', '\\\\') | |
| 121 if action: | |
| 122 node_content.append( | |
| 123 " S_%s -> S_%s [ label = \"%s {%s} -> %s\" ];" % | |
| 124 (node.node_number(), state.node_number(), key, action[1], | |
| 125 action[2])) | |
| 126 else: | |
| 127 node_content.append( | |
| 128 " S_%s -> S_%s [ label = \"%s\" ];" % | |
| 129 (node.node_number(), state.node_number(), key)) | |
| 130 return node_content | |
| 131 | |
| 132 node_content = self.__visit_edges(self.__start, f, []) | |
| 133 terminals = ["S_%d;" % x.node_number() for x in self.__terminal_set] | |
| 134 start_number = self.__start.node_number() | |
| 135 start_shape = "circle" | |
| 136 if self.__start in self.__terminal_set: | |
| 137 start_shape = "doublecircle" | |
| 138 | |
| 139 return ''' | |
| 140 digraph finite_state_machine { | |
| 141 rankdir=LR; | |
| 142 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s | |
| 143 node [shape = doublecircle, style=unfilled]; %s | |
| 144 node [shape = circle]; | |
| 145 %s | |
| 146 } | |
| 147 ''' % (start_shape, | |
| 148 start_number, | |
| 149 " ".join(terminals), | |
| 150 "\n".join(node_content)) | |
| OLD | NEW |