From 53996b2364f76758d40c77bc9d514d2ca6312068 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Fri, 19 Mar 2010 17:50:35 -0400 Subject: [PATCH] parse() is now asynch, and uses a callback --- benchmark/less-benchmark.js | 42 +++++++++++++++++++------------------ lib/less/parser.js | 12 +++++------ test/less-test.js | 22 +++++++++---------- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/benchmark/less-benchmark.js b/benchmark/less-benchmark.js index de0ff73..5587fe9 100644 --- a/benchmark/less-benchmark.js +++ b/benchmark/less-benchmark.js @@ -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); + } + }); }); }); }); diff --git a/lib/less/parser.js b/lib/less/parser.js index 69f6ae9..9276f39 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -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); }, // diff --git a/test/less-test.js b/test/less-test.js index 75a0d13..43cc096 100644 --- a/test/less-test.js +++ b/test/less-test.js @@ -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); + } } - } + }); }); }