From e2526f11b1e5d7709a04dda7f2156b845d03db7b Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Sun, 29 Oct 2023 11:56:56 -0700 Subject: [PATCH] feat: Print names in one segment separated with . --- Pantograph/Serial.lean | 14 +++++++------- Test/Integration.lean | 2 +- Test/Serial.lean | 10 +++++++++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Pantograph/Serial.lean b/Pantograph/Serial.lean index d109188..89bc076 100644 --- a/Pantograph/Serial.lean +++ b/Pantograph/Serial.lean @@ -46,14 +46,14 @@ def type_expr_to_bound (expr: Expr): MetaM Protocol.BoundExpression := do return (toString (← fvar.fvarId!.getUserName), toString (← Meta.ppExpr (← fvar.fvarId!.getType))) return { binders, target := toString (← Meta.ppExpr body) } -private def name_to_ast (name: Name) (sanitize: Bool := true): String := - if sanitize && name.isInternal then "_" - else name_to_ast_aux name |>.drop 1 +def name_to_ast (name: Name) (sanitize: Bool := true): String := + let internal := name.isInaccessibleUserName || name.hasMacroScopes + if sanitize && internal then "_" + else toString name |> enclose_if_escaped where - name_to_ast_aux: Name → String - | .anonymous => "" - | .num n i => s!"{name_to_ast_aux n} {i}" - | .str init last => s!"{name_to_ast_aux init} {last}" + enclose_if_escaped (n: String) := + let quote := "̈̈\"" + if n.contains Lean.idBeginEscape then s!"{quote}{n}{quote}" else n private def level_depth: Level → Nat | .zero => 0 diff --git a/Test/Integration.lean b/Test/Integration.lean index 39f145e..a77cb12 100644 --- a/Test/Integration.lean +++ b/Test/Integration.lean @@ -85,7 +85,7 @@ def test_malformed_command : IO LSpec.TestSeq := def test_tactic : IO LSpec.TestSeq := let goal: Protocol.Goal := { target := { pp? := .some "∀ (q : Prop), x ∨ q → q ∨ x" }, - vars := #[{ name := "_uniq 9", userName := "x", isInaccessible? := .some false, type? := .some { pp? := .some "Prop" }}], + vars := #[{ name := "_uniq.9", userName := "x", isInaccessible? := .some false, type? := .some { pp? := .some "Prop" }}], } subroutine_runner [ subroutine_step "goal.start" diff --git a/Test/Serial.lean b/Test/Serial.lean index 162a51d..575749d 100644 --- a/Test/Serial.lean +++ b/Test/Serial.lean @@ -10,7 +10,14 @@ open Lean deriving instance Repr, DecidableEq for Protocol.BoundExpression def test_str_to_name: LSpec.TestSeq := - LSpec.test "Symbol parsing" (Name.str (.str (.str .anonymous "Lean") "Meta") "run" = Pantograph.str_to_name "Lean.Meta.run") + LSpec.test "Symbol parsing" (Name.str (.str (.str .anonymous "Lean") "Meta") "run" = Pantograph.str_to_name "Lean.Meta.run") + +def test_name_to_ast: LSpec.TestSeq := + let quote := "̈̈\"" + LSpec.test "a.b.1" (name_to_ast (Name.num (.str (.str .anonymous "a") "b") 1) = "a.b.1") ++ + LSpec.test "seg.«a.b»" (name_to_ast (Name.str (.str .anonymous "seg") "a.b") = s!"{quote}seg.«a.b»{quote}") + -- Pathological test case + --LSpec.test s!"«̈{escape}{quote}»" (name_to_ast (Name.str .anonymous s!"{escape}{quote}") = s!"{quote}«̈{escape}{quote}»{quote}") def test_expr_to_binder (env: Environment): IO LSpec.TestSeq := do let entries: List (String × Protocol.BoundExpression) := [ @@ -70,6 +77,7 @@ def suite: IO LSpec.TestSeq := do return LSpec.group "Serialization" $ (LSpec.group "str_to_name" test_str_to_name) ++ + (LSpec.group "name_to_ast" test_name_to_ast) ++ (LSpec.group "Expression binder" (← test_expr_to_binder env)) ++ (LSpec.group "Sexp from symbol" (← test_sexp_of_symbol env))