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

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

Issue 171713005: Experimental parser: add backtracking (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 | « tools/lexer_generator/dfa_optimizer.py ('k') | tools/lexer_generator/nfa_builder.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 2014 the V8 project authors. All rights reserved. 1 # Copyright 2014 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 for child_ix in child_ixs: 66 for child_ix in child_ixs:
67 edges.append(edge_template % (node_ix[0], child_ix)) 67 edges.append(edge_template % (node_ix[0], child_ix))
68 return node_ix[0] 68 return node_ix[0]
69 raise Exception 69 raise Exception
70 70
71 process(term) 71 process(term)
72 return 'digraph { %s %s }' % ('\n'.join(nodes), '\n'.join(edges)) 72 return 'digraph { %s %s }' % ('\n'.join(nodes), '\n'.join(edges))
73 73
74 def automaton_to_dot(automaton, merge = False): 74 def automaton_to_dot(automaton, merge = False):
75 terminal_set = automaton.terminal_set() 75 terminal_set = automaton.terminal_set()
76 to_skip = set([]) 76 to_skip = set()
77 # get a replacement action for a state's omega transition 77 # get a replacement action for a state's omega transition
78 def analyse_omega_chain(state): 78 def analyse_omega_chain(state):
79 omega_chain = list(state.omega_chain_iter()) 79 omega_chain = list(state.omega_chain_iter())
80 if len(omega_chain) == 1: 80 if len(omega_chain) == 1:
81 omega_state = omega_chain[0][0] 81 omega_state = omega_chain[0][0]
82 # Handle match nodes 82 # Handle match nodes
83 if omega_chain[0][1] == 0: 83 if omega_chain[0][1] == 0:
84 if omega_state in terminal_set: 84 if omega_state in terminal_set:
85 to_skip.add(omega_state) # don't draw omega transition state 85 to_skip.add(omega_state) # don't draw omega transition state
86 terminal_set.add(state) 86 terminal_set.add(state)
87 return omega_state.action() 87 return omega_state.action()
88 elif len(omega_chain) == 2: 88 elif len(omega_chain) == 2:
89 first_action = omega_chain[0][0].action().name() 89 first_action = omega_chain[0][0].action().name()
90 if ((first_action == 'store_token' or 90 if ((first_action == 'store_token' or
91 first_action == 'store_harmony_token') and 91 first_action == 'store_harmony_token') and
92 omega_chain[1][0].action().name() == 'no_op'): 92 omega_chain[1][0].action().name() == 'no_op'):
93 if (omega_chain[0][0].action().term().args() == 93 if (omega_chain[0][0].action().term().args() ==
94 omega_chain[1][0].action().term().args()): 94 omega_chain[1][0].action().term().args()):
95 return Action(Term('goto', omega_chain[0][0].node_number()), 0) 95 return Action(Term('goto', omega_chain[0][0].node_number()), 0)
96 else: 96 else:
97 to_skip.add(omega_chain[0][0]) # don't draw first transition 97 to_skip.add(omega_chain[0][0]) # don't draw first transition
98 return Action(Term('chain', 98 return Action(Term('chain',
99 omega_chain[0][0].action().term(), 99 omega_chain[0][0].action().term(),
100 Term('goto', omega_chain[1][0].node_number())), 0) 100 Term('goto', omega_chain[1][0].node_number())), 0)
101 return Action.empty_action() 101 return Action.empty_action()
102 # draw state 102 # draw state
103 skipped = set([]) 103 skipped = set()
104 drawn = set([]) 104 drawn = set()
105 def draw(node, (node_content, edge_content)): 105 def draw(node, (node_content, edge_content)):
106 if node in to_skip: # skip match nodes 106 if node in to_skip: # skip match nodes
107 skipped.add(node) 107 skipped.add(node)
108 return (node_content, edge_content) 108 return (node_content, edge_content)
109 drawn.add(node) 109 drawn.add(node)
110 omega_action = analyse_omega_chain(node) if merge else Action.empty_action() 110 omega_action = analyse_omega_chain(node) if merge else Action.empty_action()
111 node_action = node.action() 111 node_action = node.action()
112 if node_action.name() == 'no_op': 112 if node_action.name() == 'no_op':
113 node_action = Action.empty_action() 113 node_action = Action.empty_action()
114 if node_action or omega_action: 114 if node_action or omega_action:
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 node [shape = doublecircle, style=unfilled]; %s 156 node [shape = doublecircle, style=unfilled]; %s
157 node [shape = circle]; 157 node [shape = circle];
158 %s 158 %s
159 %s 159 %s
160 } 160 }
161 ''' % (start_shape, 161 ''' % (start_shape,
162 start_number, 162 start_number,
163 " ".join(terminals), 163 " ".join(terminals),
164 "\n".join(edge_content), 164 "\n".join(edge_content),
165 "\n".join(node_content)) 165 "\n".join(node_content))
OLDNEW
« no previous file with comments | « tools/lexer_generator/dfa_optimizer.py ('k') | tools/lexer_generator/nfa_builder.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698