| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 def __init__(self, encoding, start_name, mapping): | 82 def __init__(self, encoding, start_name, mapping): |
| 83 super(Dfa, self).__init__(encoding) | 83 super(Dfa, self).__init__(encoding) |
| 84 self.__terminal_set = set() | 84 self.__terminal_set = set() |
| 85 name_map = {} | 85 name_map = {} |
| 86 for name, node_data in mapping.items(): | 86 for name, node_data in mapping.items(): |
| 87 transitions = {} | 87 transitions = {} |
| 88 node = DfaState(name, node_data['action'], transitions) | 88 node = DfaState(name, node_data['action'], transitions) |
| 89 name_map[name] = (node, transitions) | 89 name_map[name] = (node, transitions) |
| 90 if node_data['terminal']: | 90 if node_data['terminal']: |
| 91 self.__terminal_set.add(node) | 91 self.__terminal_set.add(node) |
| 92 all_keys = [] |
| 92 for name, node_data in mapping.items(): | 93 for name, node_data in mapping.items(): |
| 93 (node, transitions) = name_map[name] | 94 (node, transitions) = name_map[name] |
| 94 inversion = {} | 95 inversion = {} |
| 95 for key, state in node_data['transitions'].items(): | 96 for key, state in node_data['transitions'].items(): |
| 96 if not state in inversion: | 97 if not state in inversion: |
| 97 inversion[state] = [] | 98 inversion[state] = [] |
| 98 inversion[state].append(key) | 99 inversion[state].append(key) |
| 99 for state, keys in inversion.items(): | 100 for state, keys in inversion.items(): |
| 101 all_keys += keys |
| 100 merged_key = TransitionKey.merged_key(encoding, keys) | 102 merged_key = TransitionKey.merged_key(encoding, keys) |
| 101 self.__add_transition(transitions, merged_key, name_map[state][0]) | 103 self.__add_transition(transitions, merged_key, name_map[state][0]) |
| 102 self.__start = name_map[start_name][0] | 104 self.__start = name_map[start_name][0] |
| 103 self.__node_count = len(mapping) | 105 self.__node_count = len(mapping) |
| 106 self.__disjoint_keys = sorted( |
| 107 TransitionKey.disjoint_keys(encoding, all_keys)) |
| 104 self.__verify() | 108 self.__verify() |
| 105 | 109 |
| 106 def __verify(self): | 110 def __verify(self): |
| 107 assert self.__terminal_set | 111 assert self.__terminal_set |
| 108 state_count = self.visit_all_states(lambda state, count: count + 1, 0) | 112 state_count = self.visit_all_states(lambda state, count: count + 1, 0) |
| 109 assert self.__node_count == state_count | 113 assert self.__node_count == state_count |
| 114 # assert keys are disjoint |
| 115 def f(state, remaining): |
| 116 remaining = set(TransitionKey.disjoint_keys(self.encoding(), |
| 117 state.key_iter())) |
| 118 for key in state.key_iter(): |
| 119 to_drop = set(filter(lambda x : key.is_superset_of_key(x), remaining)) |
| 120 assert to_drop |
| 121 remaining -= to_drop |
| 122 assert not remaining |
| 123 self.visit_all_states(f, set(self.disjoint_keys_iter())) |
| 124 |
| 125 def disjoint_keys_iter(self): |
| 126 return iter(self.__disjoint_keys) |
| 110 | 127 |
| 111 def node_count(self): | 128 def node_count(self): |
| 112 return self.__node_count | 129 return self.__node_count |
| 113 | 130 |
| 114 def start_state(self): | 131 def start_state(self): |
| 115 return self.__start | 132 return self.__start |
| 116 | 133 |
| 117 def terminal_set(self): | 134 def terminal_set(self): |
| 118 return set(self.__terminal_set) | 135 return set(self.__terminal_set) |
| OLD | NEW |