Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit cbac3de

Browse files
committed
Recovery when encountering errors parsing long idents.
More fine grained error recovery from parsing longidents. E.g. Foo. gives Foo._ instead of a default expression which erases info on what's parser already. From: rescript-lang/rescript-vscode#388
1 parent 5be5a55 commit cbac3de

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/res_core.ml

+20-9
Original file line numberDiff line numberDiff line change
@@ -699,18 +699,27 @@ let parseValuePath p =
699699
| Lident ident -> Longident.Ldot(path, ident)
700700
| Uident uident ->
701701
Parser.next p;
702-
Parser.expect Dot p;
703-
aux p (Ldot (path, uident))
702+
if p.Parser.token = Dot then (
703+
Parser.expect Dot p;
704+
aux p (Ldot (path, uident))
705+
) else (
706+
Parser.err p (Diagnostics.unexpected p.Parser.token p.breadcrumbs);
707+
path
708+
)
704709
| token ->
705710
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
706-
Longident.Lident "_"
711+
Longident.Ldot (path, "_")
707712
in
708713
let ident = match p.Parser.token with
709714
| Lident ident -> Longident.Lident ident
710715
| Uident ident ->
711-
Parser.next p;
712-
Parser.expect Dot p;
713-
aux p (Lident ident)
716+
if p.Parser.token = Dot then (
717+
Parser.expect Dot p;
718+
aux p (Lident ident)
719+
) else (
720+
Parser.err p (Diagnostics.unexpected p.Parser.token p.breadcrumbs);
721+
Longident.Lident ident
722+
)
714723
| token ->
715724
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
716725
Longident.Lident "_"
@@ -730,7 +739,7 @@ let parseValuePathTail p startPos ident =
730739
loop p (Longident.Ldot (path, ident))
731740
| token ->
732741
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
733-
Location.mknoloc path
742+
Location.mkloc (Longident.Ldot (path, "_")) (mkLoc startPos p.prevEndPos)
734743
in
735744
loop p ident
736745

@@ -753,7 +762,7 @@ let parseModuleLongIdentTail ~lowercase p startPos ident =
753762
end
754763
| t ->
755764
Parser.err p (Diagnostics.uident t);
756-
Location.mkloc acc (mkLoc startPos p.prevEndPos)
765+
Location.mkloc (Longident.Ldot (acc, "_")) (mkLoc startPos p.prevEndPos)
757766
in
758767
loop p ident
759768

@@ -3582,8 +3591,10 @@ and parseValueOrConstructor p =
35823591
Ast_helper.Exp.ident ~loc (Location.mkloc lident loc)
35833592
| token ->
35843593
Parser.next p;
3594+
let loc = mkLoc startPos p.prevEndPos in
35853595
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
3586-
Recover.defaultExpr()
3596+
let lident = buildLongident ("_"::acc) in
3597+
Ast_helper.Exp.ident ~loc (Location.mkloc lident loc)
35873598
in
35883599
aux p []
35893600

0 commit comments

Comments
 (0)