did some refactoring in the parser, so we don't need to pass variables

to the $() function anymore. We also don't need to pass arrays around.
This commit is contained in:
cloudhead
2010-03-08 23:45:57 -05:00
parent c244c01dd2
commit c4413facad

View File

@@ -66,14 +66,14 @@ function peek(tok) {
//
// Parse from a token, regexp or string, and move forward if match
//
function $(tok, root) {
function $(tok) {
var match, args, length, c, index, endIndex;
//
// Non-terminal
//
if (tok instanceof Function) {
return tok.call(less.parser.parsers, root);
return tok.call(less.parser.parsers);
//
// Terminal
//
@@ -162,7 +162,7 @@ less.parser = {
}
// Start with the primary rule
root = new(tree.Ruleset)([], $(this.parsers.primary, []));
root = new(tree.Ruleset)([], $(this.parsers.primary));
root.root = true;
// If `i` is smaller than the input length - 1,
@@ -186,12 +186,12 @@ less.parser = {
return root;
},
parsers: {
primary: function (root) {
var node;
primary: function () {
var node, root = [];
while (node = $(this.mixin.definition, []) || $(this.ruleset, []) || $(this.rule) ||
$(this.mixin.call) || $(this.comment) ||
$(/[\n\s]+/g) || $(this.directive, [])) {
while (node = $(this.mixin.definition) || $(this.ruleset) || $(this.rule) ||
$(this.mixin.call) || $(this.comment) ||
$(/[\n\s]+/g) || $(this.directive)) {
root.push(node);
}
return root;
@@ -260,19 +260,17 @@ less.parser = {
},
font: function () {
},
variable: function (def) {
variable: function () {
var name;
if (input[i] !== '@') return;
if (def && (name = $(/(@[a-zA-Z0-9_-]+)\s*:/g))) { return name[1] }
else if (!def && (name = $(/@[a-zA-Z0-9_-]+/g))) { return new(tree.Variable)(name) }
if (input[i] === '@' && (name = $(/@[a-zA-Z0-9_-]+/g))) {
return new(tree.Variable)(name);
}
},
color: function () {
var rgb;
if (input[i] !== '#') return;
if (rgb = $(/#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})/g)) {
if (input[i] === '#' && (rgb = $(/#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})/g))) {
return new(tree.Color)(rgb[1]);
}
},
@@ -285,6 +283,11 @@ less.parser = {
}
}
},
variable: function () {
var name;
if (input[i] === '@' && (name = $(/(@[a-zA-Z0-9_-]+)\s*:/g))) { return name[1] }
},
mixin: {
call: function () {
var elements = [], e, c, args;
@@ -299,7 +302,7 @@ less.parser = {
return new(tree.mixin.Call)(elements, args);
}
},
definition: function (root) {
definition: function () {
var name, params = [], match, ruleset, param, value;
if (input[i] !== '.' || peek(/[^{]*(;|})/g)) return;
@@ -321,7 +324,7 @@ less.parser = {
}
if (! $(')')) throw new(Error)("Expected )");
ruleset = $(this.block, root);
ruleset = $(this.block);
if (ruleset) {
return new(tree.mixin.Definition)(name, params, ruleset);
@@ -330,16 +333,10 @@ less.parser = {
}
},
entity: function () {
var entities = [
"url", "variable", "call", "accessor",
"keyword", "literal", "font"
], e;
var e;
for (var i = 0; i < entities.length; i++) {
if (e = $(this.entities[entities[i]])) {
return e;
}
}
if (e = $(this.entities.literal) || $(this.entities.variable) || $(this.entities.url) ||
$(this.entities.call) || $(this.entities.keyword)) { return e }
},
end: function () {
return $(';') || peek('}');
@@ -395,14 +392,14 @@ less.parser = {
if (attr) { return "[" + attr + "]" }
},
block: function (node) {
block: function () {
var content;
if ($('{') && (content = $(this.primary, node)) && $('}')) {
if ($('{') && (content = $(this.primary)) && $('}')) {
return content;
}
},
ruleset: function (root) {
ruleset: function () {
var selectors = [], s, rules, match;
if (peek(/[^{]+[@;}]/g)) return;
@@ -418,7 +415,7 @@ less.parser = {
if (s) $(this.comment);
}
rules = $(this.block, root);
rules = $(this.block);
if (selectors.length > 0 && rules) {
return new(tree.Ruleset)(selectors, rules);
@@ -427,7 +424,7 @@ less.parser = {
rule: function () {
var name, value, match;
if (name = $(this.property) || $(this.entities.variable, true)) {
if (name = $(this.property) || $(this.variable)) {
if ((name[0] != '@') && (match = peek(/([^@+\/*(;{}-]*);[\s\n]*/g))) {
i += match[0].length;
return new(tree.Rule)(name, match[1]);
@@ -436,19 +433,19 @@ less.parser = {
}
}
},
directive: function (root) {
directive: function () {
var name, value, rules, types;
if (input[i] !== '@') return;
if (name = $(/@media|@page/g)) {
types = $(/[a-z:, ]+/g);
if (rules = $(this.block, root)) {
if (rules = $(this.block)) {
return new(tree.Directive)(name + " " + types, rules);
}
} else if (name = $(/@[-a-z]+/g)) {
if (name === '@font-face') {
if (rules = $(this.block, root)) {
if (rules = $(this.block)) {
return new(tree.Directive)(name, rules);
}
} else if ((value = $(this.entity)) && $(';')) {