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

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

Issue 132693018: Experimental parser: handling unique keys in merges (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 11 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_builder.py ('k') | tools/lexer_generator/transition_keys.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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
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': None,
90 'catch_all' : None, 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 | CATCH_ALL action''' 102 | CATCH_ALL action'''
103 precedence = RuleParser.__rule_precedence_counter 103 precedence = RuleParser.__rule_precedence_counter
104 RuleParser.__rule_precedence_counter += 1 104 RuleParser.__rule_precedence_counter += 1
105 action = p[2] 105 action = p[2]
106 (entry_action, match_action, transition) = action 106 (entry_action, match_action, transition) = action
107 if transition and not transition in self.__keyword_transitions: 107 if transition and not transition in self.__keyword_transitions:
108 assert not transition == 'default', "can't append default graph" 108 assert not transition == 'default', "can't append default graph"
109 self.__state.transitions.add(transition) 109 self.__state.transitions.add(transition)
110 rules = self.__state.rules[self.__state.current_state] 110 rules = self.__state.rules[self.__state.current_state]
111 if p[1] == 'default_action': 111 if p[1] == 'default_action':
112 assert self.__state.current_state == 'default' 112 assert self.__state.current_state == 'default'
113 assert not rules['default_action'] 113 assert not rules['default_action']
114 rules['default_action'] = action 114 rules['default_action'] = action
115 elif p[1] == 'catch_all': 115 elif p[1] == 'catch_all':
116 assert not rules['catch_all'] 116 assert p[1] not in rules['uniques']
117 rules['catch_all'] = (precedence, action) 117 rules['uniques'][p[1]] = True
118 rules['regex'].append((NfaBuilder.unique_key(p[1]), precedence, action))
118 else: 119 else:
119 regex = p[1] 120 regex = p[1]
120 rules['regex'].append((regex, precedence, action)) 121 rules['regex'].append((regex, precedence, action))
121 122
122 def p_action(self, p): 123 def p_action(self, p):
123 '''action : ACTION_OPEN maybe_identifier_action OR maybe_identifier_action O R maybe_transition ACTION_CLOSE''' 124 '''action : ACTION_OPEN maybe_identifier_action OR maybe_identifier_action O R maybe_transition ACTION_CLOSE'''
124 p[0] = (p[2], p[4], p[6]) 125 p[0] = (p[2], p[4], p[6])
125 126
126 def p_default_action(self, p): 127 def p_default_action(self, p):
127 'default_action : ACTION_OPEN identifier_action ACTION_CLOSE' 128 'default_action : ACTION_OPEN identifier_action ACTION_CLOSE'
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 if not transition: 311 if not transition:
311 pass 312 pass
312 elif transition == 'continue': 313 elif transition == 'continue':
313 assert not subgraph == 'default' 314 assert not subgraph == 'default'
314 graph = NfaBuilder.add_continue(graph) 315 graph = NfaBuilder.add_continue(graph)
315 else: 316 else:
316 assert subgraph == 'default' 317 assert subgraph == 'default'
317 graph = NfaBuilder.join_subgraph( 318 graph = NfaBuilder.join_subgraph(
318 graph, transition, rule_map[transition]) 319 graph, transition, rule_map[transition])
319 graphs.append(graph) 320 graphs.append(graph)
320 if v['catch_all']:
321 (precedence, catch_all) = v['catch_all']
322 assert catch_all == (None, None, 'continue'), "unimplemented"
323 graphs.append(NfaBuilder.add_continue(NfaBuilder.catch_all()))
324 graph = NfaBuilder.or_graphs(graphs) 321 graph = NfaBuilder.or_graphs(graphs)
325 rule_map[subgraph] = graph 322 rule_map[subgraph] = graph
326 # process first the subgraphs, then the default graph 323 # process first the subgraphs, then the default graph
327 for k, v in parser_state.rules.items(): 324 for k, v in parser_state.rules.items():
328 if k == 'default': continue 325 if k == 'default': continue
329 process(k, v) 326 process(k, v)
330 process('default', parser_state.rules['default']) 327 process('default', parser_state.rules['default'])
331 # build the automata 328 # build the automata
332 for rule_name, graph in rule_map.items(): 329 for rule_name, graph in rule_map.items():
333 self.__automata[rule_name] = RuleProcessor.Automata(builder, graph) 330 self.__automata[rule_name] = RuleProcessor.Automata(builder, graph)
334 self.__rule_trees[rule_name] = graph 331 self.__rule_trees[rule_name] = graph
335 # process default_action 332 # process default_action
336 default_action = parser_state.rules['default']['default_action'] 333 default_action = parser_state.rules['default']['default_action']
337 self.default_action = Action(None, default_action[1]) if default_action else None 334 self.default_action = Action(None, default_action[1]) if default_action else None
OLDNEW
« no previous file with comments | « tools/lexer_generator/nfa_builder.py ('k') | tools/lexer_generator/transition_keys.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698