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

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

Issue 158823002: Experimental parser: refactor TransitionKey to use Term (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 10 matching lines...) Expand all
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 StringType, IntType 28 from types import StringType, IntType
29 29
30 class Term(object): 30 class Term(object):
31 '''A class representing a function and its arguments. 31 '''An immutable class representing a function and its arguments.
32 f(a,b,c) would be represented as ('f', a, b, c) where 32 f(a,b,c) would be represented as ('f', a, b, c) where
33 a, b, and c are strings or Terms.''' 33 a, b, and c are strings, integers or Terms.'''
34 34
35 __empty_term = None 35 __empty_term = None
36 36
37 @staticmethod 37 @staticmethod
38 def empty_term(): 38 def empty_term():
39 if Term.__empty_term == None: 39 if Term.__empty_term == None:
40 Term.__empty_term = Term('') 40 Term.__empty_term = Term('')
41 return Term.__empty_term 41 return Term.__empty_term
42 42
43 def __init__(self, name, *args): 43 def __init__(self, name, *args):
44 assert type(name) == StringType 44 assert type(name) == StringType
45 if not name: 45 if not name:
46 assert not args, 'empty term must not have args' 46 assert not args, 'empty term must not have args'
47 for v in args: 47 for v in args:
48 if type(v) == StringType: 48 if type(v) == IntType or type(v) == StringType:
49 assert v, 'string args must be non empty' 49 continue
50 else: 50 else:
51 assert isinstance(v, Term) 51 assert isinstance(v, Term)
52 self.__tuple = tuple([name] + list(args)) 52 self.__tuple = tuple([name] + list(args))
53 self.__str = None 53 self.__str = None
54 54
55 def name(self): 55 def name(self):
56 return self.__tuple[0] 56 return self.__tuple[0]
57 57
58 def args(self): 58 def args(self):
59 return self.__tuple[1:] 59 return self.__tuple[1:]
60 60
61 def __hash__(self): 61 def __hash__(self):
62 return hash(self.__tuple) 62 return hash(self.__tuple)
63 63
64 def __nonzero__(self): 64 def __nonzero__(self):
65 'true <==> self == empty_term'
65 return bool(self.__tuple[0]) 66 return bool(self.__tuple[0])
66 67
67 def __eq__(self, other): 68 def __eq__(self, other):
68 return (isinstance(other, self.__class__) and self.__tuple == other.__tuple) 69 return (isinstance(other, self.__class__) and self.__tuple == other.__tuple)
69 70
70 # TODO(dcarney): escape '(', ')' and ',' in strings 71 # TODO(dcarney): escape '(', ')' and ',' in strings
71 def __str__(self): 72 def __str__(self):
72 if self.__str == None: 73 if self.__str == None:
73 self.__str = '(%s)' % ','.join(map(str, self.__tuple)) 74 self.__str = '(%s)' % ','.join(map(str, self.__tuple))
74 return self.__str 75 return self.__str
75 76
76 def to_dot(self): 77 def to_dot(self):
77 node_ix = [0] 78 node_ix = [0]
78 node_template = 'node [label="%s"]; N_%d;' 79 node_template = 'node [label="%s"]; N_%d;'
79 edge_template = 'N_%d -> N_%d' 80 edge_template = 'N_%d -> N_%d'
80 nodes = [] 81 nodes = []
81 edges = [] 82 edges = []
82 83
83 def escape(v): 84 def escape(v): # TODO(dcarney): abstract into utilities
84 v = str(v) 85 v = str(v)
85 v = v.replace('\r', '\\\\r').replace('\t', '\\\\t').replace('\n', '\\\\n') 86 v = v.replace('\r', '\\\\r').replace('\t', '\\\\t').replace('\n', '\\\\n')
86 v = v.replace('\\', '\\\\').replace('\"', '\\\"') 87 v = v.replace('\\', '\\\\').replace('\"', '\\\"')
87 return v 88 return v
88 89
89 def process(term): 90 def process(term):
90 if isinstance(term, str): 91 if type(term) == StringType or type(term) == IntType:
91 node_ix[0] += 1 92 node_ix[0] += 1
92 nodes.append(node_template % (escape(term), node_ix[0])) 93 nodes.append(node_template % (escape(str(term)), node_ix[0]))
93 return node_ix[0] 94 return node_ix[0]
94 elif isinstance(term, Term): 95 elif isinstance(term, Term):
95 child_ixs = map(process, term.args()) 96 child_ixs = map(process, term.args())
96 node_ix[0] += 1 97 node_ix[0] += 1
97 nodes.append(node_template % (escape(term.name()), node_ix[0])) 98 nodes.append(node_template % (escape(term.name()), node_ix[0]))
98 for child_ix in child_ixs: 99 for child_ix in child_ixs:
99 edges.append(edge_template % (node_ix[0], child_ix)) 100 edges.append(edge_template % (node_ix[0], child_ix))
100 return node_ix[0] 101 return node_ix[0]
101 raise Exception 102 raise Exception
102 103
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 164
164 def __eq__(self, other): 165 def __eq__(self, other):
165 return (isinstance(other, self.__class__) and 166 return (isinstance(other, self.__class__) and
166 self.__entry_action == other.__entry_action and 167 self.__entry_action == other.__entry_action and
167 self.__match_action == other.__match_action) 168 self.__match_action == other.__match_action)
168 169
169 def __str__(self): 170 def __str__(self):
170 parts = map(lambda action : '' if not action else str(action), 171 parts = map(lambda action : '' if not action else str(action),
171 [self.__entry_action, self.__match_action]) 172 [self.__entry_action, self.__match_action])
172 return "action< %s >" % " | ".join(parts) 173 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