| 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 |
| 11 # with the distribution. | 11 # with the distribution. |
| 12 # * Neither the name of Google Inc. nor the names of its | 12 # * Neither the name of Google Inc. nor the names of its |
| 13 # contributors may be used to endorse or promote products derived | 13 # contributors may be used to endorse or promote products derived |
| 14 # from this software without specific prior written permission. | 14 # from this software without specific prior written permission. |
| 15 # | 15 # |
| 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 |
| 28 class TransitionKey: | 29 class TransitionKey: |
| 29 | 30 |
| 30 __single_char_type = 0 | 31 __lower_bound = 0 |
| 31 __epsilon_type = 1 | 32 __latin_1_upper_bound = 255 |
| 32 __any_type = 2 | 33 __unicode_whitespace_bounds = (256, 256) |
| 33 __class_type = 3 | 34 __unicode_literal_bounds = (257, 257) |
| 35 __upper_bound = 257 |
| 34 | 36 |
| 35 @staticmethod | 37 @staticmethod |
| 36 def __create(key_type, value): | 38 def __create(ranges): |
| 39 # TODO - verify ranges |
| 37 key = TransitionKey() | 40 key = TransitionKey() |
| 38 key.__type = key_type | 41 key.__ranges = ranges |
| 39 key.__value = value | 42 key.__cached_hash = None |
| 40 return key | 43 return key |
| 41 | 44 |
| 45 __cached_epsilon = None |
| 42 @staticmethod | 46 @staticmethod |
| 43 def epsilon(): | 47 def epsilon(): |
| 44 return TransitionKey.__create(TransitionKey.__epsilon_type, None) | 48 if (TransitionKey.__cached_epsilon == None): |
| 49 TransitionKey.__cached_epsilon = TransitionKey.__create([]) |
| 50 return TransitionKey.__cached_epsilon |
| 45 | 51 |
| 52 __cached_any = None |
| 46 @staticmethod | 53 @staticmethod |
| 47 def any(): | 54 def any(): |
| 48 return TransitionKey.__create(TransitionKey.__any_type, None) | 55 if (TransitionKey.__cached_any == None): |
| 56 bounds = [(TransitionKey.__lower_bound, TransitionKey.__upper_bound)] |
| 57 TransitionKey.__cached_any = TransitionKey.__create(bounds) |
| 58 return TransitionKey.__cached_any |
| 49 | 59 |
| 50 @staticmethod | 60 @staticmethod |
| 51 def single_char(char): | 61 def single_char(char): |
| 52 return TransitionKey.__create(TransitionKey.__single_char_type, char) | 62 char = ord(char) |
| 63 assert (TransitionKey.__lower_bound <= char and |
| 64 char <= TransitionKey.__latin_1_upper_bound) |
| 65 return TransitionKey.__create([(char, char)]) |
| 53 | 66 |
| 54 @staticmethod | 67 @staticmethod |
| 55 def character_class(invert, graph): | 68 def character_class(invert, graph): |
| 56 # TODO | 69 # TODO |
| 57 return TransitionKey.__create(TransitionKey.__class_type, (invert, graph)) | 70 return TransitionKey.__create([(129, 129)]) |
| 58 | |
| 59 @staticmethod | |
| 60 def __class_match(class_graph, char): | |
| 61 assert False | |
| 62 | |
| 63 __char_matchers = { | |
| 64 __single_char_type: (lambda x, y : x == y), | |
| 65 __epsilon_type: (lambda x, y : False), | |
| 66 __any_type: (lambda x, y : True), | |
| 67 __class_type: __class_match, | |
| 68 } | |
| 69 | 71 |
| 70 def matches_char(self, char): | 72 def matches_char(self, char): |
| 71 return TransitionKey.__char_matchers[self.__type](self.__value, char) | 73 for r in self.__ranges: |
| 74 if r[0] <= char and char <= r[1]: return True |
| 75 return False |
| 72 | 76 |
| 73 def matches_key(self, key): | 77 def matches_key(self, key): |
| 78 assert isinstance(key, self.__class__) |
| 74 assert key != TransitionKey.epsilon() | 79 assert key != TransitionKey.epsilon() |
| 75 assert self != TransitionKey.epsilon() | 80 if self == key: return True |
| 76 if (key == self): return True | 81 if self == TransitionKey.epsilon(): return False |
| 77 # TODO need to test intersection/symmetric diff | 82 # TODO |
| 78 assert self != TransitionKey.any() | |
| 79 return False | 83 return False |
| 80 | 84 |
| 81 def __hash__(self): | 85 def __hash__(self): |
| 82 if self.__value == None: | 86 if self.__cached_hash == None: |
| 83 return hash(self.__type) | 87 initial_hash = hash((-1, TransitionKey.__upper_bound + 1)) |
| 84 return hash(self.__type) ^ hash(self.__value) | 88 f = lambda acc, r: acc ^ hash(r) |
| 89 self.__cached_hash = reduce(f, self.__ranges, initial_hash) |
| 90 return self.__cached_hash |
| 85 | 91 |
| 86 def __eq__(self, other): | 92 def __eq__(self, other): |
| 87 return ( | 93 return isinstance(other, self.__class__) and self.__ranges == other.__ranges |
| 88 isinstance(other, self.__class__) and | 94 |
| 89 self.__type == other.__type and | 95 @staticmethod |
| 90 self.__value == other.__value) | 96 def __print_range(r): |
| 97 if r[0] == r[1]: |
| 98 return "'%s'" % chr(r[0]) |
| 99 else: |
| 100 return "['%s'-'%s']" % (chr(r[0]), chr(r[1])) |
| 91 | 101 |
| 92 def __str__(self): | 102 def __str__(self): |
| 93 if self.__type == self.__single_char_type: | 103 if self == self.epsilon(): |
| 94 return "'%s'" % self.__value | |
| 95 if self.__type == self.__epsilon_type: | |
| 96 return "epsilon" | 104 return "epsilon" |
| 97 if self.__type == self.__any_type: | 105 if self == self.any(): |
| 98 return "any" | 106 return "any" |
| 99 # TODO | 107 return ", ".join(TransitionKey.__print_range(x) for x in self.__ranges) |
| 100 return "class" | |
| 101 | 108 |
| 102 @staticmethod | 109 @staticmethod |
| 103 def merge_key_set(key_set): | 110 def merge_key_set(key_set): |
| 104 key_set -= set([TransitionKey.epsilon()]) | 111 key_set -= set([TransitionKey.epsilon()]) |
| 105 # TODO need intersections and symmetric differences | 112 # TODO need intersections and symmetric differences |
| 106 return key_set | 113 return key_set |
| OLD | NEW |