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

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

Issue 152823002: Experimental parser: use sentinal values for actions (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/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
(...skipping 16 matching lines...) Expand all
27 27
28 from types import TupleType, ListType, StringType 28 from types import TupleType, ListType, StringType
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 Term(object): 32 class Term(object):
33 '''A class representing a function and its arguments. 33 '''A class representing a function and its arguments.
34 f(a,b,c) would be represented as ('f', a, b, c) where 34 f(a,b,c) would be represented as ('f', a, b, c) where
35 a, b, and c are strings or Terms.''' 35 a, b, and c are strings or Terms.'''
36 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
37 @staticmethod 45 @staticmethod
38 def __verify_string(v): 46 def __verify_string(v):
39 assert (not ',' in v) and (not '(' in v) 47 assert (not ',' in v) and (not '(' in v)
40 48
41 def __init__(self, name, *args): 49 def __init__(self, name, *args):
42 assert type(name) == StringType 50 assert type(name) == StringType
43 self.__verify_string(name) 51 self.__verify_string(name)
52 if not name:
53 assert not args, 'empty term must not have args'
44 for v in args: 54 for v in args:
55 assert v, 'args must be non empty'
45 if type(v) == StringType: 56 if type(v) == StringType:
46 self.__verify_string(v) 57 self.__verify_string(v)
47 else: 58 else:
48 assert isinstance(v, self.__class__) 59 assert isinstance(v, self.__class__)
49 self.__tuple = tuple([name] + list(args)) 60 self.__tuple = tuple([name] + list(args))
50 self.__str = None 61 self.__str = None
51 62
52 def name(self): 63 def name(self):
53 return self.__tuple[0] 64 return self.__tuple[0]
54 65
55 def args(self): 66 def args(self):
56 return self.__tuple[1:] 67 return self.__tuple[1:]
57 68
58 def __hash__(self): 69 def __hash__(self):
59 return hash(self.__tuple) 70 return hash(self.__tuple)
60 71
72 def __nonzero__(self):
73 return bool(self.__tuple[0])
74
61 def __eq__(self, other): 75 def __eq__(self, other):
62 return (isinstance(other, self.__class__) and 76 return (isinstance(other, self.__class__) and self.__tuple == other.__tuple)
63 self.__tuple == other.__tuple)
64 77
65 def __str__(self): 78 def __str__(self):
66 if self.__str == None: 79 if self.__str == None:
67 self.__str = '(%s)' % ','.join(map(str, self.__tuple)) 80 self.__str = '(%s)' % ','.join(map(str, self.__tuple))
68 return self.__str 81 return self.__str
69 82
70 class Action(object): 83 class Action(object):
71 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
72 @staticmethod 93 @staticmethod
73 def dominant_action(state_set): 94 def dominant_action(state_set):
74 action = None 95 action = Action.empty_action()
75 for state in state_set: 96 for state in state_set:
76 if not state.action(): 97 if not state.action():
77 continue 98 continue
78 if not action: 99 if not action:
79 action = state.action() 100 action = state.action()
80 continue 101 continue
81 if state.action().precedence() == action.precedence(): 102 if state.action().precedence() == action.precedence():
82 assert state.action() == action 103 assert state.action() == action
83 elif state.action().precedence() < action.precedence(): 104 elif state.action().precedence() < action.precedence():
84 action = state.action() 105 action = state.action()
85 return action 106 return action
86 107
87 def __init__(self, entry_action, match_action, precedence = -1): 108 def __init__(self, entry_action, match_action, precedence = -1):
88 for action in [entry_action, match_action]: 109 for action in [entry_action, match_action]:
89 if action == None:
90 continue
91 assert isinstance(action, Term) 110 assert isinstance(action, Term)
92 assert entry_action or match_action
93 self.__entry_action = entry_action 111 self.__entry_action = entry_action
94 self.__match_action = match_action 112 self.__match_action = match_action
95 self.__precedence = precedence 113 self.__precedence = precedence
96 114
97 def entry_action(self): 115 def entry_action(self):
98 return self.__entry_action 116 return self.__entry_action
99 117
100 def match_action(self): 118 def match_action(self):
101 return self.__match_action 119 return self.__match_action
102 120
103 def precedence(self): 121 def precedence(self):
104 return self.__precedence 122 return self.__precedence
105 123
124 def __nonzero__(self):
125 return bool(self.__entry_action) or bool(self.__match_action)
126
106 def __hash__(self): 127 def __hash__(self):
107 return hash((self.__entry_action, self.__match_action)) 128 return hash((self.__entry_action, self.__match_action))
108 129
109 def __eq__(self, other): 130 def __eq__(self, other):
110 return (isinstance(other, self.__class__) and 131 return (isinstance(other, self.__class__) and
111 self.__entry_action == other.__entry_action and 132 self.__entry_action == other.__entry_action and
112 self.__match_action == other.__match_action) 133 self.__match_action == other.__match_action)
113 134
114 def __str__(self): 135 def __str__(self):
115 parts = [] 136 parts = []
116 for action in [self.__entry_action, self.__match_action]: 137 for action in [self.__entry_action, self.__match_action]:
117 part = "" 138 parts.append('' if not action else str(action))
118 if action:
119 part += str(action)
120 parts.append(part)
121 return "action< %s >" % " | ".join(parts) 139 return "action< %s >" % " | ".join(parts)
122 140
123 class AutomatonState(object): 141 class AutomatonState(object):
124 142
125 __node_number_counter = 0 143 __node_number_counter = 0
126 144
127 def __init__(self): 145 def __init__(self):
128 self.__node_number = AutomatonState.__node_number_counter 146 self.__node_number = AutomatonState.__node_number_counter
129 AutomatonState.__node_number_counter += 1 147 AutomatonState.__node_number_counter += 1
130 148
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 node [shape = doublecircle, style=unfilled]; %s 300 node [shape = doublecircle, style=unfilled]; %s
283 node [shape = circle]; 301 node [shape = circle];
284 %s 302 %s
285 %s 303 %s
286 } 304 }
287 ''' % (start_shape, 305 ''' % (start_shape,
288 start_number, 306 start_number,
289 " ".join(terminals), 307 " ".join(terminals),
290 "\n".join(edge_content), 308 "\n".join(edge_content),
291 "\n".join(node_content)) 309 "\n".join(node_content))
OLDNEW
« no previous file with comments | « no previous file | tools/lexer_generator/code_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698