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

Commit 53ee86b

Browse files
committed
Do not parse past Eof
Parsing past Eof can lead to infinite loops, as it violates the assumptions of the termination checker. See: #540 This PR makes `Parser.next` assert false when called on Eof. This should not happen as one should check the token before calling `Parser.next`. The one exception is during lookahead, for which we provide a `nextUnsafe` function which does not make progress.
1 parent dd8339a commit 53ee86b

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

src/res_core.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ let isEs6ArrowExpression ~inTernary p =
259259
* *)
260260
false
261261
| _ ->
262-
Parser.next state;
262+
Parser.nextUnsafe state;
263263
(* error recovery, peek at the next token,
264264
* (elements, providerId] => {
265265
* in the example above, we have an unbalanced ] here

src/res_parser.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ let endRegion p =
4949
* in the parser's state. Every comment contains the end position of its
5050
* previous token to facilite comment interleaving *)
5151
let rec next ?prevEndPos p =
52+
if p.token = Eof then assert false;
5253
let prevEndPos = match prevEndPos with Some pos -> pos | None -> p.endPos in
5354
let (startPos, endPos, token) = Scanner.scan p.scanner in
5455
match token with
@@ -65,6 +66,9 @@ let rec next ?prevEndPos p =
6566
p.startPos <- startPos;
6667
p.endPos <- endPos
6768

69+
let nextUnsafe p =
70+
if p.token <> Eof then next p
71+
6872
let nextTemplateLiteralToken p =
6973
let (startPos, endPos, token) = Scanner.scanTemplateLiteralToken p.scanner in
7074
p.token <- token;
@@ -82,7 +86,7 @@ let make ?(mode=ParseForTypeChecker) src filename =
8286
let parserState = {
8387
mode;
8488
scanner;
85-
token = Token.Eof;
89+
token = Token.Semicolon;
8690
startPos = Lexing.dummy_pos;
8791
prevEndPos = Lexing.dummy_pos;
8892
endPos = Lexing.dummy_pos;

src/res_parser.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ val make: ?mode:mode -> string -> string -> t
2828
val expect: ?grammar:Grammar.t -> Token.t -> t -> unit
2929
val optional: t -> Token.t -> bool
3030
val next: ?prevEndPos:Lexing.position -> t -> unit
31+
val nextUnsafe: t -> unit (* Does not assert on Eof, makes no progress *)
3132
val nextTemplateLiteralToken: t -> unit
3233
val lookahead: t -> (t -> 'a) -> 'a
3334
val err:

0 commit comments

Comments
 (0)