More accurate parse error messages

Because of the backtracking, we lose the previous point of failure.
The solution is to store the furthest point the parser has parsed to,
when backtracking, and using it when showing parse errors.
This commit is contained in:
cloudhead
2010-04-25 00:06:45 -04:00
parent 3047f7603e
commit 2ebbf37c4b

View File

@@ -47,6 +47,7 @@ less.Parser = function Parser(env) {
var input, // LeSS input string
i, // current index in `input`
j, // current chunk
furthest, // furthest index the parser has gone to
chunks, // chunkified input
current, // index of current chunk, in `input`
inputLength,
@@ -180,7 +181,7 @@ less.Parser = function Parser(env) {
parse: function (str, callback) {
var root, start, end, zone, line, buff = [], c, error = null;
i = j = current = 0;
i = j = current = furthest = 0;
chunks = [];
input = str.replace(/\r\n/g, '\n');
inputLength = input.length;
@@ -225,6 +226,7 @@ less.Parser = function Parser(env) {
// We split it up into two parts (the part which parsed,
// and the part which didn't), so we can color them differently.
if (i < input.length - 1) {
i = furthest;
start = (function () {
for (var n = i; n > 0; n--) {
if (input[n] === '\n') { break }
@@ -694,6 +696,7 @@ less.Parser = function Parser(env) {
return new(tree.Ruleset)(selectors, rules);
} else {
// Backtrack
furthest = i;
i = memo;
}
},
@@ -714,6 +717,7 @@ less.Parser = function Parser(env) {
if ($(this.end)) {
return new(tree.Rule)(name, value);
} else {
furthest = i;
i = memo;
}
}