I just started with pyparsing and I have problems with line breaks.
My grammar:
from pyparsing import * newline = LineEnd () #Literal ('\n').leaveWhitespace () minus = Literal ('-') plus = Literal ('+') lparen = Literal ('(') rparen = Literal (')') ident = Word (alphas) integer = Word (nums) arith = Forward () parenthized = Group (lparen + arith + rparen) atom = ident | integer | parenthized factor = ZeroOrMore (minus | plus) + atom arith << (ZeroOrMore (factor + (minus | plus) ) + factor) statement = arith + newline program = OneOrMore (statement)
Now that I am parsing the following:
print (program.parseString ('--1-(-a-3+n)\nx\n') )
The result will be as expected:
['-', '-', '1', '-', ['(', '-', 'a', '-', '3', '+', 'n', ')'], '\n', 'x', '\n']
But when the second line can be analyzed as the tail of the first line, does the first \n wave away?
code:
print (program.parseString ('--1-(-a-3+n)\nx\n') )
Actual result:
['-', '-', '1', '-', ['(', '-', 'a', '-', '3', '+', 'n', ')'], '-', 'x', '\n']
Expected Result:
['-', '-', '1', '-', ['(', '-', 'a', '-', '3', '+', 'n', ')'], '\n', '-', 'x', '\n']
Actually, I don't want the parser to automatically attach to statements.
1. What am I doing wrong?
2. How can I fix this?
3. What happens under the hood, causing this behavior (which is certainly reasonable, but I just don't see the point)?