| 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 from types import TupleType, ListType | 28 from types import TupleType, ListType, StringType |
| 29 from itertools import chain | 29 from itertools import chain |
| 30 from transition_keys import TransitionKey | 30 from transition_keys import TransitionKey |
| 31 | 31 |
| 32 class Term(object): |
| 33 '''A class representing a function and its arguments. |
| 34 f(a,b,c) would be represented as ('f', a, b, c) where |
| 35 a, b, and c are strings or Terms.''' |
| 36 |
| 37 @staticmethod |
| 38 def __verify_string(v): |
| 39 assert (not ',' in v) and (not '(' in v) |
| 40 |
| 41 def __init__(self, name, *args): |
| 42 assert type(name) == StringType |
| 43 self.__verify_string(name) |
| 44 for v in args: |
| 45 if type(v) == StringType: |
| 46 self.__verify_string(v) |
| 47 else: |
| 48 assert isinstance(v, self.__class__) |
| 49 self.__tuple = tuple([name] + list(args)) |
| 50 self.__str = None |
| 51 |
| 52 def name(self): |
| 53 return self.__tuple[0] |
| 54 |
| 55 def args(self): |
| 56 return self.__tuple[1:] |
| 57 |
| 58 def __hash__(self): |
| 59 return hash(self.__tuple) |
| 60 |
| 61 def __eq__(self, other): |
| 62 return (isinstance(other, self.__class__) and |
| 63 self.__tuple == other.__tuple) |
| 64 |
| 65 def __str__(self): |
| 66 if self.__str == None: |
| 67 self.__str = '(%s)' % ','.join(map(str, self.__tuple)) |
| 68 return self.__str |
| 69 |
| 32 class Action(object): | 70 class Action(object): |
| 33 | 71 |
| 34 @staticmethod | 72 @staticmethod |
| 35 def dominant_action(state_set): | 73 def dominant_action(state_set): |
| 36 action = None | 74 action = None |
| 37 for state in state_set: | 75 for state in state_set: |
| 38 if not state.action(): | 76 if not state.action(): |
| 39 continue | 77 continue |
| 40 if not action: | 78 if not action: |
| 41 action = state.action() | 79 action = state.action() |
| 42 continue | 80 continue |
| 43 if state.action().precedence() == action.precedence(): | 81 if state.action().precedence() == action.precedence(): |
| 44 assert state.action() == action | 82 assert state.action() == action |
| 45 elif state.action().precedence() < action.precedence(): | 83 elif state.action().precedence() < action.precedence(): |
| 46 action = state.action() | 84 action = state.action() |
| 47 return action | 85 return action |
| 48 | 86 |
| 49 def __init__(self, entry_action, match_action, precedence = -1): | 87 def __init__(self, entry_action, match_action, precedence = -1): |
| 50 for action in [entry_action, match_action]: | 88 for action in [entry_action, match_action]: |
| 51 if action == None: | 89 if action == None: |
| 52 continue | 90 continue |
| 53 assert type(action) == TupleType and len(action) | 91 assert isinstance(action, Term) |
| 54 assert action[0] != None | |
| 55 assert entry_action or match_action | 92 assert entry_action or match_action |
| 56 self.__entry_action = entry_action | 93 self.__entry_action = entry_action |
| 57 self.__match_action = match_action | 94 self.__match_action = match_action |
| 58 self.__precedence = precedence | 95 self.__precedence = precedence |
| 59 | 96 |
| 60 def entry_action(self): | 97 def entry_action(self): |
| 61 return self.__entry_action | 98 return self.__entry_action |
| 62 | 99 |
| 63 def match_action(self): | 100 def match_action(self): |
| 64 return self.__match_action | 101 return self.__match_action |
| 65 | 102 |
| 66 def precedence(self): | 103 def precedence(self): |
| 67 return self.__precedence | 104 return self.__precedence |
| 68 | 105 |
| 69 def __hash__(self): | 106 def __hash__(self): |
| 70 return hash((self.__entry_action, self.__match_action)) | 107 return hash((self.__entry_action, self.__match_action)) |
| 71 | 108 |
| 72 def __eq__(self, other): | 109 def __eq__(self, other): |
| 73 return (isinstance(other, self.__class__) and | 110 return (isinstance(other, self.__class__) and |
| 74 self.__entry_action == other.__entry_action and | 111 self.__entry_action == other.__entry_action and |
| 75 self.__match_action == other.__match_action) | 112 self.__match_action == other.__match_action) |
| 76 | 113 |
| 77 def __str__(self): | 114 def __str__(self): |
| 78 parts = [] | 115 parts = [] |
| 79 for action in [self.__entry_action, self.__match_action]: | 116 for action in [self.__entry_action, self.__match_action]: |
| 80 part = "" | 117 part = "" |
| 81 if action: | 118 if action: |
| 82 part += action[0] | 119 part += str(action) |
| 83 if action[1]: | |
| 84 part += "(%s)" % str(action[1]) | |
| 85 parts.append(part) | 120 parts.append(part) |
| 86 return "action< %s >" % " | ".join(parts) | 121 return "action< %s >" % " | ".join(parts) |
| 87 | 122 |
| 88 class AutomatonState(object): | 123 class AutomatonState(object): |
| 89 | 124 |
| 90 __node_number_counter = 0 | 125 __node_number_counter = 0 |
| 91 | 126 |
| 92 def __init__(self): | 127 def __init__(self): |
| 93 self.__node_number = AutomatonState.__node_number_counter | 128 self.__node_number = AutomatonState.__node_number_counter |
| 94 AutomatonState.__node_number_counter += 1 | 129 AutomatonState.__node_number_counter += 1 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 node [shape = doublecircle, style=unfilled]; %s | 282 node [shape = doublecircle, style=unfilled]; %s |
| 248 node [shape = circle]; | 283 node [shape = circle]; |
| 249 %s | 284 %s |
| 250 %s | 285 %s |
| 251 } | 286 } |
| 252 ''' % (start_shape, | 287 ''' % (start_shape, |
| 253 start_number, | 288 start_number, |
| 254 " ".join(terminals), | 289 " ".join(terminals), |
| 255 "\n".join(edge_content), | 290 "\n".join(edge_content), |
| 256 "\n".join(node_content)) | 291 "\n".join(node_content)) |
| OLD | NEW |