kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.57k stars 232 forks source link

Grammar error line info incorrect when using additional JS #539

Open aliclark opened 3 years ago

aliclark commented 3 years ago

The following grammar produces a Syntax Error with line 2 and col 12:

@{%
%}
test[a] -> ]

Since offset is 18, nearley is able to display the correct location of the error when formatting, but I think it would be nice to reflect the original file line and column also.

For example, this PR with moo to provide contextual errors makes use of line and col over offset, so would point to the wrong location in this example.

A workaround can be for the grammar author to move additional JS blocks towards the bottom of the grammar file, therefore not changing the line locations of the grammar above.

I imagine a straightforward fix could be to "blank" out the locations where additional JS is present in any intermediate grammar, ie. intermediate result looking like:


test[a] -> ]
kach commented 3 years ago

@airportyh You're the error messages person :) Could you take a look please?

aliclark commented 3 years ago

This turned out to be quite straightforward, and turned out that "additional javascript" was a red herring as it also applies to backtick strings.