diff options
author | Tommy Messbauer <tommy@vast.com> | 2013-02-11 23:00:47 -0600 |
---|---|---|
committer | Tommy Messbauer <tommy@vast.com> | 2013-02-11 23:00:47 -0600 |
commit | 1ca7462497b5207e7e58f3a37274d486068ed06a (patch) | |
tree | d6b2e65ea2c4da666660d70d5857f008bf5a41fe /lib/handlebars/compiler/ast.js | |
parent | 514c9391e0fbee8a0c8c184bcef77d6be38722aa (diff) | |
parent | 7c633b604df137f062d96c30f015847bad20e34e (diff) | |
download | handlebars.js-1ca7462497b5207e7e58f3a37274d486068ed06a.zip handlebars.js-1ca7462497b5207e7e58f3a37274d486068ed06a.tar.gz handlebars.js-1ca7462497b5207e7e58f3a37274d486068ed06a.tar.bz2 |
merge
Diffstat (limited to 'lib/handlebars/compiler/ast.js')
-rw-r--r-- | lib/handlebars/compiler/ast.js | 192 |
1 files changed, 101 insertions, 91 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index 27cd6cc..1ba1fcf 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -1,126 +1,136 @@ exports.attach = function(Handlebars) { // BEGIN(BROWSER) +(function() { -Handlebars.AST = {}; + Handlebars.AST = {}; -Handlebars.AST.ProgramNode = function(statements, inverse) { - this.type = "program"; - this.statements = statements; - if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); } -}; - -Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) { - this.type = "mustache"; - this.escaped = !unescaped; - this.hash = hash; - - var id = this.id = rawParams[0]; - var params = this.params = rawParams.slice(1); + Handlebars.AST.ProgramNode = function(statements, inverse) { + this.type = "program"; + this.statements = statements; + if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); } + }; - // a mustache is an eligible helper if: - // * its id is simple (a single part, not `this` or `..`) - var eligibleHelper = this.eligibleHelper = id.isSimple; + Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) { + this.type = "mustache"; + this.escaped = !unescaped; + this.hash = hash; - // a mustache is definitely a helper if: - // * it is an eligible helper, and - // * it has at least one parameter or hash segment - this.isHelper = eligibleHelper && (params.length || hash); + var id = this.id = rawParams[0]; + var params = this.params = rawParams.slice(1); - // 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. -}; + // a mustache is an eligible helper if: + // * its id is simple (a single part, not `this` or `..`) + var eligibleHelper = this.eligibleHelper = id.isSimple; -Handlebars.AST.PartialNode = function(id, context) { - this.type = "partial"; + // a mustache is definitely a helper if: + // * it is an eligible helper, and + // * it has at least one parameter or hash segment + this.isHelper = eligibleHelper && (params.length || hash); - // TODO: disallow complex IDs + // 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. + }; - this.id = id; - this.context = context; -}; + Handlebars.AST.PartialNode = function(partialName, context) { + this.type = "partial"; + this.partialName = partialName; + this.context = context; + }; -Handlebars.AST.BlockNode = function(mustache, program, inverse, close) { - var verifyMatch = function(open, close) { - if(open.original !== close.original) { - throw new Handlebars.Exception(open.original + " doesn't match " + close.original); + Handlebars.AST.BlockNode = function(mustache, program, inverse, close) { + var verifyMatch = function(open, close) { + if(open.original !== close.original) { + throw new Handlebars.Exception(open.original + " doesn't match " + close.original); + } + }; + + verifyMatch(mustache.id, close); + this.type = "block"; + this.mustache = mustache; + this.program = program; + this.inverse = inverse; + + if (this.inverse && !this.program) { + this.isInverse = true; } }; - verifyMatch(mustache.id, close); - this.type = "block"; - this.mustache = mustache; - this.program = program; - this.inverse = inverse; + Handlebars.AST.ContentNode = function(string) { + this.type = "content"; + this.string = string; + }; - if (this.inverse && !this.program) { - this.isInverse = true; - } -}; + Handlebars.AST.HashNode = function(pairs) { + this.type = "hash"; + this.pairs = pairs; + }; -Handlebars.AST.ContentNode = function(string) { - this.type = "content"; - this.string = string; -}; + Handlebars.AST.IdNode = function(parts) { + this.type = "ID"; + this.original = parts.join("."); -Handlebars.AST.HashNode = function(pairs) { - this.type = "hash"; - this.pairs = pairs; -}; + var dig = [], depth = 0; -Handlebars.AST.IdNode = function(parts) { - this.type = "ID"; - this.original = parts.join("."); + for(var i=0,l=parts.length; i<l; i++) { + var part = parts[i]; - var dig = [], depth = 0; + if (part === ".." || part === "." || part === "this") { + if (dig.length > 0) { throw new Handlebars.Exception("Invalid path: " + this.original); } + else if (part === "..") { depth++; } + else { this.isScoped = true; } + } + else { dig.push(part); } + } - for(var i=0,l=parts.length; i<l; i++) { - var part = parts[i]; + this.parts = dig; + this.string = dig.join('.'); + this.depth = depth; - if(part === "..") { depth++; } - else if(part === "." || part === "this") { this.isScoped = true; } - else { dig.push(part); } - } + // an ID is simple if it only has one part, and that part is not + // `..` or `this`. + this.isSimple = parts.length === 1 && !this.isScoped && depth === 0; - this.parts = dig; - this.string = dig.join('.'); - this.depth = depth; + this.stringModeValue = this.string; + }; - // an ID is simple if it only has one part, and that part is not - // `..` or `this`. - this.isSimple = parts.length === 1 && !this.isScoped && depth === 0; -}; + Handlebars.AST.PartialNameNode = function(name) { + this.type = "PARTIAL_NAME"; + this.name = name; + }; -Handlebars.AST.DataNode = function(id) { - this.type = "DATA"; - this.id = id; -}; + Handlebars.AST.DataNode = function(id) { + this.type = "DATA"; + this.id = id; + }; -Handlebars.AST.StringNode = function(string) { - this.type = "STRING"; - this.string = string; -}; + Handlebars.AST.StringNode = function(string) { + this.type = "STRING"; + this.string = string; + this.stringModeValue = string; + }; -Handlebars.AST.IntegerNode = function(integer) { - this.type = "INTEGER"; - this.integer = integer; -}; + Handlebars.AST.IntegerNode = function(integer) { + this.type = "INTEGER"; + this.integer = integer; + this.stringModeValue = Number(integer); + }; -Handlebars.AST.BooleanNode = function(bool) { - this.type = "BOOLEAN"; - this.bool = bool; -}; + Handlebars.AST.BooleanNode = function(bool) { + this.type = "BOOLEAN"; + this.bool = bool; + this.stringModeValue = bool === "true"; + }; -Handlebars.AST.CommentNode = function(comment) { - this.type = "comment"; - this.comment = comment; -}; + Handlebars.AST.CommentNode = function(comment) { + this.type = "comment"; + this.comment = comment; + }; +})(); // END(BROWSER) return Handlebars; }; - - |