Skip to content
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

Syntax error points to wrong position in json5 #47

Closed
plinnie opened this issue Aug 25, 2021 · 4 comments
Closed

Syntax error points to wrong position in json5 #47

plinnie opened this issue Aug 25, 2021 · 4 comments

Comments

@plinnie
Copy link

plinnie commented Aug 25, 2021

When there is an error in a json5 file, the error message seems to always point to the first element in the json5 file.

For example:

import json5
JSON5 = """
{  
    version: "1.0",
    author: "John Smith",
    people : [
        "Monty",
        "Python"foo
    ]    
}
"""
json5.loads(JSON5)

Gives the error:

Traceback (most recent call last):
  File "C:/Users/Admin/surfdrive/client/stuff/tests/testjson5.py", line 12, in <module>
    json5.loads(JSON5)
  File "C:\Users\Admin\Anaconda3\envs\python38\lib\site-packages\json5\lib.py", line 82, in loads
    raise ValueError(err)
ValueError: <string>:3 Unexpected "v" at column 5

I would expect the error: ValueError: <string>:7 Unexpected "f" at column 16.

json5 version: 0.9.6
OS: Windows 10
Python: 3.8.8

@dpranke
Copy link
Owner

dpranke commented Aug 25, 2021

Yup, that looks like a bug, I'd expect the "f" as well. I'll take a look.

@dpranke
Copy link
Owner

dpranke commented Sep 1, 2021

Looks like this was introduced with the fix for issue #26, which was (partially) attempting to fix a different case where the position was misreported. I'll have to figure out some way to handle both cases ...

@thepackrat
Copy link

+1 on this one, just cost me some sanity-questioning until I figured out what the parser error was doing.

dpranke added a commit that referenced this issue May 9, 2022
Due to the way the parser was handing nested parts of the JSON5
grammar, we would incorrectly report errors as having happened
at the start of a block of content, rather than where the error
actually happened.

A simple case of this would be reporting `"foo"bar` as happening
at column 1 rather than column 5. The bug report (#47) has a more
complicated and realistic example.

To fix this we needed to rev the underlying parser generator (glop)
to v0.6.4 and also rewrite the JSON5 grammar to use a positive
lookahead.
@dpranke
Copy link
Owner

dpranke commented May 9, 2022

This is fixed in beb97a9. I've just pushed v0.9.8, which contains this fix, to PyPI.

Sorry this took so long for a fix, there were $REASONS why I wasn't on top of this, plus once I started looking at it, the bug ended up being a rather subtle problem in the generated parsing code (i.e., in the parser generator).

@dpranke dpranke closed this as completed May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants