From 6c854ad221bc420396963710d31732c6ff986b58 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Fri, 5 Sep 2014 15:31:15 +0200 Subject: [PATCH] [loose parser] Be more careful about calling resetTo It will try to eat whitespace, and can thus raise an unterminated comment exception Issue #375 --- acorn_loose.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/acorn_loose.js b/acorn_loose.js index 208c188afc..b3506b46b5 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -86,7 +86,7 @@ // Try to skip some text, based on the error message, and then continue var msg = e.message, pos = e.raisedAt, replace = true; if (/unterminated/i.test(msg)) { - pos = lineEnd(e.pos); + pos = lineEnd(e.pos + 1); if (/string/.test(msg)) { replace = {start: e.pos, end: pos, type: tt.string, value: input.slice(e.pos + 1, pos)}; } else if (/regular expr/i.test(msg)) { @@ -125,10 +125,18 @@ } function resetTo(pos) { - var ch = input.charAt(pos - 1); - var reAllowed = !ch || /[\[\{\(,;:?\/*=+\-~!|&%^<>]/.test(ch) || - /[enwfd]/.test(ch) && /\b(keywords|case|else|return|throw|new|in|(instance|type)of|delete|void)$/.test(input.slice(pos - 10, pos)); - fetchToken.jumpTo(pos, reAllowed); + for (;;) { + try { + var ch = input.charAt(pos - 1); + var reAllowed = !ch || /[\[\{\(,;:?\/*=+\-~!|&%^<>]/.test(ch) || + /[enwfd]/.test(ch) && /\b(keywords|case|else|return|throw|new|in|(instance|type)of|delete|void)$/.test(input.slice(pos - 10, pos)); + return fetchToken.jumpTo(pos, reAllowed); + } catch(e) { + if (!(e instanceof SyntaxError && /unterminated comment/i.test(e.message))) throw e; + pos = lineEnd(e.pos + 1); + if (pos >= input.length) return; + } + } } function lookAhead(n) {