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

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

Issue 59953002: Experimental lexer generator: parse {} in regexps. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: rebased 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/automata_test.py ('k') | tools/lexer_generator/regex_lexer.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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 def __matches(self, match_func, value): 101 def __matches(self, match_func, value):
102 f = lambda acc, (k, vs): acc | vs if match_func(k, value) else acc 102 f = lambda acc, (k, vs): acc | vs if match_func(k, value) else acc
103 return reduce(f, self.__transitions.items(), set()) 103 return reduce(f, self.__transitions.items(), set())
104 104
105 def char_matches(self, value): 105 def char_matches(self, value):
106 return self.__matches(lambda k, v : k.matches_char(v), value) 106 return self.__matches(lambda k, v : k.matches_char(v), value)
107 107
108 def key_matches(self, value): 108 def key_matches(self, value):
109 return self.__matches(lambda k, v : k.matches_key(v), value) 109 return self.__matches(lambda k, v : k.matches_key(v), value)
110 110
111 def __str__(self):
112 return "NfaState(" + str(self.__node_number) + ")"
113
111 @staticmethod 114 @staticmethod
112 def gather_transition_keys(state_set): 115 def gather_transition_keys(state_set):
113 f = lambda acc, state: acc | set(state.__transitions.keys()) 116 f = lambda acc, state: acc | set(state.__transitions.keys())
114 return TransitionKey.disjoint_keys(reduce(f, state_set, set())) 117 return TransitionKey.disjoint_keys(reduce(f, state_set, set()))
115 118
116 class NfaBuilder: 119 class NfaBuilder:
117 120
118 def __init__(self): 121 def __init__(self):
119 self.__node_number = 0 122 self.__node_number = 0
120 self.__operation_map = {} 123 self.__operation_map = {}
(...skipping 25 matching lines...) Expand all
146 start.add_epsilon_transition(node) 149 start.add_epsilon_transition(node)
147 self.__patch_ends(ends, start) 150 self.__patch_ends(ends, start)
148 return (start, [start]) 151 return (start, [start])
149 152
150 def __zero_or_one(self, graph): 153 def __zero_or_one(self, graph):
151 (node, ends) = self.__process(graph[1]) 154 (node, ends) = self.__process(graph[1])
152 start = self.__new_state() 155 start = self.__new_state()
153 start.add_epsilon_transition(node) 156 start.add_epsilon_transition(node)
154 return (start, ends + [start]) 157 return (start, ends + [start])
155 158
159 def __repeat(self, graph):
160 param_min = int(graph[1])
161 param_max = int(graph[2])
162 subgraph = graph[3]
163 (start, ends) = self.__process(subgraph)
164 for i in xrange(1, param_min):
165 (start2, ends2) = self.__process(subgraph)
166 self.__patch_ends(ends, start2)
167 ends = ends2
168 if param_min == param_max:
169 return (start, ends)
170
171 midpoints = []
172 for i in xrange(param_min, param_max):
173 midpoint = self.__new_state()
174 self.__patch_ends(ends, midpoint)
175 (start2, ends) = self.__process(subgraph)
176 midpoint.add_epsilon_transition(start2)
177 midpoints.append(midpoint)
178
179 return (start, ends + midpoints)
180
156 def __cat(self, graph): 181 def __cat(self, graph):
157 (left, right) = (self.__process(graph[1]), self.__process(graph[2])) 182 (left, right) = (self.__process(graph[1]), self.__process(graph[2]))
158 self.__patch_ends(left[1], right[0]) 183 self.__patch_ends(left[1], right[0])
159 return (left[0], right[1]) 184 return (left[0], right[1])
160 185
161 def __key_state(self, key): 186 def __key_state(self, key):
162 state = self.__new_state() 187 state = self.__new_state()
163 state.add_unclosed_transition(key) 188 state.add_unclosed_transition(key)
164 return (state, [state]) 189 return (state, [state])
165 190
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 digraph finite_state_machine { 363 digraph finite_state_machine {
339 rankdir=LR; 364 rankdir=LR;
340 node [shape = circle, style=filled, bgcolor=lightgrey]; S_%s 365 node [shape = circle, style=filled, bgcolor=lightgrey]; S_%s
341 node [shape = doublecircle, style=unfilled]; S_%s 366 node [shape = doublecircle, style=unfilled]; S_%s
342 node [shape = circle]; 367 node [shape = circle];
343 %s 368 %s
344 } 369 }
345 ''' % (self.__start.node_number(), 370 ''' % (self.__start.node_number(),
346 self.__end.node_number(), 371 self.__end.node_number(),
347 "\n".join(node_content)) 372 "\n".join(node_content))
OLDNEW
« no previous file with comments | « tools/lexer_generator/automata_test.py ('k') | tools/lexer_generator/regex_lexer.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698