Преглед изворни кода

Don't use function call for token so that activation context doesn't have to be created.

tags/0.5.0-vsc
thegrandpoobah пре 15 година
родитељ
комит
9e5a50e5c8
1 измењених фајлова са 36 додато и 40 уклоњено
  1. +36
    -40
      mustache.js

+ 36
- 40
mustache.js Прегледај датотеку

@@ -146,7 +146,7 @@ var Mustache = function() {
createParserContext: function(tokens, partials, openTag, closeTag) { createParserContext: function(tokens, partials, openTag, closeTag) {
return { return {
tokens: tokens, tokens: tokens,
token: function() { return this.tokens[this.index]; },
token: tokens[0],
index: 0, index: 0,
length: tokens.length, length: tokens.length,
partials: partials, partials: partials,
@@ -185,15 +185,7 @@ var Mustache = function() {
regex = this._createTokenizerRegex(openTag, closeTag); regex = this._createTokenizerRegex(openTag, closeTag);
} }
var tokens = this.splitFunc.call(template, regex);
var cleaned_tokens = [];
for (var i = 0, n = tokens.length; i<n; ++i) {
if (tokens[i]!=='') {
cleaned_tokens.push(tokens[i]);
}
}
return cleaned_tokens;
return this.splitFunc.call(template, regex);
}, },
/* /*
@@ -243,6 +235,10 @@ var Mustache = function() {
var state = 'text'; var state = 'text';
for (; parserContext.index<parserContext.length; ++parserContext.index) { for (; parserContext.index<parserContext.length; ++parserContext.index) {
parserContext.token = parserContext.tokens[parserContext.index];
if (parserContext.token === '') continue;
state = this.stateMachine[state].call(this, parserContext, contextStack); state = this.stateMachine[state].call(this, parserContext, contextStack);
} }
@@ -273,19 +269,19 @@ var Mustache = function() {
stateMachine: { stateMachine: {
text: function(parserContext, contextStack) { text: function(parserContext, contextStack) {
switch (parserContext.token()) {
switch (parserContext.token) {
case parserContext.openTag: case parserContext.openTag:
this.commandSet.text.call(this); this.commandSet.text.call(this);
return 'openMustache'; return 'openMustache';
default: default:
this.send_func(parserContext.token());
this.send_func(parserContext.token);
return 'text'; return 'text';
} }
}, },
openMustache: function(parserContext, contextStack) { openMustache: function(parserContext, contextStack) {
switch (parserContext.token()) {
switch (parserContext.token) {
case '{': case '{':
parserContext.stack.push({tagType:'unescapedVariable', subtype: 'tripleMustache'}); parserContext.stack.push({tagType:'unescapedVariable', subtype: 'tripleMustache'});
return 'keyName'; return 'keyName';
@@ -321,24 +317,24 @@ var Mustache = function() {
} }
}, },
closeMustache: function(parserContext, contextStack) { closeMustache: function(parserContext, contextStack) {
if (this.isWhitespace(parserContext.token())) {
if (this.isWhitespace(parserContext.token)) {
return 'closeMustache'; return 'closeMustache';
} else if (parserContext.token()===parserContext.closeTag) {
} else if (parserContext.token===parserContext.closeTag) {
return this.dispatchCommand(parserContext, contextStack); return this.dispatchCommand(parserContext, contextStack);
} }
}, },
expectClosingMustache: function(parserContext, contextStack) { expectClosingMustache: function(parserContext, contextStack) {
if (parserContext.closeTag==='}}' && if (parserContext.closeTag==='}}' &&
parserContext.token()==='}}') {
parserContext.token==='}}') {
return 'expectClosingParenthesis'; return 'expectClosingParenthesis';
} else if (parserContext.token()==='}') {
} else if (parserContext.token==='}') {
return 'closeMustache'; return 'closeMustache';
} else { } else {
throw new ParserException('Unexpected token encountered.'); throw new ParserException('Unexpected token encountered.');
} }
}, },
expectClosingParenthesis: function(parserContext, contextStack) { expectClosingParenthesis: function(parserContext, contextStack) {
if (parserContext.token()==='}') {
if (parserContext.token==='}') {
return this.dispatchCommand(parserContext, contextStack); return this.dispatchCommand(parserContext, contextStack);
} else { } else {
throw new ParserException('Unexpected token encountered.'); throw new ParserException('Unexpected token encountered.');
@@ -369,59 +365,59 @@ var Mustache = function() {
} }
}, },
simpleKeyName: function(parserContext, contextStack) { simpleKeyName: function(parserContext, contextStack) {
if (this.isWhitespace(parserContext.token())) {
if (this.isWhitespace(parserContext.token)) {
return 'simpleKeyName'; return 'simpleKeyName';
} else { } else {
parserContext.stack.push(parserContext.token());
parserContext.stack.push(parserContext.token);
return 'closeMustache'; return 'closeMustache';
} }
}, },
setDelimiterStart: function(parserContext, contextStack) { setDelimiterStart: function(parserContext, contextStack) {
if (this.isWhitespace(parserContext.token()) ||
parserContext.token()==='=') {
if (this.isWhitespace(parserContext.token) ||
parserContext.token==='=') {
throw new ParserException('Syntax error in Set Delimiter tag'); throw new ParserException('Syntax error in Set Delimiter tag');
} else { } else {
parserContext.stack.push(parserContext.token());
parserContext.stack.push(parserContext.token);
return 'setDelimiterStartOrWhitespace'; return 'setDelimiterStartOrWhitespace';
} }
}, },
setDelimiterStartOrWhitespace: function(parserContext, contextStack) { setDelimiterStartOrWhitespace: function(parserContext, contextStack) {
if (this.isWhitespace(parserContext.token())) {
if (this.isWhitespace(parserContext.token)) {
return 'setDelimiterEnd'; return 'setDelimiterEnd';
} else if (parserContext.token()==='='){
} else if (parserContext.token==='='){
throw new ParserException('Syntax error in Set Delimiter tag'); throw new ParserException('Syntax error in Set Delimiter tag');
} else { } else {
parserContext.stack.push(parserContext.stack.pop() + parserContext.token());
parserContext.stack.push(parserContext.stack.pop() + parserContext.token);
return 'setDelimiterStartOrWhitespace'; return 'setDelimiterStartOrWhitespace';
} }
}, },
setDelimiterEnd: function(parserContext, contextStack) { setDelimiterEnd: function(parserContext, contextStack) {
if (this.isWhitespace(parserContext.token())) {
if (this.isWhitespace(parserContext.token)) {
return 'setDelimiterEnd'; return 'setDelimiterEnd';
} else if (parserContext.token()==='=') {
} else if (parserContext.token==='=') {
throw new ParserException('Syntax error in Set Delimiter tag'); throw new ParserException('Syntax error in Set Delimiter tag');
} else { } else {
parserContext.stack.push(parserContext.token());
parserContext.stack.push(parserContext.token);
return 'setDelimiterEndOrEqualSign'; return 'setDelimiterEndOrEqualSign';
} }
}, },
setDelimiterEndOrEqualSign: function(parserContext, contextStack) { setDelimiterEndOrEqualSign: function(parserContext, contextStack) {
if (parserContext.token()==='=') {
if (parserContext.token==='=') {
return 'setDelimiterExpectClosingTag'; return 'setDelimiterExpectClosingTag';
} else if (this.isWhitespace(parserContext.token())) {
} else if (this.isWhitespace(parserContext.token)) {
throw new ParserException('Syntax error in Set Delimiter tag'); throw new ParserException('Syntax error in Set Delimiter tag');
} else { } else {
parserContext.stack.push(parserContext.stack.pop() + parserContext.token());
parserContext.stack.push(parserContext.stack.pop() + parserContext.token);
return 'setDelimiterEndOrEqualSign'; return 'setDelimiterEndOrEqualSign';
} }
}, },
setDelimiterExpectClosingTag: function(parserContext, contextStack) { setDelimiterExpectClosingTag: function(parserContext, contextStack) {
if (parserContext.token()===parserContext.closeTag) {
if (parserContext.token===parserContext.closeTag) {
var newCloseTag = parserContext.stack.pop(); var newCloseTag = parserContext.stack.pop();
var newOpenTag = parserContext.stack.pop(); var newOpenTag = parserContext.stack.pop();
var command = parserContext.stack.pop(); var command = parserContext.stack.pop();
@@ -453,32 +449,32 @@ var Mustache = function() {
}, },
endSectionScan: function(parserContext, contextStack) { endSectionScan: function(parserContext, contextStack) {
switch (parserContext.token()) {
switch (parserContext.token) {
case parserContext.openTag: case parserContext.openTag:
return 'expectSectionOrEndSection'; return 'expectSectionOrEndSection';
default: default:
parserContext.stack[parserContext.stack.length-1].content.push(parserContext.token());
parserContext.stack[parserContext.stack.length-1].content.push(parserContext.token);
return 'endSectionScan'; return 'endSectionScan';
} }
}, },
expectSectionOrEndSection: function(parserContext, contextStack) { expectSectionOrEndSection: function(parserContext, contextStack) {
switch (parserContext.token()) {
switch (parserContext.token) {
case '#': case '#':
case '^': case '^':
parserContext.stack[parserContext.stack.length-1].depth++; parserContext.stack[parserContext.stack.length-1].depth++;
parserContext.stack[parserContext.stack.length-1].content.push(parserContext.openTag, parserContext.token());
parserContext.stack[parserContext.stack.length-1].content.push(parserContext.openTag, parserContext.token);
return 'endSectionScan'; return 'endSectionScan';
case '/': case '/':
parserContext.stack.push({tagType:'endSection'}); parserContext.stack.push({tagType:'endSection'});
return 'simpleKeyName'; return 'simpleKeyName';
default: default:
parserContext.stack[parserContext.stack.length-1].content.push(parserContext.openTag, parserContext.token());
parserContext.stack[parserContext.stack.length-1].content.push(parserContext.openTag, parserContext.token);
return 'endSectionScan'; return 'endSectionScan';
} }
}, },
discard: function(parserContext, contextStack) { discard: function(parserContext, contextStack) {
if (parserContext.token()==='!') {
if (parserContext.token==='!') {
return 'closeComment'; return 'closeComment';
} else { } else {
return 'discard'; return 'discard';
@@ -486,7 +482,7 @@ var Mustache = function() {
}, },
closeComment: function(parserContext, contextStack) { closeComment: function(parserContext, contextStack) {
if (parserContext.token()!==parserContext.closeTag) {
if (parserContext.token!==parserContext.closeTag) {
return 'discard'; return 'discard';
} else { } else {
return 'text'; return 'text';


Loading…
Откажи
Сачувај