| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 48 |
| 49 def add_transition(self, key, state): | 49 def add_transition(self, key, state): |
| 50 assert not self.__transitions.has_key(key) | 50 assert not self.__transitions.has_key(key) |
| 51 self.__transitions[key] = state | 51 self.__transitions[key] = state |
| 52 | 52 |
| 53 def transitions(self): | 53 def transitions(self): |
| 54 return self.__transitions | 54 return self.__transitions |
| 55 | 55 |
| 56 # TODO abstract state matching | 56 # TODO abstract state matching |
| 57 def __matches(self, match_func, value): | 57 def __matches(self, match_func, value): |
| 58 f = lambda acc, (k, vs): acc | set([vs]) if match_func(k, value) else acc | 58 # f collects states whose corresponding TransitionKey matches 'value'. |
| 59 f = (lambda acc, (key, state): |
| 60 acc | set([state]) if match_func(key, value) else acc) |
| 59 matches = reduce(f, self.__transitions.items(), set()) | 61 matches = reduce(f, self.__transitions.items(), set()) |
| 62 # Since this is a dfa, we should have at most one such state. Return it. |
| 60 if not matches: | 63 if not matches: |
| 61 return None | 64 return None |
| 62 assert len(matches) == 1 | 65 assert len(matches) == 1 |
| 63 return iter(matches).next() | 66 return iter(matches).next() |
| 64 | 67 |
| 65 def char_matches(self, value): | 68 def next_state_with_char(self, value): |
| 66 return self.__matches(lambda k, v : k.matches_char(v), value) | 69 return self.__matches(lambda k, v : k.matches_char(v), value) |
| 67 | 70 |
| 68 def key_matches(self, value): | 71 def key_matches(self, value): |
| 69 return self.__matches(lambda k, v : k.is_superset_of_key(v), value) | 72 return self.__matches(lambda k, v : k.is_superset_of_key(v), value) |
| 70 | 73 |
| 71 class Dfa(Automaton): | 74 class Dfa(Automaton): |
| 72 | 75 |
| 73 def __init__(self, start_name, mapping): | 76 def __init__(self, start_name, mapping): |
| 74 super(Dfa, self).__init__() | 77 super(Dfa, self).__init__() |
| 75 self.__terminal_set = set() | 78 self.__terminal_set = set() |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 elif len(intersection) <= len(difference): | 364 elif len(intersection) <= len(difference): |
| 362 working_set.add(intersection) | 365 working_set.add(intersection) |
| 363 else: | 366 else: |
| 364 working_set.add(difference) | 367 working_set.add(difference) |
| 365 if old_partitions: | 368 if old_partitions: |
| 366 partitions -= old_partitions | 369 partitions -= old_partitions |
| 367 if new_partitions: | 370 if new_partitions: |
| 368 partitions |= new_partitions | 371 partitions |= new_partitions |
| 369 (start_name, mapping) = self.__merge_partitions(partitions) | 372 (start_name, mapping) = self.__merge_partitions(partitions) |
| 370 return Dfa(start_name, mapping) | 373 return Dfa(start_name, mapping) |
| OLD | NEW |