diff --git a/src/res_core.ml b/src/res_core.ml index 31258253..b1e0530b 100644 --- a/src/res_core.ml +++ b/src/res_core.ml @@ -556,6 +556,9 @@ let rec parseLident p = Parser.next p; let loc = mkLoc startPos p.prevEndPos in (ident, loc) + | Eof -> + Parser.err ~startPos p (Diagnostics.unexpected p.Parser.token p.breadcrumbs); + ("_", mkLoc startPos p.prevEndPos) | _ -> ( match recoverLident p with | Some () -> parseLident p @@ -600,6 +603,9 @@ let parseHashIdent ~startPos p = in Parser.next p; (i, mkLoc startPos p.prevEndPos) + | Eof -> + Parser.err ~startPos p (Diagnostics.unexpected p.token p.breadcrumbs); + ("", mkLoc startPos p.prevEndPos) | _ -> parseIdent ~startPos ~msg:ErrorMessages.variantIdent p (* Ldot (Ldot (Lident "Foo", "Bar"), "baz") *) @@ -635,11 +641,11 @@ let parseValuePath p = Parser.err p (Diagnostics.unexpected p.Parser.token p.breadcrumbs); Longident.Lident ident) in - if p.token <> Eof then Parser.next p; + Parser.nextUnsafe p; res | token -> Parser.err p (Diagnostics.unexpected token p.breadcrumbs); - Parser.next p; + Parser.nextUnsafe p; Longident.Lident "_" in Location.mkloc ident (mkLoc startPos p.prevEndPos) @@ -826,7 +832,7 @@ let parseConstant p = Parser.err p (Diagnostics.unexpected token p.breadcrumbs); Pconst_string ("", None) in - Parser.next p; + Parser.nextUnsafe p; constant let parseTemplateConstant ~prefix (p : Parser.t) = @@ -1090,6 +1096,10 @@ let rec parsePattern ?(alias = true) ?(or_ = true) p = in Parser.next p; (i, mkLoc startPos p.prevEndPos) + | Eof -> + Parser.err ~startPos p + (Diagnostics.unexpected p.token p.breadcrumbs); + ("", mkLoc startPos p.prevEndPos) | _ -> parseIdent ~msg:ErrorMessages.variantIdent ~startPos p in match p.Parser.token with @@ -1113,6 +1123,9 @@ let rec parsePattern ?(alias = true) ?(or_ = true) p = let extension = parseExtension p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Pat.extension ~loc ~attrs extension + | Eof -> + Parser.err p (Diagnostics.unexpected p.Parser.token p.breadcrumbs); + Recover.defaultPattern () | token -> ( Parser.err p (Diagnostics.unexpected token p.breadcrumbs); match @@ -1859,6 +1872,10 @@ and parseAtomicExpr p = Parser.err p (Diagnostics.lident token); Parser.next p; Recover.defaultExpr () + | Eof -> + Parser.err ~startPos:p.prevEndPos p + (Diagnostics.unexpected p.Parser.token p.breadcrumbs); + Recover.defaultExpr () | token -> ( let errPos = p.prevEndPos in Parser.err ~startPos:errPos p (Diagnostics.unexpected token p.breadcrumbs); @@ -1897,7 +1914,7 @@ and parseFirstClassModuleExpr ~startPos p = and parseBracketAccess p expr startPos = Parser.leaveBreadcrumb p Grammar.ExprArrayAccess; let lbracket = p.startPos in - Parser.next p; + Parser.expect Lbracket p; let stringStart = p.startPos in match p.Parser.token with | String s -> ( @@ -3249,7 +3266,10 @@ and parseForRest hasOpeningParen pattern startPos p = Parser.err p (Diagnostics.unexpected token p.breadcrumbs); Asttypes.Upto in - Parser.next p; + if p.Parser.token = Eof then + Parser.err ~startPos:p.startPos p + (Diagnostics.unexpected p.Parser.token p.breadcrumbs) + else Parser.next p; let e2 = parseExpr ~context:WhenExpr p in if hasOpeningParen then Parser.expect Rparen p; Parser.expect Lbrace p; @@ -3607,7 +3627,7 @@ and parseValueOrConstructor p = Ast_helper.Exp.ident ~loc (Location.mkloc lident loc) | token -> if acc = [] then ( - Parser.next p; + Parser.nextUnsafe p; Parser.err p (Diagnostics.unexpected token p.breadcrumbs); Recover.defaultExpr ()) else @@ -3808,7 +3828,11 @@ and parseAtomicTypExpr ~attrs p = | SingleQuote -> Parser.next p; let ident, loc = - parseIdent ~msg:ErrorMessages.typeVar ~startPos:p.startPos p + if p.Parser.token = Eof then ( + Parser.err ~startPos:p.startPos p + (Diagnostics.unexpected p.Parser.token p.breadcrumbs); + ("", mkLoc p.startPos p.prevEndPos)) + else parseIdent ~msg:ErrorMessages.typeVar ~startPos:p.startPos p in Ast_helper.Typ.var ~loc ~attrs ident | Underscore -> @@ -3854,6 +3878,9 @@ and parseAtomicTypExpr ~attrs p = let loc = mkLoc startPos p.prevEndPos in Ast_helper.Typ.extension ~attrs ~loc extension | Lbrace -> parseRecordOrObjectType ~attrs p + | Eof -> + Parser.err p (Diagnostics.unexpected p.Parser.token p.breadcrumbs); + Recover.defaultType () | token -> ( Parser.err p (Diagnostics.unexpected token p.breadcrumbs); match @@ -4596,7 +4623,11 @@ and parseTypeParam p = | SingleQuote -> Parser.next p; let ident, loc = - parseIdent ~msg:ErrorMessages.typeParam ~startPos:p.startPos p + if p.Parser.token = Eof then ( + Parser.err ~startPos:p.startPos p + (Diagnostics.unexpected p.Parser.token p.breadcrumbs); + ("", mkLoc p.startPos p.prevEndPos)) + else parseIdent ~msg:ErrorMessages.typeParam ~startPos:p.startPos p in Some (Ast_helper.Typ.var ~loc ident, variance) | Underscore -> diff --git a/tests/parsing/errors/other/expected/for.res.txt b/tests/parsing/errors/other/expected/for.res.txt new file mode 100644 index 00000000..de41bea0 --- /dev/null +++ b/tests/parsing/errors/other/expected/for.res.txt @@ -0,0 +1,12 @@ + + Syntax error! + tests/parsing/errors/other/for.res:1:6-2:0 + + 1 │ for x + 2 │ + + Did you forget a `in` here? + +;;for x = [%rescript.exprhole ] to [%rescript.exprhole ] do + [%rescript.exprhole ] + done \ No newline at end of file diff --git a/tests/parsing/errors/other/for.res b/tests/parsing/errors/other/for.res new file mode 100644 index 00000000..5c8c39ba --- /dev/null +++ b/tests/parsing/errors/other/for.res @@ -0,0 +1 @@ +for x diff --git a/tests/parsing/errors/typeDef/expected/keywordOnly.res.txt b/tests/parsing/errors/typeDef/expected/keywordOnly.res.txt new file mode 100644 index 00000000..e15e02ad --- /dev/null +++ b/tests/parsing/errors/typeDef/expected/keywordOnly.res.txt @@ -0,0 +1,9 @@ + + Syntax error! + tests/parsing/errors/typeDef/keywordOnly.res:1:5 + + 1 │ type + + I'm not sure what to parse here when looking at "eof". + +type nonrec _ \ No newline at end of file diff --git a/tests/parsing/errors/typeDef/keywordOnly.res b/tests/parsing/errors/typeDef/keywordOnly.res new file mode 100644 index 00000000..c3dc0520 --- /dev/null +++ b/tests/parsing/errors/typeDef/keywordOnly.res @@ -0,0 +1 @@ +type \ No newline at end of file