parse() is now asynch, and uses a callback

This commit is contained in:
cloudhead 2010-03-19 17:50:35 -04:00
parent 6345491495
commit 53996b2364
3 changed files with 39 additions and 37 deletions

View File

@ -16,33 +16,35 @@ fs.stat(file, function (e, stats) {
parseInt(data.length / 1024) + " KB)", "");
start = new(Date);
tree = less.parser.parse(data);
end = new(Date);
if (less.parser.error) {
process.stdio.writeError(less.parser.error.message);
process.exit(3);
}
less.parser.parse(data, function (err, tree) {
end = new(Date);
total = end - start;
total = end - start;
sys.puts("Parsing: " +
total + " ms (" +
parseInt(1000 / total *
data.length / 1024) + " KB\/s)");
sys.puts("Parsing: " +
total + " ms (" +
parseInt(1000 / total *
data.length / 1024) + " KB\/s)");
start = new(Date);
css = tree.toCSS([], {frames: []});
end = new(Date);
start = new(Date);
css = tree.toCSS([], {frames: []});
end = new(Date);
sys.puts("Generation: " + (end - start) + " ms (" +
parseInt(1000 / (end - start) *
data.length / 1024) + " KB\/s)");
sys.puts("Generation: " + (end - start) + " ms (" +
parseInt(1000 / (end - start) *
data.length / 1024) + " KB\/s)");
total += end - start;
total += end - start;
sys.puts("Total: " + total + "ms (" +
parseInt(1000 / total * data.length / 1024) + " KB/s)");
sys.puts("Total: " + total + "ms (" +
parseInt(1000 / total * data.length / 1024) + " KB/s)");
if (err) {
process.stdio.writeError(err.message);
process.exit(3);
}
});
});
});
});

View File

@ -138,16 +138,16 @@ less.parser = {
optimization: 2,
//
// Parse an input string into an abstract syntax tree
// Parse an input string into an abstract syntax tree,
// call `callback` when done.
//
parse: function (str) {
var root, start, end, zone, line, buff = [], c;
parse: function (str, callback) {
var root, start, end, zone, line, buff = [], c, error = null;
i = j = current = 0;
chunks = [];
input = str.replace(/\r\n/g, '\n');
inputLength = input.length;
this.error = null;
// Split the input into chunks,
// Either delimited by /\n\n/ or
@ -200,9 +200,9 @@ less.parser = {
stylize(stylize(input[i], 'inverse') +
input.slice(i + 1, end),'yellow') + '\033[0m\n';
this.error = { name: "ParseError", message: "Parse Error on line " + line + ":\n" + zone };
error = { name: "ParseError", message: "Parse Error on line " + line + ":\n" + zone };
}
return root;
callback(error, root);
},
//

View File

@ -42,18 +42,18 @@ function toCSS(path, callback) {
read(path, function (e, str) {
if (e) { return callback(e) }
tree = less.parser.parse(str);
if (less.parser.error) {
callback(less.parser.error);
} else {
try {
css = tree.toCSS([], {frames: []});
callback(null, css);
} catch (e) {
callback(e);
less.parser.parse(str, function (err, tree) {
if (err) {
callback(err);
} else {
try {
css = tree.toCSS([], {frames: []});
callback(null, css);
} catch (e) {
callback(e);
}
}
}
});
});
}