Wrong column number of error reported in certain cases
Brought to you by:
ptmcg
If I run this code:
def test(): ParserElement.setDefaultWhitespaceChars(" \t") EOL = OneOrMore(LineEnd()).suppress() foo = Keyword("foo") bar = Keyword("bar") baz = Keyword("baz") line = foo - bar - baz - EOL lines = OneOrMore(line) lines.parseString("foo bar\nfoo bar baz", parseAll=True) test()
I get back this exception:
pyparsing.ParseSyntaxException: Expected "baz" (at char 7), (line:1, col:1)
Notice it reports the error in line 1, col 1 which is not correct. The problem seems to be in the col
function:
return (loc<len(strg) and strg[loc] == '\n') and 1 or loc - strg.rfind("\n", 0, loc)
If the location of the error falls onto a newline character, pyparsing sets the column of the error to 1. Why is this? It seems that it should return the position immediately before the newline (or, alternatively, increase the line number by 1 along with setting the column to 1, but this would be bad behaviour for line-oriented parsers).
Furthemore, the use of rfind
seems problematic... What if there is no newline in the file before the loc
? The result of rfind
will then be -1 which will result in loc - (-1) == loc + 1 which doesn't seem right.
Hmm... After some thought, it seems that loc + 1 is exactly where the error should be reported in that case so I guess the result would be correct though a little unobvious.
col
s behavior was fixed in 2.1.10