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

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

Issue 71313002: Experimental parser: action 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/automata_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 12 matching lines...) Expand all
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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 pass 33
34 def __init__(self, type, data = None, precedence = -1):
35 assert type
36 self.__type = type
37 self.__data = data
38 self.__precedence = precedence
39
40 def type(self):
41 return self.__type
42
43 def data(self):
44 return self.__data
45
46 def precedence(self):
47 return self.__precedence
48
49 def __hash__(self):
50 return hash((self.__type, self.__data))
51
52 def __eq__(self, other):
53 return (isinstance(other, self.__class__) and
54 self.__type == other.__type and
55 self.__data == other.__data)
56
57 def __str__(self):
58 if not self.__data:
59 return "action<%s>" % self.__type
60 return "action<%s, %s>" % (self.__type, self.__data)
34 61
35 class AutomatonState(object): 62 class AutomatonState(object):
36 63
37 def __init__(self, node_number): 64 __node_number_counter = 0
38 self.__node_number = node_number 65
66 def __init__(self):
67 self.__node_number = AutomatonState.__node_number_counter
68 AutomatonState.__node_number_counter += 1
69
70 def __hash__(self):
71 return hash(self.__node_number)
72
73 def __eq__(self, other):
74 return (isinstance(other, self.__class__) and
75 self.__node_number == other.__node_number)
39 76
40 def node_number(self): 77 def node_number(self):
41 return self.__node_number 78 return self.__node_number
42 79
43 def __str__(self): 80 def __str__(self):
44 return "%s(%d)" % (type(self), self.node_number()) 81 return "%s(%d)" % (type(self), self.node_number())
45 82
46 __pass = lambda x : True 83 __pass = lambda x : True
47 84
48 def key_iter(self, key_filter = __pass): 85 def key_iter(self, key_filter = __pass):
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 visited |= edge 121 visited |= edge
85 edge = next_edge - visited 122 edge = next_edge - visited
86 return visit_state 123 return visit_state
87 124
88 def visit_all_states(self, visitor, visit_state = None, state_iter = None): 125 def visit_all_states(self, visitor, visit_state = None, state_iter = None):
89 return self.visit_states(self.start_set(), visitor, visit_state, state_iter) 126 return self.visit_states(self.start_set(), visitor, visit_state, state_iter)
90 127
91 def to_dot(self): 128 def to_dot(self):
92 129
93 def escape(v): 130 def escape(v):
94 v = str(v).replace('\r', '\\\\r') 131 v = str(v)
95 v = str(v).replace('\t', '\\\\t') 132 v = v.replace('\r', '\\\\r').replace('\t', '\\\\t').replace('\n', '\\\\n')
96 v = str(v).replace('\n', '\\\\n') 133 v = v.replace('\\', '\\\\').replace('\"', '\\\"')
97 v = str(v).replace('\\', '\\\\')
98 v = str(v).replace('\"', '\\\"')
99 return v 134 return v
100 135
101 def f(node, (node_content, edge_content)): 136 def f(node, (node_content, edge_content)):
102 if node.action(): 137 if node.action():
103 action_text = node.action()[1].split('\n')[0] 138 action = node.action()
139 if action.type() == 'code':
140 # assert action.data()
141 action_text = action.data()
142 else:
143 action_text = action.type()
144 action_text = escape(action_text)
104 node_content.append(' S_l%s[shape = box, label="%s"];' % 145 node_content.append(' S_l%s[shape = box, label="%s"];' %
105 (node.node_number(), action_text)) 146 (node.node_number(), action_text))
106 node_content.append(' S_%s -> S_l%s [arrowhead = none];' % 147 node_content.append(' S_%s -> S_l%s [arrowhead = none];' %
107 (node.node_number(), node.node_number())) 148 (node.node_number(), node.node_number()))
108 for key, state in node.key_state_iter(): 149 for key, state in node.key_state_iter():
109 if key == TransitionKey.epsilon(): 150 if key == TransitionKey.epsilon():
110 key = "&epsilon;" 151 key = "&epsilon;"
111 edge_content.append(" S_%s -> S_%s [ label = \"%s\" ];" % ( 152 edge_content.append(" S_%s -> S_%s [ label = \"%s\" ];" % (
112 node.node_number(), state.node_number(), escape(key))) 153 node.node_number(), state.node_number(), escape(key)))
113 return (node_content, edge_content) 154 return (node_content, edge_content)
(...skipping 18 matching lines...) Expand all
132 node [shape = doublecircle, style=unfilled]; %s 173 node [shape = doublecircle, style=unfilled]; %s
133 node [shape = circle]; 174 node [shape = circle];
134 %s 175 %s
135 %s 176 %s
136 } 177 }
137 ''' % (start_shape, 178 ''' % (start_shape,
138 start_number, 179 start_number,
139 " ".join(terminals), 180 " ".join(terminals),
140 "\n".join(edge_content), 181 "\n".join(edge_content),
141 "\n".join(node_content)) 182 "\n".join(node_content))
OLDNEW
« no previous file with comments | « tools/lexer_generator/automata_test.py ('k') | tools/lexer_generator/dfa.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698