Zulip Chat Archive

Stream: lean4

Topic: structure-like custom syntax


James Gallicchio (Aug 21 2022 at 17:01):

I'm just starting to learn how the parsing infrastructure works (mainly via the metaprogramming tutorial book) -- is there a standard way to include the structure-like binder syntax in a syntax declaration?

Looking to parse commands of this form:

extensible Foo where
  bar : Baz

James Gallicchio (Aug 21 2022 at 17:03):

Also, is there somewhere in the compiler code base where the standard syntax is defined? (defs, structures, etc)

I'd find it helpful to learn from how the built-in syntax works

Henrik Böving (Aug 21 2022 at 19:03):

There is lots of locations where syntax is defined, notably:

in Init and more spread throughout the compiler, notably here:

but also in a couple of other locations, the specific elaborator for structure is here: https://github.com/leanprover/lean4/blob/master/src/Lean/Elab/Structure.lean
and its syntax here: https://github.com/leanprover/lean4/blob/master/src/Lean/Parser/Command.lean#L112

James Gallicchio (Aug 22 2022 at 01:55):

Ah, thank you greatly

pcpthm (Aug 22 2022 at 05:17):

Also, syntax elements are linked (go to definition) in VSCode after import Lean.

import Lean
structure S where
  x : Nat

Then Ctrl+click on structure token will open this elaborator definition,

@[builtinCommandElab declaration]
def elabDeclaration : CommandElab := fun stx => do

and clicking declaration name in @[builtinCommandElab declaration] will open this parser definition.

James Gallicchio (Aug 22 2022 at 09:13):

Oh, this is very helpful to know :D


Last updated: Dec 20 2023 at 11:08 UTC