Skip to content

Pratt parsing with extra metadata #617

Answered by zesterer
catapillie asked this question in Q&A
Discussion options

You must be logged in to vote

It's easier than you might think! You can simply add the span to the output type of the operator / atom like so:

atom.map_with(|x, e| (x, e.span())).pratt((
    infix(left(1), my_op.map_with(|x, e| (x, e.span())), |l, op, r, e| {
        // Note that in here, `l.1`, `op.1`, and `r.1` all give the span of their respective elements
        (Expr::MyOp(l, op, r), e.span())
    }),
))

Replies: 1 comment 1 reply

Comment options

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

Answer selected by catapillie
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