| OLD | NEW |
| 1 # Copyright 2014 the V8 project authors. All rights reserved. | 1 # Copyright 2014 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 24 matching lines...) Expand all Loading... |
| 35 __empty_term = None | 35 __empty_term = None |
| 36 | 36 |
| 37 @staticmethod | 37 @staticmethod |
| 38 def empty_term(): | 38 def empty_term(): |
| 39 if Term.__empty_term == None: | 39 if Term.__empty_term == None: |
| 40 Term.__empty_term = Term('') | 40 Term.__empty_term = Term('') |
| 41 return Term.__empty_term | 41 return Term.__empty_term |
| 42 | 42 |
| 43 def __init__(self, name, *args): | 43 def __init__(self, name, *args): |
| 44 assert type(name) == StringType | 44 assert type(name) == StringType |
| 45 if not name: | 45 assert name or not args, 'empty term must not have args' |
| 46 assert not args, 'empty term must not have args' | |
| 47 for v in args: | 46 for v in args: |
| 48 if type(v) == IntType or type(v) == StringType: | 47 assert type(v) == IntType or type(v) == StringType or isinstance(v, Term) |
| 49 continue | |
| 50 else: | |
| 51 assert isinstance(v, Term) | |
| 52 self.__tuple = tuple([name] + list(args)) | 48 self.__tuple = tuple([name] + list(args)) |
| 53 self.__str = None | 49 self.__str = None |
| 54 | 50 |
| 55 def name(self): | 51 def name(self): |
| 56 return self.__tuple[0] | 52 return self.__tuple[0] |
| 57 | 53 |
| 58 def args(self): | 54 def args(self): |
| 59 return self.__tuple[1:] | 55 return self.__tuple[1:] |
| 60 | 56 |
| 61 def __hash__(self): | 57 def __hash__(self): |
| (...skipping 12 matching lines...) Expand all Loading... |
| 74 self.__str = '(%s)' % ','.join(map(str, self.__tuple)) | 70 self.__str = '(%s)' % ','.join(map(str, self.__tuple)) |
| 75 return self.__str | 71 return self.__str |
| 76 | 72 |
| 77 class Action(object): | 73 class Action(object): |
| 78 | 74 |
| 79 __empty_action = None | 75 __empty_action = None |
| 80 | 76 |
| 81 @staticmethod | 77 @staticmethod |
| 82 def empty_action(): | 78 def empty_action(): |
| 83 if Action.__empty_action == None: | 79 if Action.__empty_action == None: |
| 84 Action.__empty_action = Action(Term.empty_term(), Term.empty_term()) | 80 Action.__empty_action = Action(Term.empty_term(), -1) |
| 85 return Action.__empty_action | 81 return Action.__empty_action |
| 86 | 82 |
| 87 @staticmethod | 83 @staticmethod |
| 88 def dominant_action(state_set): | 84 def dominant_action(actions): |
| 89 action = Action.empty_action() | 85 dominant = Action.empty_action() |
| 90 for state in state_set: | 86 for action in actions: |
| 91 if not state.action(): | 87 if not action: |
| 92 continue | 88 continue |
| 93 if not action: | 89 if not dominant: |
| 94 action = state.action() | 90 dominant = action |
| 95 continue | 91 continue |
| 96 if state.action().precedence() == action.precedence(): | 92 if action.precedence() == dominant.precedence(): |
| 97 assert state.action() == action | 93 assert action.__term == dominant.__term |
| 98 elif state.action().precedence() < action.precedence(): | 94 elif action.precedence() < dominant.precedence(): |
| 99 action = state.action() | 95 dominant = action |
| 100 return action | 96 return dominant |
| 101 | 97 |
| 102 def __init__(self, entry_action, match_action, precedence = -1): | 98 def __init__(self, term, precedence): |
| 103 assert isinstance(match_action, Term) | 99 assert isinstance(term, Term) |
| 104 assert isinstance(entry_action, Term) | |
| 105 assert type(precedence) == IntType | 100 assert type(precedence) == IntType |
| 106 self.__entry_action = entry_action | 101 assert not term or precedence >= 0, 'action must have positive precedence' |
| 107 self.__match_action = match_action | 102 self.__term = term |
| 108 self.__precedence = precedence | 103 self.__precedence = precedence |
| 109 | 104 |
| 110 def entry_action(self): | 105 def name(self): |
| 111 return self.__entry_action | 106 return self.__term.name() |
| 112 | 107 |
| 113 def match_action(self): | 108 def term(self): |
| 114 return self.__match_action | 109 return self.__term |
| 115 | 110 |
| 116 def precedence(self): | 111 def precedence(self): |
| 117 return self.__precedence | 112 return self.__precedence |
| 118 | 113 |
| 119 def to_term(self): | |
| 120 return Term( | |
| 121 'action_serialization', | |
| 122 self.__entry_action, self.__match_action, str(self.__precedence)) | |
| 123 | |
| 124 @staticmethod | |
| 125 def from_term(term): | |
| 126 assert term.name() == 'action_serialization' | |
| 127 return Action(term.args()[0], term.args()[1], int(term.args()[2])) | |
| 128 | |
| 129 def __nonzero__(self): | 114 def __nonzero__(self): |
| 130 return bool(self.__entry_action) or bool(self.__match_action) | 115 'true <==> self == empty_action' |
| 131 | 116 return bool(self.__term) |
| 132 def __hash__(self): | |
| 133 return hash((self.__entry_action, self.__match_action)) | |
| 134 | 117 |
| 135 def __eq__(self, other): | 118 def __eq__(self, other): |
| 136 return (isinstance(other, self.__class__) and | 119 return isinstance(other, self.__class__) and self.__term == other.__term |
| 137 self.__entry_action == other.__entry_action and | |
| 138 self.__match_action == other.__match_action) | |
| 139 | 120 |
| 140 def __str__(self): | 121 def __str__(self): |
| 141 parts = map(lambda action : '' if not action else str(action), | 122 return "action <%s>" % ('' if not self.__term else str(self.__term)) |
| 142 [self.__entry_action, self.__match_action]) | |
| 143 return "action< %s >" % " | ".join(parts) | |
| OLD | NEW |