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

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

Issue 159853009: Experimental parser: split off dot processing (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/automata_test.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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 67
68 def __eq__(self, other): 68 def __eq__(self, other):
69 return (isinstance(other, self.__class__) and self.__tuple == other.__tuple) 69 return (isinstance(other, self.__class__) and self.__tuple == other.__tuple)
70 70
71 # TODO(dcarney): escape '(', ')' and ',' in strings 71 # TODO(dcarney): escape '(', ')' and ',' in strings
72 def __str__(self): 72 def __str__(self):
73 if self.__str == None: 73 if self.__str == None:
74 self.__str = '(%s)' % ','.join(map(str, self.__tuple)) 74 self.__str = '(%s)' % ','.join(map(str, self.__tuple))
75 return self.__str 75 return self.__str
76 76
77 def to_dot(self):
78 node_ix = [0]
79 node_template = 'node [label="%s"]; N_%d;'
80 edge_template = 'N_%d -> N_%d'
81 nodes = []
82 edges = []
83
84 def escape(v): # TODO(dcarney): abstract into utilities
85 v = str(v)
86 v = v.replace('\r', '\\\\r').replace('\t', '\\\\t').replace('\n', '\\\\n')
87 v = v.replace('\\', '\\\\').replace('\"', '\\\"')
88 return v
89
90 def process(term):
91 if type(term) == StringType or type(term) == IntType:
92 node_ix[0] += 1
93 nodes.append(node_template % (escape(str(term)), node_ix[0]))
94 return node_ix[0]
95 elif isinstance(term, Term):
96 child_ixs = map(process, term.args())
97 node_ix[0] += 1
98 nodes.append(node_template % (escape(term.name()), node_ix[0]))
99 for child_ix in child_ixs:
100 edges.append(edge_template % (node_ix[0], child_ix))
101 return node_ix[0]
102 raise Exception
103
104 process(self)
105 return 'digraph { %s %s }' % ('\n'.join(nodes), '\n'.join(edges))
106
107 class Action(object): 77 class Action(object):
108 78
109 __empty_action = None 79 __empty_action = None
110 80
111 @staticmethod 81 @staticmethod
112 def empty_action(): 82 def empty_action():
113 if Action.__empty_action == None: 83 if Action.__empty_action == None:
114 Action.__empty_action = Action(Term.empty_term(), Term.empty_term()) 84 Action.__empty_action = Action(Term.empty_term(), Term.empty_term())
115 return Action.__empty_action 85 return Action.__empty_action
116 86
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 134
165 def __eq__(self, other): 135 def __eq__(self, other):
166 return (isinstance(other, self.__class__) and 136 return (isinstance(other, self.__class__) and
167 self.__entry_action == other.__entry_action and 137 self.__entry_action == other.__entry_action and
168 self.__match_action == other.__match_action) 138 self.__match_action == other.__match_action)
169 139
170 def __str__(self): 140 def __str__(self):
171 parts = map(lambda action : '' if not action else str(action), 141 parts = map(lambda action : '' if not action else str(action),
172 [self.__entry_action, self.__match_action]) 142 [self.__entry_action, self.__match_action])
173 return "action< %s >" % " | ".join(parts) 143 return "action< %s >" % " | ".join(parts)
OLDNEW
« no previous file with comments | « no previous file | tools/lexer_generator/automata_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698