| 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 'catch_all' : None, | 91 'catch_all' : None, |
| 92 'regex' : [] | 92 'regex' : [] |
| 93 } | 93 } |
| 94 p[0] = state.current_state | 94 p[0] = state.current_state |
| 95 | 95 |
| 96 def p_transition_rules(self, p): | 96 def p_transition_rules(self, p): |
| 97 '''transition_rules : transition_rule transition_rules | 97 '''transition_rules : transition_rule transition_rules |
| 98 | empty''' | 98 | empty''' |
| 99 | 99 |
| 100 def p_transition_rule(self, p): | 100 def p_transition_rule(self, p): |
| 101 '''transition_rule : composite_regex code action | 101 '''transition_rule : composite_regex code_or_token action |
| 102 | composite_regex empty action | 102 | composite_regex empty action |
| 103 | composite_regex code empty | 103 | composite_regex code_or_token empty |
| 104 | DEFAULT_ACTION code empty | 104 | DEFAULT_ACTION code_or_token empty |
| 105 | CATCH_ALL empty action''' | 105 | CATCH_ALL empty action''' |
| 106 transition = p[3] | 106 transition = p[3] |
| 107 if transition and not transition in self.__keyword_transitions: | 107 if transition and not transition in self.__keyword_transitions: |
| 108 assert not transition == 'default' | 108 assert not transition == 'default' |
| 109 self.__state.transitions.add(transition) | 109 self.__state.transitions.add(transition) |
| 110 RuleParser.__rule_precedence_counter += 1 | 110 RuleParser.__rule_precedence_counter += 1 |
| 111 rules = self.__state.rules[self.__state.current_state] | 111 rules = self.__state.rules[self.__state.current_state] |
| 112 code = p[2] | 112 code = p[2] |
| 113 if p[1] == 'default_action': | 113 if p[1] == 'default_action': |
| 114 assert not rules['default_action'] | 114 assert not rules['default_action'] |
| 115 rules['default_action'] = code | 115 rules['default_action'] = code |
| 116 elif p[1] == 'catch_all': | 116 elif p[1] == 'catch_all': |
| 117 assert not rules['catch_all'] | 117 assert not rules['catch_all'] |
| 118 rules['catch_all'] = transition | 118 rules['catch_all'] = transition |
| 119 else: | 119 else: |
| 120 rule = (p[1], RuleParser.__rule_precedence_counter, code, transition) | 120 rule = (p[1], RuleParser.__rule_precedence_counter, code, transition) |
| 121 rules['regex'].append(rule) | 121 rules['regex'].append(rule) |
| 122 | 122 |
| 123 def p_code_or_token(self, p): |
| 124 '''code_or_token : code |
| 125 | push_token''' |
| 126 p[0] = p[1] |
| 127 |
| 128 def p_push_token(self, p): |
| 129 'push_token : PUSH_TOKEN LEFT_PARENTHESIS IDENTIFIER RIGHT_PARENTHESIS' |
| 130 p[0] = (p[1], p[3]) |
| 131 |
| 123 def p_action(self, p): | 132 def p_action(self, p): |
| 124 'action : ACTION_OPEN IDENTIFIER ACTION_CLOSE' | 133 'action : ACTION_OPEN IDENTIFIER ACTION_CLOSE' |
| 125 p[0] = p[2] | 134 p[0] = p[2] |
| 126 | 135 |
| 127 def p_composite_regex(self, p): | 136 def p_composite_regex(self, p): |
| 128 '''composite_regex : regex_parts OR regex_parts | 137 '''composite_regex : regex_parts OR regex_parts |
| 129 | regex_parts''' | 138 | regex_parts''' |
| 130 if len(p) == 2: | 139 if len(p) == 2: |
| 131 p[0] = p[1] | 140 p[0] = p[1] |
| 132 else: | 141 else: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 | 180 |
| 172 def p_modifier(self, p): | 181 def p_modifier(self, p): |
| 173 '''modifier : PLUS | 182 '''modifier : PLUS |
| 174 | QUESTION_MARK | 183 | QUESTION_MARK |
| 175 | STAR | 184 | STAR |
| 176 | empty''' | 185 | empty''' |
| 177 p[0] = p[1] | 186 p[0] = p[1] |
| 178 | 187 |
| 179 def p_code(self, p): | 188 def p_code(self, p): |
| 180 'code : LEFT_BRACKET code_fragments RIGHT_BRACKET' | 189 'code : LEFT_BRACKET code_fragments RIGHT_BRACKET' |
| 181 p[0] = p[2].strip() | 190 p[0] = ('code', p[2].strip()) |
| 182 | 191 |
| 183 def p_code_fragments(self, p): | 192 def p_code_fragments(self, p): |
| 184 '''code_fragments : CODE_FRAGMENT code_fragments | 193 '''code_fragments : CODE_FRAGMENT code_fragments |
| 185 | empty''' | 194 | empty''' |
| 186 p[0] = p[1] | 195 p[0] = p[1] |
| 187 if len(p) == 3 and p[2]: | 196 if len(p) == 3 and p[2]: |
| 188 p[0] = p[1] + p[2] | 197 p[0] = p[1] + p[2] |
| 189 | 198 |
| 190 def p_empty(self, p): | 199 def p_empty(self, p): |
| 191 'empty :' | 200 'empty :' |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 rule_map = {} | 271 rule_map = {} |
| 263 builder = NfaBuilder() | 272 builder = NfaBuilder() |
| 264 builder.set_character_classes(parser_state.character_classes) | 273 builder.set_character_classes(parser_state.character_classes) |
| 265 assert 'default' in parser_state.rules | 274 assert 'default' in parser_state.rules |
| 266 def process(k, v): | 275 def process(k, v): |
| 267 graphs = [] | 276 graphs = [] |
| 268 continues = 0 | 277 continues = 0 |
| 269 for (graph, precedence, code, transition) in v['regex']: | 278 for (graph, precedence, code, transition) in v['regex']: |
| 270 default_code = v['default_action'] | 279 default_code = v['default_action'] |
| 271 if code or default_code: | 280 if code or default_code: |
| 272 action = Action('code', code if code else default_code, precedence) | 281 (code_type, code_value) = code if code else default_code |
| 282 action = Action(code_type, code_value, precedence) |
| 273 graph = NfaBuilder.add_action(graph, action) | 283 graph = NfaBuilder.add_action(graph, action) |
| 274 if not transition or transition == 'break': | 284 if not transition or transition == 'break': |
| 275 pass | 285 pass |
| 276 elif transition == 'continue': | 286 elif transition == 'continue': |
| 277 assert not k == 'default' | 287 assert not k == 'default' |
| 278 continues += 1 | 288 continues += 1 |
| 279 graph = NfaBuilder.add_continue(graph) | 289 graph = NfaBuilder.add_continue(graph) |
| 280 elif (transition == 'terminate' or | 290 elif (transition == 'terminate' or |
| 281 transition == 'terminate_illegal'): | 291 transition == 'terminate_illegal'): |
| 282 assert not code | 292 assert not code |
| (...skipping 13 matching lines...) Expand all Loading... |
| 296 rule_map[k] = graph | 306 rule_map[k] = graph |
| 297 # process first the subgraphs, then the default graph | 307 # process first the subgraphs, then the default graph |
| 298 for k, v in parser_state.rules.items(): | 308 for k, v in parser_state.rules.items(): |
| 299 if k == 'default': continue | 309 if k == 'default': continue |
| 300 process(k, v) | 310 process(k, v) |
| 301 process('default', parser_state.rules['default']) | 311 process('default', parser_state.rules['default']) |
| 302 # build the automata | 312 # build the automata |
| 303 for rule_name, graph in rule_map.items(): | 313 for rule_name, graph in rule_map.items(): |
| 304 self.__automata[rule_name] = RuleProcessor.Automata(builder, graph) | 314 self.__automata[rule_name] = RuleProcessor.Automata(builder, graph) |
| 305 self.default_action = parser_state.rules['default']['default_action'] | 315 self.default_action = parser_state.rules['default']['default_action'] |
| OLD | NEW |