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

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

Issue 62103017: Experimental parser: rule grammar refactor (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: 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/action_test.py ('k') | tools/lexer_generator/dfa.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 13 matching lines...) Expand all
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 from types import TupleType, ListType 28 from types import TupleType, ListType
29 from itertools import chain 29 from itertools import chain
30 from transition_keys import TransitionKey 30 from transition_keys import TransitionKey
31 31
32 class Action(object): 32 class Action(object):
33 33
34 def __init__(self, type, data = None, precedence = -1): 34 def __init__(self, entry_action, match_action = None, precedence = -1):
35 assert type 35 assert type
36 self.__type = type 36 self.__entry_action = entry_action
37 self.__data = data 37 self.__match_action = match_action
38 self.__precedence = precedence 38 self.__precedence = precedence
39 39
40 def type(self): 40 def entry_action(self):
41 return self.__type 41 return self.__entry_action
42 42
43 def data(self): 43 def match_action(self):
44 return self.__data 44 return self.__match_action
45 45
46 def precedence(self): 46 def precedence(self):
47 return self.__precedence 47 return self.__precedence
48 48
49 def __hash__(self): 49 def __hash__(self):
50 return hash((self.__type, self.__data)) 50 return hash((self.__entry_action, self.__match_action))
51 51
52 def __eq__(self, other): 52 def __eq__(self, other):
53 return (isinstance(other, self.__class__) and 53 return (isinstance(other, self.__class__) and
54 self.__type == other.__type and 54 self.__entry_action == other.__entry_action and
55 self.__data == other.__data) 55 self.__match_action == other.__match_action)
56 56
57 def __str__(self): 57 def __str__(self):
58 if not self.__data: 58 return "action<%s, %s>" % (self.__entry_action, self.__match_action)
59 return "action<%s>" % self.__type
60 return "action<%s, %s>" % (self.__type, self.__data)
61 59
62 class AutomatonState(object): 60 class AutomatonState(object):
63 61
64 __node_number_counter = 0 62 __node_number_counter = 0
65 63
66 def __init__(self): 64 def __init__(self):
67 self.__node_number = AutomatonState.__node_number_counter 65 self.__node_number = AutomatonState.__node_number_counter
68 AutomatonState.__node_number_counter += 1 66 AutomatonState.__node_number_counter += 1
69 67
70 def __hash__(self): 68 def __hash__(self):
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 def to_dot(self): 126 def to_dot(self):
129 127
130 def escape(v): 128 def escape(v):
131 v = str(v) 129 v = str(v)
132 v = v.replace('\r', '\\\\r').replace('\t', '\\\\t').replace('\n', '\\\\n') 130 v = v.replace('\r', '\\\\r').replace('\t', '\\\\t').replace('\n', '\\\\n')
133 v = v.replace('\\', '\\\\').replace('\"', '\\\"') 131 v = v.replace('\\', '\\\\').replace('\"', '\\\"')
134 return v 132 return v
135 133
136 def f(node, (node_content, edge_content)): 134 def f(node, (node_content, edge_content)):
137 if node.action(): 135 if node.action():
138 action = node.action() 136 action_text = escape(node.action())
139 if action.type() == 'code':
140 action_text = action.data()
141 elif action.type() == 'push_token':
142 action_text = "token(" + action.data() + ")"
143 else:
144 action_text = action.type()
145 action_text = escape(action_text)
146 node_content.append(' S_l%s[shape = box, label="%s"];' % 137 node_content.append(' S_l%s[shape = box, label="%s"];' %
147 (node.node_number(), action_text)) 138 (node.node_number(), action_text))
148 node_content.append(' S_%s -> S_l%s [arrowhead = none];' % 139 node_content.append(' S_%s -> S_l%s [arrowhead = none];' %
149 (node.node_number(), node.node_number())) 140 (node.node_number(), node.node_number()))
150 for key, state in node.key_state_iter(): 141 for key, state in node.key_state_iter():
151 if key == TransitionKey.epsilon(): 142 if key == TransitionKey.epsilon():
152 key = "&epsilon;" 143 key = "&epsilon;"
153 edge_content.append(" S_%s -> S_%s [ label = \"%s\" ];" % ( 144 edge_content.append(" S_%s -> S_%s [ label = \"%s\" ];" % (
154 node.node_number(), state.node_number(), escape(key))) 145 node.node_number(), state.node_number(), escape(key)))
155 return (node_content, edge_content) 146 return (node_content, edge_content)
(...skipping 18 matching lines...) Expand all
174 node [shape = doublecircle, style=unfilled]; %s 165 node [shape = doublecircle, style=unfilled]; %s
175 node [shape = circle]; 166 node [shape = circle];
176 %s 167 %s
177 %s 168 %s
178 } 169 }
179 ''' % (start_shape, 170 ''' % (start_shape,
180 start_number, 171 start_number,
181 " ".join(terminals), 172 " ".join(terminals),
182 "\n".join(edge_content), 173 "\n".join(edge_content),
183 "\n".join(node_content)) 174 "\n".join(node_content))
OLDNEW
« no previous file with comments | « tools/lexer_generator/action_test.py ('k') | tools/lexer_generator/dfa.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698