Skip to content

When using logos lexer, how to get access to the token location when generating AST. #865

Answered by yannham
aspizu asked this question in Q&A
Discussion options

You must be logged in to vote

Let me know if that work for you, but here is how it's done in Nickel:

The type of token produced by the Logos wrapper is defined here as Spanned = (usize, YourToken, usize): https://github.com/tweag/nickel/blob/d899cf5848e6ac0958062376696aa39389de580e/core/src/parser/lexer.rs#L476

The Item type of the Logos wrapper is Result<Spanned, SomeCustomError>, defined here: https://github.com/tweag/nickel/blob/d899cf5848e6ac0958062376696aa39389de580e/core/src/parser/lexer.rs#L950

However if you don't have custom errors I believe you can just use Item = Spanned directly and call it a day. Then LALRPOP recognizes the tuple and allows to capture position information directly in the grammar rules wit…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@aspizu
Comment options

Answer selected by aspizu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants