Permalink
Browse files

parsing/compiling speedups

  • Loading branch information...
1 parent e8ac71c commit e618119e03d23da9f155697a8905e94ac9ea3fa8 @cloudhead cloudhead committed Jan 16, 2012
Showing with 19 additions and 26 deletions.
  1. +19 −26 lib/less/parser.js
View
@@ -255,19 +255,18 @@ less.Parser = function Parser(env) {
var root, start, end, zone, line, lines, buff = [], c, error = null;
i = j = current = furthest = 0;
- chunks = [];
input = str.replace(/\r\n/g, '\n');
// Split the input into chunks.
chunks = (function (chunks) {
var j = 0,
- skip = /[^"'`\{\}\/\(\)]+/g,
+ skip = /[^"'`\{\}\/\(\)\\]+/g,
comment = /\/\*(?:[^*]|\*+[^\/*])*\*+\/|\/\/.*/g,
+ string = /"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'|`((?:[^`\\\r\n]|\\.)*)`/g,
level = 0,
match,
chunk = chunks[0],
- inParam,
- inString;
+ inParam;
for (var i = 0, c, cc; i < input.length; i++) {
skip.lastIndex = i;
@@ -278,9 +277,17 @@ less.Parser = function Parser(env) {
}
}
c = input.charAt(i);
- comment.lastIndex = i;
+ comment.lastIndex = string.lastIndex = i;
- if (!inString && !inParam && c === '/') {
+ if (match = string.exec(input)) {
+ if (match.index === i) {
+ i += match[0].length;
+ chunk.push(match[0]);
+ c = input.charAt(i);
+ }
+ }
+
+ if (!inParam && c === '/') {
cc = input.charAt(i + 1);
if (cc === '/' || cc === '*') {
if (match = comment.exec(input)) {
@@ -293,26 +300,12 @@ less.Parser = function Parser(env) {
}
}
- if (c === '{' && !inString && !inParam) { level ++;
- chunk.push(c);
- } else if (c === '}' && !inString && !inParam) { level --;
- chunk.push(c);
- chunks[++j] = chunk = [];
- } else if (c === '(' && !inString && !inParam) {
- chunk.push(c);
- inParam = true;
- } else if (c === ')' && !inString && inParam) {
- chunk.push(c);
- inParam = false;
- } else {
- if (c === '"' || c === "'" || c === '`') {
- if (! inString) {
- inString = c;
- } else {
- inString = inString === c ? false : inString;
- }
- }
- chunk.push(c);
+ switch (c) {
+ case '{': if (! inParam) { level ++; chunk.push(c); break }
+ case '}': if (! inParam) { level --; chunk.push(c); chunks[++j] = chunk = []; break }
+ case '(': if (! inParam) { inParam = true; chunk.push(c); break }
+ case ')': if ( inParam) { inParam = false; chunk.push(c); break }
+ default: chunk.push(c);
}
}
if (level > 0) {

0 comments on commit e618119

Please sign in to comment.