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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 def terminal_set(self): | 110 def terminal_set(self): |
111 return set(self.__terminal_set) | 111 return set(self.__terminal_set) |
112 | 112 |
113 @staticmethod | 113 @staticmethod |
114 def __match_char(state, char): | 114 def __match_char(state, char): |
115 match = list(state.state_iter(key_filter = lambda k: k.matches_char(char))) | 115 match = list(state.state_iter(key_filter = lambda k: k.matches_char(char))) |
116 if not match: return None | 116 if not match: return None |
117 assert len(match) == 1 | 117 assert len(match) == 1 |
118 return match[0] | 118 return match[0] |
119 | 119 |
| 120 @staticmethod |
| 121 def terminal_action(): |
| 122 return Action(None, ('TERMINATE', None)) |
| 123 |
| 124 @staticmethod |
| 125 def miss_action(): |
| 126 return Action(None, ('Miss', None)) |
| 127 |
120 def collect_actions(self, string): | 128 def collect_actions(self, string): |
121 state = self.__start | 129 state = self.__start |
122 for c in string: | 130 for c in string: |
123 state = Dfa.__match_char(state, c) | 131 state = Dfa.__match_char(state, c) |
124 if not state: | 132 if not state: |
125 yield Action('MISS') | 133 yield self.miss_action() |
126 return | 134 return |
127 if state.action(): | 135 if state.action(): |
128 yield state.action() | 136 yield state.action() |
129 if state in self.__terminal_set: | 137 if state in self.__terminal_set: |
130 yield Action('TERMINATE') | 138 yield self.terminal_action() |
131 else: | 139 else: |
132 yield Action('MISS') | 140 yield self.miss_action() |
133 | 141 |
134 def matches(self, string): | 142 def matches(self, string): |
135 actions = list(self.collect_actions(string)) | 143 actions = list(self.collect_actions(string)) |
136 return actions and actions[-1].entry_action() == 'TERMINATE' | 144 return actions and actions[-1] == self.terminal_action() |
137 | 145 |
138 def lex(self, string): | 146 def lex(self, string): |
139 state = self.__start | 147 state = self.__start |
140 last_position = 0 | 148 last_position = 0 |
141 for pos, c in enumerate(string): | 149 for pos, c in enumerate(string): |
142 next = Dfa.__match_char(state, c) | 150 next = Dfa.__match_char(state, c) |
143 if not next: | 151 if not next: |
144 assert state.action() # must invoke default action here | 152 assert state.action() # must invoke default action here |
145 yield (state.action(), last_position, pos) | 153 yield (state.action(), last_position, pos) |
146 last_position = pos | 154 last_position = pos |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 elif len(intersection) <= len(difference): | 355 elif len(intersection) <= len(difference): |
348 working_set.add(intersection) | 356 working_set.add(intersection) |
349 else: | 357 else: |
350 working_set.add(difference) | 358 working_set.add(difference) |
351 if old_partitions: | 359 if old_partitions: |
352 partitions -= old_partitions | 360 partitions -= old_partitions |
353 if new_partitions: | 361 if new_partitions: |
354 partitions |= new_partitions | 362 partitions |= new_partitions |
355 (start_name, mapping) = self.__merge_partitions(partitions) | 363 (start_name, mapping) = self.__merge_partitions(partitions) |
356 return Dfa(start_name, mapping) | 364 return Dfa(start_name, mapping) |
OLD | NEW |