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

Parser error position #134

Merged
merged 34 commits into from
Mar 21, 2020
Merged

Parser error position #134

merged 34 commits into from
Mar 21, 2020

Conversation

sabelka
Copy link
Contributor

@sabelka sabelka commented Feb 2, 2020

This PR adds a new parser_error exception which contains the error location, i.e. the position of the last token parsed.
With this information it is possible to count the line number where the error happened (which would fix also #10)

@pantor
Copy link
Owner

pantor commented Mar 17, 2020

Sorry for the late response, and thanks so much for your initiative! I've added the calculation of the exact text position in line numbers and column offsets. What do you think about that?

@sabelka
Copy link
Contributor Author

sabelka commented Mar 17, 2020

Hi, thanks for considering my pull request! I had a quick look in you additions: I appreciate the calculation of lines/columns. (In my application had to do this after catching the exception anyways, so this makes things simpler). However, now the position is only available in the text of the error message and I think there is no way to get it programmatically (without parsing the error message, which I'd like to avoid). In my case, I'd need the exact location of the error because I want to draw squiggles in a template editor.

@pantor
Copy link
Owner

pantor commented Mar 18, 2020

Ah, I haven't thought about your use case. I've updated my code so you can get the location programmatically.

@pantor pantor merged commit 7ce19f9 into pantor:master Mar 21, 2020
@sabelka sabelka deleted the parser-error-position branch June 4, 2020 10:40
@sabelka
Copy link
Contributor Author

sabelka commented Jun 4, 2020

@pantor : Hi, finally I got time to integrate the new version of the line numbers patch back into my project - thanks for merging!
However, I noticed a small problem with the error locations which is best explained with an example. Consider the following template:

{{
aaa
bbb
ccc
ddd
eee
fff

Here, I would expect an error at the end of line 2 or beginning of line 3 because {{ aaa is valid, but then the closing '}}' are missing. However, I get:

[inja.exception.parser_error] (at 4:2) expected expression close, got 'bbb'

So, instead of 4:2 I would expect 3:1.

There is probably something wrong in Lexer::current_position() but I'm mot sure if I understand this function. Can you have a look?

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

Successfully merging this pull request may close these issues.

2 participants