| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are |
| 4 # met: |
| 5 # |
| 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided |
| 11 # with the distribution. |
| 12 # * Neither the name of Google Inc. nor the names of its |
| 13 # contributors may be used to endorse or promote products derived |
| 14 # from this software without specific prior written permission. |
| 15 # |
| 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 |
| 28 class TransitionKey: |
| 29 |
| 30 __single_char_type = 0 |
| 31 __epsilon_type = 1 |
| 32 __any_type = 2 |
| 33 __class_type = 3 |
| 34 |
| 35 @staticmethod |
| 36 def __create(key_type, value): |
| 37 key = TransitionKey() |
| 38 key.__type = key_type |
| 39 key.__value = value |
| 40 return key |
| 41 |
| 42 @staticmethod |
| 43 def epsilon(): |
| 44 return TransitionKey.__create(TransitionKey.__epsilon_type, None) |
| 45 |
| 46 @staticmethod |
| 47 def any(): |
| 48 return TransitionKey.__create(TransitionKey.__any_type, None) |
| 49 |
| 50 @staticmethod |
| 51 def single_char(char): |
| 52 return TransitionKey.__create(TransitionKey.__single_char_type, char) |
| 53 |
| 54 @staticmethod |
| 55 def character_class(invert, graph): |
| 56 # TODO |
| 57 return TransitionKey.__create(TransitionKey.__class_type, (invert, graph)) |
| 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 |
| 70 def matches_char(self, char): |
| 71 return TransitionKey.__char_matchers[self.__type](self.__value, char) |
| 72 |
| 73 def matches_key(self, key): |
| 74 assert key != TransitionKey.epsilon() |
| 75 assert self != TransitionKey.epsilon() |
| 76 if (key == self): return True |
| 77 # TODO need to test intersection/symmetric diff |
| 78 assert self != TransitionKey.any() |
| 79 return False |
| 80 |
| 81 def __hash__(self): |
| 82 if self.__value == None: |
| 83 return hash(self.__type) |
| 84 return hash(self.__type) ^ hash(self.__value) |
| 85 |
| 86 def __eq__(self, other): |
| 87 return ( |
| 88 isinstance(other, self.__class__) and |
| 89 self.__type == other.__type and |
| 90 self.__value == other.__value) |
| 91 |
| 92 def __str__(self): |
| 93 if self.__type == self.__single_char_type: |
| 94 return "'%s'" % self.__value |
| 95 if self.__type == self.__epsilon_type: |
| 96 return "epsilon" |
| 97 if self.__type == self.__any_type: |
| 98 return "any" |
| 99 # TODO |
| 100 return "class" |
| 101 |
| 102 @staticmethod |
| 103 def merge_key_set(key_set): |
| 104 key_set -= set([TransitionKey.epsilon()]) |
| 105 # TODO need intersections and symmetric differences |
| 106 return key_set |
| OLD | NEW |