chore: Repr Syntax and minor cleanup

This commit is contained in:
Wojciech Nawrocki 2021-08-03 22:51:14 -04:00 committed by Leonardo de Moura
parent 1bab9f6ffc
commit 77365abc8b
2 changed files with 13 additions and 9 deletions

View file

@ -115,6 +115,8 @@ instance : ToString Name where
instance : Repr Name where
reprPrec n _ := Std.Format.text "`" ++ n.toString
deriving instance Repr for Syntax
def capitalize : Name → Name
| Name.str p s _ => Name.mkStr p s.capitalize
| n => n

View file

@ -135,16 +135,19 @@ def indent (x : Formatter) (indent : Option Int := none) : Formatter := do
concat x
let ctx ← read
let indent := indent.getD $ Std.Format.getIndent ctx.options
modify fun st => { st with stack := st.stack.pop.push (Format.nest indent st.stack.back) }
modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) (Format.nest indent) }
def group (x : Formatter) : Formatter := do
concat x
modify fun st => { st with stack := st.stack.pop.push (Format.fill st.stack.back) }
modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) Format.fill }
/-- Tags the top of the stack with `stx`'s position, if it has one. -/
def tagTop (stx : Syntax) : Formatter := do
if let some p := stx.getPos? then
modify fun st => { st with stack := st.stack.pop.push (Format.tag p st.stack.back) }
/-- If `pos?` has a position, run `x` and tag its results with that position. Otherwise just run `x`. -/
def withMaybeTag (pos? : Option String.Pos) (x : FormatterM Unit) : Formatter := do
if let some p := pos? then
concat x
modify fun st => { st with stack := st.stack.modify (st.stack.size - 1) (Format.tag p) }
else
x
@[combinatorFormatter Lean.Parser.orelse] def orelse.formatter (p1 p2 : Formatter) : Formatter :=
-- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try
@ -167,10 +170,9 @@ unsafe def formatterForKindUnsafe (k : SyntaxNodeKind) : Formatter := do
push "<missing>"
goLeft
else
let f ← runForNodeKind formatterAttribute k interpretParserDescr'
let stx ← getCur
concat f
tagTop stx
let f ← runForNodeKind formatterAttribute k interpretParserDescr'
withMaybeTag stx.getPos? f
@[implementedBy formatterForKindUnsafe]
constant formatterForKind (k : SyntaxNodeKind) : Formatter