Any way of implementing location information for tokens?
Lutymane opened this issue · comments
Like row and column number
Yes, I have looked into it briefly but haven't made progress. It's a big TODO for the AST.
Peggyjs exposes lexeme locations using the built in location()
function https://peggyjs.org/documentation.html#locations
I tried putting location: location()
in the AST node constructor function:
glsl-parser/src/parser/glsl-grammar.pegjs
Lines 86 to 89 in 28bffb9
location is not defined
- so I think there are times node()
is called without having the right context(?) to know the location.
Adding location to a peggyjs grammar seems like a pretty standard thing, so I'm marking this as a good first issue, in case anyone gets to it before I do.
Reading through the peggy docs, turned out location()
is not available in global scope, so it was easy to fix and implement it by just moving the functions into per-parse scope
Thanks for this! A big improvement to the parser.
I updated the AST API a little bit to rename the AST key from loc
to location
, and I removed the source
key from location. source
/ grammarSource
in peggyjs is apparently the "source file name" the code came from, like 'main.js', which doesn't make sense to repeat on every AST node. I kept in the top level grammarSource
key because it shows up in formatted parse errors
I also put this behind a parser option includeLocation
.
Available in @shaderfrog/glsl-parser@1.3.0
Nice! I used loc
key to have some similarity to Babel 😄 And I thought it's also more convenient due to its smaller length (easier and faster to type)