Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(490)

Side by Side Diff: tools/lexer_generator/rule_parser.py

Issue 59603003: Experimental parser: parsing regex subexpressions (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/lexer_generator/rule_lexer.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 # with the distribution. 11 # with the distribution.
12 # * Neither the name of Google Inc. nor the names of its 12 # * Neither the name of Google Inc. nor the names of its
13 # contributors may be used to endorse or promote products derived 13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission. 14 # from this software without specific prior written permission.
15 # 15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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 from regex_parser import RegexParser
30 31
31 class RuleParser: 32 class RuleParser:
32 33
33 tokens = RuleLexer.tokens 34 tokens = RuleLexer.tokens
34 35
35 def __init__(self): 36 def __init__(self):
36 self.aliases = {} 37 self.aliases = {
38 'eof' : "eof rule",
39 'any' : "any rule",
40 }
37 self.current_transition = None 41 self.current_transition = None
38 self.rules = {} 42 self.rules = {}
39 43
40 def p_statements(self, p): 44 def p_statements(self, p):
41 'statements : statement maybe_statements' 45 'statements : statement maybe_statements'
42 46
43 def p_maybe_statement(self, p): 47 def p_maybe_statement(self, p):
44 '''maybe_statements : statements 48 '''maybe_statements : statements
45 | empty''' 49 | empty'''
46 50
(...skipping 26 matching lines...) Expand all
73 '''transition : LESS_THAN IDENTIFIER GREATER_THAN''' 77 '''transition : LESS_THAN IDENTIFIER GREATER_THAN'''
74 # | empty''' TODO skipping transition without sr conflict 78 # | empty''' TODO skipping transition without sr conflict
75 if p[1]: 79 if p[1]:
76 self.current_transition = p[2] 80 self.current_transition = p[2]
77 assert self.current_transition 81 assert self.current_transition
78 if not self.current_transition in self.rules: 82 if not self.current_transition in self.rules:
79 self.rules[self.current_transition] = [] 83 self.rules[self.current_transition] = []
80 p[0] = self.current_transition 84 p[0] = self.current_transition
81 85
82 def p_composite_regex(self, p): 86 def p_composite_regex(self, p):
83 '''composite_regex : regex_part OR regex_part maybe_regex_parts 87 '''composite_regex : regex_parts OR regex_parts
84 | regex_part maybe_regex_parts''' 88 | regex_parts'''
85 if p[len(p)-1]: 89 if p[len(p)-1]:
86 p[0] = p[1:] 90 p[0] = p[1:]
87 else: 91 else:
88 p[0] = p[1:-1] 92 p[0] = p[1:-1]
89 93
90 def p_maybe_regex_part(self, p): 94 def p_regex_parts(self, p):
91 '''maybe_regex_parts : composite_regex 95 '''regex_parts : regex_part
92 | empty''' 96 | regex_part regex_parts'''
93 p[0] = p[1] 97 p[0] = p[1:]
94 98
95 def p_regex_part(self, p): 99 def p_regex_part(self, p):
96 '''regex_part : LEFT_PARENTHESIS composite_regex RIGHT_PARENTHESIS modifier 100 '''regex_part : LEFT_PARENTHESIS composite_regex RIGHT_PARENTHESIS modifier
97 | STRING_REGEX modifier 101 | regex_string_literal modifier
98 | CHARACTER_CLASS_REGEX modifier 102 | regex_class modifier
99 | IDENTIFIER modifier''' 103 | regex modifier
104 | regex_alias modifier'''
100 if p[len(p)-1]: 105 if p[len(p)-1]:
101 p[0] = p[1:] 106 p[0] = p[1:]
102 else: 107 else:
103 p[0] = p[1:-1] 108 p[0] = p[1:-1]
104 109
110 def p_regex_string_literal(self, p):
111 'regex_string_literal : STRING'
112 string = p[1][1:-1]
113 for c in "\+?|*[]()":
114 string = string.replace(c, "\\" + c)
115 p[0] = RegexParser.parse(string)
116
117 def p_regex(self, p):
118 'regex : REGEX'
119 p[0] = RegexParser.parse(p[1][1:-1])
120
121 def p_regex_class(self, p):
122 'regex_class : CHARACTER_CLASS_REGEX'
123 p[0] = RegexParser.parse(p[1])
124
125 def p_regex_alias(self, p):
126 'regex_alias : IDENTIFIER'
127 p[0] = self.aliases[p[1]]
128
105 def p_modifier(self, p): 129 def p_modifier(self, p):
106 '''modifier : PLUS 130 '''modifier : PLUS
107 | QUESTION_MARK 131 | QUESTION_MARK
108 | STAR 132 | STAR
109 | empty''' 133 | empty'''
110 p[0] = p[1] 134 p[0] = p[1]
111 135
112 def p_code(self, p): 136 def p_code(self, p):
113 'code : LEFT_BRACKET code_fragments RIGHT_BRACKET' 137 'code : LEFT_BRACKET code_fragments RIGHT_BRACKET'
114 p[0] = p[2].strip() 138 p[0] = p[2].strip()
(...skipping 11 matching lines...) Expand all
126 def p_error(self, p): 150 def p_error(self, p):
127 raise Exception("Syntax error in input '%s'" % p) 151 raise Exception("Syntax error in input '%s'" % p)
128 152
129 def build(self, **kwargs): 153 def build(self, **kwargs):
130 self.parser = yacc.yacc(module=self, debug=0, write_tables=0, **kwargs) 154 self.parser = yacc.yacc(module=self, debug=0, write_tables=0, **kwargs)
131 self.lexer = RuleLexer() 155 self.lexer = RuleLexer()
132 self.lexer.build(**kwargs) 156 self.lexer.build(**kwargs)
133 157
134 def parse(self, data): 158 def parse(self, data):
135 return self.parser.parse(data, lexer=self.lexer.lexer) 159 return self.parser.parse(data, lexer=self.lexer.lexer)
OLDNEW
« no previous file with comments | « tools/lexer_generator/rule_lexer.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698