| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 def actions(self): | 49 def actions(self): |
| 50 return self.__actions | 50 return self.__actions |
| 51 | 51 |
| 52 def add_transition(self, key, state): | 52 def add_transition(self, key, state): |
| 53 assert not self.__transitions.has_key(key) | 53 assert not self.__transitions.has_key(key) |
| 54 self.__transitions[key] = state | 54 self.__transitions[key] = state |
| 55 | 55 |
| 56 def transitions(self): | 56 def transitions(self): |
| 57 return self.__transitions | 57 return self.__transitions |
| 58 | 58 |
| 59 def to_code(self, start_node_number): | |
| 60 # FIXME: add different check types (if, switch, lookup table) | |
| 61 # FIXME: add action + break / continue | |
| 62 # FIXME: add default action | |
| 63 code = ''' | |
| 64 code_%s: | |
| 65 fprintf(stderr, "state %s\\n");''' % (self.node_number(), self.node_number()
) | |
| 66 | |
| 67 action = self.action() | |
| 68 if action: | |
| 69 if action[1] == 'terminate': | |
| 70 code += 'return 0;' | |
| 71 return code | |
| 72 elif action[1] == 'terminate_illegal': | |
| 73 code += 'return 1;' | |
| 74 return code | |
| 75 | |
| 76 code += ''' | |
| 77 yych = *(++cursor_); | |
| 78 fprintf(stderr, "char at hand is %c (%d)\\n", yych, yych);\n''' | |
| 79 | |
| 80 for key, state in self.__transitions.items(): | |
| 81 code += key.to_code() | |
| 82 code += ''' { | |
| 83 goto code_%s; | |
| 84 } | |
| 85 ''' % state.node_number() | |
| 86 | |
| 87 if action: | |
| 88 code += '%s\nyych = *(--cursor_);\ngoto code_%s;\n' % (self.action()[1], s
tart_node_number) | |
| 89 | |
| 90 return code | |
| 91 | |
| 92 class Dfa(Automaton): | 59 class Dfa(Automaton): |
| 93 | 60 |
| 94 def __init__(self, start_name, mapping): | 61 def __init__(self, start_name, mapping): |
| 95 super(Dfa, self).__init__() | 62 super(Dfa, self).__init__() |
| 96 self.__terminal_set = set() | 63 self.__terminal_set = set() |
| 97 self.__name_map = {} | 64 self.__name_map = {} |
| 98 for i, (name, node_data) in enumerate(mapping.items()): | 65 for i, (name, node_data) in enumerate(mapping.items()): |
| 99 node = DfaState(name, i, node_data['actions']) | 66 node = DfaState(name, i, node_data['actions']) |
| 100 self.__name_map[name] = node | 67 self.__name_map[name] = node |
| 101 if node_data['terminal']: | 68 if node_data['terminal']: |
| 102 self.__terminal_set.add(node) | 69 self.__terminal_set.add(node) |
| 103 for name, node_data in mapping.items(): | 70 for name, node_data in mapping.items(): |
| 104 node = self.__name_map[name] | 71 node = self.__name_map[name] |
| 105 inversion = {} | 72 inversion = {} |
| 106 for key, state in node_data['transitions'].items(): | 73 for key, state in node_data['transitions'].items(): |
| 107 if not state in inversion: | 74 if not state in inversion: |
| 108 inversion[state] = [] | 75 inversion[state] = [] |
| 109 inversion[state].append(key) | 76 inversion[state].append(key) |
| 110 for state, keys in inversion.items(): | 77 for state, keys in inversion.items(): |
| 111 merged_key = TransitionKey.merged_key(keys) | 78 merged_key = TransitionKey.merged_key(keys) |
| 112 node.add_transition(merged_key, self.__name_map[state]) | 79 node.add_transition(merged_key, self.__name_map[state]) |
| 113 self.__start = self.__name_map[start_name] | 80 self.__start = self.__name_map[start_name] |
| 114 assert self.__terminal_set | 81 assert self.__terminal_set |
| 115 | 82 |
| 83 def start_state(self): |
| 84 return self.__start |
| 85 |
| 116 def start_set(self): | 86 def start_set(self): |
| 117 return set([self.__start]) | 87 return set([self.__start]) |
| 118 | 88 |
| 119 def terminal_set(self): | 89 def terminal_set(self): |
| 120 return set(self.__terminal_set) | 90 return set(self.__terminal_set) |
| 121 | 91 |
| 92 def all_states_iter(self): |
| 93 return iter(self.__name_map.values()) |
| 94 |
| 122 @staticmethod | 95 @staticmethod |
| 123 def __match_char(state, char): | 96 def __match_char(state, char): |
| 124 match = list(state.state_iter(key_filter = lambda k: k.matches_char(char))) | 97 match = list(state.state_iter(key_filter = lambda k: k.matches_char(char))) |
| 125 if not match: return None | 98 if not match: return None |
| 126 assert len(match) == 1 | 99 assert len(match) == 1 |
| 127 return match[0] | 100 return match[0] |
| 128 | 101 |
| 129 def collect_actions(self, string): | 102 def collect_actions(self, string): |
| 130 state = self.__start | 103 state = self.__start |
| 131 for c in string: | 104 for c in string: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 155 last_position = pos | 128 last_position = pos |
| 156 # lex next token | 129 # lex next token |
| 157 next = Dfa.__match_char(self.__start, c) | 130 next = Dfa.__match_char(self.__start, c) |
| 158 assert next | 131 assert next |
| 159 state = next | 132 state = next |
| 160 assert state.action() # must invoke default action here | 133 assert state.action() # must invoke default action here |
| 161 yield (state.action()[1], last_position, len(string)) | 134 yield (state.action()[1], last_position, len(string)) |
| 162 | 135 |
| 163 def minimize(self): | 136 def minimize(self): |
| 164 pass | 137 pass |
| 165 | |
| 166 def to_code(self): | |
| 167 code = ''' | |
| 168 YYCTYPE yych = *cursor_; | |
| 169 goto code_%s; | |
| 170 ''' % (self.__start.node_number()) | |
| 171 for n in self.__name_map.values(): | |
| 172 code += n.to_code(self.__start.node_number()) | |
| 173 return code | |
| OLD | NEW |