From 554448d49bea7bc295e7d7c95bd497d8bf879eea Mon Sep 17 00:00:00 2001 From: seven-phases-max Date: Sat, 1 Feb 2014 03:00:44 +0400 Subject: [PATCH 1/3] Improved missing `(` and `{` error detection. --- lib/less/parser.js | 25 +++++++++++-------- test/less/errors/import-subfolder2.txt | 6 +++-- .../errors/parse-error-curly-bracket.less | 5 +++- .../less/errors/parse-error-curly-bracket.txt | 6 +++-- test/less/errors/parse-error-extra-parens.txt | 6 ++--- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/lib/less/parser.js b/lib/less/parser.js index 70d2256ba..e18383b71 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -372,7 +372,7 @@ less.Parser = function Parser(env) { // Split the input into chunks. chunks = (function (input) { var len = input.length, level = 0, parenLevel = 0, - lastOpening, lastClosing, lastMultiComment, lastMultiCommentEndBrace, + lastOpening, lastMultiComment, lastMultiCommentEndBrace, chunks = [], emitFrom = 0, parserCurrentIndex, currentChunkStartIndex, cc, cc2, matched; @@ -405,14 +405,19 @@ less.Parser = function Parser(env) { case 40: // ( parenLevel++; continue; case 41: // ) - parenLevel--; continue; + if (--parenLevel < 0) { + return fail("missing opening `(`"); + } + continue; case 59: // ; if (!parenLevel) { emitChunk(); } continue; case 123: // { level++; lastOpening = parserCurrentIndex; continue; case 125: // } - level--; lastClosing = parserCurrentIndex; + if (--level < 0) { + return fail("missing opening `{`"); + } if (!level) { emitChunk(); } continue; case 92: // \ @@ -437,6 +442,7 @@ less.Parser = function Parser(env) { if (matched) { continue; } return fail("unmatched `" + String.fromCharCode(cc) + "`", currentChunkStartIndex); case 47: // /, check for comment + // if (chunks.length >= 47) debugger; if (parenLevel || (parserCurrentIndex == len - 1)) { continue; } cc2 = input.charCodeAt(parserCurrentIndex + 1); if (cc2 == 47) { @@ -468,16 +474,13 @@ less.Parser = function Parser(env) { } if (level !== 0) { - if (level > 0) { - if ((lastMultiComment > lastOpening) && (lastMultiCommentEndBrace > lastMultiComment)) { - return fail("missing closing `}` or `*/`", lastOpening); - } else { - return fail("missing closing `}`", lastOpening); - } + if ((lastMultiComment > lastOpening) && (lastMultiCommentEndBrace > lastMultiComment)) { + return fail("missing closing `}` or `*/`", lastOpening); + } else { + return fail("missing closing `}`", lastOpening); } - return fail("missing opening `{`", lastClosing); } else if (parenLevel !== 0) { - return fail((parenLevel > 0) ? "missing closing `)`" : "missing opening `(`"); + return fail("missing closing `)`"); } emitChunk(true); diff --git a/test/less/errors/import-subfolder2.txt b/test/less/errors/import-subfolder2.txt index 7d514efc8..b5b1a69ba 100644 --- a/test/less/errors/import-subfolder2.txt +++ b/test/less/errors/import-subfolder2.txt @@ -1,2 +1,4 @@ -ParseError: missing opening `{` in {path}parse-error-curly-bracket.less on line 1, column 2: -1 }} +ParseError: missing opening `{` in {path}parse-error-curly-bracket.less on line 4, column 1: +3 } +4 } +5 diff --git a/test/less/errors/parse-error-curly-bracket.less b/test/less/errors/parse-error-curly-bracket.less index a2950a11c..f78ceb701 100644 --- a/test/less/errors/parse-error-curly-bracket.less +++ b/test/less/errors/parse-error-curly-bracket.less @@ -1 +1,4 @@ -}} \ No newline at end of file +body { + background-color: #fff; + } +} diff --git a/test/less/errors/parse-error-curly-bracket.txt b/test/less/errors/parse-error-curly-bracket.txt index 7d514efc8..b5b1a69ba 100644 --- a/test/less/errors/parse-error-curly-bracket.txt +++ b/test/less/errors/parse-error-curly-bracket.txt @@ -1,2 +1,4 @@ -ParseError: missing opening `{` in {path}parse-error-curly-bracket.less on line 1, column 2: -1 }} +ParseError: missing opening `{` in {path}parse-error-curly-bracket.less on line 4, column 1: +3 } +4 } +5 diff --git a/test/less/errors/parse-error-extra-parens.txt b/test/less/errors/parse-error-extra-parens.txt index cd70195ca..5c1aaef2a 100644 --- a/test/less/errors/parse-error-extra-parens.txt +++ b/test/less/errors/parse-error-extra-parens.txt @@ -1,3 +1,3 @@ -ParseError: missing opening `(` in {path}parse-error-extra-parens.less on line 6, column 1: -5 } -6 +ParseError: missing opening `(` in {path}parse-error-extra-parens.less on line 1, column 24: +1 @media (extra: bracket)) { +2 body { From 98df50e016e24ea6b568e13ae6bb8e7698a1ec1a Mon Sep 17 00:00:00 2001 From: seven-phases-max Date: Sat, 1 Feb 2014 03:58:24 +0400 Subject: [PATCH 2/3] removed debugger statement mistakenly left out. --- lib/less/parser.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/less/parser.js b/lib/less/parser.js index e18383b71..1d280c2f5 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -442,7 +442,6 @@ less.Parser = function Parser(env) { if (matched) { continue; } return fail("unmatched `" + String.fromCharCode(cc) + "`", currentChunkStartIndex); case 47: // /, check for comment - // if (chunks.length >= 47) debugger; if (parenLevel || (parserCurrentIndex == len - 1)) { continue; } cc2 = input.charCodeAt(parserCurrentIndex + 1); if (cc2 == 47) { From 7768a91b3c77aec74cb10d7b87c2161c9940765d Mon Sep 17 00:00:00 2001 From: seven-phases-max Date: Sat, 1 Feb 2014 08:31:40 +0400 Subject: [PATCH 3/3] Improved positioning of "missing closing `)`" error. --- lib/less/parser.js | 12 ++++++++---- test/less/errors/parse-error-missing-parens.txt | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/less/parser.js b/lib/less/parser.js index 1d280c2f5..43a0b58c8 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -372,7 +372,7 @@ less.Parser = function Parser(env) { // Split the input into chunks. chunks = (function (input) { var len = input.length, level = 0, parenLevel = 0, - lastOpening, lastMultiComment, lastMultiCommentEndBrace, + lastOpening, lastOpeningParen, lastMultiComment, lastMultiCommentEndBrace, chunks = [], emitFrom = 0, parserCurrentIndex, currentChunkStartIndex, cc, cc2, matched; @@ -403,7 +403,9 @@ less.Parser = function Parser(env) { switch (cc) { case 40: // ( - parenLevel++; continue; + parenLevel++; + lastOpeningParen = parserCurrentIndex; + continue; case 41: // ) if (--parenLevel < 0) { return fail("missing opening `(`"); @@ -413,7 +415,9 @@ less.Parser = function Parser(env) { if (!parenLevel) { emitChunk(); } continue; case 123: // { - level++; lastOpening = parserCurrentIndex; continue; + level++; + lastOpening = parserCurrentIndex; + continue; case 125: // } if (--level < 0) { return fail("missing opening `{`"); @@ -479,7 +483,7 @@ less.Parser = function Parser(env) { return fail("missing closing `}`", lastOpening); } } else if (parenLevel !== 0) { - return fail("missing closing `)`"); + return fail("missing closing `)`", lastOpeningParen); } emitChunk(true); diff --git a/test/less/errors/parse-error-missing-parens.txt b/test/less/errors/parse-error-missing-parens.txt index d11127ca8..a7a67067a 100644 --- a/test/less/errors/parse-error-missing-parens.txt +++ b/test/less/errors/parse-error-missing-parens.txt @@ -1,3 +1,3 @@ -ParseError: missing closing `)` in {path}parse-error-missing-parens.less on line 6, column 1: -5 } -6 +ParseError: missing closing `)` in {path}parse-error-missing-parens.less on line 1, column 8: +1 @media (missing: bracket { +2 body {