@@ -20,13 +20,13 @@ class TrampolinedGrammarInterpreter(object):
20
20
An interpreter for OMeta grammars that processes input
21
21
incrementally.
22
22
"""
23
- def __init__ (self , grammar , ruleName , callback = None , globals = None ):
23
+ def __init__ (self , grammar , rule , callback = None , globals = None ):
24
24
self .grammar = grammar
25
25
self .position = 0
26
26
self .callback = callback
27
27
self .globals = globals or {}
28
28
self .rules = decomposeGrammar (grammar )
29
- self .next = self .apply ( ruleName , None , () )
29
+ self .next = self .setNext ( rule )
30
30
self ._localsStack = []
31
31
self .currentResult = None
32
32
self .input = InputStream ([], 0 )
@@ -68,6 +68,14 @@ def end(self):
68
68
self .callback (* x )
69
69
70
70
71
+ def setNext (self , rule ):
72
+ if isinstance (rule , basestring ):
73
+ rule = (rule , )
74
+ if not isinstance (rule , (list , tuple )):
75
+ raise TypeError ("rule should be a string, list or a tuple." )
76
+ return self .apply (rule [0 ], None , rule [1 :])
77
+
78
+
71
79
## Implementation note: each method, instead of being a function
72
80
## returning a value, is a generator that will yield '_feed_me' an
73
81
## arbitrary number of times, then finally yield the value of the
@@ -148,10 +156,15 @@ def apply(self, ruleName, codeName, args):
148
156
Invoke a rule, optionally with arguments.
149
157
"""
150
158
argvals = []
151
- for a in args :
152
- for x in self ._eval (a ):
153
- if x is _feed_me : yield x
154
- argvals .append (x [0 ])
159
+ # we tell whether a rule is a manually set one by the codeName
160
+ # if it's None, then we think it's set by setNext
161
+ if codeName is None :
162
+ argvals = args
163
+ else :
164
+ for a in args :
165
+ for x in self ._eval (a ):
166
+ if x is _feed_me : yield x
167
+ argvals .append (x [0 ])
155
168
_locals = {}
156
169
self ._localsStack .append (_locals )
157
170
try :
0 commit comments