| 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 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 class RuleParserState: | 34 class RuleParserState: |
| 35 | 35 |
| 36 def __init__(self): | 36 def __init__(self): |
| 37 self.aliases = { | 37 self.aliases = { |
| 38 'eof' : RegexParser.parse("[\\0]"), | 38 'eof' : RegexParser.parse("[\\0]"), |
| 39 } | 39 } |
| 40 self.character_classes = {} | 40 self.character_classes = {} |
| 41 self.current_state = None | 41 self.current_state = None |
| 42 self.rules = {} | 42 self.rules = {} |
| 43 self.transitions = set() |
| 43 | 44 |
| 44 def parse(self, string): | 45 def parse(self, string): |
| 45 return RuleParser.parse(string, self) | 46 return RuleParser.parse(string, self) |
| 46 | 47 |
| 47 class RuleParser: | 48 class RuleParser: |
| 48 | 49 |
| 49 tokens = RuleLexer.tokens | 50 tokens = RuleLexer.tokens |
| 50 __rule_precedence_counter = 0 | 51 __rule_precedence_counter = 0 |
| 52 __keyword_transitions = set([ |
| 53 'continue', 'break', 'terminate', 'terminate_illegal']) |
| 51 | 54 |
| 52 def __init__(self): | 55 def __init__(self): |
| 53 self.__state = None | 56 self.__state = None |
| 54 | 57 |
| 55 def p_statements(self, p): | 58 def p_statements(self, p): |
| 56 'statements : aliases rules' | 59 'statements : aliases rules' |
| 57 | 60 |
| 58 def p_aliases(self, p): | 61 def p_aliases(self, p): |
| 59 '''aliases : alias_rule aliases | 62 '''aliases : alias_rule aliases |
| 60 | empty''' | 63 | empty''' |
| (...skipping 27 matching lines...) Expand all Loading... |
| 88 p[0] = state.current_state | 91 p[0] = state.current_state |
| 89 | 92 |
| 90 def p_transition_rules(self, p): | 93 def p_transition_rules(self, p): |
| 91 '''transition_rules : transition_rule transition_rules | 94 '''transition_rules : transition_rule transition_rules |
| 92 | empty''' | 95 | empty''' |
| 93 | 96 |
| 94 def p_transition_rule(self, p): | 97 def p_transition_rule(self, p): |
| 95 '''transition_rule : composite_regex_or_default code action | 98 '''transition_rule : composite_regex_or_default code action |
| 96 | composite_regex_or_default empty action | 99 | composite_regex_or_default empty action |
| 97 | composite_regex_or_default code empty''' | 100 | composite_regex_or_default code empty''' |
| 101 transition = p[3] if p[3] else 'continue' |
| 102 if transition == 'continue' and self.__state.current_state == 'default': |
| 103 transition = 'break' |
| 104 if not transition in self.__keyword_transitions: |
| 105 assert not transition == 'default' |
| 106 self.__state.transitions.add(transition) |
| 107 rule = (p[1], (RuleParser.__rule_precedence_counter, p[2], transition)) |
| 108 RuleParser.__rule_precedence_counter += 1 |
| 98 rules = self.__state.rules[self.__state.current_state] | 109 rules = self.__state.rules[self.__state.current_state] |
| 99 rule = (p[1], RuleParser.__rule_precedence_counter, p[2], p[3]) | |
| 100 RuleParser.__rule_precedence_counter += 1 | |
| 101 if p[1] == 'default': | 110 if p[1] == 'default': |
| 102 assert not rules['default'] | 111 assert not rules['default'] |
| 103 rules['default'] = rule | 112 rules['default'] = rule |
| 104 else: | 113 else: |
| 105 rules['regex'].append(rule) | 114 rules['regex'].append(rule) |
| 106 | 115 |
| 107 def p_action(self, p): | 116 def p_action(self, p): |
| 108 'action : ACTION_OPEN IDENTIFIER ACTION_CLOSE' | 117 'action : ACTION_OPEN IDENTIFIER ACTION_CLOSE' |
| 109 p[0] = p[2] | 118 p[0] = p[2] |
| 110 | 119 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 @staticmethod | 199 @staticmethod |
| 191 def parse(data, parser_state): | 200 def parse(data, parser_state): |
| 192 parser = RuleParser.__static_instance | 201 parser = RuleParser.__static_instance |
| 193 if not parser: | 202 if not parser: |
| 194 parser = RuleParser() | 203 parser = RuleParser() |
| 195 parser.build() | 204 parser.build() |
| 196 RuleParser.__static_instance = parser | 205 RuleParser.__static_instance = parser |
| 197 parser.__state = parser_state | 206 parser.__state = parser_state |
| 198 try: | 207 try: |
| 199 parser.parser.parse(data, lexer=parser.lexer.lexer) | 208 parser.parser.parse(data, lexer=parser.lexer.lexer) |
| 200 except Exception as e: | 209 except Exception: |
| 201 RuleParser.__static_instance = None | 210 RuleParser.__static_instance = None |
| 202 raise e | 211 raise |
| 212 assert parser_state.transitions <= set(parser_state.rules.keys()) |
| 203 parser.__state = None | 213 parser.__state = None |
| OLD | NEW |