mirror of
https://github.com/less/less.js.git
synced 2026-05-01 03:00:22 -04:00
replace simple regex's with simple string comparisons
This commit is contained in:
@@ -29,56 +29,11 @@ module.exports = function() {
|
||||
parserInput.forget = function() {
|
||||
saveStack.pop();
|
||||
};
|
||||
function sync() {
|
||||
if (parserInput.i > currentPos) {
|
||||
current = current.slice(parserInput.i - currentPos);
|
||||
currentPos = parserInput.i;
|
||||
}
|
||||
}
|
||||
parserInput.isWhitespace = function (offset) {
|
||||
var pos = parserInput.i + (offset || 0),
|
||||
code = input.charCodeAt(pos);
|
||||
return (code === CHARCODE_SPACE || code === CHARCODE_CR || code === CHARCODE_TAB || code === CHARCODE_LF);
|
||||
};
|
||||
//
|
||||
// Parse from a token, regexp or string, and move forward if match
|
||||
//
|
||||
parserInput.$ = function(tok) {
|
||||
var tokType = typeof tok,
|
||||
match, length;
|
||||
|
||||
// Either match a single character in the input,
|
||||
// or match a regexp in the current chunk (`current`).
|
||||
//
|
||||
if (tokType === "string") {
|
||||
if (input.charAt(parserInput.i) !== tok) {
|
||||
return null;
|
||||
}
|
||||
skipWhitespace(1);
|
||||
return tok;
|
||||
}
|
||||
|
||||
// regexp
|
||||
sync();
|
||||
if (! (match = tok.exec(current))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
length = match[0].length;
|
||||
|
||||
// The match is confirmed, add the match length to `i`,
|
||||
// and consume any extra white-space characters (' ' || '\n')
|
||||
// which come after that. The reason for this is that LeSS's
|
||||
// grammar is mostly white-space insensitive.
|
||||
//
|
||||
skipWhitespace(length);
|
||||
|
||||
if (typeof match === 'string') {
|
||||
return match;
|
||||
} else {
|
||||
return match.length === 1 ? match[0] : match;
|
||||
}
|
||||
};
|
||||
|
||||
// Specialization of $(tok)
|
||||
parserInput.$re = function(tok) {
|
||||
@@ -86,6 +41,7 @@ module.exports = function() {
|
||||
current = current.slice(parserInput.i - currentPos);
|
||||
currentPos = parserInput.i;
|
||||
}
|
||||
|
||||
var m = tok.exec(current);
|
||||
if (!m) {
|
||||
return null;
|
||||
@@ -99,7 +55,6 @@ module.exports = function() {
|
||||
return m.length === 1 ? m[0] : m;
|
||||
};
|
||||
|
||||
// Specialization of $(tok)
|
||||
parserInput.$char = function(tok) {
|
||||
if (input.charAt(parserInput.i) !== tok) {
|
||||
return null;
|
||||
@@ -108,6 +63,19 @@ module.exports = function() {
|
||||
return tok;
|
||||
};
|
||||
|
||||
parserInput.$str = function(tok) {
|
||||
var tokLength = tok.length;
|
||||
|
||||
for (var i = 0; i < tokLength; i++) {
|
||||
if (input.charAt(parserInput.i + i) !== tok.charAt(i)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
skipWhitespace(tokLength);
|
||||
return tok;
|
||||
};
|
||||
|
||||
var CHARCODE_SPACE = 32,
|
||||
CHARCODE_TAB = 9,
|
||||
CHARCODE_LF = 10,
|
||||
|
||||
Reference in New Issue
Block a user