Zulip Chat Archive
Stream: lean4
Topic: Writing parsers
Thomas Murrills (Nov 02 2022 at 23:35):
I've got a couple of related questions about manually-defined parsers (via def myParser : Parser := ...
):
- What are the rules around using functions that return parsers within
syntax
declarations? For example, something likesepByIndent ident ", " (allowTrailingSep := true)
doesn't seem to work in that context, butmany(p)
is ok (but notmany p
). (I couldn't find any info on this in the metaprogramming book.) - If your parser is too complicated for
syntax
, how do you then match on syntax that gets parsed by it—or even just extract various pieces? For example, how would you extract the syntax consumed bysepByIndent ...
and act on it, say, during elaboration? - Can you use parsers in antiquotations somehow? For example in
$x:ident
,ident
is a parser; but how do we use more complicated expressions that evaluate to parsers?
Thomas Murrills (Nov 03 2022 at 01:57):
@Gabriel Ebner provided one possible answer to number 3 implicitly in Creating SyntaxNodeKinds for parsers: if you give a name to your parser using def
with leading_parser
, i.e. def myParser : Parser := leading_parser ...
, then antiquotations of the form$x:myParser
will be automatically usable. (Thanks!)
Gabriel Ebner (Nov 03 2022 at 03:09):
To answer your other questions. Iirc you can use any Parser in a ParserDescr (what syntax
produces). The other way around doesn't work (without criminal energy)
Gabriel Ebner (Nov 03 2022 at 03:30):
Parser combinators like many1 work in ParserDescr if you export them using register_parser_alias
Gabriel Ebner (Nov 03 2022 at 03:31):
That only works for Parser -> Parser
and Parser -> Parser -> Parser
though.
Gabriel Ebner (Nov 03 2022 at 03:31):
sepBy is hard coded
Gabriel Ebner (Nov 03 2022 at 03:33):
As for 2: antiquotations are part of the parser. sepBy also parses $x,*
and $[x],*
.
Gabriel Ebner (Nov 03 2022 at 03:34):
For those strings the parser simply produces an antiquotation node.
Gabriel Ebner (Nov 03 2022 at 03:35):
The syntax matches are hard coded iirc. You can't define your own antiquotation with a HashMap Name Term
for example.
Sebastian Ullrich (Nov 03 2022 at 09:06):
The antiquotation kinds do not directly correspond to parsers but to invocations of docs4#Lean.Parser.mkAntiquot. syntax :=
and leading_parser
implicitly introduce this parser, yes.
Thomas Murrills (Nov 03 2022 at 17:45):
That's all very useful info! Thanks! :)
Last updated: Dec 20 2023 at 11:08 UTC