Skip to content

Commit e1f39d6

Browse files
committed
chore: improve unexpected token messages
1 parent aa9f7ab commit e1f39d6

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Lean/Parser/Basic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ def identEqFn (id : Name) : ParserFn := fun c s =>
13111311
if s.hasError then
13121312
s
13131313
else match s.stxStack.back with
1314-
| .ident _ _ val _ => if val != id then s.mkUnexpectedTokenError s!"identifier '{id}'" else s
1314+
| .ident _ _ val _ => if val != id then s.mkUnexpectedTokenError s!"identifier `{id}`" else s
13151315
| _ => s.mkUnexpectedTokenError "identifier"
13161316

13171317
def identEq (id : Name) : Parser := {

src/Lean/Parser/Module.lean

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,31 @@ private partial def mkErrorMessage (c : InputContext) (pos : String.Pos.Raw) (st
5858
pos := r.start
5959
endPos? := some r.stop
6060
let unexpected := match e.unexpectedTk with
61-
| .ident .. => "unexpected identifier"
62-
| .atom _ v => s!"unexpected token '{v}'"
63-
| _ => "unexpected token" -- TODO: categorize (custom?) literals as well?
61+
| .ident _ rawVal _ _ =>
62+
s!"unexpected identifier `{Substring.Raw.Internal.toString rawVal}`"
63+
| .atom _ v =>
64+
s!"unexpected token `{v}`"
65+
| .node _ k #[.atom _ v] =>
66+
if k == numLitKind then
67+
s!"unexpected numeral `{v}`"
68+
else if k == strLitKind then
69+
s!"unexpected string literal `{v}`"
70+
else if k == charLitKind then
71+
s!"unexpected character literal `{v}`"
72+
else if k == scientificLitKind then
73+
s!"unexpected scientific numeral `{v}`"
74+
else if k == nameLitKind then
75+
s!"unexpected name literal {v}"
76+
else if k == fieldIdxKind then
77+
s!"unexpected field index `{v}`"
78+
else if k == hexnumKind then
79+
s!"unexpected hexadecimal number `{v}`"
80+
else if k == interpolatedStrLitKind then
81+
s!"unexpected interpolated string fragment `{v}`"
82+
else
83+
s!"unexpected token `{v}` (kind `{k}`)"
84+
| _ =>
85+
"unexpected token"
6486
e := { e with unexpected }
6587
-- if there is an unexpected token, include preceding whitespace as well as the expected token could
6688
-- be inserted at any of these places to fix the error; see tests/lean/1971.lean

0 commit comments

Comments
 (0)