Skip to content

Commit

Permalink
builtin Nat nonterminal
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoohey31 committed Sep 19, 2024
1 parent 43ecbff commit 960f4c3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
1 change: 0 additions & 1 deletion Lott/DSL/Elab/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ where
| results => throwErrorAt symbolName
"ambiguous lott symbol {symbolName}; found multiple matches: {results.map (·.alias)}"

private
def texEscape (s : String) : String :=
String.join <| s.data.map fun c => match c with
| '&' | '%' | '$' | '#' | '_' | '{' | '}' => "\\" ++ c.toString
Expand Down
39 changes: 39 additions & 0 deletions Lott/DSL/Elab/Nat.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Lott.DSL.Elab

namespace Lott.DSL.Elab

open Lean
open Lean.Elab
open Lean.Parser

declare_syntax_cat Lott.Symbol.Nat

run_cmd setEnv <| symbolExt.addEntry (← getEnv) { canon := `Nat, alias := `n }

@[Lott.Symbol.Nat_parser]
def nat.n_parser : Parser := leadingNode `Lott.Symbol.Nat maxPrec <| identPrefix "n"

@[Lott.Symbol.Nat_parser]
def nat.num_parser : Parser := leadingNode `Lott.Symbol.Nat maxPrec numLit

@[Lott.Symbol.Nat_parser]
def nat.term_parser : Parser :=
leadingNode `Lott.Symbol.Nat maxPrec <| "(" >> checkLineEq >> termParser >> checkLineEq >> ")"

@[lott_term_elab Lott.Symbol.Nat]
def natTermElab : TermElab
| `($i:ident) => Term.elabTerm i <| .some (.const `Nat [])
| `($n:num) => Term.elabTerm n <| .some (.const `Nat [])
| `(($t:term)) => Term.elabTerm t <| .some (.const `Nat [])
| _ => throwUnsupportedSyntax

@[lott_tex_elab Lott.Symbol.Nat]
def natTexElab : TexElab
| _, `($i:ident) => return texEscape <| i.getId.toString (escape := false)
| _, `($n:num) => return texEscape <| toString n.getNat
| _, `(($t:term)) => do
let some ss := t.raw.getSubstring? | throwUnsupportedSyntax
return texEscape ss.toString
| _, _ => throwUnsupportedSyntax

end Lott.DSL.Elab

0 comments on commit 960f4c3

Please sign in to comment.