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

Report row and line number when generating errors, instead of an absolute position #166

Closed
CarterLi opened this issue May 4, 2024 · 10 comments
Labels
enhancement New feature or request

Comments

@CarterLi
Copy link
Contributor

CarterLi commented May 4, 2024

Is your feature request related to a problem? Please describe.

Currently yyjson reports

Error: failed to parse JSON config file `config.jsonc` at pos 513: unexpected character

Describe the solution you'd like

Error: failed to parse JSON config file `config.jsonc` at line X, row Y: unexpected character

Describe alternatives you've considered

Error: failed to parse JSON config file `config.jsonc` at pos X,Y: unexpected character

Additional context

I use yyjson for parsing config files. It would be easier to find where the problem is if users can get its line and row number.

@CarterLi CarterLi changed the title Report row and line number when printing errors, instead of an absolute position Report row and line number when generating errors, instead of an absolute position May 4, 2024
@ibireme
Copy link
Owner

ibireme commented May 5, 2024

I think it would be better to add a new helper function.

@ibireme ibireme added the enhancement New feature or request label May 5, 2024
@TkTech
Copy link
Contributor

TkTech commented May 5, 2024

A new api that returns detailed parser/ error information as structured data instead of an error message would be fantastic - it would let me make the python yyjson wrapper truly drop-in to replace the standard library. Right now, I can't properly populate the JSONDecoderError exception because of the lack of pos. A similar issue in simdjson has been open for years.

@ibireme
Copy link
Owner

ibireme commented May 5, 2024

I've come up with a new API, let's see if it meets the needs? @CarterLi @TkTech

/**
 Locate the line and column number for a byte position in a string.
 This can be used to get better description for error position.
 
 @param str The input string.
 @param len The byte length of the input string.
 @param pos The byte position within the input string.
 @param line A pointer to receive the line number, starting from 1.
 @param col  A pointer to receive the column number, starting from 1.
 @param chr  A pointer to receive the character index, starting from 0.
 @return true on success, false if `str` is NULL or `pos` is out of bounds.
 @note Line/column/character are calculated based on Unicode characters for
    compatibility with text editors. For multi-byte UTF-8 characters,
    the returned value may not directly correspond to the byte position.
 */
yyjson_api bool yyjson_locate_pos(const char *str, size_t len, size_t pos,
                                  size_t *line, size_t *col, size_t *chr);

ibireme added a commit that referenced this issue May 7, 2024
@TkTech
Copy link
Contributor

TkTech commented May 9, 2024

I believe this checks all the boxes 👍 . I'm in Korea until the 15th and will test it in py_yyjson shortly after I return.

@CarterLi
Copy link
Contributor Author

CarterLi commented May 9, 2024

I think it's good enough for position locating. I'm waiting for a new release then.

Another question: we are reporting unexpected character, which is a general error and may not be very helpful. Can we report what charactor yyjson expects?

For example: fastfetch-cli/fastfetch#866 (comment)

Can we report a comma is expected?

@ibireme
Copy link
Owner

ibireme commented May 9, 2024

Can we report a comma is expected?

Error messages like these could be easily improved, please create a new issue and I will address it soon.

@CarterLi
Copy link
Contributor Author

I found it a little bit hard to use with yyjson_read_file, since you don't have the json string but have to load the file content manually

@ibireme
Copy link
Owner

ibireme commented May 14, 2024

I found it a little bit hard to use with yyjson_read_file, since you don't have the json string but have to load the file content manually

The yyjson_read_file function reads the file into a string, which gets modified during parsing, so this string can only be used once and can't be used for row and column locating. It seems more appropriate to add a yyjson_locate_pos_file function to read the file again.

@CarterLi
Copy link
Contributor Author

The yyjson_read_file function reads the file into a string, which gets modified during parsing, so this string can only be used once and can't be used for row and column locating

Got it.

It seems more appropriate to add a yyjson_locate_pos_file function to read the file again.

Seems not necessary IMO. yyjson_locate_pos should be ok.

@ibireme
Copy link
Owner

ibireme commented May 14, 2024

Seems not necessary IMO. yyjson_locate_pos should be ok.

Okay, then I won't reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants