www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I get line numbers using pegged?

reply TheZipCreator <thezipcreator protonmail.com> writes:
So I've looked at the [pegged 
library](https://github.com/PhilippeSigaud/Pegged) and used it a 
little bit and it seems really good. But there doesn't seem like 
there's a built-in way to get line numbers of nodes in the parse 
tree (or maybe I'm missing something). This would useful because 
if an error occurs during interpreting/compiling, you could give 
the user a line number where their error occurred by just getting 
the line number of the node that's currently being processed. Is 
there a way to do this in pegged?
Aug 15 2022
parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 15 August 2022 at 22:55:30 UTC, TheZipCreator wrote:
 So I've looked at the [pegged 
 library](https://github.com/PhilippeSigaud/Pegged) and used it 
 a little bit and it seems really good. But there doesn't seem 
 like there's a built-in way to get line numbers of nodes in the 
 parse tree (or maybe I'm missing something). This would useful 
 because if an error occurs during interpreting/compiling, you 
 could give the user a line number where their error occurred by 
 just getting the line number of the node that's currently being 
 processed. Is there a way to do this in pegged?
It looks like the parse tree in pegged stores the start and end byte indices of the portion of the input text it corresponds to: https://github.com/PhilippeSigaud/Pegged/blob/v0.4.6/pegged/peg.d#L251-L252 It should be possible to compute line numbers based on these indices.
Aug 15 2022
parent TheZipCreator <thezipcreator protonmail.com> writes:
On Monday, 15 August 2022 at 23:38:09 UTC, Paul Backus wrote:
 On Monday, 15 August 2022 at 22:55:30 UTC, TheZipCreator wrote:
 [...]
It looks like the parse tree in pegged stores the start and end byte indices of the portion of the input text it corresponds to: https://github.com/PhilippeSigaud/Pegged/blob/v0.4.6/pegged/peg.d#L251-L252 It should be possible to compute line numbers based on these indices.
ah, that's what I was looking for, thanks
Aug 15 2022