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

Side by Side Diff: tools/lexer_generator/nfa.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 | « no previous file | tools/lexer_generator/rule_parser.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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 def gather_transition_keys(state_set): 115 def gather_transition_keys(state_set):
116 f = lambda acc, state: acc | set(state.__transitions.keys()) 116 f = lambda acc, state: acc | set(state.__transitions.keys())
117 return TransitionKey.disjoint_keys(reduce(f, state_set, set())) 117 return TransitionKey.disjoint_keys(reduce(f, state_set, set()))
118 118
119 class NfaBuilder: 119 class NfaBuilder:
120 120
121 def __init__(self): 121 def __init__(self):
122 self.__node_number = 0 122 self.__node_number = 0
123 self.__operation_map = {} 123 self.__operation_map = {}
124 self.__members = getmembers(self) 124 self.__members = getmembers(self)
125 self.__character_classes = {}
126
127 def set_character_classes(self, classes):
128 self.__character_classes = classes
125 129
126 def __new_state(self): 130 def __new_state(self):
127 self.__node_number += 1 131 self.__node_number += 1
128 return NfaState(self.__node_number - 1) 132 return NfaState(self.__node_number - 1)
129 133
130 def __or(self, graph): 134 def __or(self, graph):
131 start = self.__new_state() 135 start = self.__new_state()
132 ends = [] 136 ends = []
133 for x in [self.__process(graph[1]), self.__process(graph[2])]: 137 for x in [self.__process(graph[1]), self.__process(graph[2])]:
134 start.add_epsilon_transition(x[0]) 138 start.add_epsilon_transition(x[0])
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 189
186 def __key_state(self, key): 190 def __key_state(self, key):
187 state = self.__new_state() 191 state = self.__new_state()
188 state.add_unclosed_transition(key) 192 state.add_unclosed_transition(key)
189 return (state, [state]) 193 return (state, [state])
190 194
191 def __literal(self, graph): 195 def __literal(self, graph):
192 return self.__key_state(TransitionKey.single_char(graph[1])) 196 return self.__key_state(TransitionKey.single_char(graph[1]))
193 197
194 def __class(self, graph): 198 def __class(self, graph):
195 return self.__key_state(TransitionKey.character_class(False, graph[1])) 199 return self.__key_state(
200 TransitionKey.character_class(graph, self.__character_classes))
196 201
197 def __not_class(self, graph): 202 def __not_class(self, graph):
198 return self.__key_state(TransitionKey.character_class(True, graph[1])) 203 return self.__key_state(
204 TransitionKey.character_class(graph, self.__character_classes))
199 205
200 def __any(self, graph): 206 def __any(self, graph):
201 return self.__key_state(TransitionKey.any()) 207 return self.__key_state(TransitionKey.any())
202 208
203 def __action(self, graph): 209 def __action(self, graph):
204 result = self.__process(graph[1]) 210 result = self.__process(graph[1])
205 for end in result[1]: 211 for end in result[1]:
206 end.set_transition_action(graph[2]) 212 end.set_transition_action(graph[2])
207 return result 213 return result
208 214
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 digraph finite_state_machine { 369 digraph finite_state_machine {
364 rankdir=LR; 370 rankdir=LR;
365 node [shape = circle, style=filled, bgcolor=lightgrey]; S_%s 371 node [shape = circle, style=filled, bgcolor=lightgrey]; S_%s
366 node [shape = doublecircle, style=unfilled]; S_%s 372 node [shape = doublecircle, style=unfilled]; S_%s
367 node [shape = circle]; 373 node [shape = circle];
368 %s 374 %s
369 } 375 }
370 ''' % (self.__start.node_number(), 376 ''' % (self.__start.node_number(),
371 self.__end.node_number(), 377 self.__end.node_number(),
372 "\n".join(node_content)) 378 "\n".join(node_content))
OLDNEW
« no previous file with comments | « no previous file | tools/lexer_generator/rule_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698