blob: 436955003b049fb90197771c87ec31a5c5f6abe4 [file]
/-
Copyright (c) 2020 Sebastian Ullrich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
import Lean.Elab.Term
namespace Lean.Elab.Term.Quotation
open Lean.Syntax
register_builtin_option hygiene : Bool := {
defValue := true
descr := "Annotate identifiers in quotations such that they are resolved relative to the scope at their declaration, not that at their eventual use/expansion, to avoid accidental capturing. Note that quotations/notations already defined are unaffected."
}
def getAntiquotationIds (stx : Syntax) : TermElabM (Array Ident) := do
let mut ids := #[]
for stx in stx.topDown (firstChoiceOnly := true) do
if (isAntiquot stx || isTokenAntiquot stx) && !isEscapedAntiquot stx then
let anti := getAntiquotTerm stx
if anti.isIdent then ids := ids.push ⟨anti⟩
else if anti.isOfKind ``Parser.Term.hole then pure ()
else throwErrorAt stx "complex antiquotation not allowed here"
return ids
/-- Get all pattern vars (as `Syntax.ident`s) in `stx` -/
partial def getPatternVars (stx : Syntax) : TermElabM (Array Syntax) :=
if stx.isQuot then
getAntiquotationIds stx
else match stx with
| `(_) => return #[]
| `($id:ident) => return #[id]
| `($id:ident@$e) => return (← getPatternVars e).push id
| _ => throwErrorAt stx "unsupported pattern in syntax match{indentD stx}"
def getPatternsVars (pats : Array Syntax) : TermElabM (Array Syntax) :=
pats.foldlM (fun vars pat => do return vars ++ (← getPatternVars pat)) #[]
/--
Given an antiquotation like `$e:term` (i.e. `Syntax.antiquotKind?` returns `some`),
returns the `"term"` atom if present.
-/
def getAntiquotKindSpec? (antiquot : Syntax) : Option Syntax :=
let name := antiquot[3][1]
if name.isMissing then none else some name
end Lean.Elab.Term.Quotation