Zulip Chat Archive

Stream: lean4

Topic: Creating SyntaxNodeKinds for parsers


Thomas Murrills (Nov 03 2022 at 00:25):

How do you define a term elaborator for a manually-defined parser (via e.g. def myParser : Parser := ...? With syntax, you can use syntax (name := myNodeName) ... and then write @[termElab myNodeName]—but I'm not sure how to attach a SyntaxNodeKind to what's consumed by a manually-defined parser. (I would have expected the SyntaxNodeKind to be myParser, but it doesn't seem to be created automatically.)

Gabriel Ebner (Nov 03 2022 at 00:34):

Usually the best approach is to use elab_rules:

import Lean
syntax "my_syntax" term : term
elab_rules <= expectedType
  | `(my_syntax $t) => Lean.Elab.Term.elabTerm t expectedType

Gabriel Ebner (Nov 03 2022 at 00:53):

I would have expected the SyntaxNodeKind to be myParser, but it doesn't seem to be created automatically.

Right, you need to create the node explicitly. We usually do this with the leading_parser macro:

def myParser : Parser := leading_parser ...

This will add a node (with the parser declaration name as the syntax kind), and also a $foo:myParser antiquotation.

Thomas Murrills (Nov 03 2022 at 01:10):

Hmmm…I am using leading_parser, but it still doesn’t like @[termElab myParser]. #check shows that I do have access to the parser I defined, so it’s not something silly…I’ll investigate a bit! Should I expect that elab will recognize it as a valid SyntaxNodeKind while @[termElab …] won’t?

Gabriel Ebner (Nov 03 2022 at 01:24):

Please post an #mwe!

Thomas Murrills (Nov 03 2022 at 01:52):

Ah! Thanks for asking me to—I figured it out while trying to make the #mwe. I hadn't marked the parser with the attribute @[termParser], so termElab didn't know that this parser parsed terms! 🙃


Last updated: Dec 20 2023 at 11:08 UTC