| 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 102 |
| 103 def visit_all_states(self, visitor, visit_state = None, state_iter = None): | 103 def visit_all_states(self, visitor, visit_state = None, state_iter = None): |
| 104 return self.visit_states(self.start_set(), visitor, visit_state, state_iter) | 104 return self.visit_states(self.start_set(), visitor, visit_state, state_iter) |
| 105 | 105 |
| 106 @staticmethod | 106 @staticmethod |
| 107 def epsilon_closure(states): | 107 def epsilon_closure(states): |
| 108 f = lambda state : state.epsilon_closure_iter() | 108 f = lambda state : state.epsilon_closure_iter() |
| 109 return set(chain(iter(states), *map(f, states))) | 109 return set(chain(iter(states), *map(f, states))) |
| 110 | 110 |
| 111 @staticmethod | 111 @staticmethod |
| 112 def __transition_states_for_char(valid_states, c): | 112 def __omega_closure(states): |
| 113 f = lambda state : state.transition_state_iter_for_char(c) | 113 f = lambda s : s.transition_state_iter_for_key(TransitionKey.omega()) |
| 114 return set(chain(*map(f, Automaton.epsilon_closure(valid_states)))) | 114 new_states = set(chain(*map(f, states))) |
| 115 return set(chain(iter(states), iter(Automaton.epsilon_closure(new_states)))) |
| 116 |
| 117 @staticmethod |
| 118 def __transition_states_for_char(states, c): |
| 119 f = lambda s : s.transition_state_iter_for_char(c) |
| 120 states = set(chain(*map(f, Automaton.epsilon_closure(states)))) |
| 121 return Automaton.__omega_closure(states) |
| 115 | 122 |
| 116 def matches(self, string): | 123 def matches(self, string): |
| 117 valid_states = self.start_set() | 124 valid_states = self.start_set() |
| 118 for c in string: | 125 for c in string: |
| 119 valid_states = Automaton.__transition_states_for_char(valid_states, c) | 126 valid_states = Automaton.__transition_states_for_char(valid_states, c) |
| 120 if not valid_states: | 127 if not valid_states: |
| 121 return False | 128 return False |
| 122 valid_states = self.epsilon_closure(valid_states) | 129 valid_states = self.__omega_closure(self.epsilon_closure(valid_states)) |
| 123 return len(self.terminal_set().intersection(valid_states)) > 0 | 130 return len(self.terminal_set().intersection(valid_states)) > 0 |
| 124 | 131 |
| 132 @staticmethod |
| 133 def dominant_action(states): |
| 134 return Action.dominant_action(map(lambda s: s.action(), states)) |
| 135 |
| 125 def lex(self, string, default_action): | 136 def lex(self, string, default_action): |
| 126 last_position = 0 | 137 last_position = 0 |
| 127 valid_states = self.start_set() | 138 valid_states = self.start_set() |
| 128 for pos, c in enumerate(string): | 139 for pos, c in enumerate(string): |
| 129 transitions = Automaton.__transition_states_for_char(valid_states, c) | 140 transitions = Automaton.__transition_states_for_char(valid_states, c) |
| 130 if transitions: | 141 if transitions: |
| 131 valid_states = transitions | 142 valid_states = transitions |
| 132 continue | 143 continue |
| 133 action = Action.dominant_action(valid_states) | 144 # TODO(dcarney): action collection should walk omega transitions |
| 145 action = self.dominant_action(valid_states) |
| 134 if not action: | 146 if not action: |
| 135 assert default_action | 147 assert default_action |
| 136 action = default_action | 148 action = default_action |
| 137 yield (action, last_position, pos) | 149 yield (action, last_position, pos) |
| 138 last_position = pos | 150 last_position = pos |
| 139 # lex next token | 151 # lex next token |
| 140 valid_states = Automaton.__transition_states_for_char(self.start_set(), c) | 152 valid_states = Automaton.__transition_states_for_char(self.start_set(), c) |
| 141 assert valid_states | 153 assert valid_states |
| 142 valid_states = self.epsilon_closure(valid_states) | 154 valid_states = self.__omega_closure(self.epsilon_closure(valid_states)) |
| 143 action = Action.dominant_action(valid_states) | 155 action = self.dominant_action(valid_states) |
| 144 if not action: | 156 if not action: |
| 145 assert default_action | 157 assert default_action |
| 146 action = default_action | 158 action = default_action |
| 147 yield (action, last_position, len(string)) | 159 yield (action, last_position, len(string)) |
| OLD | NEW |