-
-
Notifications
You must be signed in to change notification settings - Fork 290
Adding a setResultsName() breaks grammar? #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There are actually 2 changes here - the prior line you also added the 'operation' results name. But this change is not enough to break the grammar. What does break things is the line you indicated. This is because In your case, the original was a Forward. When you created a copy by setting the results name to 'operator', that was not a breaking change for the grammar, because it was the copy that you assigned to expr. But then later when you used When you want to tag specific nodes and operators with names, it sounds like you want to build an AST. If so, I strongly encourage you to switch gears and look at using It may be worth adding a "diagnostic" mode to pyparsing that will emit warnings during grammar construction, such as calling setResultsName on a Forward that has not yet had an expression assigned to it - not guaranteed to cause a problem, but a definite warning flag. |
Thanks for Your answer. Can You, please, explain (or point me to an explanation) why all this copying is necessary and the rationale behind it? In that case I think it would be useful to keep track of copies, somehow and, especially in the case of diff --git a/pyparsing.py b/pyparsing.py
index 0fc3917..585a378 100644
--- a/pyparsing.py
+++ b/pyparsing.py
@@ -4745,6 +4745,7 @@ class Forward(ParseElementEnhance):
"""
def __init__( self, other=None ):
super(Forward,self).__init__( other, savelist=False )
+ self.copies = []
def __lshift__( self, other ):
if isinstance( other, basestring ):
@@ -4757,6 +4758,8 @@ class Forward(ParseElementEnhance):
self.skipWhitespace = self.expr.skipWhitespace
self.saveAsList = self.expr.saveAsList
self.ignoreExprs.extend(self.expr.ignoreExprs)
+ for c in self.copies:
+ c << other
return self
def __ilshift__(self, other):
@@ -4799,11 +4802,13 @@ class Forward(ParseElementEnhance):
def copy(self):
if self.expr is not None:
- return super(Forward,self).copy()
+ ret = super(Forward,self).copy() # todo: check if this is really necessary
+ ret.copies = [] # todo: double assignments could be flagged as errors
else:
ret = Forward()
ret <<= self
- return ret
+ self.copies.append(ret)
+ return ret
class TokenConverter(ParseElementEnhance):
""" About the "diagnostic mode": is it something already existing? I just found some reference in |
No the diagnostic mode does not yet exist. If you would open an issue for it, I'll use that to start attaching notes on what would make for good warnings. (Note; I am thinking this is specifically for diagnostics while creating the parser, not while running it. That might change if we come up with some good parse-time warnings, but for parse time we already have ParseExceptions.) The copying is done so that an expression can be used multiple times in the same parser with different results names. Each instance with a different name is implemented with a copy of the given expression. I've answered this question before in more detail, I'll try to track down that discussion thread. |
The next release will include a |
New |
I'm still experimenting with grammars and I'm quite baffled.
The following example is lifted from
examples/fourFn.py
almost verbatim.I deleted the "exwcuting" parseActions and (hopefully just cosmetically) changed grammar to better understand some constructs.
Nest step should be:
Even the very first change (adding a single
ResultsName
) breaks the grammar and I'm unable to understand what I did so wrong :(The text was updated successfully, but these errors were encountered: