diff options
author | kpdecker <kpdecker@gmail.com> | 2014-11-27 08:37:48 -0600 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2014-11-27 08:37:48 -0600 |
commit | 1124908d2a0d36493912eb2ce062545b1b5e5445 (patch) | |
tree | f6f8a5f5f0a96a6aa4152aeb576775021c390390 /lib/handlebars/compiler/compiler.js | |
parent | e1cba432a68fe208782f8f943ebd57d7641d705a (diff) | |
download | handlebars.js-1124908d2a0d36493912eb2ce062545b1b5e5445.zip handlebars.js-1124908d2a0d36493912eb2ce062545b1b5e5445.tar.gz handlebars.js-1124908d2a0d36493912eb2ce062545b1b5e5445.tar.bz2 |
Update subexpression and hash AST constructs
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 118 |
1 files changed, 54 insertions, 64 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 5dbbfc4..e73ce37 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -11,16 +11,16 @@ function helperExpr(sexpr) { return !!(sexpr.isHelper || sexpr.params.length || sexpr.hash); } -function scopedId(id) { - return (/^\.|this\b/).test(id.original); +function scopedId(path) { + return (/^\.|this\b/).test(path.original); } // an ID is simple if it only has one part, and that part is not // `..` or `this`. -function simpleId(id) { - var part = id.parts[0]; +function simpleId(path) { + var part = path.parts[0]; - return id.parts.length === 1 && !scopedId(id) && !id.depth; + return path.parts.length === 1 && !scopedId(path) && !path.depth; } export function Compiler() {} @@ -147,7 +147,7 @@ Compiler.prototype = { this.opcode('pushProgram', block, program); this.opcode('pushProgram', block, inverse); this.opcode('emptyHash', block); - this.opcode('blockValue', block, sexpr.id.original); + this.opcode('blockValue', block, sexpr.path.original); } else { this.ambiguousSexpr(sexpr, program, inverse); @@ -163,7 +163,7 @@ Compiler.prototype = { }, PartialStatement: function(partial) { - var partialName = partial.sexpr.id.original; + var partialName = partial.sexpr.path.original; this.usePartial = true; if (partial.sexpr.hash) { @@ -194,7 +194,7 @@ Compiler.prototype = { }, MustacheStatement: function(mustache) { - this.sexpr(mustache.sexpr); + this.accept(mustache.sexpr); if(mustache.escaped && !this.options.noEscape) { this.opcode('appendEscaped', mustache); @@ -211,91 +211,67 @@ Compiler.prototype = { CommentStatement: function() {}, - sexpr: function(sexpr) { + SubExpression: function(sexpr) { var type = this.classifySexpr(sexpr); - if (type === "simple") { + if (type === 'simple') { this.simpleSexpr(sexpr); - } else if (type === "helper") { + } else if (type === 'helper') { this.helperSexpr(sexpr); } else { this.ambiguousSexpr(sexpr); } }, ambiguousSexpr: function(sexpr, program, inverse) { - var id = sexpr.id, - name = id.parts[0], + var path = sexpr.path, + name = path.parts[0], isBlock = program != null || inverse != null; - this.opcode('getContext', sexpr, id.depth); + this.opcode('getContext', sexpr, path.depth); this.opcode('pushProgram', sexpr, program); this.opcode('pushProgram', sexpr, inverse); - this.accept(id); + this.accept(path); this.opcode('invokeAmbiguous', sexpr, name, isBlock); }, simpleSexpr: function(sexpr) { - var id = sexpr.id; - - if (id.parts.length) { - this.accept(id); - } else { - // Simplified ID for `this` - this.addDepth(id.depth); - this.opcode('getContext', sexpr, id.depth); - this.opcode('pushContext', sexpr); - } - + this.accept(sexpr.path); this.opcode('resolvePossibleLambda', sexpr); }, helperSexpr: function(sexpr, program, inverse) { var params = this.setupFullMustacheParams(sexpr, program, inverse), - id = sexpr.id, - name = id.parts[0]; + path = sexpr.path, + name = path.parts[0]; if (this.options.knownHelpers[name]) { this.opcode('invokeKnownHelper', sexpr, params.length, name); } else if (this.options.knownHelpersOnly) { throw new Exception("You specified knownHelpersOnly, but used the unknown helper " + name, sexpr); } else { - id.falsy = true; - - this.accept(id); - this.opcode('invokeHelper', sexpr, params.length, id.original, simpleId(id)); - } - }, - - hash: function(hash) { - var pairs = hash.pairs, i, l; - - this.opcode('pushHash', hash); + path.falsy = true; - for(i=0, l=pairs.length; i<l; i++) { - this.pushParam(pairs[i][1]); + this.accept(path); + this.opcode('invokeHelper', sexpr, params.length, path.original, simpleId(path)); } - while(i--) { - this.opcode('assignToHash', hash, pairs[i][0]); - } - this.opcode('popHash', hash); }, - PathExpression: function(id) { - this.addDepth(id.depth); - this.opcode('getContext', id, id.depth); + PathExpression: function(path) { + this.addDepth(path.depth); + this.opcode('getContext', path, path.depth); - var name = id.parts[0]; + var name = path.parts[0]; if (!name) { // Context reference, i.e. `{{foo .}}` or `{{foo ..}}` - this.opcode('pushContext', id); - } else if (id.data) { + this.opcode('pushContext', path); + } else if (path.data) { this.options.data = true; - this.opcode('lookupData', id, id.depth, id.parts); + this.opcode('lookupData', path, path.depth, path.parts); } else { - this.opcode('lookupOnContext', id, id.parts, id.falsy, scopedId(id)); + this.opcode('lookupOnContext', path, path.parts, path.falsy, scopedId(path)); } }, @@ -311,6 +287,20 @@ Compiler.prototype = { this.opcode('pushLiteral', bool, bool.value); }, + Hash: function(hash) { + var pairs = hash.pairs, i, l; + + this.opcode('pushHash', hash); + + for (i=0, l=pairs.length; i<l; i++) { + this.pushParam(pairs[i].value); + } + while (i--) { + this.opcode('assignToHash', hash, pairs[i].key); + } + this.opcode('popHash', hash); + }, + // HELPERS opcode: function(name, node) { this.opcodes.push({ opcode: name, args: slice.call(arguments, 2), loc: node.loc }); @@ -328,19 +318,19 @@ Compiler.prototype = { classifySexpr: function(sexpr) { // a mustache is an eligible helper if: // * its id is simple (a single part, not `this` or `..`) - var isHelper = helperExpr(sexpr); + var isHelper = helperExpr(sexpr); // if a mustache is an eligible helper but not a definite // helper, it is ambiguous, and will be resolved in a later // pass or at runtime. - var isEligible = isHelper || simpleId(sexpr.id); + var isEligible = isHelper || simpleId(sexpr.path); - var options = this.options; + var options = this.options; // if ambiguous, we can possibly resolve the ambiguity now // An eligible helper is one that does not have a complex path, i.e. `this.foo`, `../foo` etc. if (isEligible && !isHelper) { - var name = sexpr.id.parts[0]; + var name = sexpr.path.parts[0]; if (options.knownHelpers[name]) { isHelper = true; @@ -349,9 +339,9 @@ Compiler.prototype = { } } - if (isHelper) { return "helper"; } - else if (isEligible) { return "ambiguous"; } - else { return "simple"; } + if (isHelper) { return 'helper'; } + else if (isEligible) { return 'ambiguous'; } + else { return 'simple'; } }, pushParams: function(params) { @@ -364,7 +354,7 @@ Compiler.prototype = { var value = val.value != null ? val.value : val.original || ''; // Force helper evaluation - if (val.type === 'sexpr') { + if (val.type === 'SubExpression') { val.isHelper = true; } @@ -381,10 +371,10 @@ Compiler.prototype = { this.opcode('getContext', val, val.depth || 0); this.opcode('pushStringParam', val, value, val.type); - if (val.type === 'sexpr') { + if (val.type === 'SubExpression') { // Subexpressions get evaluated and passed in // in string params mode. - this.sexpr(val); + this.accept(val); } } else { if (this.trackIds) { @@ -409,7 +399,7 @@ Compiler.prototype = { this.opcode('pushProgram', sexpr, inverse); if (sexpr.hash) { - this.hash(sexpr.hash); + this.accept(sexpr.hash); } else { this.opcode('emptyHash', sexpr); } |