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

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

Issue 141293003: Experimental parser: visualize rule trees. (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/generator.py ('k') | 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 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 233
234 def nfa(self, graph): 234 def nfa(self, graph):
235 (start, end, nodes_created) = self.__nfa(graph) 235 (start, end, nodes_created) = self.__nfa(graph)
236 end.close(None) 236 end.close(None)
237 self.__compute_epsilon_closures(start) 237 self.__compute_epsilon_closures(start)
238 f = lambda node, state: self.__replace_catch_all(self.__encoding, node) 238 f = lambda node, state: self.__replace_catch_all(self.__encoding, node)
239 Automaton.visit_states(set([start]), f) 239 Automaton.visit_states(set([start]), f)
240 return Nfa(self.__encoding, start, end, nodes_created) 240 return Nfa(self.__encoding, start, end, nodes_created)
241 241
242 @staticmethod 242 @staticmethod
243 def rule_tree_dot(graph):
244 node_ix = [0]
245 node_template = 'node [label="%s"]; N_%d;'
246 edge_template = 'N_%d -> N_%d'
247 nodes = []
248 edges = []
249
250 def escape(v):
251 v = str(v)
252 v = v.replace('\r', '\\\\r').replace('\t', '\\\\t').replace('\n', '\\\\n')
253 v = v.replace('\\', '\\\\').replace('\"', '\\\"')
254 return v
255
256 def process_thing(thing):
257 if isinstance(thing, str):
258 node_ix[0] += 1
259 nodes.append(node_template % (escape(thing), node_ix[0]))
260 return node_ix[0]
261 if isinstance(thing, tuple):
262 child_ixs = map(process_thing, list(thing)[1:])
263 node_ix[0] += 1
264 nodes.append(node_template % (escape(thing[0]), node_ix[0]))
265 for child_ix in child_ixs:
266 edges.append(edge_template % (node_ix[0], child_ix))
267 return node_ix[0]
268 if isinstance(thing, Action):
269 node_ix[0] += 1
270 nodes.append(node_template % (str(thing), node_ix[0]))
271 return node_ix[0]
272 raise Exception
273
274 process_thing(graph)
275 return 'digraph { %s %s }' % ('\n'.join(nodes), '\n'.join(edges))
276
277 @staticmethod
243 def add_action(graph, action): 278 def add_action(graph, action):
244 return ('ACTION', graph, action) 279 return ('ACTION', graph, action)
245 280
246 @staticmethod 281 @staticmethod
247 def add_continue(graph): 282 def add_continue(graph):
248 return ('CONTINUE', graph) 283 return ('CONTINUE', graph)
249 284
250 @staticmethod 285 @staticmethod
251 def catch_all(): 286 def catch_all():
252 return ('CATCH_ALL',) 287 return ('CATCH_ALL',)
(...skipping 16 matching lines...) Expand all
269 304
270 __modifer_map = { 305 __modifer_map = {
271 '+': 'ONE_OR_MORE', 306 '+': 'ONE_OR_MORE',
272 '?': 'ZERO_OR_ONE', 307 '?': 'ZERO_OR_ONE',
273 '*': 'ZERO_OR_MORE', 308 '*': 'ZERO_OR_MORE',
274 } 309 }
275 310
276 @staticmethod 311 @staticmethod
277 def apply_modifier(modifier, graph): 312 def apply_modifier(modifier, graph):
278 return (NfaBuilder.__modifer_map[modifier], graph) 313 return (NfaBuilder.__modifer_map[modifier], graph)
OLDNEW
« no previous file with comments | « tools/lexer_generator/generator.py ('k') | tools/lexer_generator/rule_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698