Skip to content

Commit 66d8d03

Browse files
committed
invoking rule with argument
1 parent 7ddaa41 commit 66d8d03

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

ometa/interp.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ class TrampolinedGrammarInterpreter(object):
2020
An interpreter for OMeta grammars that processes input
2121
incrementally.
2222
"""
23-
def __init__(self, grammar, ruleName, callback=None, globals=None):
23+
def __init__(self, grammar, rule, callback=None, globals=None):
2424
self.grammar = grammar
2525
self.position = 0
2626
self.callback = callback
2727
self.globals = globals or {}
2828
self.rules = decomposeGrammar(grammar)
29-
self.next = self.apply(ruleName, None, ())
29+
self.next = self.setNext(rule)
3030
self._localsStack = []
3131
self.currentResult = None
3232
self.input = InputStream([], 0)
@@ -68,6 +68,14 @@ def end(self):
6868
self.callback(*x)
6969

7070

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+
7179
## Implementation note: each method, instead of being a function
7280
## returning a value, is a generator that will yield '_feed_me' an
7381
## arbitrary number of times, then finally yield the value of the
@@ -148,10 +156,15 @@ def apply(self, ruleName, codeName, args):
148156
Invoke a rule, optionally with arguments.
149157
"""
150158
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])
155168
_locals = {}
156169
self._localsStack.append(_locals)
157170
try:

ometa/runtime.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def joinErrors(errors):
131131
"""
132132
if len(errors) == 1:
133133
return errors[0]
134-
135134
highestPos = -1
136135
results = set()
137136
trail = None

ometa/tube.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _setupInterp(self):
2626
'initial'.
2727
"""
2828
self._interp = TrampolinedGrammarInterpreter(
29-
grammar=self.grammar, ruleName=self.receiver.currentRule,
29+
grammar=self.grammar, rule=self.receiver.currentRule,
3030
callback=None, globals=self.bindings)
3131

3232

0 commit comments

Comments
 (0)