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

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

Issue 137883006: Experimental parser: use Terms instead of tuples (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/action.py ('k') | tools/lexer_generator/code_generator.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
11 # with the distribution. 11 # with the distribution.
12 # * Neither the name of Google Inc. nor the names of its 12 # * Neither the name of Google Inc. nor the names of its
13 # contributors may be used to endorse or promote products derived 13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission. 14 # from this software without specific prior written permission.
15 # 15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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, StringType 28 from types import TupleType, ListType
29 from itertools import chain 29 from itertools import chain
30 from action import Term, Action
30 from transition_keys import TransitionKey 31 from transition_keys import TransitionKey
31 32
32 class Term(object):
33 '''A class representing a function and its arguments.
34 f(a,b,c) would be represented as ('f', a, b, c) where
35 a, b, and c are strings or Terms.'''
36
37 __empty_term = None
38
39 @staticmethod
40 def empty_term():
41 if Term.__empty_term == None:
42 Term.__empty_term = Term('')
43 return Term.__empty_term
44
45 @staticmethod
46 def __verify_string(v):
47 assert (not ',' in v) and (not '(' in v)
48
49 def __init__(self, name, *args):
50 assert type(name) == StringType
51 self.__verify_string(name)
52 if not name:
53 assert not args, 'empty term must not have args'
54 for v in args:
55 assert v, 'args must be non empty'
56 if type(v) == StringType:
57 self.__verify_string(v)
58 else:
59 assert isinstance(v, self.__class__)
60 self.__tuple = tuple([name] + list(args))
61 self.__str = None
62
63 def name(self):
64 return self.__tuple[0]
65
66 def args(self):
67 return self.__tuple[1:]
68
69 def __hash__(self):
70 return hash(self.__tuple)
71
72 def __nonzero__(self):
73 return bool(self.__tuple[0])
74
75 def __eq__(self, other):
76 return (isinstance(other, self.__class__) and self.__tuple == other.__tuple)
77
78 def __str__(self):
79 if self.__str == None:
80 self.__str = '(%s)' % ','.join(map(str, self.__tuple))
81 return self.__str
82
83 class Action(object):
84
85 __empty_action = None
86
87 @staticmethod
88 def empty_action():
89 if Action.__empty_action == None:
90 Action.__empty_action = Action(Term.empty_term(), Term.empty_term())
91 return Action.__empty_action
92
93 @staticmethod
94 def dominant_action(state_set):
95 action = Action.empty_action()
96 for state in state_set:
97 if not state.action():
98 continue
99 if not action:
100 action = state.action()
101 continue
102 if state.action().precedence() == action.precedence():
103 assert state.action() == action
104 elif state.action().precedence() < action.precedence():
105 action = state.action()
106 return action
107
108 def __init__(self, entry_action, match_action, precedence = -1):
109 for action in [entry_action, match_action]:
110 assert isinstance(action, Term)
111 self.__entry_action = entry_action
112 self.__match_action = match_action
113 self.__precedence = precedence
114
115 def entry_action(self):
116 return self.__entry_action
117
118 def match_action(self):
119 return self.__match_action
120
121 def precedence(self):
122 return self.__precedence
123
124 def __nonzero__(self):
125 return bool(self.__entry_action) or bool(self.__match_action)
126
127 def __hash__(self):
128 return hash((self.__entry_action, self.__match_action))
129
130 def __eq__(self, other):
131 return (isinstance(other, self.__class__) and
132 self.__entry_action == other.__entry_action and
133 self.__match_action == other.__match_action)
134
135 def __str__(self):
136 parts = []
137 for action in [self.__entry_action, self.__match_action]:
138 parts.append('' if not action else str(action))
139 return "action< %s >" % " | ".join(parts)
140
141 class AutomatonState(object): 33 class AutomatonState(object):
34 '''A base class for dfa and nfa states. Immutable'''
142 35
143 __node_number_counter = 0 36 __node_number_counter = 0
144 37
145 def __init__(self): 38 def __init__(self):
146 self.__node_number = AutomatonState.__node_number_counter 39 self.__node_number = AutomatonState.__node_number_counter
147 AutomatonState.__node_number_counter += 1 40 AutomatonState.__node_number_counter += 1
148 41
149 def __hash__(self): 42 def __hash__(self):
150 return hash(self.__node_number) 43 return hash(self.__node_number)
151 44
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 node [shape = doublecircle, style=unfilled]; %s 193 node [shape = doublecircle, style=unfilled]; %s
301 node [shape = circle]; 194 node [shape = circle];
302 %s 195 %s
303 %s 196 %s
304 } 197 }
305 ''' % (start_shape, 198 ''' % (start_shape,
306 start_number, 199 start_number,
307 " ".join(terminals), 200 " ".join(terminals),
308 "\n".join(edge_content), 201 "\n".join(edge_content),
309 "\n".join(node_content)) 202 "\n".join(node_content))
OLDNEW
« no previous file with comments | « tools/lexer_generator/action.py ('k') | tools/lexer_generator/code_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698