fix: add support for hierachical names containing numerical parts

closes #677
This commit is contained in:
Leonardo de Moura 2021-09-17 19:21:49 -07:00
parent d413aa1dc5
commit 2a9ba9f795
2 changed files with 23 additions and 2 deletions

View file

@ -636,6 +636,9 @@ private partial def splitNameLitAux (ss : Substring) (acc : List Substring) : Li
else if isIdFirst curr then
let idPart := ss.takeWhile isIdRest
splitRest (ss.extract idPart.bsize ss.bsize) (idPart :: acc)
else if curr.isDigit then
let idPart := ss.takeWhile Char.isDigit
splitRest (ss.extract idPart.bsize ss.bsize) (idPart :: acc)
else
[]
@ -650,8 +653,16 @@ def decodeNameLit (s : String) : Option Name :=
| [] => none
| comps => some <| comps.foldr (init := Name.anonymous)
fun comp n =>
let comp := if isIdBeginEscape comp.front then comp.drop 1 |>.dropRight 1 else comp
Name.mkStr n comp.toString
let comp := comp.toString
if isIdBeginEscape comp.front then
Name.mkStr n (comp.drop 1 |>.dropRight 1)
else if comp.front.isDigit then
if let some k := decodeNatLitVal? comp then
Name.mkNum n k
else
unreachable!
else
Name.mkStr n comp
else
none

10
tests/lean/run/677.lean Normal file
View file

@ -0,0 +1,10 @@
import Lean
open Lean
def encodeDecode [ToJson α] [FromJson α] (x : α) : Except String α := do
let json ← toJson x
fromJson? json
#eval IO.ofExcept <| encodeDecode (Name.mkNum Name.anonymous 5)
#eval IO.ofExcept <| encodeDecode (Name.mkStr `bla "foo.boo")