diff options
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 99 |
1 files changed, 50 insertions, 49 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 69937a5..ea2272a 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -107,24 +107,19 @@ Compiler.prototype = { return guid; }, - block: function(block) { + BlockStatement: function(block) { var sexpr = block.sexpr, program = block.program, inverse = block.inverse; - if (program) { - program = this.compileProgram(program); - } - - if (inverse) { - inverse = this.compileProgram(inverse); - } + program = program && this.compileProgram(program); + inverse = inverse && this.compileProgram(inverse); var type = this.classifySexpr(sexpr); - if (type === "helper") { + if (type === 'helper') { this.helperSexpr(sexpr, program, inverse); - } else if (type === "simple") { + } else if (type === 'simple') { this.simpleSexpr(sexpr); // now that the simple mustache is resolved, we need to @@ -147,32 +142,23 @@ Compiler.prototype = { this.opcode('append', block); }, - 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][1]); - } - while(i--) { - this.opcode('assignToHash', hash, pairs[i][0]); - } - this.opcode('popHash', hash); - }, - - partial: function(partial) { - var partialName = partial.partialName; + PartialStatement: function(partial) { + var partialName = partial.sexpr.id.original; this.usePartial = true; - if (partial.hash) { - this.accept(partial.hash); + if (partial.sexpr.hash) { + this.accept(partial.sexpr.hash); } else { this.opcode('pushLiteral', partial, 'undefined'); } - if (partial.context) { - this.accept(partial.context); + var params = partial.sexpr.params; + if (params.length) { + if (params.length > 1) { + throw new Exception('Unsupported number of partial arguments: ' + params.length, partial); + } + + this.pushParam(params[0]); } else { this.opcode('getContext', partial, 0); this.opcode('pushContext', partial); @@ -183,16 +169,10 @@ Compiler.prototype = { this.opcode('appendContent', partial, indent); indent = ''; } - this.opcode('invokePartial', partial, partialName.name, indent); + this.opcode('invokePartial', partial, partialName, indent); this.opcode('append', partial); }, - content: function(content) { - if (content.string) { - this.opcode('appendContent', content, content.string); - } - }, - MustacheStatement: function(mustache) { this.sexpr(mustache.sexpr); @@ -203,6 +183,25 @@ Compiler.prototype = { } }, + ContentStatement: function(content) { + if (content.value) { + this.opcode('appendContent', content, content.value); + } + }, + + CommentStatement: function() {}, + + sexpr: function(sexpr) { + var type = this.classifySexpr(sexpr); + + if (type === "simple") { + this.simpleSexpr(sexpr); + } else if (type === "helper") { + this.helperSexpr(sexpr); + } else { + this.ambiguousSexpr(sexpr); + } + }, ambiguousSexpr: function(sexpr, program, inverse) { var id = sexpr.id, name = id.parts[0], @@ -252,16 +251,18 @@ Compiler.prototype = { } }, - sexpr: function(sexpr) { - var type = this.classifySexpr(sexpr); + hash: function(hash) { + var pairs = hash.pairs, i, l; - if (type === "simple") { - this.simpleSexpr(sexpr); - } else if (type === "helper") { - this.helperSexpr(sexpr); - } else { - this.ambiguousSexpr(sexpr); + this.opcode('pushHash', hash); + + for(i=0, l=pairs.length; i<l; i++) { + this.pushParam(pairs[i][1]); + } + while(i--) { + this.opcode('assignToHash', hash, pairs[i][0]); } + this.opcode('popHash', hash); }, ID: function(id) { @@ -294,8 +295,6 @@ Compiler.prototype = { this.opcode('pushLiteral', bool, bool.bool); }, - comment: function() {}, - // HELPERS opcode: function(name, node) { this.opcodes.push({ opcode: name, args: slice.call(arguments, 2), loc: node.loc }); @@ -339,12 +338,14 @@ Compiler.prototype = { }, pushParam: function(val) { + var stringModeValue = val.stringModeValue != null ? val.stringModeValue : ''; + if (this.stringParams) { if(val.depth) { this.addDepth(val.depth); } this.opcode('getContext', val, val.depth || 0); - this.opcode('pushStringParam', val, val.stringModeValue, val.type); + this.opcode('pushStringParam', val, stringModeValue, val.type); if (val.type === 'sexpr') { // Subexpressions get evaluated and passed in @@ -353,7 +354,7 @@ Compiler.prototype = { } } else { if (this.trackIds) { - this.opcode('pushId', val, val.type, val.idName || val.stringModeValue); + this.opcode('pushId', val, val.type, val.idName || stringModeValue); } this.accept(val); } |