| 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 yield action | 91 yield action |
| 92 if state in self.__terminal_set: | 92 if state in self.__terminal_set: |
| 93 yield ('TERMINATE', ) | 93 yield ('TERMINATE', ) |
| 94 else: | 94 else: |
| 95 yield ('MISS',) | 95 yield ('MISS',) |
| 96 | 96 |
| 97 def matches(self, string): | 97 def matches(self, string): |
| 98 actions = list(self.collect_actions(string)) | 98 actions = list(self.collect_actions(string)) |
| 99 return actions and actions[-1][0] == 'TERMINATE' | 99 return actions and actions[-1][0] == 'TERMINATE' |
| 100 | 100 |
| 101 def lex(self, string): |
| 102 state = self.__start |
| 103 stored_action = None |
| 104 pos = 0 |
| 105 while pos < len(string): |
| 106 c = string[pos] |
| 107 next = [s for k, s in state.transitions().items() if k.matches_char(c)] |
| 108 if not next: |
| 109 # Maybe we have a stored action before. Take it and backtrack to the |
| 110 # position where that action was. |
| 111 if stored_action: |
| 112 yield stored_action |
| 113 return |
| 114 # FIXME: Otherwise, use the default rule if this happens at the start |
| 115 # state of the automaton. |
| 116 |
| 117 assert len(next) == 1 |
| 118 (state, action) = next[0] |
| 119 |
| 120 # Special actions: terminate |
| 121 if action and action[2] == 'terminate': |
| 122 if stored_action: |
| 123 yield stored_action |
| 124 yield (action, pos) |
| 125 return |
| 126 |
| 127 # Normally we don't know yet whether to take the action - it depends on |
| 128 # what comes next. |
| 129 if action: |
| 130 stored_action = (action, pos) |
| 131 |
| 132 pos += 1 |
| 133 |
| 101 def __visit_all_edges(self, visitor, state): | 134 def __visit_all_edges(self, visitor, state): |
| 102 edge = set([self.__start]) | 135 edge = set([self.__start]) |
| 103 first = lambda v: set([x[0] for x in v]) | 136 first = lambda v: set([x[0] for x in v]) |
| 104 next_edge = lambda node: first(node.transitions().values()) | 137 next_edge = lambda node: first(node.transitions().values()) |
| 105 return self.visit_edges(edge, next_edge, visitor, state) | 138 return self.visit_edges(edge, next_edge, visitor, state) |
| 106 | 139 |
| 107 def to_dot(self): | 140 def to_dot(self): |
| 108 iterator = lambda visitor, state: self.__visit_all_edges(visitor, state) | 141 iterator = lambda visitor, state: self.__visit_all_edges(visitor, state) |
| 109 return self.generate_dot(self.__start, self.__terminal_set, iterator) | 142 return self.generate_dot(self.__start, self.__terminal_set, iterator) |
| OLD | NEW |