diff --git a/src/res_core.ml b/src/res_core.ml index 630aad2c..0d988f52 100644 --- a/src/res_core.ml +++ b/src/res_core.ml @@ -6,8 +6,10 @@ module CommentTable = Res_comments_table module ResPrinter = Res_printer module Scanner = Res_scanner module JsFfi = Res_js_ffi -module Parser = Res_parser - +module Parser = struct + include Res_parser + let next p = next p [@@raises Eof] +end let mkLoc startLoc endLoc = Location.{ loc_start = startLoc; loc_end = endLoc; @@ -35,9 +37,9 @@ module Recover = struct Ast_helper.Sig.extension (id, PStr []) let recoverEqualGreater p = - Parser.expect EqualGreater p; + Parser.expectUnsafe EqualGreater p; match p.Parser.token with - | MinusGreater -> Parser.next p + | MinusGreater -> Parser.expect MinusGreater p | _ -> () let shouldAbortListParse p = @@ -179,16 +181,16 @@ let getClosingToken = function let rec goToClosing closingToken state = match (state.Parser.token, closingToken) with | (Rparen, Token.Rparen) | (Rbrace, Rbrace) | (Rbracket, Rbracket) | (GreaterThan, GreaterThan) -> - Parser.next state; + Parser.next state [@doesNotRaise]; () | (Token.Lbracket | Lparen | Lbrace | List | LessThan) as t, _ -> - Parser.next state; + Parser.next state [@doesNotRaise]; goToClosing (getClosingToken t) state; goToClosing closingToken state | ((Rparen | Token.Rbrace | Rbracket | Eof), _) -> () (* TODO: how do report errors here? *) | _ -> - Parser.next state; + Parser.nextUnsafe state; goToClosing closingToken state (* Madness *) @@ -196,7 +198,7 @@ let isEs6ArrowExpression ~inTernary p = Parser.lookahead p (fun state -> match state.Parser.token with | Lident _ | Underscore -> - Parser.next state; + Parser.next state [@doesNotRaise]; begin match state.Parser.token with (* Don't think that this valid * Imagine: let x = (a: int) @@ -208,23 +210,23 @@ let isEs6ArrowExpression ~inTernary p = end | Lparen -> let prevEndPos = state.prevEndPos in - Parser.next state; + Parser.expect Lparen state; begin match state.token with (* arrived at `()` here *) | Rparen -> - Parser.next state; + Parser.expect Rparen state; begin match state.Parser.token with (* arrived at `() :` here *) | Colon when not inTernary -> - Parser.next state; + Parser.expect Colon state; begin match state.Parser.token with (* arrived at `() :typ` here *) | Lident _ -> - Parser.next state; + Parser.next state [@doesNotRaise]; begin match state.Parser.token with (* arrived at `() :typ<` here *) | LessThan -> - Parser.next state; + Parser.expect LessThan state; goToClosing GreaterThan state; | _ -> () end; @@ -259,7 +261,7 @@ let isEs6ArrowExpression ~inTernary p = * *) false | _ -> - Parser.next state; + Parser.nextUnsafe state; (* error recovery, peek at the next token, * (elements, providerId] => { * in the example above, we have an unbalanced ] here @@ -283,10 +285,10 @@ let isEs6ArrowFunctor p = (* | _ -> false *) (* end *) | Lparen -> - Parser.next state; + Parser.expect Lparen state; begin match state.token with | Rparen -> - Parser.next state; + Parser.expect Rparen state; begin match state.token with | Colon | EqualGreater -> true | _ -> false @@ -306,10 +308,10 @@ let isEs6ArrowType p = Parser.lookahead p (fun state -> match state.Parser.token with | Lparen -> - Parser.next state; + Parser.expect Lparen state; begin match state.Parser.token with | Rparen -> - Parser.next state; + Parser.expect Rparen state; begin match state.Parser.token with | EqualGreater -> true | _ -> false @@ -612,25 +614,25 @@ let parseStringLiteral s = if parse Start 0 0 then Buffer.contents b else s let rec parseLident p = - let recoverLident p = + let recoverLidentNoEof p = if ( Token.isKeyword p.Parser.token && p.Parser.prevEndPos.pos_lnum == p.startPos.pos_lnum ) then ( Parser.err p (Diagnostics.lident p.Parser.token); - Parser.next p; + Parser.next p [@doesNotRaise]; None ) else ( let rec loop p = - if not (Recover.shouldAbortListParse p) + if not (Recover.shouldAbortListParse p) && p.token <> Eof then begin - Parser.next p; + Parser.next p [@doesNotRaise]; loop p end in Parser.err p (Diagnostics.lident p.Parser.token); - Parser.next p; + Parser.next p [@doesNotRaise]; loop p; match p.Parser.token with | Lident _ -> Some () @@ -640,23 +642,24 @@ let rec parseLident p = let startPos = p.Parser.startPos in match p.Parser.token with | Lident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc startPos p.prevEndPos in (ident, loc) + | Eof -> + ("_", mkLoc startPos p.prevEndPos) | _ -> - begin match recoverLident p with + begin match recoverLidentNoEof p with | Some () -> parseLident p | None -> ("_", mkLoc startPos p.prevEndPos) end -let parseIdent ~msg ~startPos p = +let parseIdentNoNext ~msg ~startPos p = match p.Parser.token with | Lident ident | Uident ident -> - Parser.next p; - let loc = mkLoc startPos p.prevEndPos in + let loc = mkLoc startPos p.endPos in (ident, loc) | token when Token.isKeyword token && p.prevEndPos.pos_lnum == p.startPos.pos_lnum -> @@ -665,39 +668,45 @@ let parseIdent ~msg ~startPos p = "`" ^ tokenTxt ^ "` is a reserved keyword. Keywords need to be escaped: \\\"" ^ tokenTxt ^ "\"" in Parser.err ~startPos p (Diagnostics.message msg); - Parser.next p; (tokenTxt, mkLoc startPos p.prevEndPos) | _token -> Parser.err ~startPos p (Diagnostics.message msg); - Parser.next p; ("", mkLoc startPos p.prevEndPos) -let parseHashIdent ~startPos p = - Parser.expect Hash p; - match p.token with - | String text -> - let text = if p.mode = ParseForTypeChecker then parseStringLiteral text else text in - Parser.next p; - (text, mkLoc startPos p.prevEndPos) - | Int {i; suffix} -> - let () = match suffix with - | Some _ -> - Parser.err p (Diagnostics.message (ErrorMessages.polyVarIntWithSuffix i)) - | None -> () - in - Parser.next p; - (i, mkLoc startPos p.prevEndPos) - | _ -> - parseIdent ~startPos ~msg:ErrorMessages.variantIdent p +let parseIdent ~msg ~startPos p = + let res = parseIdentNoNext ~msg ~startPos p in + Parser.nextUnsafe p; + res + +let parseHashIdent ~startPos p = + if p.Parser.token <> Hash then + None + else Some ( + Parser.expect Hash p; + match p.token with + | String text -> + let text = if p.mode = ParseForTypeChecker then parseStringLiteral text else text in + Parser.next p [@doesNotRaise]; + (text, mkLoc startPos p.prevEndPos) + | Int {i; suffix} -> + let () = match suffix with + | Some _ -> + Parser.err p (Diagnostics.message (ErrorMessages.polyVarIntWithSuffix i)) + | None -> () + in + Parser.next p [@doesNotRaise]; + (i, mkLoc startPos p.prevEndPos) + | _ -> + parseIdent ~startPos ~msg:ErrorMessages.variantIdent p + ) (* Ldot (Ldot (Lident "Foo", "Bar"), "baz") *) -let parseValuePath p = - let startPos = p.Parser.startPos in +let parseValuePathNoNext p = let rec aux p path = match p.Parser.token with | Lident ident -> Longident.Ldot(path, ident) | Uident uident -> - Parser.next p; + Parser.next p [@doesNotRaise]; if p.Parser.token = Dot then ( Parser.expect Dot p; aux p (Ldot (path, uident)) @@ -710,21 +719,33 @@ let parseValuePath p = Longident.Ldot (path, "_") in let ident = match p.Parser.token with - | Lident ident -> Longident.Lident ident - | Uident ident -> - Parser.next p; - if p.Parser.token = Dot then ( - Parser.expect Dot p; - aux p (Lident ident) - ) else ( - Parser.err p (Diagnostics.unexpected p.Parser.token p.breadcrumbs); + | Lident ident -> Longident.Lident ident - ) - | token -> - Parser.err p (Diagnostics.unexpected token p.breadcrumbs); - Longident.Lident "_" + | Uident ident -> + Parser.next p [@doesNotRaise]; + if p.Parser.token = Dot then ( + Parser.expect Dot p; + aux p (Lident ident) + ) else ( + Parser.err p (Diagnostics.unexpected p.Parser.token p.breadcrumbs); + Longident.Lident ident + ) + | token -> + Parser.err p (Diagnostics.unexpected token p.breadcrumbs); + Longident.Lident "_" in - Parser.next p; + ident + +let parseValuePath p = + let startPos = p.Parser.startPos in + let ident = parseValuePathNoNext p in + Parser.nextUnsafe p; + Location.mkloc ident (mkLoc startPos p.prevEndPos) + +let parseValuePathNotEof p = (* Only call when current token is not Eof *) + let startPos = p.Parser.startPos in + let ident = parseValuePathNoNext p in + Parser.next p [@doesNotRaise]; Location.mkloc ident (mkLoc startPos p.prevEndPos) let parseValuePathAfterDot p = @@ -741,11 +762,11 @@ let parseValuePathTail p startPos ident = let rec loop p path = match p.Parser.token with | Lident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; Location.mkloc (Longident.Ldot(path, ident)) (mkLoc startPos p.prevEndPos) | Uident ident -> - Parser.next p; - Parser.expect Dot p; + Parser.next p [@doesNotRaise]; + Parser.expectUnsafe Dot p; loop p (Longident.Ldot (path, ident)) | token -> Parser.err p (Diagnostics.unexpected token p.breadcrumbs); @@ -757,16 +778,16 @@ let parseModuleLongIdentTail ~lowercase p startPos ident = let rec loop p acc = match p.Parser.token with | Lident ident when lowercase -> - Parser.next p; + Parser.next p [@doesNotRaise]; let lident = (Longident.Ldot (acc, ident)) in Location.mkloc lident (mkLoc startPos p.prevEndPos) | Uident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; let endPos = p.prevEndPos in let lident = (Longident.Ldot (acc, ident)) in begin match p.Parser.token with | Dot -> - Parser.next p; + Parser.expect Dot p; loop p lident | _ -> Location.mkloc lident (mkLoc startPos endPos) end @@ -786,15 +807,15 @@ let parseModuleLongIdent ~lowercase p = | Lident ident when lowercase -> let loc = mkLoc startPos p.endPos in let lident = Longident.Lident ident in - Parser.next p; + Parser.next p [@doesNotRaise]; Location.mkloc lident loc | Uident ident -> let lident = Longident.Lident ident in let endPos = p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; begin match p.Parser.token with | Dot -> - Parser.next p; + Parser.expect Dot p; parseModuleLongIdentTail ~lowercase p startPos lident | _ -> Location.mkloc lident (mkLoc startPos endPos) end @@ -810,11 +831,11 @@ let parseIdentPath p = let rec loop p acc = match p.Parser.token with | Uident ident | Lident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; let lident = (Longident.Ldot (acc, ident)) in begin match p.Parser.token with | Dot -> - Parser.next p; + Parser.expect Dot p; loop p lident | _ -> lident end @@ -822,10 +843,10 @@ let parseIdentPath p = in match p.Parser.token with | Lident ident | Uident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; begin match p.Parser.token with | Dot -> - Parser.next p; + Parser.expect Dot p; loop p (Longident.Lident ident) | _ -> Longident.Lident ident end @@ -834,7 +855,7 @@ let parseIdentPath p = let verifyJsxOpeningClosingName p nameExpr = let closing = match p.Parser.token with - | Lident lident -> Parser.next p; Longident.Lident lident + | Lident lident -> Parser.next p [@doesNotRaise]; Longident.Lident lident | Uident _ -> (parseModuleLongIdent ~lowercase:true p).txt | _ -> Longident.Lident "" @@ -867,7 +888,7 @@ let string_of_pexp_ident nameExpr = let parseOpenDescription ~attrs p = Parser.leaveBreadcrumb p Grammar.OpenDescription; let startPos = p.Parser.startPos in - Parser.expect Open p; + Parser.expectUnsafe Open p; let override = if Parser.optional p Token.Bang then Asttypes.Override else @@ -926,8 +947,8 @@ let parseTemplateStringLiteral s = (* ∣ string-literal *) let parseConstant p = let isNegative = match p.Parser.token with - | Token.Minus -> Parser.next p; true - | Plus -> Parser.next p; false + | Token.Minus -> Parser.expect Minus p; true + | Plus -> Parser.expect Plus p; false | _ -> false in let constant = match p.Parser.token with @@ -954,7 +975,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) = @@ -963,15 +984,17 @@ let parseTemplateConstant ~prefix (p : Parser.t) = Parser.nextTemplateLiteralToken p; match p.token with | TemplateTail txt -> - Parser.next p; + Parser.next p [@doesNotRaise]; let txt = if p.mode = ParseForTypeChecker then parseTemplateStringLiteral txt else txt in Parsetree.Pconst_string (txt, prefix) | _ -> let rec skipTokens () = - Parser.next p; - match p.token with - | Backtick -> Parser.next p; () - | _ -> skipTokens () + if p.token <> Eof then ( + Parser.next p [@doesNotRaise]; + match p.token with + | Backtick -> Parser.expect Backtick p; () + | _ -> skipTokens () + ) in skipTokens (); Parser.err ~startPos ~endPos:p.prevEndPos p @@ -985,7 +1008,7 @@ let parseCommaDelimitedRegion p ~grammar ~closing ~f = | Some node -> begin match p.Parser.token with | Comma -> - Parser.next p; + Parser.expect Comma p; loop (node::nodes) | token when token = closing || token = Eof -> List.rev (node::nodes) @@ -1007,7 +1030,7 @@ let parseCommaDelimitedRegion p ~grammar ~closing ~f = | _ -> if not (p.token = Eof || p.token = closing || Recover.shouldAbortListParse p) then Parser.expect Comma p; - if p.token = Semicolon then Parser.next p; + if p.token = Semicolon then Parser.expect Semicolon p; loop (node::nodes) end | None -> @@ -1015,7 +1038,7 @@ let parseCommaDelimitedRegion p ~grammar ~closing ~f = List.rev nodes else ( Parser.err p (Diagnostics.unexpected p.token p.breadcrumbs); - Parser.next p; + Parser.next p [@doesNotRaise]; loop nodes ); in @@ -1030,7 +1053,7 @@ let parseCommaDelimitedReversedList p ~grammar ~closing ~f = | Some node -> begin match p.Parser.token with | Comma -> - Parser.next p; + Parser.expect Comma p; loop (node::nodes) | token when token = closing || token = Eof -> (node::nodes) @@ -1052,7 +1075,7 @@ let parseCommaDelimitedReversedList p ~grammar ~closing ~f = | _ -> if not (p.token = Eof || p.token = closing || Recover.shouldAbortListParse p) then Parser.expect Comma p; - if p.token = Semicolon then Parser.next p; + if p.token = Semicolon then Parser.expect Semicolon p; loop (node::nodes) end | None -> @@ -1060,7 +1083,7 @@ let parseCommaDelimitedReversedList p ~grammar ~closing ~f = nodes else ( Parser.err p (Diagnostics.unexpected p.token p.breadcrumbs); - Parser.next p; + Parser.next p [@doesNotRaise]; loop nodes ); in @@ -1083,7 +1106,7 @@ let parseDelimitedRegion p ~grammar ~closing ~f = List.rev nodes else ( Parser.err p (Diagnostics.unexpected p.token p.breadcrumbs); - Parser.next p; + Parser.next p [@doesNotRaise]; loop nodes ) in @@ -1102,7 +1125,7 @@ let parseRegion p ~grammar ~f = List.rev nodes else ( Parser.err p (Diagnostics.unexpected p.token p.breadcrumbs); - Parser.next p; + Parser.next p [@doesNotRaise]; loop nodes ) in @@ -1137,7 +1160,7 @@ let rec parsePattern ?(alias=true) ?(or_=true) p = let pat = match p.Parser.token with | (True | False) as token -> let endPos = p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc startPos endPos in Ast_helper.Pat.construct ~loc (Location.mkloc (Longident.Lident (Token.toString token)) loc) None @@ -1145,7 +1168,7 @@ let rec parsePattern ?(alias=true) ?(or_=true) p = let c = parseConstant p in begin match p.token with | DotDot -> - Parser.next p; + Parser.expect DotDot p; let c2 = parseConstant p in Ast_helper.Pat.interval ~loc:(mkLoc startPos p.prevEndPos) c c2 | _ -> @@ -1155,10 +1178,10 @@ let rec parsePattern ?(alias=true) ?(or_=true) p = let constant = parseTemplateConstant ~prefix:(Some "js") p in Ast_helper.Pat.constant ~attrs:[templateLiteralAttr] ~loc:(mkLoc startPos p.prevEndPos) constant | Lparen -> - Parser.next p; + Parser.expect Lparen p; begin match p.token with | Rparen -> - Parser.next p; + Parser.expect Rparen p; let loc = mkLoc startPos p.prevEndPos in let lid = Location.mkloc (Longident.Lident "()") loc in Ast_helper.Pat.construct ~loc lid None @@ -1166,10 +1189,10 @@ let rec parsePattern ?(alias=true) ?(or_=true) p = let pat = parseConstrainedPattern p in begin match p.token with | Comma -> - Parser.next p; + Parser.expect Comma p; parseTuplePattern ~attrs ~first:pat ~startPos p | _ -> - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let loc = mkLoc startPos p.prevEndPos in {pat with ppat_loc = loc} end @@ -1181,12 +1204,12 @@ let rec parsePattern ?(alias=true) ?(or_=true) p = | Underscore -> let endPos = p.endPos in let loc = mkLoc startPos endPos in - Parser.next p; + Parser.expect Underscore p; Ast_helper.Pat.any ~loc ~attrs () | Lident ident -> let endPos = p.endPos in let loc = mkLoc startPos endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; begin match p.token with | Backtick -> let constant = parseTemplateConstant ~prefix:(Some ident) p in @@ -1203,9 +1226,9 @@ let rec parsePattern ?(alias=true) ?(or_=true) p = Ast_helper.Pat.construct ~loc:constr.loc ~attrs constr None end | Hash -> - Parser.next p; + Parser.expect Hash p; if p.Parser.token == DotDotDot then ( - Parser.next p; + Parser.expect DotDotDot p; let ident = parseValuePath p in let loc = mkLoc startPos ident.loc.loc_end in Ast_helper.Pat.type_ ~loc ~attrs ident @@ -1213,7 +1236,7 @@ let rec parsePattern ?(alias=true) ?(or_=true) p = let (ident, loc) = match p.token with | String text -> let text = if p.mode = ParseForTypeChecker then parseStringLiteral text else text in - Parser.next p; + Parser.next p [@doesNotRaise]; (text, mkLoc startPos p.prevEndPos) | Int {i; suffix} -> let () = match suffix with @@ -1221,7 +1244,7 @@ let rec parsePattern ?(alias=true) ?(or_=true) p = Parser.err p (Diagnostics.message (ErrorMessages.polyVarIntWithSuffix i)) | None -> () in - Parser.next p; + Parser.next p [@doesNotRaise]; (i, mkLoc startPos p.prevEndPos) | _ -> parseIdent ~msg:ErrorMessages.variantIdent ~startPos p @@ -1234,17 +1257,17 @@ let rec parsePattern ?(alias=true) ?(or_=true) p = end ) | Exception -> - Parser.next p; + Parser.expect Exception p; let pat = parsePattern ~alias:false ~or_:false p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Pat.exception_ ~loc ~attrs pat | Lazy -> - Parser.next p; + Parser.expect Lazy p; let pat = parsePattern ~alias:false ~or_:false p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Pat.lazy_ ~loc ~attrs pat | List -> - Parser.next p; + Parser.expect List p; parseListPattern ~startPos ~attrs p | Module -> parseModulePattern ~attrs p @@ -1268,26 +1291,28 @@ and skipTokensAndMaybeRetry p ~isStartOfGrammar = if Token.isKeyword p.Parser.token && p.Parser.prevEndPos.pos_lnum == p.startPos.pos_lnum then ( - Parser.next p; + Parser.next p [@doesNotRaise]; None ) else ( if Recover.shouldAbortListParse p then begin if isStartOfGrammar p.Parser.token then begin - Parser.next p; + Parser.next p [@doesNotRaise]; Some () end else None end + else if p.token = Eof then + None else begin - Parser.next p; + Parser.next p [@doesNotRaise]; let rec loop p = - if not (Recover.shouldAbortListParse p) + if not (Recover.shouldAbortListParse p) && p.token <> Eof then begin - Parser.next p; + Parser.next p [@doesNotraise]; loop p end in loop p; @@ -1302,7 +1327,7 @@ and skipTokensAndMaybeRetry p ~isStartOfGrammar = and parseAliasPattern ~attrs pattern p = match p.Parser.token with | As -> - Parser.next p; + Parser.expect As p; let (name, loc) = parseLident p in let name = Location.mkloc name loc in Ast_helper.Pat.alias @@ -1318,7 +1343,7 @@ and parseOrPattern pattern1 p = let rec loop pattern1 = match p.Parser.token with | Bar -> - Parser.next p; + Parser.expect Bar p; let pattern2 = parsePattern ~or_:false p in let loc = { pattern1.Parsetree.ppat_loc with loc_end = pattern2.ppat_loc.loc_end @@ -1332,7 +1357,7 @@ and parseNonSpreadPattern ~msg p = let () = match p.Parser.token with | DotDotDot -> Parser.err p (Diagnostics.message msg); - Parser.next p; + Parser.expect DotDotDot p; | _ -> () in match p.Parser.token with @@ -1340,7 +1365,7 @@ and parseNonSpreadPattern ~msg p = let pat = parsePattern p in begin match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; let typ = parseTypExpr p in let loc = mkLoc pat.ppat_loc.loc_start typ.Parsetree.ptyp_loc.loc_end in Some (Ast_helper.Pat.constraint_ ~loc pat typ) @@ -1352,7 +1377,7 @@ and parseConstrainedPattern p = let pat = parsePattern p in match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; let typ = parseTypExpr p in let loc = mkLoc pat.ppat_loc.loc_start typ.Parsetree.ptyp_loc.loc_end in Ast_helper.Pat.constraint_ ~loc pat typ @@ -1374,11 +1399,10 @@ and parseConstrainedPatternRegion p = * | field , _ * | field , _, *) -and parseRecordPatternField p = - let label = parseValuePath p in +and parseRecordPatternField ~(label: Longident.t Location.loc) p = let pattern = match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; parsePattern p | _ -> Ast_helper.Pat.var @@ -1391,26 +1415,26 @@ and parseRecordPatternField p = and parseRecordPatternItem p = match p.Parser.token with | DotDotDot -> - Parser.next p; - Some (true, PatField (parseRecordPatternField p)) + Parser.expect DotDotDot p; + Some (true, PatField (parseRecordPatternField ~label:(parseValuePath p) p)) | Uident _ | Lident _ -> - Some (false, PatField (parseRecordPatternField p)) + Some (false, PatField (parseRecordPatternField ~label:(parseValuePathNotEof p) p)) | Underscore -> - Parser.next p; + Parser.expect Underscore p; Some (false, PatUnderscore) | _ -> None and parseRecordPattern ~attrs p = let startPos = p.startPos in - Parser.expect Lbrace p; + Parser.expectUnsafe Lbrace p; let rawFields = parseCommaDelimitedReversedList p ~grammar:PatternRecord ~closing:Rbrace ~f:parseRecordPatternItem in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let (fields, closedFlag) = let (rawFields, flag) = match rawFields with | (_hasSpread, PatUnderscore)::rest -> @@ -1443,7 +1467,7 @@ and parseTuplePattern ~attrs ~first ~startPos p = ~f:parseConstrainedPatternRegion ) in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let () = match patterns with | [_] -> Parser.err ~startPos ~endPos:p.prevEndPos p @@ -1456,7 +1480,7 @@ and parseTuplePattern ~attrs ~first ~startPos p = and parsePatternRegion p = match p.Parser.token with | DotDotDot -> - Parser.next p; + Parser.expect DotDotDot p; Some (true, parseConstrainedPattern p) | token when Grammar.isPatternStart token -> Some (false, parseConstrainedPattern p) @@ -1464,12 +1488,12 @@ and parsePatternRegion p = and parseModulePattern ~attrs p = let startPos = p.Parser.startPos in - Parser.expect Module p; - Parser.expect Lparen p; + Parser.expectUnsafe Module p; + Parser.expectUnsafe Lparen p; let uident = match p.token with | Uident uident -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; Location.mkloc uident loc | _ -> (* TODO: error recovery *) Location.mknoloc "_" @@ -1477,10 +1501,10 @@ and parseModulePattern ~attrs p = begin match p.token with | Colon -> let colonStart = p.Parser.startPos in - Parser.next p; + Parser.expect Colon p; let packageTypAttrs = parseAttributes p in let packageType = parsePackageType ~startPos:colonStart ~attrs:packageTypAttrs p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let loc = mkLoc startPos p.prevEndPos in let unpack = Ast_helper.Pat.unpack ~loc:uident.loc uident in Ast_helper.Pat.constraint_ @@ -1489,7 +1513,7 @@ and parseModulePattern ~attrs p = unpack packageType | _ -> - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Pat.unpack ~loc ~attrs uident end @@ -1501,7 +1525,7 @@ and parseListPattern ~startPos ~attrs p = ~closing:Rbrace ~f:parsePatternRegion in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let filterSpread (hasSpread, pattern) = if hasSpread then ( @@ -1525,7 +1549,7 @@ and parseListPattern ~startPos ~attrs p = and parseArrayPattern ~attrs p = let startPos = p.startPos in - Parser.expect Lbracket p; + Parser.expectUnsafe Lbracket p; let patterns = parseCommaDelimitedRegion p @@ -1533,17 +1557,17 @@ and parseArrayPattern ~attrs p = ~closing:Rbracket ~f:(parseNonSpreadPattern ~msg:ErrorMessages.arrayPatternSpread) in - Parser.expect Rbracket p; + Parser.expectUnsafe Rbracket p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Pat.array ~loc ~attrs patterns and parseConstructorPatternArgs p constr startPos attrs = let lparen = p.startPos in - Parser.expect Lparen p; + Parser.expectUnsafe Lparen p; let args = parseCommaDelimitedRegion p ~grammar:Grammar.PatternList ~closing:Rparen ~f:parseConstrainedPatternRegion in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let args = match args with | [] -> let loc = mkLoc lparen p.prevEndPos in @@ -1565,7 +1589,7 @@ and parseConstructorPatternArgs p constr startPos attrs = and parseVariantPatternArgs p ident startPos attrs = let lparen = p.startPos in - Parser.expect Lparen p; + Parser.expectUnsafe Lparen p; let patterns = parseCommaDelimitedRegion p ~grammar:Grammar.PatternList ~closing:Rparen ~f:parseConstrainedPatternRegion in @@ -1587,7 +1611,7 @@ and parseVariantPatternArgs p ident startPos attrs = | patterns -> Some (Ast_helper.Pat.tuple ~loc:(mkLoc lparen p.endPos) patterns) in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; Ast_helper.Pat.variant ~loc:(mkLoc startPos p.prevEndPos) ~attrs ident args and parseExpr ?(context=OrdinaryExpr) p = @@ -1600,9 +1624,9 @@ and parseTernaryExpr leftOperand p = match p.Parser.token with | Question -> Parser.leaveBreadcrumb p Grammar.Ternary; - Parser.next p; + Parser.expect Question p; let trueBranch = parseExpr ~context:TernaryTrueBranchExpr p in - Parser.expect Colon p; + Parser.expectUnsafe Colon p; let falseBranch = parseExpr p in Parser.eatBreadcrumb p; let loc = {leftOperand.Parsetree.pexp_loc with @@ -1624,12 +1648,12 @@ and parseEs6ArrowExpression ?context ?parameters p = in let returnType = match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; Some (parseTypExpr ~es6Arrow:false p) | _ -> None in - Parser.expect EqualGreater p; + Parser.expectUnsafe EqualGreater p; let body = let expr = parseExpr ?context p in match returnType with @@ -1689,13 +1713,13 @@ and parseParameter p = * *) let attrs = parseAttributes p in if p.Parser.token = Typ then ( - Parser.next p; + Parser.expect Typ p; let lidents = parseLidentList p in Some (TypeParameter {uncurried; attrs; locs = lidents; pos = startPos}) ) else ( let (attrs, lbl, pat) = match p.Parser.token with | Tilde -> - Parser.next p; + Parser.expect Tilde p; let (lblName, loc) = parseLident p in let propLocAttr = (Location.mkloc "ns.namedArgLoc" loc, Parsetree.PStr []) in begin match p.Parser.token with @@ -1708,7 +1732,7 @@ and parseParameter p = ) | Colon -> let lblEnd = p.prevEndPos in - Parser.next p; + Parser.expect Colon p; let typ = parseTypExpr p in let loc = mkLoc startPos lblEnd in let pat = @@ -1717,7 +1741,7 @@ and parseParameter p = Ast_helper.Pat.constraint_ ~attrs:[propLocAttr] ~loc pat typ in (attrs, Asttypes.Labelled lblName, pat) | As -> - Parser.next p; + Parser.expect As p; let pat = let pat = parseConstrainedPattern p in {pat with ppat_attributes = propLocAttr::pat.ppat_attributes} @@ -1739,7 +1763,7 @@ and parseParameter p = in match p.Parser.token with | Equal -> - Parser.next p; + Parser.expect Equal p; let lbl = match lbl with | Asttypes.Labelled lblName -> Asttypes.Optional lblName | Asttypes.Nolabel -> @@ -1752,7 +1776,7 @@ and parseParameter p = in begin match p.Parser.token with | Question -> - Parser.next p; + Parser.expect Question p; Some (TermParameter {uncurried; attrs; label = lbl; expr = None; pat; pos = startPos}) | _ -> let expr = parseConstrainedOrCoercedExpr p in @@ -1771,7 +1795,7 @@ and parseParameterList p = ~closing:Rparen p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; parameters (* parameters ::= @@ -1785,7 +1809,7 @@ and parseParameters p = let startPos = p.Parser.startPos in match p.Parser.token with | Lident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc startPos p.Parser.prevEndPos in [TermParameter { uncurried = false; @@ -1796,24 +1820,24 @@ and parseParameters p = pos = startPos; }] | Underscore -> - Parser.next p; + Parser.expect Underscore p; let loc = mkLoc startPos p.Parser.prevEndPos in [TermParameter {uncurried = false; attrs = []; label = Asttypes.Nolabel; expr = None; pat = Ast_helper.Pat.any ~loc (); pos = startPos}] | Lparen -> - Parser.next p; + Parser.expect Lparen p; begin match p.Parser.token with | Rparen -> - Parser.next p; + Parser.expect Rparen p; let loc = mkLoc startPos p.Parser.prevEndPos in let unitPattern = Ast_helper.Pat.construct ~loc (Location.mkloc (Longident.Lident "()") loc) None in [TermParameter {uncurried = false; attrs = []; label = Asttypes.Nolabel; expr = None; pat = unitPattern; pos = startPos}] | Dot -> - Parser.next p; + Parser.expect Dot p; begin match p.token with | Rparen -> - Parser.next p; + Parser.expect Rparen p; let loc = mkLoc startPos p.Parser.prevEndPos in let unitPattern = Ast_helper.Pat.construct ~loc (Location.mkloc (Longident.Lident "()") loc) None @@ -1833,7 +1857,7 @@ and parseParameters p = [] and parseCoercedExpr ~(expr: Parsetree.expression) p = - Parser.expect ColonGreaterThan p; + Parser.expectUnsafe ColonGreaterThan p; let typ = parseTypExpr p in let loc = mkLoc expr.pexp_loc.loc_start p.prevEndPos in Ast_helper.Exp.coerce ~loc expr None typ @@ -1844,7 +1868,7 @@ and parseConstrainedOrCoercedExpr p = | ColonGreaterThan -> parseCoercedExpr ~expr p | Colon -> - Parser.next p; + Parser.expect Colon p; begin match p.token with | _ -> let typ = parseTypExpr p in @@ -1866,7 +1890,7 @@ and parseConstrainedExprRegion p = let expr = parseExpr p in begin match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; let typ = parseTypExpr p in let loc = mkLoc expr.pexp_loc.loc_start typ.ptyp_loc.loc_end in Some (Ast_helper.Exp.constraint_ ~loc expr typ) @@ -1882,7 +1906,7 @@ and parseAtomicExpr p = let startPos = p.Parser.startPos in let expr = match p.Parser.token with | (True | False) as token -> - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.construct ~loc (Location.mkloc (Longident.Lident (Token.toString token)) loc) None @@ -1898,10 +1922,10 @@ and parseAtomicExpr p = | Hash -> parsePolyVariantExpr p | Lparen -> - Parser.next p; + Parser.expect Lparen p; begin match p.Parser.token with | Rparen -> - Parser.next p; + Parser.expect Rparen p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.construct ~loc (Location.mkloc (Longident.Lident "()") loc) None @@ -1909,10 +1933,10 @@ and parseAtomicExpr p = let expr = parseConstrainedOrCoercedExpr p in begin match p.token with | Comma -> - Parser.next p; + Parser.expect Comma p; parseTupleExpr ~startPos ~first:expr p | _ -> - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; expr (* {expr with pexp_loc = mkLoc startPos p.prevEndPos} * What does this location mean here? It means that when there's @@ -1923,10 +1947,10 @@ and parseAtomicExpr p = end end | List -> - Parser.next p; + Parser.expect List p; parseListExpr ~startPos p | Module -> - Parser.next p; + Parser.expect Module p; parseFirstClassModuleExpr ~startPos p | Lbracket -> parseArrayExp p @@ -1941,7 +1965,7 @@ and parseAtomicExpr p = | Underscore as token -> (* This case is for error recovery. Not sure if it's the correct place *) Parser.err p (Diagnostics.lident token); - Parser.next p; + Parser.expect Underscore p; Recover.defaultExpr () | token -> let errPos = p.prevEndPos in @@ -1957,38 +1981,35 @@ and parseAtomicExpr p = (* module(module-expr) * module(module-expr : package-type) *) and parseFirstClassModuleExpr ~startPos p = - Parser.expect Lparen p; + Parser.expectUnsafe Lparen p; let modExpr = parseModuleExpr p in let modEndLoc = p.prevEndPos in begin match p.Parser.token with | Colon -> let colonStart = p.Parser.startPos in - Parser.next p; + Parser.expect Colon p; let attrs = parseAttributes p in let packageType = parsePackageType ~startPos:colonStart ~attrs p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let loc = mkLoc startPos modEndLoc in let firstClassModule = Ast_helper.Exp.pack ~loc modExpr in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.constraint_ ~loc firstClassModule packageType | _ -> - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.pack ~loc modExpr end -and parseBracketAccess p expr startPos = - Parser.leaveBreadcrumb p Grammar.ExprArrayAccess; - let lbracket = p.startPos in - Parser.next p; - let stringStart = p.startPos in +and parseBracketAccess p ~lbracket expr startPos = + let stringStart = p.Parser.startPos in match p.Parser.token with | String s -> let s = if p.mode = ParseForTypeChecker then parseStringLiteral s else s in - Parser.next p; + Parser.next p [@doesNotRaise]; let stringEnd = p.prevEndPos in - Parser.expect Rbracket p; + Parser.expectUnsafe Rbracket p; Parser.eatBreadcrumb p; let rbracket = p.prevEndPos in let e = @@ -2000,7 +2021,7 @@ and parseBracketAccess p expr startPos = let equalStart = p.startPos in begin match p.token with | Equal -> - Parser.next p; + Parser.expect Equal p; let equalEnd = p.prevEndPos in let rhsExpr = parseExpr p in let loc = mkLoc startPos rhsExpr.pexp_loc.loc_end in @@ -2012,14 +2033,14 @@ and parseBracketAccess p expr startPos = end | _ -> let accessExpr = parseConstrainedOrCoercedExpr p in - Parser.expect Rbracket p; + Parser.expectUnsafe Rbracket p; Parser.eatBreadcrumb p; let rbracket = p.prevEndPos in let arrayLoc = mkLoc lbracket rbracket in begin match p.token with | Equal -> Parser.leaveBreadcrumb p ExprArrayMutation; - Parser.next p; + Parser.expect Equal p; let rhsExpr = parseExpr p in let arraySet = Location.mkloc (Longident.Ldot(Lident "Array", "set")) @@ -2060,12 +2081,12 @@ and parsePrimaryExpr ~operand ?(noCall=false) p = let rec loop p expr = match p.Parser.token with | Dot -> - Parser.next p; + Parser.expect Dot p; let lident = parseValuePathAfterDot p in begin match p.Parser.token with | Equal when noCall = false -> Parser.leaveBreadcrumb p Grammar.ExprSetField; - Parser.next p; + Parser.expect Equal p; let targetExpr = parseExpr p in let loc = mkLoc startPos p.prevEndPos in let setfield = Ast_helper.Exp.setfield ~loc expr lident targetExpr in @@ -2077,7 +2098,10 @@ and parsePrimaryExpr ~operand ?(noCall=false) p = loop p (Ast_helper.Exp.field ~loc expr lident) end | Lbracket when noCall = false && p.prevEndPos.pos_lnum == p.startPos.pos_lnum -> - parseBracketAccess p expr startPos + Parser.leaveBreadcrumb p Grammar.ExprArrayAccess; + let lbracket = p.startPos in + Parser.expect Lbracket p; + parseBracketAccess p ~lbracket expr startPos | Lparen when noCall = false && p.prevEndPos.pos_lnum == p.startPos.pos_lnum -> loop p (parseCallExpr p expr) | Backtick when noCall = false && p.prevEndPos.pos_lnum == p.startPos.pos_lnum -> @@ -2108,7 +2132,7 @@ and parseUnaryExpr p = | (Minus | MinusDot | Plus | PlusDot | Bang) as token -> Parser.leaveBreadcrumb p Grammar.ExprUnary; let tokenEnd = p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; let operand = parseUnaryExpr p in let unaryExpr = makeUnaryExpr startPos tokenEnd token operand in Parser.eatBreadcrumb p; @@ -2124,12 +2148,12 @@ and parseOperandExpr ~context p = let attrs = parseAttributes p in let expr = match p.Parser.token with | Assert -> - Parser.next p; + Parser.expect Assert p; let expr = parseUnaryExpr p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.assert_ ~loc expr | Lazy -> - Parser.next p; + Parser.expect Lazy p; let expr = parseUnaryExpr p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.lazy_ ~loc expr @@ -2189,11 +2213,11 @@ and parseBinaryExpr ?(context=OrdinaryExpr) ?a p prec = ) && p.startPos.pos_lnum > p.prevEndPos.pos_lnum -> -1 | token -> Token.precedence token in - if tokenPrec < prec then a + if tokenPrec < prec || token = Eof then a else begin Parser.leaveBreadcrumb p (Grammar.ExprBinaryAfterOp token); let startPos = p.startPos in - Parser.next p; + Parser.next p [@doesNotRaise]; let endPos = p.prevEndPos in let b = parseBinaryExpr ~context p (tokenPrec + 1) in let loc = mkLoc a.Parsetree.pexp_loc.loc_start b.pexp_loc.loc_end in @@ -2251,14 +2275,14 @@ and parseTemplateExpr ?(prefix="js") p = Parser.nextTemplateLiteralToken p; match p.token with | TemplateTail txt -> - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc startPos p.prevEndPos in let txt = if p.mode = ParseForTypeChecker then parseTemplateStringLiteral txt else txt in let str = Ast_helper.Exp.constant ~attrs:[templateLiteralAttr] ~loc (Pconst_string(txt, Some prefix)) in Ast_helper.Exp.apply ~attrs:[templateLiteralAttr] ~loc hiddenOperator [Nolabel, acc; Nolabel, str] | TemplatePart txt -> - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc startPos p.prevEndPos in let expr = parseExprBlock p in let fullLoc = mkLoc startPos p.prevEndPos in @@ -2278,11 +2302,11 @@ and parseTemplateExpr ?(prefix="js") p = Parser.nextTemplateLiteralToken p; match p.token with | TemplateTail txt -> - Parser.next p; + Parser.next p [@doesNotRaise]; let txt = if p.mode = ParseForTypeChecker then parseTemplateStringLiteral txt else txt in Ast_helper.Exp.constant ~attrs:[templateLiteralAttr] ~loc:(mkLoc startPos p.prevEndPos) (Pconst_string(txt, Some prefix)) | TemplatePart txt -> - Parser.next p; + Parser.next p [@doesNotRaise]; let constantLoc = mkLoc startPos p.prevEndPos in let expr = parseExprBlock p in let fullLoc = mkLoc startPos p.prevEndPos in @@ -2310,11 +2334,11 @@ and overParseConstrainedOrCoercedOrArrowExpression p expr = | ColonGreaterThan -> parseCoercedExpr ~expr p | Colon -> - Parser.next p; + Parser.expect Colon p; let typ = parseTypExpr ~es6Arrow:false p in begin match p.Parser.token with | EqualGreater -> - Parser.next p; + Parser.expect EqualGreater p; let body = parseExpr p in let pat = match expr.pexp_desc with | Pexp_ident longident -> @@ -2395,14 +2419,14 @@ and parseLetBindingBody ~startPos ~attrs p = Parser.eatBreadcrumb p; match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; begin match p.token with | Typ -> (* locally abstract types *) - Parser.next p; + Parser.expect Typ p; let newtypes = parseLidentList p in - Parser.expect Dot p; + Parser.expectUnsafe Dot p; let typ = parseTypExpr p in - Parser.expect Equal p; + Parser.expectUnsafe Equal p; let expr = parseExpr p in let loc = mkLoc startPos p.prevEndPos in let exp, poly = wrapTypeAnnotation ~loc newtypes typ expr in @@ -2412,13 +2436,13 @@ and parseLetBindingBody ~startPos ~attrs p = let polyType = parsePolyTypeExpr p in let loc = {pat.ppat_loc with loc_end = polyType.Parsetree.ptyp_loc.loc_end} in let pat = Ast_helper.Pat.constraint_ ~loc pat polyType in - Parser.expect Token.Equal p; + Parser.expectUnsafe Equal p; let exp = parseExpr p in let exp = overParseConstrainedOrCoercedOrArrowExpression p exp in (pat, exp) end | _ -> - Parser.expect Token.Equal p; + Parser.expectUnsafe Equal p; let exp = overParseConstrainedOrCoercedOrArrowExpression p (parseExpr p) in (pat, exp) in @@ -2500,11 +2524,11 @@ and parseLetBindings ~attrs p = let attrs = parseAttributesAndBinding p in match p.Parser.token with | And -> - Parser.next p; + Parser.expect And p; let attrs = match p.token with | Export -> let exportLoc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.expect Export p; let genTypeAttr = (Location.mkloc "genType" exportLoc, Parsetree.PStr []) in genTypeAttr::attrs | _ -> attrs @@ -2527,7 +2551,7 @@ and parseJsxName p = | Lident ident -> let identStart = p.startPos in let identEnd = p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc identStart identEnd in Location.mkloc (Longident.Lident ident) loc | Uident _ -> @@ -2548,26 +2572,26 @@ and parseJsxOpeningOrSelfClosingElement ~startPos p = let children = match p.Parser.token with | Forwardslash -> (* *) let childrenStartPos = p.Parser.startPos in - Parser.next p; + Parser.expect Forwardslash p; let childrenEndPos = p.Parser.startPos in - Parser.expect GreaterThan p; + Parser.expectUnsafe GreaterThan p; let loc = mkLoc childrenStartPos childrenEndPos in makeListExpression loc [] None (* no children *) | GreaterThan -> (* bar *) let childrenStartPos = p.Parser.startPos in Scanner.setJsxMode p.scanner; - Parser.next p; + Parser.expect GreaterThan p; let (spread, children) = parseJsxChildren p in let childrenEndPos = p.Parser.startPos in let () = match p.token with - | LessThanSlash -> Parser.next p - | LessThan -> Parser.next p; Parser.expect Forwardslash p + | LessThanSlash -> Parser.expect LessThanSlash p + | LessThan -> Parser.expect LessThan p; Parser.expectUnsafe Forwardslash p | token when Grammar.isStructureItemStart token -> () - | _ -> Parser.expect LessThanSlash p + | _ -> Parser.expectUnsafe LessThanSlash p in begin match p.Parser.token with | Lident _ | Uident _ when verifyJsxOpeningClosingName p name -> - Parser.expect GreaterThan p; + Parser.expectUnsafe GreaterThan p; let loc = mkLoc childrenStartPos childrenEndPos in ( match spread, children with | true, child :: _ -> @@ -2584,7 +2608,7 @@ and parseJsxOpeningOrSelfClosingElement ~startPos p = let opening = "" in let msg = "Closing jsx name should be the same as the opening name. Did you mean " ^ opening ^ " ?" in Parser.err ~startPos ~endPos:p.prevEndPos p (Diagnostics.message msg); - Parser.expect GreaterThan p + Parser.expectUnsafe GreaterThan p ) in let loc = mkLoc childrenStartPos childrenEndPos in @@ -2620,7 +2644,7 @@ and parseJsxOpeningOrSelfClosingElement ~startPos p = and parseJsx p = Parser.leaveBreadcrumb p Grammar.Jsx; let startPos = p.Parser.startPos in - Parser.expect LessThan p; + Parser.expectUnsafe LessThan p; let jsxExpr = match p.Parser.token with | Lident _ | Uident _ -> parseJsxOpeningOrSelfClosingElement ~startPos p @@ -2640,11 +2664,11 @@ and parseJsx p = and parseJsxFragment p = let childrenStartPos = p.Parser.startPos in Scanner.setJsxMode p.scanner; - Parser.expect GreaterThan p; + Parser.expectUnsafe GreaterThan p; let (_spread, children) = parseJsxChildren p in let childrenEndPos = p.Parser.startPos in - Parser.expect LessThanSlash p; - Parser.expect GreaterThan p; + Parser.expectUnsafe LessThanSlash p; + Parser.expectUnsafe GreaterThan p; let loc = mkLoc childrenStartPos childrenEndPos in makeListExpression loc children None @@ -2672,7 +2696,7 @@ and parseJsxProp p = else begin match p.Parser.token with | Equal -> - Parser.next p; + Parser.expect Equal p; (* no punning *) let optional = Parser.optional p Question in let attrExpr = @@ -2731,43 +2755,43 @@ and parseJsxChildren p = in match p.Parser.token with | DotDotDot -> - Parser.next p; + Parser.expect DotDotDot p; (true, [parsePrimaryExpr ~operand:(parseAtomicExpr p) ~noCall:true p]) | _ -> (false, loop p []) and parseBracedOrRecordExpr p = let startPos = p.Parser.startPos in - Parser.expect Lbrace p; + Parser.expectUnsafe Lbrace p; match p.Parser.token with | Rbrace -> Parser.err p (Diagnostics.unexpected Rbrace p.breadcrumbs); - Parser.next p; + Parser.expect Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in Ast_helper.Exp.construct ~attrs:[braces] ~loc (Location.mkloc (Longident.Lident "()") loc) None | DotDotDot -> (* beginning of record spread, parse record *) - Parser.next p; + Parser.expect DotDotDot p; let spreadExpr = parseConstrainedOrCoercedExpr p in - Parser.expect Comma p; + Parser.expectUnsafe Comma p; let expr = parseRecordExpr ~startPos ~spread:(Some spreadExpr) [] p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; expr | String s -> let s = if p.mode = ParseForTypeChecker then parseStringLiteral s else s in let field = let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; Location.mkloc (Longident.Lident s) loc in begin match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; let fieldExpr = parseExpr p in Parser.optional p Comma |> ignore; let expr = parseRecordExprWithStringKeys ~startPos (field, fieldExpr) p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; expr | _ -> let tag = if p.mode = ParseForTypeChecker then Some "js" else None in @@ -2778,18 +2802,18 @@ and parseBracedOrRecordExpr p = begin match p.Parser.token with | Semicolon -> let expr = parseExprBlock ~first:e p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {expr with Parsetree.pexp_attributes = braces::expr.Parsetree.pexp_attributes} | Rbrace -> - Parser.next p; + Parser.expect Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {e with pexp_attributes = braces::e.pexp_attributes} | _ -> let expr = parseExprBlock ~first:e p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {expr with pexp_attributes = braces::expr.pexp_attributes} @@ -2803,49 +2827,49 @@ and parseBracedOrRecordExpr p = let identEndPos = p.prevEndPos in begin match p.Parser.token with | Comma -> - Parser.next p; + Parser.expect Comma p; let valueOrConstructor = match startToken with | Uident _ -> removeModuleNameFromPunnedFieldValue(valueOrConstructor) | _ -> valueOrConstructor in let expr = parseRecordExpr ~startPos [(pathIdent, valueOrConstructor)] p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; expr | Colon -> - Parser.next p; + Parser.expect Colon p; let fieldExpr = parseExpr p in begin match p.token with | Rbrace -> - Parser.next p; + Parser.expect Rbrace p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.record ~loc [(pathIdent, fieldExpr)] None | _ -> - Parser.expect Comma p; + Parser.expectUnsafe Comma p; let expr = parseRecordExpr ~startPos [(pathIdent, fieldExpr)] p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; expr end (* error case *) | Lident _ -> if p.prevEndPos.pos_lnum < p.startPos.pos_lnum then ( - Parser.expect Comma p; + Parser.expectUnsafe Comma p; let expr = parseRecordExpr ~startPos [(pathIdent, valueOrConstructor)] p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; expr ) else ( - Parser.expect Colon p; + Parser.expectUnsafe Colon p; let expr = parseRecordExpr ~startPos [(pathIdent, valueOrConstructor)] p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; expr ) | Semicolon -> let expr = parseExprBlock ~first:(Ast_helper.Exp.ident pathIdent) p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {expr with pexp_attributes = braces::expr.pexp_attributes} | Rbrace -> - Parser.next p; + Parser.expect Rbrace p; let expr = Ast_helper.Exp.ident ~loc:pathIdent.loc pathIdent in let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in @@ -2869,18 +2893,18 @@ and parseBracedOrRecordExpr p = begin match p.Parser.token with | Semicolon -> let expr = parseExprBlock ~first:e p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {expr with pexp_attributes = braces::expr.pexp_attributes} | Rbrace -> - Parser.next p; + Parser.expect Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {e with pexp_attributes = braces::e.pexp_attributes} | _ -> let expr = parseExprBlock ~first:e p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {expr with pexp_attributes = braces::expr.pexp_attributes} @@ -2894,18 +2918,18 @@ and parseBracedOrRecordExpr p = begin match p.Parser.token with | Semicolon -> let expr = parseExprBlock ~first:e p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {expr with pexp_attributes = braces::expr.pexp_attributes} | Rbrace -> - Parser.next p; + Parser.expect Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {e with pexp_attributes = braces::e.pexp_attributes} | _ -> let expr = parseExprBlock ~first:e p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {expr with pexp_attributes = braces::expr.pexp_attributes} @@ -2920,18 +2944,18 @@ and parseBracedOrRecordExpr p = begin match p.Parser.token with | Semicolon -> let expr = parseExprBlock ~first:e p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {expr with pexp_attributes = braces::expr.pexp_attributes} | Rbrace -> - Parser.next p; + Parser.expect Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {e with pexp_attributes = braces::e.pexp_attributes} | _ -> let expr = parseExprBlock ~first:e p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {expr with pexp_attributes = braces::expr.pexp_attributes} @@ -2939,7 +2963,7 @@ and parseBracedOrRecordExpr p = end | _ -> let expr = parseExprBlock p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let braces = makeBracesAttr loc in {expr with pexp_attributes = braces::expr.pexp_attributes} @@ -2948,11 +2972,11 @@ and parseRecordRowWithStringKey p = match p.Parser.token with | String s -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; let field = Location.mkloc (Longident.Lident s) loc in begin match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; let fieldExpr = parseExpr p in Some (field, fieldExpr) | _ -> @@ -2964,16 +2988,16 @@ and parseRecordRow p = let () = match p.Parser.token with | Token.DotDotDot -> Parser.err p (Diagnostics.message ErrorMessages.recordExprSpread); - Parser.next p; + Parser.expect DotDotDot p; | _ -> () in match p.Parser.token with | Lident _ | Uident _ -> let startToken = p.token in - let field = parseValuePath p in + let field = parseValuePathNotEof p in begin match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; let fieldExpr = parseExpr p in Some (field, fieldExpr) | _ -> @@ -3018,7 +3042,7 @@ and parseRecordExpr ~startPos ?(spread=None) rows p = and parseNewlineOrSemicolonExprBlock p = match p.Parser.token with | Semicolon -> - Parser.next p + Parser.expect Semicolon p | token when Grammar.isBlockExprStart token -> if p.prevEndPos.pos_lnum < p.startPos.pos_lnum then () else @@ -3034,7 +3058,7 @@ and parseExprBlockItem p = let attrs = parseAttributes p in match p.Parser.token with | Module -> - Parser.next p; + Parser.expect Module p; begin match p.token with | Lparen -> let expr = parseFirstClassModuleExpr ~startPos p in @@ -3045,7 +3069,7 @@ and parseExprBlockItem p = let name = match p.Parser.token with | Uident ident -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; Location.mkloc ident loc | t -> Parser.err p (Diagnostics.uident t); @@ -3125,12 +3149,12 @@ and parseExprBlock ?first p = and parseTryExpression p = let startPos = p.Parser.startPos in - Parser.expect Try p; + Parser.expectUnsafe Try p; let expr = parseExpr ~context:WhenExpr p in - Parser.expect Res_token.catch p; - Parser.expect Lbrace p; + Parser.expectUnsafe Res_token.catch p; + Parser.expectUnsafe Lbrace p; let cases = parsePatternMatching p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.try_ ~loc expr cases @@ -3143,16 +3167,16 @@ and parseIfCondition p = and parseThenBranch p = Parser.leaveBreadcrumb p IfBranch; - Parser.expect Lbrace p; + Parser.expectUnsafe Lbrace p; let thenExpr = parseExprBlock p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; Parser.eatBreadcrumb p; thenExpr and parseElseBranch p = - Parser.expect Lbrace p; + Parser.expectUnsafe Lbrace p; let blockExpr = parseExprBlock p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; blockExpr; and parseIfExpr startPos p = @@ -3162,7 +3186,7 @@ and parseIfExpr startPos p = | Else -> Parser.endRegion p; Parser.leaveBreadcrumb p Grammar.ElseBranch; - Parser.next p; + Parser.expect Else p; Parser.beginRegion p; let elseExpr = match p.token with | If -> @@ -3182,14 +3206,14 @@ and parseIfExpr startPos p = and parseIfLetExpr startPos p = let pattern = parsePattern p in - Parser.expect Equal p; + Parser.expectUnsafe Equal p; let conditionExpr = parseIfCondition p in let thenExpr = parseThenBranch p in let elseExpr = match p.Parser.token with | Else -> Parser.endRegion p; Parser.leaveBreadcrumb p Grammar.ElseBranch; - Parser.next p; + Parser.expect Else p; Parser.beginRegion p; let elseExpr = match p.token with | If -> @@ -3216,10 +3240,10 @@ and parseIfOrIfLetExpression p = Parser.beginRegion p; Parser.leaveBreadcrumb p Grammar.ExprIf; let startPos = p.Parser.startPos in - Parser.expect If p; + Parser.expectUnsafe If p; let expr = match p.Parser.token with | Let -> - Parser.next p; + Parser.expect Let p; let ifLetExpr = parseIfLetExpr startPos p in Parser.err ~startPos:ifLetExpr.pexp_loc.loc_start @@ -3234,7 +3258,7 @@ and parseIfOrIfLetExpression p = expr; and parseForRest hasOpeningParen pattern startPos p = - Parser.expect In p; + Parser.expectUnsafe In p; let e1 = parseExpr p in let direction = match p.Parser.token with | Lident "to" -> Asttypes.Upto @@ -3243,27 +3267,27 @@ and parseForRest hasOpeningParen pattern startPos p = Parser.err p (Diagnostics.unexpected token p.breadcrumbs); Asttypes.Upto in - Parser.next p; + Parser.nextUnsafe p; let e2 = parseExpr ~context:WhenExpr p in - if hasOpeningParen then Parser.expect Rparen p; - Parser.expect Lbrace p; + if hasOpeningParen then Parser.expectUnsafe Rparen p; + Parser.expectUnsafe Lbrace p; let bodyExpr = parseExprBlock p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.for_ ~loc pattern e1 e2 direction bodyExpr and parseForExpression p = let startPos = p.Parser.startPos in Parser.leaveBreadcrumb p Grammar.ExprFor; - Parser.expect For p; + Parser.expectUnsafe For p; Parser.beginRegion p; let forExpr = match p.token with | Lparen -> let lparen = p.startPos in - Parser.next p; + Parser.expect Lparen p; begin match p.token with | Rparen -> - Parser.next p; + Parser.expect Rparen p; let unitPattern = let loc = mkLoc lparen p.prevEndPos in let lid = Location.mkloc (Longident.Lident "()") loc in @@ -3276,7 +3300,7 @@ and parseForExpression p = Parser.eatBreadcrumb p; begin match p.token with | Comma -> - Parser.next p; + Parser.expect Comma p; let tuplePattern = parseTuplePattern ~attrs:[] ~startPos:lparen ~first:pat p in @@ -3299,18 +3323,18 @@ and parseForExpression p = and parseWhileExpression p = let startPos = p.Parser.startPos in - Parser.expect While p; + Parser.expectUnsafe While p; let expr1 = parseExpr ~context:WhenExpr p in - Parser.expect Lbrace p; + Parser.expectUnsafe Lbrace p; let expr2 = parseExprBlock p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.while_ ~loc expr1 expr2 and parsePatternGuard p = match p.Parser.token with | When | If -> - Parser.next p; + Parser.next p [@doesNotRaise]; Some (parseExpr ~context:WhenExpr p) | _ -> None @@ -3320,13 +3344,13 @@ and parsePatternMatchCase p = Parser.leaveBreadcrumb p Grammar.PatternMatchCase; match p.Parser.token with | Token.Bar -> - Parser.next p; + Parser.expect Bar p; Parser.leaveBreadcrumb p Grammar.Pattern; let lhs = parsePattern p in Parser.eatBreadcrumb p; let guard = parsePatternGuard p in let () = match p.token with - | EqualGreater -> Parser.next p + | EqualGreater -> Parser.expect EqualGreater p | _ -> Recover.recoverEqualGreater p in let rhs = parseExprBlock p in @@ -3356,11 +3380,11 @@ and parsePatternMatching p = and parseSwitchExpression p = let startPos = p.Parser.startPos in - Parser.expect Switch p; + Parser.expectUnsafe Switch p; let switchExpr = parseExpr ~context:WhenExpr p in - Parser.expect Lbrace p; + Parser.expectUnsafe Lbrace p; let cases = parsePatternMatching p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Exp.match_ ~loc switchExpr cases @@ -3392,7 +3416,7 @@ and parseArgument p = match p.Parser.token with | Dot -> let uncurried = true in - Parser.next(p); + Parser.expect Dot p; begin match p.token with (* apply(.) *) | Rparen -> @@ -3414,18 +3438,18 @@ and parseArgument2 p ~uncurried = (* foo(_), do not confuse with foo(_ => x), TODO: performance *) | Underscore when not (isEs6ArrowExpression ~inTernary:false p) -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.expect Underscore p; let exp = Ast_helper.Exp.ident ~loc ( Location.mkloc (Longident.Lident "_") loc ) in Some (uncurried, Asttypes.Nolabel, exp) | Tilde -> - Parser.next p; + Parser.expect Tilde p; (* TODO: nesting of pattern matches not intuitive for error recovery *) begin match p.Parser.token with | Lident ident -> let startPos = p.startPos in - Parser.next p; + Parser.next p [@doesNotRaise]; let endPos = p.prevEndPos in let loc = mkLoc startPos endPos in let propLocAttr = (Location.mkloc "ns.namedArgLoc" loc, Parsetree.PStr []) in @@ -3434,13 +3458,13 @@ and parseArgument2 p ~uncurried = ) in begin match p.Parser.token with | Question -> - Parser.next p; + Parser.expect Question p; Some (uncurried, Asttypes.Optional ident, identExpr) | Equal -> - Parser.next p; + Parser.expect Equal p; let label = match p.Parser.token with | Question -> - Parser.next p; + Parser.expect Question p; Asttypes.Optional ident | _ -> Labelled ident @@ -3448,7 +3472,7 @@ and parseArgument2 p ~uncurried = let expr = match p.Parser.token with | Underscore when not (isEs6ArrowExpression ~inTernary:false p) -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.expect Underscore p; Ast_helper.Exp.ident ~loc ( Location.mkloc (Longident.Lident "_") loc ) @@ -3458,7 +3482,7 @@ and parseArgument2 p ~uncurried = in Some (uncurried, label, expr) | Colon -> - Parser.next p; + Parser.expect Colon p; let typ = parseTypExpr p in let loc = mkLoc startPos p.prevEndPos in let expr = Ast_helper.Exp.constraint_ ~attrs:[propLocAttr] ~loc identExpr typ in @@ -3482,7 +3506,7 @@ and parseCallExpr p funExpr = ~closing:Rparen ~f:parseArgument p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let args = match args with | [] -> let loc = mkLoc startPos p.prevEndPos in @@ -3560,10 +3584,10 @@ and parseValueOrConstructor p = match p.Parser.token with | Uident ident -> let endPosLident = p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; begin match p.Parser.token with | Dot -> - Parser.next p; + Parser.expect Dot p; aux p (ident::acc) | Lparen when p.prevEndPos.pos_lnum == p.startPos.pos_lnum -> let lparen = p.startPos in @@ -3595,13 +3619,13 @@ and parseValueOrConstructor p = Ast_helper.Exp.construct ~loc (Location.mkloc lident loc) None end | Lident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc startPos p.prevEndPos in let lident = buildLongident (ident::acc) in 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 ( @@ -3615,7 +3639,9 @@ and parseValueOrConstructor p = and parsePolyVariantExpr p = let startPos = p.startPos in - let (ident, _loc) = parseHashIdent ~startPos p in + let (ident, _loc) = match parseHashIdent ~startPos p with + | Some x -> x + | None -> assert false in begin match p.Parser.token with | Lparen when p.prevEndPos.pos_lnum == p.startPos.pos_lnum -> let lparen = p.startPos in @@ -3645,12 +3671,12 @@ and parsePolyVariantExpr p = and parseConstructorArgs p = let lparen = p.Parser.startPos in - Parser.expect Lparen p; + Parser.expectUnsafe Lparen p; let args = parseCommaDelimitedRegion ~grammar:Grammar.ExprList ~f:parseConstrainedExprRegion ~closing:Rparen p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; match args with | [] -> let loc = mkLoc lparen p.prevEndPos in @@ -3668,7 +3694,7 @@ and parseTupleExpr ~first ~startPos p = ~f:parseConstrainedExprRegion ) in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let () = match exprs with | [_] -> Parser.err ~startPos ~endPos:p.prevEndPos p @@ -3681,7 +3707,7 @@ and parseTupleExpr ~first ~startPos p = and parseSpreadExprRegion p = match p.Parser.token with | DotDotDot -> - Parser.next p; + Parser.expect DotDotDot p; let expr = parseConstrainedOrCoercedExpr p in Some (true, expr) | token when Grammar.isExprStart token -> @@ -3693,7 +3719,7 @@ and parseListExpr ~startPos p = parseCommaDelimitedReversedList p ~grammar:Grammar.ListExpr ~closing:Rbrace ~f:parseSpreadExprRegion in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in match listExprs with | (true, expr)::exprs -> @@ -3715,7 +3741,7 @@ and parseNonSpreadExp ~msg p = let () = match p.Parser.token with | DotDotDot -> Parser.err p (Diagnostics.message msg); - Parser.next p; + Parser.expect DotDotDot p; | _ -> () in match p.Parser.token with @@ -3723,7 +3749,7 @@ and parseNonSpreadExp ~msg p = let expr = parseExpr p in begin match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; let typ = parseTypExpr p in let loc = mkLoc expr.pexp_loc.loc_start typ.ptyp_loc.loc_end in Some (Ast_helper.Exp.constraint_ ~loc expr typ) @@ -3733,7 +3759,7 @@ and parseNonSpreadExp ~msg p = and parseArrayExp p = let startPos = p.Parser.startPos in - Parser.expect Lbracket p; + Parser.expectUnsafe Lbracket p; let exprs = parseCommaDelimitedRegion p @@ -3741,7 +3767,7 @@ and parseArrayExp p = ~closing:Rbracket ~f:(parseNonSpreadExp ~msg:ErrorMessages.arrayExprSpread) in - Parser.expect Rbracket p; + Parser.expectUnsafe Rbracket p; Ast_helper.Exp.array ~loc:(mkLoc startPos p.prevEndPos) exprs (* TODO: check attributes in the case of poly type vars, @@ -3753,19 +3779,19 @@ and parsePolyTypeExpr p = let vars = parseTypeVarList p in begin match vars with | _v1::_v2::_ -> - Parser.expect Dot p; + Parser.expectUnsafe Dot p; let typ = parseTypExpr p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Typ.poly ~loc vars typ | [var] -> begin match p.Parser.token with | Dot -> - Parser.next p; + Parser.expect Dot p; let typ = parseTypExpr p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Typ.poly ~loc vars typ | EqualGreater -> - Parser.next p; + Parser.expect EqualGreater p; let typ = Ast_helper.Typ.var ~loc:var.loc var.txt in let returnType = parseTypExpr ~alias:false p in let loc = mkLoc typ.Parsetree.ptyp_loc.loc_start p.prevEndPos in @@ -3783,7 +3809,7 @@ and parseTypeVarList p = let rec loop p vars = match p.Parser.token with | SingleQuote -> - Parser.next p; + Parser.expect SingleQuote p; let (lident, loc) = parseLident p in let var = Location.mkloc lident loc in loop p (var::vars) @@ -3797,7 +3823,7 @@ and parseLidentList p = match p.Parser.token with | Lident lident -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; loop p ((Location.mkloc lident loc)::ls) | _ -> List.rev ls @@ -3809,18 +3835,18 @@ and parseAtomicTypExpr ~attrs p = let startPos = p.Parser.startPos in let typ = match p.Parser.token with | SingleQuote -> - Parser.next p; + Parser.expect SingleQuote p; let (ident, loc) = parseIdent ~msg:ErrorMessages.typeVar ~startPos:p.startPos p in Ast_helper.Typ.var ~loc ~attrs ident | Underscore -> let endPos = p.endPos in - Parser.next p; + Parser.expect Underscore p; Ast_helper.Typ.any ~loc:(mkLoc startPos endPos) ~attrs () | Lparen -> - Parser.next p; + Parser.expect Lparen p; begin match p.Parser.token with | Rparen -> - Parser.next p; + Parser.expect Rparen p; let loc = mkLoc startPos p.prevEndPos in let unitConstr = Location.mkloc (Longident.Lident "unit") loc in Ast_helper.Typ.constr ~attrs unitConstr [] @@ -3828,7 +3854,7 @@ and parseAtomicTypExpr ~attrs p = let t = parseTypExpr p in begin match p.token with | Comma -> - Parser.next p; + Parser.expect Comma p; parseTupleType ~attrs ~first:t ~startPos p | _ -> Parser.expect Rparen p; @@ -3844,10 +3870,10 @@ and parseAtomicTypExpr ~attrs p = let args = parseTypeConstructorArgs ~constrName:constr p in Ast_helper.Typ.constr ~loc:(mkLoc startPos p.prevEndPos) ~attrs constr args | Module -> - Parser.next p; - Parser.expect Lparen p; + Parser.expect Module p; + Parser.expectUnsafe Lparen p; let packageType = parsePackageType ~startPos ~attrs p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; {packageType with ptyp_loc = mkLoc startPos p.prevEndPos} | Percent -> let extension = parseExtension p in @@ -3876,7 +3902,7 @@ and parsePackageType ~startPos ~attrs p = let modTypePath = parseModuleLongIdent ~lowercase:true p in begin match p.Parser.token with | Lident "with" -> - Parser.next p; + Parser.next p [@doesNotRaise]; let constraints = parsePackageConstraints p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Typ.package ~loc ~attrs modTypePath constraints @@ -3888,9 +3914,9 @@ and parsePackageType ~startPos ~attrs p = (* package-constraint { and package-constraint } *) and parsePackageConstraints p = let first = - Parser.expect Typ p; + Parser.expectUnsafe Typ p; let typeConstr = parseValuePath p in - Parser.expect Equal p; + Parser.expectUnsafe Equal p; let typ = parseTypExpr p in (typeConstr, typ) in @@ -3905,10 +3931,10 @@ and parsePackageConstraints p = and parsePackageConstraint p = match p.Parser.token with | And -> - Parser.next p; - Parser.expect Typ p; + Parser.expect And p; + Parser.expectUnsafe Typ p; let typeConstr = parseValuePath p in - Parser.expect Equal p; + Parser.expectUnsafe Equal p; let typ = parseTypExpr p in Some (typeConstr, typ) | _ -> None @@ -3916,10 +3942,10 @@ and parsePackageConstraint p = and parseRecordOrObjectType ~attrs p = (* for inline record in constructor *) let startPos = p.Parser.startPos in - Parser.expect Lbrace p; + Parser.expectUnsafe Lbrace p; let closedFlag = match p.token with - | DotDot -> Parser.next p; Asttypes.Open - | Dot -> Parser.next p; Asttypes.Closed + | DotDot -> Parser.expect DotDot p; Asttypes.Open + | Dot -> Parser.expect Dot p; Asttypes.Closed | _ -> Asttypes.Closed in let () = match p.token with @@ -3942,7 +3968,7 @@ and parseRecordOrObjectType ~attrs p = (Diagnostics.message ErrorMessages.sameTypeSpread) | _ -> () in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Typ.object_ ~loc ~attrs fields closedFlag @@ -3950,8 +3976,8 @@ and parseRecordOrObjectType ~attrs p = and parseTypeAlias p typ = match p.Parser.token with | As -> - Parser.next p; - Parser.expect SingleQuote p; + Parser.expect As p; + Parser.expectUnsafe SingleQuote p; let (ident, _loc) = parseLident p in (* TODO: how do we parse attributes here? *) Ast_helper.Typ.alias ~loc:(mkLoc typ.Parsetree.ptyp_loc.loc_start p.prevEndPos) typ ident @@ -3981,18 +4007,18 @@ and parseTypeParameter p = let attrs = parseAttributes p in match p.Parser.token with | Tilde -> - Parser.next p; + Parser.expect Tilde p; let (name, loc) = parseLident p in let lblLocAttr = (Location.mkloc "ns.namedArgLoc" loc, Parsetree.PStr []) in - Parser.expect ~grammar:Grammar.TypeExpression Colon p; + Parser.expectUnsafe ~grammar:Grammar.TypeExpression Colon p; let typ = let typ = parseTypExpr p in {typ with ptyp_attributes = lblLocAttr::typ.ptyp_attributes} in begin match p.Parser.token with | Equal -> - Parser.next p; - Parser.expect Question p; + Parser.expect Equal p; + Parser.expectUnsafe Question p; Some (uncurried, attrs, Asttypes.Optional name, typ, startPos) | _ -> Some (uncurried, attrs, Asttypes.Labelled name, typ, startPos) @@ -4007,12 +4033,12 @@ and parseTypeParameter p = ) in Parser.err ~startPos:loc.loc_start ~endPos:loc.loc_end p error in - Parser.next p; + Parser.expect Colon p; let typ = parseTypExpr p in begin match p.Parser.token with | Equal -> - Parser.next p; - Parser.expect Question p; + Parser.expect Equal p; + Parser.expectUnsafe Question p; Some (uncurried, attrs, Asttypes.Optional name, typ, startPos) | _ -> Some (uncurried, attrs, Asttypes.Labelled name, typ, startPos) @@ -4037,10 +4063,10 @@ and parseTypeParameter p = (* (int, ~x:string, float) *) and parseTypeParameters p = let startPos = p.Parser.startPos in - Parser.expect Lparen p; + Parser.expectUnsafe Lparen p; match p.Parser.token with | Rparen -> - Parser.next p; + Parser.expect Rparen p; let loc = mkLoc startPos p.prevEndPos in let unitConstr = Location.mkloc (Longident.Lident "unit") loc in let typ = Ast_helper.Typ.constr unitConstr [] in @@ -4049,46 +4075,51 @@ and parseTypeParameters p = let params = parseCommaDelimitedRegion ~grammar:Grammar.TypeParameters ~closing:Rparen ~f:parseTypeParameter p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; params and parseEs6ArrowType ~attrs p = let startPos = p.Parser.startPos in match p.Parser.token with | Tilde -> - Parser.next p; + Parser.expect Tilde p; let (name, loc) = parseLident p in let lblLocAttr = (Location.mkloc "ns.namedArgLoc" loc, Parsetree.PStr []) in - Parser.expect ~grammar:Grammar.TypeExpression Colon p; + Parser.expectUnsafe ~grammar:Grammar.TypeExpression Colon p; let typ = let typ = parseTypExpr ~alias:false ~es6Arrow:false p in {typ with ptyp_attributes = lblLocAttr::typ.ptyp_attributes} in let arg = match p.Parser.token with | Equal -> - Parser.next p; - Parser.expect Question p; + Parser.expect Equal p; + Parser.expectUnsafe Question p; Asttypes.Optional name | _ -> Asttypes.Labelled name in - Parser.expect EqualGreater p; + Parser.expectUnsafe EqualGreater p; let returnType = parseTypExpr ~alias:false p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Typ.arrow ~loc ~attrs arg typ returnType | _ -> let parameters = parseTypeParameters p in - Parser.expect EqualGreater p; - let returnType = parseTypExpr ~alias:false p in - let endPos = p.prevEndPos in - let typ = List.fold_right (fun (uncurried, attrs, argLbl, typ, startPos) t -> - let attrs = if uncurried then uncurryAttr::attrs else attrs in - Ast_helper.Typ.arrow ~loc:(mkLoc startPos endPos) ~attrs argLbl typ t - ) parameters returnType - in - {typ with - ptyp_attributes = List.concat [typ.ptyp_attributes; attrs]; - ptyp_loc = mkLoc startPos p.prevEndPos} + if p.token <> Eof then ( + Parser.expect EqualGreater p; + let returnType = parseTypExpr ~alias:false p in + let endPos = p.prevEndPos in + let typ = List.fold_right (fun (uncurried, attrs, argLbl, typ, startPos) t -> + let attrs = if uncurried then uncurryAttr::attrs else attrs in + Ast_helper.Typ.arrow ~loc:(mkLoc startPos endPos) ~attrs argLbl typ t + ) parameters returnType + in + {typ with + ptyp_attributes = List.concat [typ.ptyp_attributes; attrs]; + ptyp_loc = mkLoc startPos p.prevEndPos} + ) else ( + Parser.expectUnsafe EqualGreater p; + Recover.defaultType() + ) (* * typexpr ::= @@ -4133,9 +4164,9 @@ and parseArrowTypeRest ~es6Arrow ~startPos typ p = | (EqualGreater | MinusGreater) as token when es6Arrow == true -> (* error recovery *) if token = MinusGreater then ( - Parser.expect EqualGreater p; + Parser.expectUnsafe EqualGreater p; ); - Parser.next p; + Parser.nextUnsafe p; let returnType = parseTypExpr ~alias:false p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Typ.arrow ~loc Asttypes.Nolabel typ returnType @@ -4157,7 +4188,7 @@ and parseTupleType ~attrs ~first ~startPos p = p ) in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let () = match typexprs with | [_] -> Parser.err ~startPos ~endPos:p.prevEndPos p @@ -4171,7 +4202,7 @@ and parseTypeConstructorArgRegion p = if Grammar.isTypExprStart p.Parser.token then Some (parseTypExpr p) else if p.token = LessThan then ( - Parser.next p; + Parser.expect LessThan p; parseTypeConstructorArgRegion p ) else None @@ -4183,7 +4214,7 @@ and parseTypeConstructorArgs ~constrName p = match opening with | LessThan | Lparen -> Scanner.setDiamondMode p.scanner; - Parser.next p; + Parser.next p [@doesNotRaise]; let typeArgs = (* TODO: change Grammar.TypExprList to TypArgList!!! Why did I wrote this? *) parseCommaDelimitedRegion @@ -4209,9 +4240,9 @@ and parseTypeConstructorArgs ~constrName p = ) |> Doc.toString ~width:80 in Parser.err ~startPos:openingStartPos p (Diagnostics.message msg); - Parser.next p + Parser.expectUnsafe Rparen p | _ -> - Parser.expect GreaterThan p + Parser.expectUnsafe GreaterThan p in Scanner.popMode p.scanner Diamond; typeArgs @@ -4226,21 +4257,21 @@ and parseStringFieldDeclaration p = | String name -> let nameStartPos = p.startPos in let nameEndPos = p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; let fieldName = Location.mkloc name (mkLoc nameStartPos nameEndPos) in - Parser.expect ~grammar:Grammar.TypeExpression Colon p; + Parser.expectUnsafe ~grammar:Grammar.TypeExpression Colon p; let typ = parsePolyTypeExpr p in Some(Parsetree.Otag (fieldName, attrs, typ)) | DotDotDot -> - Parser.next p; + Parser.expect DotDotDot p; let typ = parseTypExpr p in Some(Parsetree.Oinherit typ) | Lident name -> let nameLoc = mkLoc p.startPos p.endPos in Parser.err p (Diagnostics.message (ErrorMessages.objectQuotedFieldName name)); - Parser.next p; + Parser.next p [@doesNotRaise]; let fieldName = Location.mkloc name nameLoc in - Parser.expect ~grammar:Grammar.TypeExpression Colon p; + Parser.expectUnsafe ~grammar:Grammar.TypeExpression Colon p; let typ = parsePolyTypeExpr p in Some(Parsetree.Otag (fieldName, attrs, typ)) | _token -> @@ -4263,7 +4294,7 @@ and parseFieldDeclaration p = let name = Location.mkloc lident loc in let typ = match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; parsePolyTypeExpr p | _ -> Ast_helper.Typ.constr ~loc:name.loc {name with txt = Lident name.txt} [] @@ -4286,7 +4317,7 @@ and parseFieldDeclarationRegion p = let name = Location.mkloc lident loc in let typ = match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; parsePolyTypeExpr p | _ -> Ast_helper.Typ.constr ~loc:name.loc {name with txt = Lident name.txt} [] @@ -4303,7 +4334,7 @@ and parseFieldDeclarationRegion p = *) and parseRecordDeclaration p = Parser.leaveBreadcrumb p Grammar.RecordDecl; - Parser.expect Lbrace p; + Parser.expectUnsafe Lbrace p; let rows = parseCommaDelimitedRegion ~grammar:Grammar.RecordDecl @@ -4311,7 +4342,7 @@ and parseRecordDeclaration p = ~f:parseFieldDeclarationRegion p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; Parser.eatBreadcrumb p; rows @@ -4327,18 +4358,18 @@ and parseRecordDeclaration p = and parseConstrDeclArgs p = let constrArgs = match p.Parser.token with | Lparen -> - Parser.next p; + Parser.expect Lparen p; (* TODO: this could use some cleanup/stratification *) begin match p.Parser.token with | Lbrace -> let lbrace = p.startPos in - Parser.next p; + Parser.expect Lbrace p; let startPos = p.Parser.startPos in begin match p.Parser.token with | DotDot | Dot -> let closedFlag = match p.token with - | DotDot -> Parser.next p; Asttypes.Open - | Dot -> Parser.next p; Asttypes.Closed + | DotDot -> Parser.expect DotDot p; Asttypes.Open + | Dot -> Parser.expect Dot p; Asttypes.Closed | _ -> Asttypes.Closed in let fields = @@ -4348,7 +4379,7 @@ and parseConstrDeclArgs p = ~f:parseStringFieldDeclaration p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let typ = Ast_helper.Typ.object_ ~loc ~attrs:[] fields closedFlag in Parser.optional p Comma |> ignore; @@ -4359,21 +4390,21 @@ and parseConstrDeclArgs p = ~f:parseTypExprRegion p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; Parsetree.Pcstr_tuple (typ::moreArgs) | DotDotDot -> let dotdotdotStart = p.startPos in let dotdotdotEnd = p.endPos in (* start of object type spreading, e.g. `User({...a, "u": int})` *) - Parser.next p; + Parser.expect DotDotDot p; let typ = parseTypExpr p in let () = match p.token with | Rbrace -> (* {...x}, spread without extra fields *) Parser.err ~startPos:dotdotdotStart ~endPos:dotdotdotEnd p (Diagnostics.message ErrorMessages.sameTypeSpread); - Parser.next p; - | _ -> Parser.expect Comma p + Parser.expect Rbrace p; + | _ -> Parser.expectUnsafe Comma p in let () = match p.token with | Lident _ -> @@ -4390,7 +4421,7 @@ and parseConstrDeclArgs p = p ) in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let typ = Ast_helper.Typ.object_ ~loc fields Asttypes.Closed |> parseTypeAlias p @@ -4403,7 +4434,7 @@ and parseConstrDeclArgs p = ~closing:Rparen ~f:parseTypExprRegion p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; Parsetree.Pcstr_tuple (typ::moreArgs) | _ -> let attrs = parseAttributes p in @@ -4427,8 +4458,8 @@ and parseConstrDeclArgs p = (* parse comma after first *) let () = match p.Parser.token with | Rbrace | Eof -> () - | Comma -> Parser.next p - | _ -> Parser.expect Comma p + | Comma -> Parser.expect Comma p + | _ -> Parser.expectUnsafe Comma p in Parser.eatBreadcrumb p; begin match field with @@ -4443,7 +4474,7 @@ and parseConstrDeclArgs p = ~f:parseStringFieldDeclaration p ) in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let typ = Ast_helper.Typ.object_ ~loc ~attrs:[] fields closedFlag @@ -4457,7 +4488,7 @@ and parseConstrDeclArgs p = ~closing:Rparen ~f:parseTypExprRegion p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; Parsetree.Pcstr_tuple (typ::moreArgs) | _ -> let fields = match attrs with @@ -4470,7 +4501,7 @@ and parseConstrDeclArgs p = | attrs -> let first = let field = parseFieldDeclaration p in - Parser.expect Comma p; + Parser.expectUnsafe Comma p; {field with Parsetree.pld_attributes = attrs} in first::( @@ -4487,9 +4518,9 @@ and parseConstrDeclArgs p = ) | _ -> () in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; Parser.optional p Comma |> ignore; - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; Parsetree.Pcstr_record fields end end @@ -4501,14 +4532,14 @@ and parseConstrDeclArgs p = ~f:parseTypExprRegion p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; Parsetree.Pcstr_tuple args end | _ -> Pcstr_tuple [] in let res = match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; Some (parseTypExpr p) | _ -> None in @@ -4523,7 +4554,7 @@ and parseConstrDeclArgs p = match p.Parser.token with | Bar -> let startPos = p.Parser.startPos in - Parser.next p; + Parser.expect Bar p; Some (parseTypeConstructorDeclaration ~startPos p) | _ -> None @@ -4533,7 +4564,7 @@ and parseConstrDeclArgs p = match p.Parser.token with | Uident uident -> let uidentLoc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; let (args, res) = parseConstrDeclArgs p in Parser.eatBreadcrumb p; let loc = mkLoc startPos p.prevEndPos in @@ -4583,7 +4614,7 @@ and parseTypeRepresentation p = | Lbrace -> Parsetree.Ptype_record (parseRecordDeclaration p) | DotDot -> - Parser.next p; + Parser.expect DotDot p; Ptype_open | token -> Parser.err p (Diagnostics.unexpected token p.breadcrumbs); @@ -4605,26 +4636,27 @@ and parseTypeRepresentation p = *) and parseTypeParam p = let variance = match p.Parser.token with - | Plus -> Parser.next p; Asttypes.Covariant - | Minus -> Parser.next p; Contravariant + | Plus -> Parser.expect Plus p; Asttypes.Covariant + | Minus -> Parser.expect Minus p; Contravariant | _ -> Invariant in match p.Parser.token with | SingleQuote -> - Parser.next p; + Parser.expect SingleQuote p; let (ident, loc) = parseIdent ~msg:ErrorMessages.typeParam ~startPos:p.startPos p in Some (Ast_helper.Typ.var ~loc ident, variance) | Underscore -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.expect Underscore p; Some (Ast_helper.Typ.any ~loc (), variance) | (Uident _ | Lident _) as token -> Parser.err p (Diagnostics.message ( "Type params start with a singlequote: '" ^ (Token.toString token) )); let (ident, loc) = - parseIdent ~msg:ErrorMessages.typeParam ~startPos:p.startPos p in + parseIdentNoNext ~msg:ErrorMessages.typeParam ~startPos:p.startPos p in + Parser.next p [@doesNotRaise]; Some (Ast_helper.Typ.var ~loc ident, variance) | _token -> None @@ -4644,7 +4676,7 @@ and parseTypeParams ~parent p = Scanner.setDiamondMode p.scanner; let openingStartPos = p.startPos in Parser.leaveBreadcrumb p Grammar.TypeParams; - Parser.next p; + Parser.next p [@doesNotRaise]; let params = parseCommaDelimitedRegion ~grammar:Grammar.TypeParams @@ -4671,9 +4703,9 @@ and parseTypeParams ~parent p = ) |> Doc.toString ~width:80 in Parser.err ~startPos:openingStartPos p (Diagnostics.message msg); - Parser.next p + Parser.nextUnsafe p | _ -> - Parser.expect GreaterThan p + Parser.expectUnsafe GreaterThan p in Scanner.popMode p.scanner Diamond; Parser.eatBreadcrumb p; @@ -4684,14 +4716,14 @@ and parseTypeParams ~parent p = and parseTypeConstraint p = let startPos = p.Parser.startPos in match p.Parser.token with - | Token.Constraint -> - Parser.next p; - Parser.expect SingleQuote p; + | Constraint -> + Parser.expect Constraint p; + Parser.expectUnsafe SingleQuote p; begin match p.Parser.token with | Lident ident -> let identLoc = mkLoc startPos p.endPos in - Parser.next p; - Parser.expect Equal p; + Parser.next p [@doesNotRaise]; + Parser.expectUnsafe Equal p; let typ = parseTypExpr p in let loc = mkLoc startPos p.prevEndPos in Some (Ast_helper.Typ.var ~loc:identLoc ident, typ, loc) @@ -4718,10 +4750,10 @@ and parseTypeEquationOrConstrDecl p = let uidentStartPos = p.Parser.startPos in match p.Parser.token with | Uident uident -> - Parser.next p; + Parser.next p [@doesNotRaise]; begin match p.Parser.token with | Dot -> - Parser.next p; + Parser.expect Dot p; let typeConstr = parseValuePathTail p uidentStartPos (Longident.Lident uident) in @@ -4731,11 +4763,11 @@ and parseTypeEquationOrConstrDecl p = ) in begin match p.token with | Equal -> - Parser.next p; + Parser.expect Equal p; let (priv, kind) = parseTypeRepresentation p in (Some typ, priv, kind) | EqualGreater -> - Parser.next p; + Parser.expect EqualGreater p; let returnType = parseTypExpr ~alias:false p in let loc = mkLoc uidentStartPos p.prevEndPos in let arrowType = Ast_helper.Typ.arrow ~loc Asttypes.Nolabel typ returnType in @@ -4763,12 +4795,12 @@ and parseTypeEquationOrConstrDecl p = and parseRecordOrObjectDecl p = let startPos = p.Parser.startPos in - Parser.expect Lbrace p; + Parser.expectUnsafe Lbrace p; match p.Parser.token with | DotDot | Dot -> let closedFlag = match p.token with - | DotDot -> Parser.next p; Asttypes.Open - | Dot -> Parser.next p; Asttypes.Closed + | DotDot -> Parser.expect DotDot p; Asttypes.Open + | Dot -> Parser.expect Dot p; Asttypes.Closed | _ -> Asttypes.Closed in let fields = @@ -4778,7 +4810,7 @@ and parseRecordOrObjectDecl p = ~f:parseStringFieldDeclaration p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let typ = Ast_helper.Typ.object_ ~loc ~attrs:[] fields closedFlag @@ -4790,15 +4822,15 @@ and parseRecordOrObjectDecl p = let dotdotdotStart = p.startPos in let dotdotdotEnd = p.endPos in (* start of object type spreading, e.g. `type u = {...a, "u": int}` *) - Parser.next p; + Parser.expect DotDotDot p; let typ = parseTypExpr p in let () = match p.token with | Rbrace -> (* {...x}, spread without extra fields *) Parser.err ~startPos:dotdotdotStart ~endPos:dotdotdotEnd p (Diagnostics.message ErrorMessages.sameTypeSpread); - Parser.next p; - | _ -> Parser.expect Comma p + Parser.expect Rbrace p; + | _ -> Parser.expectUnsafe Comma p in let () = match p.token with | Lident _ -> @@ -4815,7 +4847,7 @@ and parseRecordOrObjectDecl p = p ) in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let typ = Ast_helper.Typ.object_ ~loc fields Asttypes.Closed |> parseTypeAlias p @@ -4844,8 +4876,8 @@ and parseRecordOrObjectDecl p = (* parse comma after first *) let () = match p.Parser.token with | Rbrace | Eof -> () - | Comma -> Parser.next p - | _ -> Parser.expect Comma p + | Comma -> Parser.expect Comma p + | _ -> Parser.expectUnsafe Comma p in Parser.eatBreadcrumb p; begin match field with @@ -4861,7 +4893,7 @@ and parseRecordOrObjectDecl p = p ) in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in let typ = Ast_helper.Typ.object_ ~loc ~attrs:[] fields closedFlag |> parseTypeAlias p @@ -4903,13 +4935,13 @@ and parseRecordOrObjectDecl p = ) | _ -> () in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; Parser.eatBreadcrumb p; (None, Asttypes.Public, Parsetree.Ptype_record fields) end and parsePrivateEqOrRepr p = - Parser.expect Private p; + Parser.expectUnsafe Private p; match p.Parser.token with | Lbrace -> let (manifest, _ ,kind) = parseRecordOrObjectDecl p in @@ -4943,10 +4975,10 @@ and parsePrivateEqOrRepr p = *) and parsePolymorphicVariantType ~attrs p = let startPos = p.Parser.startPos in - Parser.expect Lbracket p; + Parser.expectUnsafe Lbracket p; match p.token with | GreaterThan -> - Parser.next p; + Parser.expect GreaterThan p; let rowFields = begin match p.token with | Rbracket -> @@ -4961,22 +4993,23 @@ and parsePolymorphicVariantType ~attrs p = let variant = let loc = mkLoc startPos p.prevEndPos in Ast_helper.Typ.variant ~attrs ~loc rowFields Open None in - Parser.expect Rbracket p; + Parser.expectUnsafe Rbracket p; variant | LessThan -> - Parser.next p; + Parser.expect LessThan p; Parser.optional p Bar |> ignore; let rowField = parseTagSpecFull p in let rowFields = parseTagSpecFulls p in let tagNames = if p.token == GreaterThan then begin - Parser.next p; + Parser.nextUnsafe p; let rec loop p = match p.Parser.token with | Rbracket -> [] | _ -> - let (ident, _loc) = parseHashIdent ~startPos:p.startPos p in - ident :: loop p + (match parseHashIdent ~startPos:p.startPos p with + | Some (ident, _loc) -> ident :: loop p + | None -> []) in loop p end @@ -4984,7 +5017,7 @@ and parsePolymorphicVariantType ~attrs p = let variant = let loc = mkLoc startPos p.prevEndPos in Ast_helper.Typ.variant ~attrs ~loc (rowField :: rowFields) Closed (Some tagNames) in - Parser.expect Rbracket p; + Parser.expectUnsafe Rbracket p; variant | _ -> let rowFields1 = parseTagSpecFirst p in @@ -4992,7 +5025,7 @@ and parsePolymorphicVariantType ~attrs p = let variant = let loc = mkLoc startPos p.prevEndPos in Ast_helper.Typ.variant ~attrs ~loc (rowFields1 @ rowFields2) Closed None in - Parser.expect Rbracket p; + Parser.expectUnsafe Rbracket p; variant and parseTagSpecFulls p = @@ -5002,7 +5035,7 @@ and parseTagSpecFulls p = | GreaterThan -> [] | Bar -> - Parser.next p; + Parser.expect Bar p; let rowField = parseTagSpecFull p in rowField ::parseTagSpecFulls p | _ -> @@ -5020,7 +5053,7 @@ and parseTagSpecFull p = and parseTagSpecs p = match p.Parser.token with | Bar -> - Parser.next p; + Parser.expect Bar p; let rowField = parseTagSpec p in rowField :: parseTagSpecs p | _ -> @@ -5039,7 +5072,7 @@ and parseTagSpecFirst p = let attrs = parseAttributes p in match p.Parser.token with | Bar -> - Parser.next p; + Parser.expect Bar p; [parseTagSpec p] | Hash -> [parsePolymorphicVariantTypeSpecHash ~attrs ~full:false p] @@ -5050,17 +5083,19 @@ and parseTagSpecFirst p = (* example: [ListStyleType.t] *) [Parsetree.Rinherit typ;] | _ -> - Parser.expect Bar p; + Parser.expectUnsafe Bar p; [Parsetree.Rinherit typ; parseTagSpec p] end and parsePolymorphicVariantTypeSpecHash ~attrs ~full p : Parsetree.row_field = let startPos = p.Parser.startPos in - let (ident, loc) = parseHashIdent ~startPos p in + let (ident, loc) = match parseHashIdent ~startPos p with + | None -> assert false + | Some x -> x in let rec loop p = match p.Parser.token with | Band when full -> - Parser.next p; + Parser.expect Band p; let rowField = parsePolymorphicVariantTypeArgs p in rowField :: loop p | _ -> @@ -5069,7 +5104,7 @@ and parsePolymorphicVariantTypeSpecHash ~attrs ~full p : Parsetree.row_field = let firstTuple, tagContainsAConstantEmptyConstructor = match p.Parser.token with | Band when full -> - Parser.next p; + Parser.expect Band p; [parsePolymorphicVariantTypeArgs p], true | Lparen -> [parsePolymorphicVariantTypeArgs p], false @@ -5086,14 +5121,14 @@ and parsePolymorphicVariantTypeSpecHash ~attrs ~full p : Parsetree.row_field = and parsePolymorphicVariantTypeArgs p = let startPos = p.Parser.startPos in - Parser.expect Lparen p; + Parser.expectUnsafe Lparen p; let args = parseCommaDelimitedRegion ~grammar:Grammar.TypExprList ~closing:Rparen ~f:parseTypExprRegion p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let attrs = [] in let loc = mkLoc startPos p.prevEndPos in match args with @@ -5108,8 +5143,8 @@ and parsePolymorphicVariantTypeArgs p = and parseTypeEquationAndRepresentation p = match p.Parser.token with | Equal | Bar as token -> - if token = Bar then Parser.expect Equal p; - Parser.next p; + if token = Bar then Parser.expectUnsafe Equal p; + Parser.next p [@doesNotRaise]; begin match p.Parser.token with | Uident _ -> parseTypeEquationOrConstrDecl p @@ -5124,7 +5159,7 @@ and parseTypeEquationAndRepresentation p = let manifest = Some (parseTypExpr p) in begin match p.Parser.token with | Equal -> - Parser.next p; + Parser.expect Equal p; let (priv, kind) = parseTypeRepresentation p in (manifest, priv, kind) | _ -> @@ -5158,7 +5193,7 @@ and parseTypeDef ~attrs ~startPos p = typeDef and parseTypeExtension ~params ~attrs ~name p = - Parser.expect PlusEqual p; + Parser.expectUnsafe PlusEqual p; let priv = if Parser.optional p Token.Private then Asttypes.Private @@ -5169,7 +5204,7 @@ and parseTypeExtension ~params ~attrs ~name p = let first = let (attrs, name, kind) = match p.Parser.token with | Bar -> - Parser.next p; + Parser.expect Bar p; parseConstrDef ~parseAttrs:true p | _ -> parseConstrDef ~parseAttrs:true p @@ -5181,7 +5216,7 @@ and parseTypeExtension ~params ~attrs ~name p = match p.Parser.token with | Bar -> let startPos = p.Parser.startPos in - Parser.next p; + Parser.expect Bar p; let (attrs, name, kind) = parseConstrDef ~parseAttrs:true p in let extConstr = Ast_helper.Te.constructor ~attrs ~loc:(mkLoc startPos p.prevEndPos) name kind @@ -5207,11 +5242,11 @@ and parseTypeDefinitions ~attrs ~name ~params ~startPos p = let attrs = parseAttributesAndBinding p in match p.Parser.token with | And -> - Parser.next p; + Parser.expect And p; let attrs = match p.token with | Export -> let exportLoc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.expect Export p; let genTypeAttr = (Location.mkloc "genType" exportLoc, Parsetree.PStr []) in genTypeAttr::attrs | _ -> attrs @@ -5229,11 +5264,11 @@ and parseTypeDefinitions ~attrs ~name ~params ~startPos p = * this territory of the grammar *) and parseTypeDefinitionOrExtension ~attrs p = let startPos = p.Parser.startPos in - Parser.expect Token.Typ p; + Parser.expectUnsafe Typ p; let recFlag = match p.token with - | Rec -> Parser.next p; Asttypes.Recursive + | Rec -> Parser.expect Rec p; Asttypes.Recursive | Lident "nonrec" -> - Parser.next p; + Parser.next p [@doesNotRaise]; Asttypes.Nonrecursive | _ -> Asttypes.Nonrecursive in @@ -5256,16 +5291,16 @@ and parseTypeDefinitionOrExtension ~attrs p = (* external value-name : typexp = external-declaration *) and parseExternalDef ~attrs ~startPos p = Parser.leaveBreadcrumb p Grammar.External; - Parser.expect Token.External p; + Parser.expectUnsafe Token.External p; let (name, loc) = parseLident p in let name = Location.mkloc name loc in - Parser.expect ~grammar:(Grammar.TypeExpression) Colon p; + Parser.expectUnsafe ~grammar:(Grammar.TypeExpression) Colon p; let typExpr = parseTypExpr p in let equalStart = p.startPos in let equalEnd = p.endPos in - Parser.expect Equal p; + Parser.expectUnsafe Equal p; let prim = match p.token with - | String s -> Parser.next p; [s] + | String s -> Parser.next p [@doesNotRaise]; [s] | _ -> Parser.err ~startPos:equalStart ~endPos:equalEnd p (Diagnostics.message @@ -5290,7 +5325,7 @@ and parseConstrDef ~parseAttrs p = let name = match p.Parser.token with | Uident name -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; Location.mkloc name loc | t -> Parser.err p (Diagnostics.uident t); @@ -5301,11 +5336,11 @@ and parseConstrDef ~parseAttrs p = let (args, res) = parseConstrDeclArgs p in Parsetree.Pext_decl (args, res) | Equal -> - Parser.next p; + Parser.expect Equal p; let longident = parseModuleLongIdent ~lowercase:false p in Parsetree.Pext_rebind longident | Colon -> - Parser.next p; + Parser.expect Colon p; let typ = parseTypExpr p in Parsetree.Pext_decl (Pcstr_tuple [], Some typ) | _ -> @@ -5322,7 +5357,7 @@ and parseConstrDef ~parseAttrs p = * constr ::= long_uident *) and parseExceptionDef ~attrs p = let startPos = p.Parser.startPos in - Parser.expect Token.Exception p; + Parser.expectUnsafe Token.Exception p; let (_, name, kind) = parseConstrDef ~parseAttrs:false p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Te.constructor ~loc ~attrs name kind @@ -5330,7 +5365,7 @@ and parseExceptionDef ~attrs p = and parseNewlineOrSemicolonStructure p = match p.Parser.token with | Semicolon -> - Parser.next p + Parser.expect Semicolon p | token when Grammar.isStructureItemStart token -> if p.prevEndPos.pos_lnum < p.startPos.pos_lnum then () else @@ -5434,9 +5469,9 @@ and parseStructureItemRegion p = [@@progress (Parser.next, Parser.expect, Parser.checkProgress)] and parseJsImport ~startPos ~attrs p = - Parser.expect Token.Import p; + Parser.expectUnsafe Import p; let importSpec = match p.Parser.token with - | Token.Lident _ | Token.At -> + | Lident _ | At -> let decl = match parseJsFfiDeclaration p with | Some decl -> decl | None -> assert false @@ -5450,7 +5485,7 @@ and parseJsImport ~startPos ~attrs p = and parseJsExport ~attrs p = let exportStart = p.Parser.startPos in - Parser.expect Token.Export p; + Parser.expectUnsafe Export p; let exportLoc = mkLoc exportStart p.prevEndPos in let genTypeAttr = (Location.mkloc "genType" exportLoc, Parsetree.PStr []) in let attrs = genTypeAttr::attrs in @@ -5468,7 +5503,7 @@ and parseJsExport ~attrs p = and parseSignJsExport ~attrs p = let exportStart = p.Parser.startPos in - Parser.expect Token.Export p; + Parser.expectUnsafe Export p; let exportLoc = mkLoc exportStart p.prevEndPos in let genTypeAttr = (Location.mkloc "genType" exportLoc, Parsetree.PStr []) in let attrs = genTypeAttr::attrs in @@ -5490,9 +5525,9 @@ and parseSignJsExport ~attrs p = and parseJsFfiScope p = match p.Parser.token with | Token.Lident "from" -> - Parser.next p; + Parser.next p [@doesNotRaise]; begin match p.token with - | String s -> Parser.next p; JsFfi.Module s + | String s -> Parser.next p [@doesNotRaise]; JsFfi.Module s | Uident _ | Lident _ -> let value = parseIdentPath p in JsFfi.Scope value @@ -5501,14 +5536,14 @@ and parseJsFfiScope p = | _ -> JsFfi.Global and parseJsFfiDeclarations p = - Parser.expect Token.Lbrace p; + Parser.expectUnsafe Lbrace p; let decls = parseCommaDelimitedRegion ~grammar:Grammar.JsFfiImport ~closing:Rbrace ~f:parseJsFfiDeclaration p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; decls and parseJsFfiDeclaration p = @@ -5519,13 +5554,13 @@ and parseJsFfiDeclaration p = let (ident, _) = parseLident p in let alias = match p.token with | As -> - Parser.next p; + Parser.expect As p; let (ident, _) = parseLident p in ident | _ -> ident in - Parser.expect Token.Colon p; + Parser.expectUnsafe Colon p; let typ = parseTypExpr p in let loc = mkLoc startPos p.prevEndPos in Some (JsFfi.decl ~loc ~alias ~attrs ~name:ident ~typ) @@ -5534,7 +5569,7 @@ and parseJsFfiDeclaration p = (* include-statement ::= include module-expr *) and parseIncludeStatement ~attrs p = let startPos = p.Parser.startPos in - Parser.expect Token.Include p; + Parser.expectUnsafe Include p; let modExpr = parseModuleExpr p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Incl.mk ~loc ~attrs modExpr @@ -5546,7 +5581,7 @@ and parseAtomicModuleExpr p = let longident = parseModuleLongIdent ~lowercase:false p in Ast_helper.Mod.ident ~loc:longident.loc longident | Lbrace -> - Parser.next p; + Parser.expect Lbrace p; let structure = Ast_helper.Mod.structure ( parseDelimitedRegion ~grammar:Grammar.Structure @@ -5554,30 +5589,30 @@ and parseAtomicModuleExpr p = ~f:parseStructureItemRegion p ) in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let endPos = p.prevEndPos in {structure with pmod_loc = mkLoc startPos endPos} | Lparen -> - Parser.next p; + Parser.expect Lparen p; let modExpr = match p.token with | Rparen -> Ast_helper.Mod.structure ~loc:(mkLoc startPos p.prevEndPos) [] | _ -> parseConstrainedModExpr p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; modExpr | Lident "unpack" -> (* TODO: should this be made a keyword?? *) - Parser.next p; + Parser.next p [@doesNotRaise]; Parser.expect Lparen p; let expr = parseExpr p in begin match p.Parser.token with | Colon -> let colonStart = p.Parser.startPos in - Parser.next p; + Parser.expect Colon p; let attrs = parseAttributes p in let packageType = parsePackageType ~startPos:colonStart ~attrs p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let loc = mkLoc startPos p.prevEndPos in let constraintExpr = Ast_helper.Exp.constraint_ ~loc @@ -5585,7 +5620,7 @@ and parseAtomicModuleExpr p = in Ast_helper.Mod.unpack ~loc constraintExpr | _ -> - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Mod.unpack ~loc expr end @@ -5621,17 +5656,17 @@ and parseFunctorArg p = let attrs = parseAttributes p in match p.Parser.token with | Uident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; let uidentEndPos = p.prevEndPos in begin match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; let moduleType = parseModuleType p in let loc = mkLoc startPos uidentEndPos in let argName = Location.mkloc ident loc in Some (attrs, argName, Some moduleType, startPos) | Dot -> - Parser.next p; + Parser.expect Dot p; let moduleType = let moduleLongIdent = parseModuleLongIdentTail ~lowercase:false p startPos (Longident.Lident ident) in @@ -5647,14 +5682,14 @@ and parseFunctorArg p = Some (attrs, argName, Some moduleType, startPos) end | Underscore -> - Parser.next p; + Parser.expect Underscore p; let argName = Location.mkloc "_" (mkLoc startPos p.prevEndPos) in - Parser.expect Colon p; + Parser.expectUnsafe Colon p; let moduleType = parseModuleType p in Some (attrs, argName, Some moduleType, startPos) | Lparen -> - Parser.next p; - Parser.expect Rparen p; + Parser.expect Lparen p; + Parser.expectUnsafe Rparen p; let argName = Location.mkloc "*" (mkLoc startPos p.prevEndPos) in Some (attrs, argName, None, startPos) | _ -> @@ -5662,7 +5697,7 @@ and parseFunctorArg p = and parseFunctorArgs p = let startPos = p.Parser.startPos in - Parser.expect Lparen p; + Parser.expectUnsafe Lparen p; let args = parseCommaDelimitedRegion ~grammar:Grammar.FunctorArgs @@ -5670,7 +5705,7 @@ and parseFunctorArgs p = ~f:parseFunctorArg p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; match args with | [] -> [[], Location.mkloc "*" (mkLoc startPos p.prevEndPos), None, startPos] @@ -5681,11 +5716,11 @@ and parseFunctorModuleExpr p = let args = parseFunctorArgs p in let returnType = match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; Some (parseModuleType ~es6Arrow:false p) | _ -> None in - Parser.expect EqualGreater p; + Parser.expectUnsafe EqualGreater p; let rhsModuleExpr = let modExpr = parseModuleExpr p in match returnType with @@ -5727,7 +5762,7 @@ and parseConstrainedModExpr p = let modExpr = parseModuleExpr p in match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; let modType = parseModuleType p in let loc = mkLoc modExpr.pmod_loc.loc_start modType.pmty_loc.loc_end in Ast_helper.Mod.constraint_ ~loc modExpr modType @@ -5749,7 +5784,7 @@ and parseModuleApplication p modExpr = ~f:parseConstrainedModExprRegion p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; let args = match args with | [] -> let loc = mkLoc startPos p.prevEndPos in @@ -5764,7 +5799,7 @@ and parseModuleApplication p modExpr = and parseModuleOrModuleTypeImplOrPackExpr ~attrs p = let startPos = p.Parser.startPos in - Parser.expect Module p; + Parser.expectUnsafe Module p; match p.Parser.token with | Typ -> parseModuleTypeImpl ~attrs startPos p | Lparen -> @@ -5776,22 +5811,22 @@ and parseModuleOrModuleTypeImplOrPackExpr ~attrs p = | _ -> parseMaybeRecModuleBinding ~attrs ~startPos p and parseModuleTypeImpl ~attrs startPos p = - Parser.expect Typ p; + Parser.expectUnsafe Typ p; let nameStart = p.Parser.startPos in let name = match p.Parser.token with | Lident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc nameStart p.prevEndPos in Location.mkloc ident loc | Uident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc nameStart p.prevEndPos in Location.mkloc ident loc | t -> Parser.err p (Diagnostics.uident t); Location.mknoloc "_" in - Parser.expect Equal p; + Parser.expectUnsafe Equal p; let moduleType = parseModuleType p in let moduleTypeDeclaration = Ast_helper.Mtd.mk @@ -5808,8 +5843,8 @@ and parseModuleTypeImpl ~attrs startPos p = : module-type = module-expr } *) and parseMaybeRecModuleBinding ~attrs ~startPos p = match p.Parser.token with - | Token.Rec -> - Parser.next p; + | Rec -> + Parser.expect Rec p; Ast_helper.Str.rec_module (parseModuleBindings ~startPos ~attrs p) | _ -> Ast_helper.Str.module_ (parseModuleBinding ~attrs ~startPos:p.Parser.startPos p) @@ -5818,7 +5853,7 @@ and parseModuleBinding ~attrs ~startPos p = let name = match p.Parser.token with | Uident ident -> let startPos = p.Parser.startPos in - Parser.next p; + Parser.next p [@doesNotRaise]; let loc = mkLoc startPos p.prevEndPos in Location.mkloc ident loc | t -> @@ -5833,11 +5868,11 @@ and parseModuleBindingBody p = (* TODO: make required with good error message when rec module binding *) let returnModType = match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; Some (parseModuleType p) | _ -> None in - Parser.expect Equal p; + Parser.expectUnsafe Equal p; let modExpr = parseModuleExpr p in match returnModType with | Some modType -> @@ -5855,7 +5890,7 @@ and parseModuleBindings ~attrs ~startPos p = let attrs = parseAttributesAndBinding p in match p.Parser.token with | And -> - Parser.next p; + Parser.expect And p; ignore(Parser.optional p Module); (* over-parse for fault-tolerance *) let modBinding = parseModuleBinding ~attrs ~startPos p in loop p (modBinding::acc) @@ -5873,12 +5908,12 @@ and parseAtomicModuleType p = let moduleLongIdent = parseModuleLongIdent ~lowercase:true p in Ast_helper.Mty.ident ~loc:moduleLongIdent.loc moduleLongIdent | Lparen -> - Parser.next p; + Parser.expect Lparen p; let mty = parseModuleType p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; {mty with pmty_loc = mkLoc startPos p.prevEndPos} | Lbrace -> - Parser.next p; + Parser.expect Lbrace p; let spec = parseDelimitedRegion ~grammar:Grammar.Signature @@ -5886,7 +5921,7 @@ and parseAtomicModuleType p = ~f:parseSignatureItemRegion p in - Parser.expect Rbrace p; + Parser.expectUnsafe Rbrace p; let loc = mkLoc startPos p.prevEndPos in Ast_helper.Mty.signature ~loc spec | Module -> (* TODO: check if this is still atomic when implementing first class modules*) @@ -5905,7 +5940,7 @@ and parseAtomicModuleType p = and parseFunctorModuleType p = let startPos = p.Parser.startPos in let args = parseFunctorArgs p in - Parser.expect EqualGreater p; + Parser.expectUnsafe EqualGreater p; let rhs = parseModuleType p in let endPos = p.prevEndPos in let modType = List.fold_right (fun (attrs, name, moduleType, startPos) acc -> @@ -5939,7 +5974,7 @@ and parseFunctorModuleType p = let modty = parseAtomicModuleType p in match p.Parser.token with | EqualGreater when es6Arrow == true -> - Parser.next p; + Parser.expect EqualGreater p; let rhs = parseModuleType ~with_:false p in let str = Location.mknoloc "_" in let loc = mkLoc modty.pmty_loc.loc_start p.prevEndPos in @@ -5957,12 +5992,12 @@ and parseFunctorModuleType p = and parseWithConstraints moduleType p = match p.Parser.token with | Lident "with" -> - Parser.next p; + Parser.next p [@doesNotRaise]; let first = parseWithConstraint p in let rec loop p acc = match p.Parser.token with | And -> - Parser.next p; + Parser.expect And p; loop p ((parseWithConstraint p)::acc) | _ -> List.rev acc @@ -5983,15 +6018,15 @@ and parseWithConstraints moduleType p = and parseWithConstraint p = match p.Parser.token with | Module -> - Parser.next p; + Parser.expect Module p; let modulePath = parseModuleLongIdent ~lowercase:false p in begin match p.Parser.token with | ColonEqual -> - Parser.next p; + Parser.expect ColonEqual p; let lident = parseModuleLongIdent ~lowercase:false p in Parsetree.Pwith_modsubst (modulePath, lident) | Equal -> - Parser.next p; + Parser.expect Equal p; let lident = parseModuleLongIdent ~lowercase:false p in Parsetree.Pwith_module (modulePath, lident) | token -> @@ -6001,12 +6036,12 @@ and parseWithConstraint p = Parsetree.Pwith_modsubst (modulePath, lident) end | Typ -> - Parser.next p; + Parser.expect Typ p; let typeConstr = parseValuePath p in let params = parseTypeParams ~parent:typeConstr p in begin match p.Parser.token with | ColonEqual -> - Parser.next p; + Parser.expect ColonEqual p; let typExpr = parseTypExpr p in Parsetree.Pwith_typesubst ( typeConstr, @@ -6017,7 +6052,7 @@ and parseWithConstraint p = (Location.mkloc (Longident.last typeConstr.txt) typeConstr.loc) ) | Equal -> - Parser.next p; + Parser.expect Equal p; let typExpr = parseTypExpr p in let typeConstraints = parseTypeConstraints p in Parsetree.Pwith_type ( @@ -6058,16 +6093,16 @@ and parseWithConstraint p = and parseModuleTypeOf p = let startPos = p.Parser.startPos in - Parser.expect Module p; - Parser.expect Typ p; - Parser.expect Of p; + Parser.expectUnsafe Module p; + Parser.expectUnsafe Typ p; + Parser.expectUnsafe Of p; let moduleExpr = parseModuleExpr p in Ast_helper.Mty.typeof_ ~loc:(mkLoc startPos p.prevEndPos) moduleExpr and parseNewlineOrSemicolonSignature p = match p.Parser.token with | Semicolon -> - Parser.next p + Parser.expect Semicolon p | token when Grammar.isSignatureItemStart token -> if p.prevEndPos.pos_lnum < p.startPos.pos_lnum then () else @@ -6124,7 +6159,7 @@ and parseSignatureItemRegion p = let loc = mkLoc startPos p.prevEndPos in Some (Ast_helper.Sig.open_ ~loc openDescription) | Include -> - Parser.next p; + Parser.expect Include p; let moduleType = parseModuleType p in let includeDescription = Ast_helper.Incl.mk ~loc:(mkLoc startPos p.prevEndPos) @@ -6136,7 +6171,7 @@ and parseSignatureItemRegion p = Some (Ast_helper.Sig.include_ ~loc includeDescription) | Module -> Parser.beginRegion p; - Parser.next p; + Parser.expect Module p; begin match p.Parser.token with | Uident _ -> let modDecl = parseModuleDeclarationOrAlias ~attrs p in @@ -6172,7 +6207,7 @@ and parseSignatureItemRegion p = let loc = mkLoc startPos p.prevEndPos in Some (Ast_helper.Sig.extension ~attrs ~loc extension) | Import -> - Parser.next p; + Parser.expect Import p; parseSignatureItemRegion p | _ -> begin match attrs with @@ -6190,7 +6225,7 @@ and parseSignatureItemRegion p = (* module rec module-name : module-type { and module-name: module-type } *) and parseRecModuleSpec ~attrs ~startPos p = - Parser.expect Rec p; + Parser.expectUnsafe Rec p; let rec loop p spec = let startPos = p.Parser.startPos in let attrs = parseAttributesAndBinding p in @@ -6216,13 +6251,13 @@ and parseRecModuleDeclaration ~attrs ~startPos p = let name = match p.Parser.token with | Uident modName -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; Location.mkloc modName loc | t -> Parser.err p (Diagnostics.uident t); Location.mknoloc "_" in - Parser.expect Colon p; + Parser.expectUnsafe Colon p; let modType = parseModuleType p in Ast_helper.Md.mk ~loc:(mkLoc startPos p.prevEndPos) ~attrs name modType @@ -6231,7 +6266,7 @@ and parseModuleDeclarationOrAlias ~attrs p = let moduleName = match p.Parser.token with | Uident ident -> let loc = mkLoc p.Parser.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; Location.mkloc ident loc | t -> Parser.err p (Diagnostics.uident t); @@ -6239,10 +6274,10 @@ and parseModuleDeclarationOrAlias ~attrs p = in let body = match p.Parser.token with | Colon -> - Parser.next p; + Parser.expect Colon p; parseModuleType p | Equal -> - Parser.next p; + Parser.expect Equal p; let lident = parseModuleLongIdent ~lowercase:false p in Ast_helper.Mty.alias lident | token -> @@ -6253,15 +6288,15 @@ and parseModuleDeclarationOrAlias ~attrs p = Ast_helper.Md.mk ~loc ~attrs moduleName body and parseModuleTypeDeclaration ~attrs ~startPos p = - Parser.expect Typ p; + Parser.expectUnsafe Typ p; let moduleName = match p.Parser.token with | Uident ident -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; Location.mkloc ident loc | Lident ident -> let loc = mkLoc p.startPos p.endPos in - Parser.next p; + Parser.next p [@doesNotRaise]; Location.mkloc ident loc | t -> Parser.err p (Diagnostics.uident t); @@ -6269,7 +6304,7 @@ and parseModuleTypeDeclaration ~attrs ~startPos p = in let typ = match p.Parser.token with | Equal -> - Parser.next p; + Parser.expect Equal p; Some (parseModuleType p) | _ -> None in @@ -6281,7 +6316,7 @@ and parseSignLetDesc ~attrs p = Parser.optional p Let |> ignore; let (name, loc) = parseLident p in let name = Location.mkloc name loc in - Parser.expect Colon p; + Parser.expectUnsafe Colon p; let typExpr = parsePolyTypeExpr p in let loc = mkLoc startPos p.prevEndPos in Ast_helper.Val.mk ~loc ~attrs name typExpr @@ -6293,17 +6328,17 @@ and parseAttributeId ~startPos p = let rec loop p acc = match p.Parser.token with | Lident ident | Uident ident -> - Parser.next p; + Parser.next p [@doesNotRaise]; let id = acc ^ ident in begin match p.Parser.token with - | Dot -> Parser.next p; loop p (id ^ ".") + | Dot -> Parser.expect Dot p; loop p (id ^ ".") | _ -> id end | token when Token.isKeyword token -> - Parser.next p; + Parser.next p [@doesNotRaise]; let id = acc ^ (Token.toString token) in begin match p.Parser.token with - | Dot -> Parser.next p; loop p (id ^ ".") + | Dot -> Parser.expect Dot p; loop p (id ^ ".") | _ -> id end | token -> @@ -6328,10 +6363,10 @@ and parsePayload p = match p.Parser.token with | Lparen when p.startPos.pos_cnum = p.prevEndPos.pos_cnum -> Parser.leaveBreadcrumb p Grammar.AttributePayload; - Parser.next p; + Parser.expect Lparen p; begin match p.token with | Colon -> - Parser.next p; + Parser.expect Colon p; let payload = if Grammar.isSignatureItemStart p.token then Parsetree.PSig ( parseDelimitedRegion @@ -6343,20 +6378,20 @@ and parsePayload p = else Parsetree.PTyp (parseTypExpr p) in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; Parser.eatBreadcrumb p; payload | Question -> - Parser.next p; + Parser.expect Question p; let pattern = parsePattern p in let expr = match p.token with | When | If -> - Parser.next p; + Parser.next p [@doesNotRaise]; Some (parseExpr p) | _ -> None in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; Parser.eatBreadcrumb p; Parsetree.PPat (pattern, expr) | _ -> @@ -6366,7 +6401,7 @@ and parsePayload p = ~f:parseStructureItemRegion p in - Parser.expect Rparen p; + Parser.expectUnsafe Rparen p; Parser.eatBreadcrumb p; Parsetree.PStr items end @@ -6377,7 +6412,7 @@ and parseAttribute p = match p.Parser.token with | At -> let startPos = p.startPos in - Parser.next p; + Parser.expect At p; let attrId = parseAttributeId ~startPos p in let payload = parsePayload p in Some(attrId, payload) @@ -6395,7 +6430,7 @@ and parseAttributes p = *) and parseStandaloneAttribute p = let startPos = p.startPos in - Parser.expect AtAt p; + Parser.expectUnsafe AtAt p; let attrId = parseAttributeId ~startPos p in let payload = parsePayload p in (attrId, payload) @@ -6436,9 +6471,9 @@ and parseStandaloneAttribute p = and parseExtension ?(moduleLanguage=false) p = let startPos = p.Parser.startPos in if moduleLanguage then - Parser.expect PercentPercent p + Parser.expectUnsafe PercentPercent p else - Parser.expect Percent p; + Parser.expectUnsafe Percent p; let attrId = parseAttributeId ~startPos p in let payload = parsePayload p in (attrId, payload) diff --git a/src/res_parser.ml b/src/res_parser.ml index 6aa63f97..1d69c076 100644 --- a/src/res_parser.ml +++ b/src/res_parser.ml @@ -48,7 +48,7 @@ let endRegion p = (* Advance to the next non-comment token and store any encountered comment * in the parser's state. Every comment contains the end position of its * previous token to facilite comment interleaving *) -let rec next ?prevEndPos p = +let rec next_ ?prevEndPos p = let prevEndPos = match prevEndPos with Some pos -> pos | None -> p.endPos in let (startPos, endPos, token) = Scanner.scan p.scanner in match token with @@ -57,7 +57,7 @@ let rec next ?prevEndPos p = p.comments <- c::p.comments; p.prevEndPos <- p.endPos; p.endPos <- endPos; - next ~prevEndPos p + next_ ~prevEndPos p | _ -> p.token <- token; (* p.prevEndPos <- prevEndPos; *) @@ -65,6 +65,16 @@ let rec next ?prevEndPos p = p.startPos <- startPos; p.endPos <- endPos +exception Eof + +let next p = + if p.token = Eof then raise Eof; + next_ p +[@@raises Eof] + +let nextUnsafe p = + if p.token <> Eof then next_ p + let nextTemplateLiteralToken p = let (startPos, endPos, token) = Scanner.scanTemplateLiteralToken p.scanner in p.token <- token; @@ -82,7 +92,7 @@ let make ?(mode=ParseForTypeChecker) src filename = let parserState = { mode; scanner; - token = Token.Eof; + token = Token.Semicolon; startPos = Lexing.dummy_pos; prevEndPos = Lexing.dummy_pos; endPos = Lexing.dummy_pos; @@ -100,7 +110,7 @@ let make ?(mode=ParseForTypeChecker) src filename = in parserState.diagnostics <- diagnostic::parserState.diagnostics ); - next parserState; + next parserState [@doesNotRaise "initialized with Semicolon"]; parserState let leaveBreadcrumb p circumstance = @@ -114,17 +124,21 @@ let eatBreadcrumb p = let optional p token = if p.token = token then - let () = next p in true + let () = next p [@doesNotRaise] in true else false -let expect ?grammar token p = +let expectUnsafe ?grammar token p = if p.token = token then - next p + nextUnsafe p else let error = Diagnostics.expected ?grammar p.prevEndPos token in err ~startPos:p.prevEndPos p error +let expect token p = + if p.token = Eof then raise Eof [@doesNotRaise]; + expectUnsafe ?grammar:None token p + (* Don't use immutable copies here, it trashes certain heuristics * in the ocaml compiler, resulting in massive slowdowns of the parser *) let lookahead p callback = diff --git a/src/res_parser.mli b/src/res_parser.mli index 80a1c639..a8719d5c 100644 --- a/src/res_parser.mli +++ b/src/res_parser.mli @@ -25,9 +25,23 @@ type t = { val make: ?mode:mode -> string -> string -> t -val expect: ?grammar:Grammar.t -> Token.t -> t -> unit +(* To be used when it's clear the token is not Eof *) +val expect: Token.t -> t -> unit + +(* To be used when it's unclear whether the token is Eof *) +val expectUnsafe: ?grammar:Grammar.t -> Token.t -> t -> unit + val optional: t -> Token.t -> bool -val next: ?prevEndPos:Lexing.position -> t -> unit + +exception Eof + +(* The function raises Eof if called when the current token is Eof *) +val next: t -> unit +[@@raises Eof] + +(* Does not raise on Eof: makes no progress for termination analysis. *) +val nextUnsafe: t -> unit + val nextTemplateLiteralToken: t -> unit val lookahead: t -> (t -> 'a) -> 'a val err: diff --git a/tests/parsing/infiniteLoops/expected/poly.res.txt b/tests/parsing/infiniteLoops/expected/poly.res.txt new file mode 100644 index 00000000..9c6343f5 --- /dev/null +++ b/tests/parsing/infiniteLoops/expected/poly.res.txt @@ -0,0 +1,11 @@ + + Syntax error! + tests/parsing/infiniteLoops/poly.res:1:14-3:0 + + 1 │ type x = [ + 2 │ + 3 │ + + Did you forget a `]` here? + +type nonrec x = [< y] \ No newline at end of file diff --git a/tests/parsing/infiniteLoops/poly.res b/tests/parsing/infiniteLoops/poly.res new file mode 100644 index 00000000..6ead2bdd --- /dev/null +++ b/tests/parsing/infiniteLoops/poly.res @@ -0,0 +1,2 @@ +type x = [ +