| 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | empty''' | 78 | empty''' |
| 79 | 79 |
| 80 def p_state_change(self, p): | 80 def p_state_change(self, p): |
| 81 '''state_change : LESS_THAN IDENTIFIER GREATER_THAN | 81 '''state_change : LESS_THAN IDENTIFIER GREATER_THAN |
| 82 | LESS_THAN DEFAULT GREATER_THAN''' | 82 | LESS_THAN DEFAULT GREATER_THAN''' |
| 83 state = self.__state | 83 state = self.__state |
| 84 state.current_state = p[2] | 84 state.current_state = p[2] |
| 85 assert state.current_state | 85 assert state.current_state |
| 86 if not state.current_state in state.rules: | 86 if not state.current_state in state.rules: |
| 87 state.rules[state.current_state] = { | 87 state.rules[state.current_state] = { |
| 88 'default': None, | 88 'default_action': None, |
| 89 'regex' : [] | 89 'regex' : [] |
| 90 } | 90 } |
| 91 p[0] = state.current_state | 91 p[0] = state.current_state |
| 92 | 92 |
| 93 def p_transition_rules(self, p): | 93 def p_transition_rules(self, p): |
| 94 '''transition_rules : transition_rule transition_rules | 94 '''transition_rules : transition_rule transition_rules |
| 95 | empty''' | 95 | empty''' |
| 96 | 96 |
| 97 def p_transition_rule(self, p): | 97 def p_transition_rule(self, p): |
| 98 '''transition_rule : composite_regex_or_default code action | 98 '''transition_rule : composite_regex code action |
| 99 | composite_regex_or_default empty action | 99 | composite_regex empty action |
| 100 | composite_regex_or_default code empty''' | 100 | composite_regex code empty |
| 101 transition = p[3] if p[3] else 'continue' | 101 | DEFAULT_ACTION code empty''' |
| 102 if transition == 'continue' and self.__state.current_state == 'default': | 102 transition = p[3] if p[3] else 'break' |
| 103 transition = 'break' | |
| 104 if not transition in self.__keyword_transitions: | 103 if not transition in self.__keyword_transitions: |
| 105 assert not transition == 'default' | 104 assert not transition == 'default' |
| 106 self.__state.transitions.add(transition) | 105 self.__state.transitions.add(transition) |
| 107 rule = (p[1], (RuleParser.__rule_precedence_counter, p[2], transition)) | |
| 108 RuleParser.__rule_precedence_counter += 1 | 106 RuleParser.__rule_precedence_counter += 1 |
| 109 rules = self.__state.rules[self.__state.current_state] | 107 rules = self.__state.rules[self.__state.current_state] |
| 110 if p[1] == 'default': | 108 code = p[2] |
| 111 assert not rules['default'] | 109 if p[1] == 'default_action': |
| 112 rules['default'] = rule | 110 assert not rules['default_action'] |
| 111 rules['default_action'] = code |
| 113 else: | 112 else: |
| 113 rule = (p[1], (RuleParser.__rule_precedence_counter, code, transition)) |
| 114 rules['regex'].append(rule) | 114 rules['regex'].append(rule) |
| 115 | 115 |
| 116 def p_action(self, p): | 116 def p_action(self, p): |
| 117 'action : ACTION_OPEN IDENTIFIER ACTION_CLOSE' | 117 'action : ACTION_OPEN IDENTIFIER ACTION_CLOSE' |
| 118 p[0] = p[2] | 118 p[0] = p[2] |
| 119 | 119 |
| 120 def p_composite_regex_or_default(self, p): | |
| 121 '''composite_regex_or_default : DEFAULT | |
| 122 | composite_regex''' | |
| 123 p[0] = p[1] | |
| 124 | |
| 125 def p_composite_regex(self, p): | 120 def p_composite_regex(self, p): |
| 126 '''composite_regex : regex_parts OR regex_parts | 121 '''composite_regex : regex_parts OR regex_parts |
| 127 | regex_parts''' | 122 | regex_parts''' |
| 128 if len(p) == 2: | 123 if len(p) == 2: |
| 129 p[0] = p[1] | 124 p[0] = p[1] |
| 130 else: | 125 else: |
| 131 p[0] = NfaBuilder.or_graphs([p[1], p[3]]) | 126 p[0] = NfaBuilder.or_graphs([p[1], p[3]]) |
| 132 | 127 |
| 133 def p_regex_parts(self, p): | 128 def p_regex_parts(self, p): |
| 134 '''regex_parts : regex_part | 129 '''regex_parts : regex_part |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 parser.build() | 200 parser.build() |
| 206 RuleParser.__static_instance = parser | 201 RuleParser.__static_instance = parser |
| 207 parser.__state = parser_state | 202 parser.__state = parser_state |
| 208 try: | 203 try: |
| 209 parser.parser.parse(data, lexer=parser.lexer.lexer) | 204 parser.parser.parse(data, lexer=parser.lexer.lexer) |
| 210 except Exception: | 205 except Exception: |
| 211 RuleParser.__static_instance = None | 206 RuleParser.__static_instance = None |
| 212 raise | 207 raise |
| 213 parser.__state = None | 208 parser.__state = None |
| 214 assert parser_state.transitions <= set(parser_state.rules.keys()) | 209 assert parser_state.transitions <= set(parser_state.rules.keys()) |
| OLD | NEW |