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

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

Issue 145723010: Experimental parser: better rule tree visualization (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 | « no previous file | tools/lexer_generator/regex_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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 52
53 def __new_state(self): 53 def __new_state(self):
54 self.__node_number += 1 54 self.__node_number += 1
55 return NfaState() 55 return NfaState()
56 56
57 def __global_end(self): 57 def __global_end(self):
58 if not self.__global_end_node: 58 if not self.__global_end_node:
59 self.__global_end_node = self.__new_state() 59 self.__global_end_node = self.__new_state()
60 return self.__global_end_node 60 return self.__global_end_node
61 61
62 def __or(self, left, right): 62 def __or(self, *trees):
63 start = self.__new_state() 63 start = self.__new_state()
64 ends = [] 64 ends = []
65 for (sub_start, sub_end) in [self.__process(left), self.__process(right)]: 65 for tree in trees:
66 (sub_start, sub_end) = self.__process(tree)
66 start.add_epsilon_transition(sub_start) 67 start.add_epsilon_transition(sub_start)
67 ends += sub_end 68 ends += sub_end
68 start.close(None) 69 start.close(None)
69 return (start, ends) 70 return (start, ends)
70 71
71 def __one_or_more(self, subtree): 72 def __one_or_more(self, subtree):
72 (start, ends) = self.__process(subtree) 73 (start, ends) = self.__process(subtree)
73 end = self.__new_state() 74 end = self.__new_state()
74 end.add_epsilon_transition(start) 75 end.add_epsilon_transition(start)
75 self.__patch_ends(ends, end) 76 self.__patch_ends(ends, end)
(...skipping 27 matching lines...) Expand all
103 # join in (param_max - param_min) optional subtrees 104 # join in (param_max - param_min) optional subtrees
104 midpoints = [] 105 midpoints = []
105 for i in xrange(param_min, param_max): 106 for i in xrange(param_min, param_max):
106 midpoint = self.__new_state() 107 midpoint = self.__new_state()
107 self.__patch_ends(ends, midpoint) 108 self.__patch_ends(ends, midpoint)
108 (start2, ends) = self.__process(subtree) 109 (start2, ends) = self.__process(subtree)
109 midpoint.add_epsilon_transition(start2) 110 midpoint.add_epsilon_transition(start2)
110 midpoints.append(midpoint) 111 midpoints.append(midpoint)
111 return (start, ends + midpoints) 112 return (start, ends + midpoints)
112 113
113 def __cat(self, left_tree, right_tree): 114 def __cat(self, *trees):
114 (left, right) = (self.__process(left_tree), self.__process(right_tree)) 115 (start, ends) = (None, None)
115 self.__patch_ends(left[1], right[0]) 116 for tree in trees:
116 return (left[0], right[1]) 117 (sub_start, sub_ends) = self.__process(tree)
118 if start == None:
119 start = sub_start
120 else:
121 assert sub_ends, "this creates unreachable nodes"
122 self.__patch_ends(ends, sub_start)
123 ends = sub_ends
124 return (start, ends)
117 125
118 def __key_state(self, key): 126 def __key_state(self, key):
119 state = self.__new_state() 127 state = self.__new_state()
120 state.add_unclosed_transition(key) 128 state.add_unclosed_transition(key)
121 return (state, [state]) 129 return (state, [state])
122 130
123 def __literal(self, char): 131 def __literal(self, chars):
132 terms = map(lambda c : Term('SINGLE_CHAR', c), chars)
133 return self.__process(self.cat_terms(terms))
134
135 def __single_char(self, char):
124 return self.__key_state( 136 return self.__key_state(
125 TransitionKey.single_char(self.__encoding, char)) 137 TransitionKey.single_char(self.__encoding, char))
126 138
127 def __class(self, subtree): 139 def __class(self, subtree):
128 return self.__key_state(TransitionKey.character_class( 140 return self.__key_state(TransitionKey.character_class(
129 self.__encoding, Term('CLASS', subtree), self.__character_classes)) 141 self.__encoding, Term('CLASS', subtree), self.__character_classes))
130 142
131 def __not_class(self, subtree): 143 def __not_class(self, subtree):
132 return self.__key_state(TransitionKey.character_class( 144 return self.__key_state(TransitionKey.character_class(
133 self.__encoding, Term('NOT_CLASS', subtree), self.__character_classes)) 145 self.__encoding, Term('NOT_CLASS', subtree), self.__character_classes))
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 @staticmethod 290 @staticmethod
279 def unique_key(name): 291 def unique_key(name):
280 return Term('UNIQUE_KEY', name) 292 return Term('UNIQUE_KEY', name)
281 293
282 @staticmethod 294 @staticmethod
283 def join_subtree(tree, subtree_name): 295 def join_subtree(tree, subtree_name):
284 return Term('JOIN', tree, subtree_name) 296 return Term('JOIN', tree, subtree_name)
285 297
286 @staticmethod 298 @staticmethod
287 def or_terms(terms): 299 def or_terms(terms):
288 return reduce(lambda acc, g: Term('OR', acc, g), terms) 300 if len(terms) == 1: return terms[0]
301 return Term('OR', *terms) if terms else Term.empty()
289 302
290 @staticmethod 303 @staticmethod
291 def cat_terms(terms): 304 def cat_terms(terms):
292 return reduce(lambda acc, g: Term('CAT', acc, g), terms) 305 if len(terms) == 1: return terms[0]
306 return Term('CAT', *terms) if terms else Term.empty()
293 307
294 __modifer_map = { 308 __modifer_map = {
295 '+': 'ONE_OR_MORE', 309 '+': 'ONE_OR_MORE',
296 '?': 'ZERO_OR_ONE', 310 '?': 'ZERO_OR_ONE',
297 '*': 'ZERO_OR_MORE', 311 '*': 'ZERO_OR_MORE',
298 } 312 }
299 313
300 @staticmethod 314 @staticmethod
301 def apply_modifier(modifier, term): 315 def apply_modifier(modifier, term):
302 return Term(NfaBuilder.__modifer_map[modifier], term) 316 return Term(NfaBuilder.__modifer_map[modifier], term)
OLDNEW
« no previous file with comments | « no previous file | tools/lexer_generator/regex_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698