| 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 14 matching lines...) Expand all Loading... |
| 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 import ply.yacc as yacc | 28 import ply.yacc as yacc |
| 29 from rule_lexer import RuleLexer | 29 from rule_lexer import RuleLexer |
| 30 | 30 |
| 31 class RuleParser: | 31 class RuleParser: |
| 32 | 32 |
| 33 tokens = RuleLexer.tokens | 33 tokens = RuleLexer.tokens |
| 34 | 34 |
| 35 aliases = dict() | 35 def __init__(self): |
| 36 transitions = dict() | 36 self.aliases = dict() |
| 37 self.transitions = dict() |
| 37 | 38 |
| 38 def p_statement_alias(self, p): | 39 def p_statement_alias(self, p): |
| 39 'statement : ALIAS EQUALS REGEX' | 40 'statement : ALIAS EQUALS REGEX' |
| 40 regex = self.lexer.lexer.lexmatch.group('regex') | 41 regex = self.lexer.lexer.lexmatch.group('regex') |
| 41 self.aliases[p[1]] = regex | 42 self.aliases[p[1]] = regex |
| 42 | 43 |
| 43 def p_statement_condition_transition(self, p): | 44 def p_statement_condition_transition(self, p): |
| 44 'statement : CONDITION_BEGIN CONDITION CONDITION_END REGEX_AND_TRANSITION' | 45 'statement : CONDITION_BEGIN CONDITION CONDITION_END REGEX_TRANSITION' |
| 45 old_condition = p[2] | 46 old_condition = p[2] |
| 46 regex = self.lexer.lexer.lexmatch.group('regex') | 47 regex = self.lexer.lexer.lexmatch.group('regex').strip() |
| 47 new_condition = self.lexer.lexer.lexmatch.group('new') | 48 new_condition = self.lexer.lexer.lexmatch.group('new') |
| 48 if old_condition not in self.transitions: | 49 if old_condition not in self.transitions: |
| 49 self.transitions[old_condition] = [] | 50 self.transitions[old_condition] = dict() |
| 50 self.transitions[old_condition].append((regex, new_condition)) | 51 self.transitions[old_condition][regex] = ('condition', new_condition) |
| 51 | 52 |
| 52 def p_statement_condition_body(self, p): | 53 def p_statement_condition_body(self, p): |
| 53 'statement : CONDITION_BEGIN CONDITION CONDITION_END REGEX_AND_BODY' | 54 'statement : CONDITION_BEGIN CONDITION CONDITION_END REGEX_BODY' |
| 54 old_condition = p[2] | 55 old_condition = p[2] |
| 55 regex = self.lexer.lexer.lexmatch.group('regex') | 56 regex = self.lexer.lexer.lexmatch.group('regex').strip() |
| 56 body = self.lexer.lexer.lexmatch.group('body') | 57 body = self.lexer.lexer.lexmatch.group('body').strip() |
| 57 if old_condition not in self.transitions: | 58 if old_condition not in self.transitions: |
| 58 self.transitions[old_condition] = [] | 59 self.transitions[old_condition] = dict() |
| 59 self.transitions[old_condition].append((regex, body)) | 60 self.transitions[old_condition][regex] = ('body', body) |
| 60 | 61 |
| 61 def p_empty(self, p): | 62 def p_statement_condition_transition_body(self, p): |
| 62 'empty :' | 63 'statement : CONDITION_BEGIN CONDITION CONDITION_END REGEX_TRANSITION_BODY' |
| 64 old_condition = p[2] |
| 65 regex = self.lexer.lexer.lexmatch.group('regex').strip() |
| 66 new_condition = self.lexer.lexer.lexmatch.group('new').strip() |
| 67 body = self.lexer.lexer.lexmatch.group('body').strip() |
| 68 if old_condition not in self.transitions: |
| 69 self.transitions[old_condition] = dict() |
| 70 self.transitions[old_condition][regex] = ( |
| 71 'condition_and_body', new_condition, body) |
| 63 | 72 |
| 64 def p_error(self, p): | 73 def p_error(self, p): |
| 65 raise Exception("Syntax error in input '%s'" % p) | 74 raise Exception("Syntax error in input '%s'" % p) |
| 66 | 75 |
| 67 def build(self, **kwargs): | 76 def build(self, **kwargs): |
| 68 self.parser = yacc.yacc(module=self, **kwargs) | 77 self.parser = yacc.yacc(module=self, **kwargs) |
| 69 self.lexer = RuleLexer() | 78 self.lexer = RuleLexer() |
| 70 self.lexer.build(**kwargs) | 79 self.lexer.build(**kwargs) |
| 71 | 80 |
| 72 def parse(self, data): | 81 def parse(self, data): |
| 73 return self.parser.parse(data, lexer=self.lexer.lexer) | 82 return self.parser.parse(data, lexer=self.lexer.lexer) |
| OLD | NEW |