Menu

#78 Wrong column number of error reported in certain cases

v1.0 (example)
closed-fixed
nobody
None
5
2018-03-31
2015-06-17
dkasak
No

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.

Discussion

  • dkasak

    dkasak - 2015-06-17

    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.

     
  • Paul McGuire

    Paul McGuire - 2018-03-31
    • status: open --> closed-fixed
     
  • Paul McGuire

    Paul McGuire - 2018-03-31

    cols behavior was fixed in 2.1.10

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.