| 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 13 matching lines...) Expand all Loading... |
| 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 automaton import * |
| 29 from nfa import Nfa | 29 from nfa import Nfa |
| 30 from transition_keys import TransitionKey | 30 from transition_keys import TransitionKey |
| 31 | 31 |
| 32 class DfaState(AutomatonState): | 32 class DfaState(AutomatonState): |
| 33 | 33 |
| 34 def __init__(self, name, node_number, actions): | 34 def __init__(self, name, action): |
| 35 super(DfaState, self).__init__(node_number) | 35 super(DfaState, self).__init__() |
| 36 self.__name = name | 36 self.__name = name |
| 37 self.__transitions = {} | 37 self.__transitions = {} |
| 38 self.__actions = actions | 38 self.__action = action |
| 39 | 39 |
| 40 def transitions_to_multiple_states(self): | 40 def transitions_to_multiple_states(self): |
| 41 return False | 41 return False |
| 42 | 42 |
| 43 def name(self): | 43 def name(self): |
| 44 return self.__name | 44 return self.__name |
| 45 | 45 |
| 46 def action(self): | 46 def action(self): |
| 47 return self.__actions[0] if self.__actions else None | 47 return self.__action |
| 48 | |
| 49 def actions(self): | |
| 50 return self.__actions | |
| 51 | 48 |
| 52 def add_transition(self, key, state): | 49 def add_transition(self, key, state): |
| 53 assert not self.__transitions.has_key(key) | 50 assert not self.__transitions.has_key(key) |
| 54 self.__transitions[key] = state | 51 self.__transitions[key] = state |
| 55 | 52 |
| 56 def transitions(self): | 53 def transitions(self): |
| 57 return self.__transitions | 54 return self.__transitions |
| 58 | 55 |
| 59 class Dfa(Automaton): | 56 class Dfa(Automaton): |
| 60 | 57 |
| 61 def __init__(self, start_name, mapping): | 58 def __init__(self, start_name, mapping): |
| 62 super(Dfa, self).__init__() | 59 super(Dfa, self).__init__() |
| 63 self.__terminal_set = set() | 60 self.__terminal_set = set() |
| 64 self.__name_map = {} | 61 self.__name_map = {} |
| 65 for i, (name, node_data) in enumerate(mapping.items()): | 62 for name, node_data in mapping.items(): |
| 66 node = DfaState(name, i, node_data['actions']) | 63 node = DfaState(name, node_data['action']) |
| 67 self.__name_map[name] = node | 64 self.__name_map[name] = node |
| 68 if node_data['terminal']: | 65 if node_data['terminal']: |
| 69 self.__terminal_set.add(node) | 66 self.__terminal_set.add(node) |
| 70 for name, node_data in mapping.items(): | 67 for name, node_data in mapping.items(): |
| 71 node = self.__name_map[name] | 68 node = self.__name_map[name] |
| 72 inversion = {} | 69 inversion = {} |
| 73 for key, state in node_data['transitions'].items(): | 70 for key, state in node_data['transitions'].items(): |
| 74 if not state in inversion: | 71 if not state in inversion: |
| 75 inversion[state] = [] | 72 inversion[state] = [] |
| 76 inversion[state].append(key) | 73 inversion[state].append(key) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 97 match = list(state.state_iter(key_filter = lambda k: k.matches_char(char))) | 94 match = list(state.state_iter(key_filter = lambda k: k.matches_char(char))) |
| 98 if not match: return None | 95 if not match: return None |
| 99 assert len(match) == 1 | 96 assert len(match) == 1 |
| 100 return match[0] | 97 return match[0] |
| 101 | 98 |
| 102 def collect_actions(self, string): | 99 def collect_actions(self, string): |
| 103 state = self.__start | 100 state = self.__start |
| 104 for c in string: | 101 for c in string: |
| 105 state = Dfa.__match_char(state, c) | 102 state = Dfa.__match_char(state, c) |
| 106 if not state: | 103 if not state: |
| 107 yield ('MISS',) | 104 yield Action('MISS') |
| 108 return | 105 return |
| 109 if state.action(): | 106 if state.action(): |
| 110 yield state.action() | 107 yield state.action() |
| 111 if state in self.__terminal_set: | 108 if state in self.__terminal_set: |
| 112 yield ('TERMINATE', ) | 109 yield Action('TERMINATE') |
| 113 else: | 110 else: |
| 114 yield ('MISS',) | 111 yield Action('MISS') |
| 115 | 112 |
| 116 def matches(self, string): | 113 def matches(self, string): |
| 117 actions = list(self.collect_actions(string)) | 114 actions = list(self.collect_actions(string)) |
| 118 return actions and actions[-1][0] == 'TERMINATE' | 115 return actions and actions[-1].type() == 'TERMINATE' |
| 119 | 116 |
| 120 def lex(self, string): | 117 def lex(self, string): |
| 121 state = self.__start | 118 state = self.__start |
| 122 last_position = 0 | 119 last_position = 0 |
| 123 for pos, c in enumerate(string): | 120 for pos, c in enumerate(string): |
| 124 next = Dfa.__match_char(state, c) | 121 next = Dfa.__match_char(state, c) |
| 125 if not next: | 122 if not next: |
| 126 assert state.action() # must invoke default action here | 123 assert state.action() # must invoke default action here |
| 127 yield (state.action()[1], last_position, pos) | 124 yield (state.action(), last_position, pos) |
| 128 last_position = pos | 125 last_position = pos |
| 129 # lex next token | 126 # lex next token |
| 130 next = Dfa.__match_char(self.__start, c) | 127 next = Dfa.__match_char(self.__start, c) |
| 131 assert next | 128 assert next |
| 132 state = next | 129 state = next |
| 133 assert state.action() # must invoke default action here | 130 assert state.action() # must invoke default action here |
| 134 yield (state.action()[1], last_position, len(string)) | 131 yield (state.action(), last_position, len(string)) |
| 135 | 132 |
| 136 def minimize(self): | 133 def minimize(self): |
| 137 pass | 134 pass |
| OLD | NEW |