lean4-htt/library/init/lean/parser/command.lean

98 lines
3.2 KiB
Text

/-
Copyright (c) 2018 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Sebastian Ullrich
Command parsers
-/
prelude
import init.lean.parser.declaration
namespace lean
namespace parser
open combinators monad_parsec
open parser.has_tokens parser.has_view
local postfix `?`:10000 := optional
local postfix *:10000 := combinators.many
local postfix +:10000 := combinators.many1
@[derive parser.has_view parser.has_tokens]
def command_parser.recurse : command_parser := recurse ()
set_option class.instance_max_depth 200
namespace «command»
@[derive parser.has_view parser.has_tokens]
def open_spec.parser : command_parser :=
node! open_spec [
id: ident.parser,
as: node! open_spec.as ["as", id: ident.parser]?,
only: node! open_spec.only [try ["(", id: ident.parser], ids: ident.parser*, ")"]?,
«renaming»: node! open_spec.renaming [try ["(", "renaming"], items: node! open_spec.renaming.item [«from»: ident.parser, "->", to: ident.parser]+, ")"]?,
«hiding»: node! open_spec.hiding ["(", "hiding", ids: ident.parser+, ")"]?
]+
@[derive parser.has_tokens]
def open.parser : command_parser :=
node! «open» ["open", spec: open_spec.parser]
@[derive parser.has_tokens]
def section.parser : command_parser :=
node! «section» ["section", name: ident.parser?, commands: command_parser.recurse*, "end", end_name: ident.parser?]
@[derive parser.has_tokens]
def universe.parser : command_parser :=
any_of [
-- local
node! universe_variables [try ["universe", "variables"], ids: ident.parser+],
-- global
node! «universes» ["universes", ids: ident.parser+],
node! «universe» ["universe", id: ident.parser]
]
@[derive parser.has_tokens parser.has_view]
def check.parser : command_parser :=
node! check ["#check", term: term.parser]
end «command»
open «command»
@[derive parser.has_tokens parser.has_view]
def command.parser : command_parser :=
any_of [open.parser, section.parser, universe.parser, notation.parser, reserve_notation.parser,
mixfix.parser, reserve_mixfix.parser, check.parser, declaration.parser] <?> "command"
end parser
namespace parser
namespace «command»
open combinators notation_spec
-- example macro
def mixfix.expand (stx : tysyntax mixfix.view) : option (tysyntax notation.view) :=
do v ← view stx,
-- TODO: reserved token case
notation_symbol.view.quoted quoted ← view v.symbol,
quoted ← view quoted,
prec ← view quoted.prec,
-- `notation` allows more syntax after `:` than mixfix commands, so we have to do a small conversion
let prec_to_action : precedence.view → action.view :=
λ prec, {action := review $ action_kind.view.prec prec.prec, ..prec},
k ← view v.kind,
let spec := view.rules $ match k with
| mixfix.kind.view.prefix _ := review {
id := review none,
rules := review [review {
symbol := v.symbol,
transition := review $ some $ review $ transition.view.arg $ review {
id := review {part := review $ ident_part.view.default "b", suffix := review none},
action := review $ do prec ← prec, prec ← view prec, pure $ review $ prec_to_action prec}}]}
| _ := sorry,
pure $ review ⟨"notation", review spec, ":=", v.term⟩
end «command»
end parser
end lean