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)", ""); parseInt(data.length / 1024) + " KB)", "");
start = new(Date); start = new(Date);
tree = less.parser.parse(data);
end = new(Date);
if (less.parser.error) { less.parser.parse(data, function (err, tree) {
process.stdio.writeError(less.parser.error.message); end = new(Date);
process.exit(3);
}
total = end - start; total = end - start;
sys.puts("Parsing: " + sys.puts("Parsing: " +
total + " ms (" + total + " ms (" +
parseInt(1000 / total * parseInt(1000 / total *
data.length / 1024) + " KB\/s)"); data.length / 1024) + " KB\/s)");
start = new(Date); start = new(Date);
css = tree.toCSS([], {frames: []}); css = tree.toCSS([], {frames: []});
end = new(Date); end = new(Date);
sys.puts("Generation: " + (end - start) + " ms (" + sys.puts("Generation: " + (end - start) + " ms (" +
parseInt(1000 / (end - start) * parseInt(1000 / (end - start) *
data.length / 1024) + " KB\/s)"); data.length / 1024) + " KB\/s)");
total += end - start; total += end - start;
sys.puts("Total: " + total + "ms (" + sys.puts("Total: " + total + "ms (" +
parseInt(1000 / total * data.length / 1024) + " KB/s)"); 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, 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) { parse: function (str, callback) {
var root, start, end, zone, line, buff = [], c; var root, start, end, zone, line, buff = [], c, error = null;
i = j = current = 0; i = j = current = 0;
chunks = []; chunks = [];
input = str.replace(/\r\n/g, '\n'); input = str.replace(/\r\n/g, '\n');
inputLength = input.length; inputLength = input.length;
this.error = null;
// Split the input into chunks, // Split the input into chunks,
// Either delimited by /\n\n/ or // Either delimited by /\n\n/ or
@ -200,9 +200,9 @@ less.parser = {
stylize(stylize(input[i], 'inverse') + stylize(stylize(input[i], 'inverse') +
input.slice(i + 1, end),'yellow') + '\033[0m\n'; 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) { read(path, function (e, str) {
if (e) { return callback(e) } if (e) { return callback(e) }
tree = less.parser.parse(str); less.parser.parse(str, function (err, tree) {
if (err) {
if (less.parser.error) { callback(err);
callback(less.parser.error); } else {
} else { try {
try { css = tree.toCSS([], {frames: []});
css = tree.toCSS([], {frames: []}); callback(null, css);
callback(null, css); } catch (e) {
} catch (e) { callback(e);
callback(e); }
} }
} });
}); });
} }