replace simple regex's with simple string comparisons

This commit is contained in:
Luke Page
2015-03-15 10:24:41 +00:00
parent d04e1d7464
commit f8de5bcb16
2 changed files with 25 additions and 55 deletions

View File

@@ -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,