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

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

Issue 59973005: Experimental parser: user defined key classes (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/nfa.py ('k') | tools/lexer_generator/transition_key_test.py » ('j') | 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
(...skipping 11 matching lines...) Expand all
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 from regex_parser import RegexParser
31 from nfa import NfaBuilder 31 from nfa import NfaBuilder
32 from transition_keys import TransitionKey
32 33
33 class RuleParser: 34 class RuleParser:
34 35
35 tokens = RuleLexer.tokens 36 tokens = RuleLexer.tokens
36 37
37 def __init__(self): 38 def __init__(self):
38 self.aliases = { 39 self.aliases = {
39 'eof' : RegexParser.parse("eof"), #RegexParser.parse("[\0]"), 40 'eof' : RegexParser.parse("eof"), #RegexParser.parse("[\0]"),
40 'any' : RegexParser.parse("."), 41 'any' : RegexParser.parse("."),
41 } 42 }
43 self.character_classes = {}
42 self.current_transition = None 44 self.current_transition = None
43 self.rules = {} 45 self.rules = {}
44 46
45 def p_statements(self, p): 47 def p_statements(self, p):
46 'statements : statement maybe_statements' 48 'statements : statement maybe_statements'
47 49
48 def p_maybe_statement(self, p): 50 def p_maybe_statement(self, p):
49 '''maybe_statements : statements 51 '''maybe_statements : statements
50 | empty''' 52 | empty'''
51 53
52 def p_statement(self, p): 54 def p_statement(self, p):
53 '''statement : alias_rule 55 '''statement : alias_rule
54 | transition_rule''' 56 | transition_rule'''
55 57
56 def p_alias_rule(self, p): 58 def p_alias_rule(self, p):
57 'alias_rule : IDENTIFIER EQUALS composite_regex SEMICOLON' 59 'alias_rule : IDENTIFIER EQUALS composite_regex SEMICOLON'
58 assert not p[1] in self.aliases 60 assert not p[1] in self.aliases
61 graph = p[3]
59 self.aliases[p[1]] = p[3] 62 self.aliases[p[1]] = p[3]
63 if graph[0] == 'CLASS' or graph[0] == 'NOT_CLASS':
64 classes = self.character_classes
65 assert not p[1] in classes
66 classes[p[1]] = TransitionKey.character_class(graph, classes)
60 67
61 def p_transition_rule(self, p): 68 def p_transition_rule(self, p):
62 '''transition_rule : transition composite_regex code 69 '''transition_rule : transition composite_regex code
63 | transition composite_regex TRANSITION IDENTIFIER 70 | transition composite_regex TRANSITION IDENTIFIER
64 | transition composite_regex TRANSITION_WITH_CODE IDENTIFIER code''' 71 | transition composite_regex TRANSITION_WITH_CODE IDENTIFIER code'''
65 transition = p[0] 72 transition = p[0]
66 regex = p[2] 73 regex = p[2]
67 rules = self.rules[self.current_transition] 74 rules = self.rules[self.current_transition]
68 if len(p) == 4: 75 if len(p) == 4:
69 rules.append(('simple', regex, p[3])) 76 rules.append(('simple', regex, p[3]))
(...skipping 14 matching lines...) Expand all
84 self.rules[self.current_transition] = [] 91 self.rules[self.current_transition] = []
85 p[0] = self.current_transition 92 p[0] = self.current_transition
86 93
87 def p_composite_regex(self, p): 94 def p_composite_regex(self, p):
88 '''composite_regex : regex_parts OR regex_parts 95 '''composite_regex : regex_parts OR regex_parts
89 | regex_parts''' 96 | regex_parts'''
90 if len(p) == 2: 97 if len(p) == 2:
91 p[0] = p[1] 98 p[0] = p[1]
92 else: 99 else:
93 p[0] = NfaBuilder.or_graphs([p[1], p[3]]) 100 p[0] = NfaBuilder.or_graphs([p[1], p[3]])
94 # NfaBuilder().nfa(p[0]) 101 # builder = NfaBuilder()
102 # builder.set_character_classes(self.character_classes)
103 # builder.nfa(p[0])
95 104
96 def p_regex_parts(self, p): 105 def p_regex_parts(self, p):
97 '''regex_parts : regex_part 106 '''regex_parts : regex_part
98 | regex_part regex_parts''' 107 | regex_part regex_parts'''
99 p[0] = NfaBuilder.cat_graphs(p[1:]) 108 p[0] = NfaBuilder.cat_graphs(p[1:])
100 109
101 def p_regex_part(self, p): 110 def p_regex_part(self, p):
102 '''regex_part : LEFT_PARENTHESIS composite_regex RIGHT_PARENTHESIS modifier 111 '''regex_part : LEFT_PARENTHESIS composite_regex RIGHT_PARENTHESIS modifier
103 | regex_string_literal modifier 112 | regex_string_literal modifier
104 | regex_class modifier 113 | regex_class modifier
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 def p_error(self, p): 163 def p_error(self, p):
155 raise Exception("Syntax error in input '%s'" % p) 164 raise Exception("Syntax error in input '%s'" % p)
156 165
157 def build(self, **kwargs): 166 def build(self, **kwargs):
158 self.parser = yacc.yacc(module=self, debug=0, write_tables=0, **kwargs) 167 self.parser = yacc.yacc(module=self, debug=0, write_tables=0, **kwargs)
159 self.lexer = RuleLexer() 168 self.lexer = RuleLexer()
160 self.lexer.build(**kwargs) 169 self.lexer.build(**kwargs)
161 170
162 def parse(self, data): 171 def parse(self, data):
163 return self.parser.parse(data, lexer=self.lexer.lexer) 172 return self.parser.parse(data, lexer=self.lexer.lexer)
OLDNEW
« no previous file with comments | « tools/lexer_generator/nfa.py ('k') | tools/lexer_generator/transition_key_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698