proper parser error messages
This commit is contained in:
parent
93a0a0bebf
commit
c72d5da9c5
@ -39,10 +39,10 @@ var less = exports || {};
|
||||
//
|
||||
//
|
||||
var input, // LeSS input string
|
||||
i = 0, // current index in `input`
|
||||
j = 0, // current chunk
|
||||
i, // current index in `input`
|
||||
j, // current chunk
|
||||
chunks = [], // chunkified input
|
||||
current = 0, // index of current chunk, in `input`
|
||||
current, // index of current chunk, in `input`
|
||||
inputLength;
|
||||
|
||||
function peek(regex) {
|
||||
@ -119,8 +119,9 @@ less.parser = {
|
||||
// Parse an input string into an abstract syntax tree
|
||||
//
|
||||
parse: function (str) {
|
||||
var tree;
|
||||
var tree, start, end, zone, line;
|
||||
|
||||
i = j = current = 0;
|
||||
input = str;
|
||||
inputLength = input.length;
|
||||
|
||||
@ -134,8 +135,22 @@ less.parser = {
|
||||
tree = new(node.Ruleset)([], $(this.parsers.primary, []));
|
||||
tree.root = true;
|
||||
|
||||
// If `i` is smaller than the input length - 1,
|
||||
// it means the parser wasn't able to parse the whole
|
||||
// string, so we've got a parsing error.
|
||||
if (i < input.length - 1) {
|
||||
throw new(Error)("Parse Error: " + input.slice(0, i));
|
||||
start = (function () {
|
||||
for (var n = i; n > 0; n--) {
|
||||
if (input[n] === '\n') { break }
|
||||
}
|
||||
return n;
|
||||
})() + 1;
|
||||
line = input.slice(0, i).match(/\n/g).length + 1;
|
||||
end = input.slice(i).indexOf('\n') + i;
|
||||
zone = stylize(input.slice(start, i), 'green') +
|
||||
stylize(input.slice(i, end), 'yellow');
|
||||
|
||||
throw new(Error)("Parse Error on line " + line + ":\n" + zone);
|
||||
}
|
||||
return tree;
|
||||
},
|
||||
@ -435,3 +450,16 @@ less.parser = {
|
||||
}
|
||||
};
|
||||
|
||||
// Stylize a string
|
||||
function stylize(str, style) {
|
||||
var styles = {
|
||||
'bold' : [1, 22],
|
||||
'underline' : [4, 24],
|
||||
'yellow' : [33, 39],
|
||||
'green' : [32, 39],
|
||||
'red' : [31, 39]
|
||||
};
|
||||
return '\033[' + styles[style][0] + 'm' + str +
|
||||
'\033[' + styles[style][1] + 'm';
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user