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

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

Issue 152823002: Experimental parser: use sentinal values for actions (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 10 months 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') | 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
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 '''rules : state_change transition_rules rules 79 '''rules : state_change transition_rules rules
80 | empty''' 80 | empty'''
81 81
82 def p_state_change(self, p): 82 def p_state_change(self, p):
83 'state_change : GRAPH_OPEN IDENTIFIER GRAPH_CLOSE' 83 'state_change : GRAPH_OPEN IDENTIFIER GRAPH_CLOSE'
84 state = self.__state 84 state = self.__state
85 state.current_state = p[2] 85 state.current_state = p[2]
86 assert state.current_state 86 assert state.current_state
87 if not state.current_state in state.rules: 87 if not state.current_state in state.rules:
88 state.rules[state.current_state] = { 88 state.rules[state.current_state] = {
89 'default_action': None, 89 'default_action': Term.empty_term(),
90 'uniques' : {}, 90 'uniques' : {},
91 'regex' : [] 91 'regex' : []
92 } 92 }
93 p[0] = state.current_state 93 p[0] = state.current_state
94 94
95 def p_transition_rules(self, p): 95 def p_transition_rules(self, p):
96 '''transition_rules : transition_rule transition_rules 96 '''transition_rules : transition_rule transition_rules
97 | empty''' 97 | empty'''
98 98
99 def p_transition_rule(self, p): 99 def p_transition_rule(self, p):
100 '''transition_rule : composite_regex action 100 '''transition_rule : composite_regex action
101 | DEFAULT_ACTION default_action 101 | DEFAULT_ACTION default_action
102 | EOS eos 102 | EOS eos
103 | CATCH_ALL action''' 103 | CATCH_ALL action'''
104 precedence = RuleParser.__rule_precedence_counter 104 precedence = RuleParser.__rule_precedence_counter
105 RuleParser.__rule_precedence_counter += 1 105 RuleParser.__rule_precedence_counter += 1
106 action = p[2] 106 action = p[2]
107 (entry_action, match_action, transition) = action 107 (entry_action, match_action, transition) = action
108 if transition and not transition in self.__keyword_transitions: 108 if transition and not transition in self.__keyword_transitions:
109 assert not transition == 'default', "can't append default graph" 109 assert not transition == 'default', "can't append default graph"
110 self.__state.transitions.add(transition) 110 self.__state.transitions.add(transition)
111 rules = self.__state.rules[self.__state.current_state] 111 rules = self.__state.rules[self.__state.current_state]
112 if p[1] == 'default_action': 112 if p[1] == 'default_action':
113 assert self.__state.current_state == 'default' 113 assert self.__state.current_state == 'default'
114 assert not rules['default_action'] 114 assert not rules['default_action']
115 rules['default_action'] = action 115 assert not entry_action
116 rules['default_action'] = match_action
116 elif p[1] == 'eos' or p[1] == 'catch_all': 117 elif p[1] == 'eos' or p[1] == 'catch_all':
117 assert p[1] not in rules['uniques'] 118 assert p[1] not in rules['uniques']
118 rules['uniques'][p[1]] = True 119 rules['uniques'][p[1]] = True
119 rules['regex'].append((NfaBuilder.unique_key(p[1]), precedence, action)) 120 rules['regex'].append((NfaBuilder.unique_key(p[1]), precedence, action))
120 else: 121 else:
121 regex = p[1] 122 regex = p[1]
122 rules['regex'].append((regex, precedence, action)) 123 rules['regex'].append((regex, precedence, action))
123 124
124 def p_action(self, p): 125 def p_action(self, p):
125 '''action : ACTION_OPEN maybe_identifier_action OR maybe_identifier_action O R maybe_transition ACTION_CLOSE''' 126 '''action : ACTION_OPEN maybe_identifier_action OR maybe_identifier_action O R maybe_transition ACTION_CLOSE'''
126 p[0] = (p[2], p[4], p[6]) 127 p[0] = (p[2], p[4], p[6])
127 128
128 def p_default_action(self, p): 129 def p_default_action(self, p):
129 'default_action : ACTION_OPEN identifier_action ACTION_CLOSE' 130 'default_action : ACTION_OPEN identifier_action ACTION_CLOSE'
130 p[0] = (None, p[2], None) 131 p[0] = (Term.empty_term(), p[2], None)
131 132
132 def p_eos(self, p): 133 def p_eos(self, p):
133 'eos : ACTION_OPEN identifier_action ACTION_CLOSE' 134 'eos : ACTION_OPEN identifier_action ACTION_CLOSE'
134 p[0] = (None, p[2], None) 135 p[0] = (Term.empty_term(), p[2], None)
135 136
136 def p_maybe_identifier_action(self, p): 137 def p_maybe_identifier_action(self, p):
137 '''maybe_identifier_action : identifier_action 138 '''maybe_identifier_action : identifier_action
138 | empty''' 139 | empty'''
139 p[0] = p[1] 140 p[0] = p[1] if p[1] else Term.empty_term()
140 141
141 def p_maybe_transition(self, p): 142 def p_maybe_transition(self, p):
142 '''maybe_transition : IDENTIFIER 143 '''maybe_transition : IDENTIFIER
143 | empty''' 144 | empty'''
144 p[0] = p[1] 145 p[0] = p[1]
145 146
146 def p_identifier_action(self, p): 147 def p_identifier_action(self, p):
147 '''identifier_action : IDENTIFIER 148 '''identifier_action : IDENTIFIER
148 | IDENTIFIER LEFT_PARENTHESIS RIGHT_PARENTHESIS 149 | IDENTIFIER LEFT_PARENTHESIS RIGHT_PARENTHESIS
149 | IDENTIFIER LEFT_PARENTHESIS action_params RIGHT_PAREN THESIS''' 150 | IDENTIFIER LEFT_PARENTHESIS action_params RIGHT_PAREN THESIS'''
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 def process(subgraph, v): 307 def process(subgraph, v):
307 graphs = [] 308 graphs = []
308 for graph, precedence, action in v['regex']: 309 for graph, precedence, action in v['regex']:
309 (entry_action, match_action, transition) = action 310 (entry_action, match_action, transition) = action
310 if entry_action or match_action: 311 if entry_action or match_action:
311 action = Action(entry_action, match_action, precedence) 312 action = Action(entry_action, match_action, precedence)
312 graph = NfaBuilder.add_action(graph, action) 313 graph = NfaBuilder.add_action(graph, action)
313 if not transition: 314 if not transition:
314 pass 315 pass
315 elif transition == 'continue': 316 elif transition == 'continue':
316 assert not subgraph == 'default' 317 assert not subgraph == 'default', 'unimplemented'
317 graph = NfaBuilder.add_continue(graph) 318 graph = NfaBuilder.add_continue(graph)
318 else: 319 else:
319 assert subgraph == 'default' 320 assert subgraph == 'default', 'unimplemented'
320 graph = NfaBuilder.join_subgraph( 321 graph = NfaBuilder.join_subgraph(
321 graph, transition, rule_map[transition]) 322 graph, transition, rule_map[transition])
322 graphs.append(graph) 323 graphs.append(graph)
323 graph = NfaBuilder.or_graphs(graphs) 324 graph = NfaBuilder.or_graphs(graphs)
324 rule_map[subgraph] = graph 325 rule_map[subgraph] = graph
325 # process first the subgraphs, then the default graph 326 # process first the subgraphs, then the default graph
326 for k, v in parser_state.rules.items(): 327 for k, v in parser_state.rules.items():
327 if k == 'default': continue 328 if k == 'default': continue
328 process(k, v) 329 process(k, v)
329 process('default', parser_state.rules['default']) 330 process('default', parser_state.rules['default'])
330 # build the automata 331 # build the automata
331 for rule_name, graph in rule_map.items(): 332 for rule_name, graph in rule_map.items():
332 self.__automata[rule_name] = RuleProcessor.Automata(builder, graph) 333 self.__automata[rule_name] = RuleProcessor.Automata(builder, graph)
333 self.__rule_trees[rule_name] = graph 334 self.__rule_trees[rule_name] = graph
334 # process default_action 335 # process default_action
335 default_action = parser_state.rules['default']['default_action'] 336 default_action = parser_state.rules['default']['default_action']
336 self.default_action = Action(None, default_action[1]) if default_action else None 337 self.default_action = Action(Term.empty_term(), default_action)
OLDNEW
« no previous file with comments | « tools/lexer_generator/nfa.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698