diff options
Diffstat (limited to 'lib/handlebars/compiler')
-rw-r--r-- | lib/handlebars/compiler/ast.js | 241 | ||||
-rw-r--r-- | lib/handlebars/compiler/base.js | 7 | ||||
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 12 | ||||
-rw-r--r-- | lib/handlebars/compiler/index.js | 19 | ||||
-rw-r--r-- | lib/handlebars/compiler/printer.js | 7 | ||||
-rw-r--r-- | lib/handlebars/compiler/visitor.js | 7 |
6 files changed, 163 insertions, 130 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index 25abe0a..9648ef0 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -1,123 +1,126 @@ -var Handlebars = require('./base'); +exports.attach = function(Handlebars) { // BEGIN(BROWSER) -(function() { - - 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); - - // a mustache is an eligible helper if: - // * its id is simple (a single part, not `this` or `..`) - var eligibleHelper = this.eligibleHelper = id.isSimple; - - // 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); - - // 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. - }; - - Handlebars.AST.PartialNode = function(id, context) { - this.type = "partial"; - - // TODO: disallow complex IDs - - this.id = id; - this.context = context; - }; - - 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) { - verifyMatch(mustache.id, close); - this.type = "block"; - this.mustache = mustache; - this.program = program; - this.inverse = inverse; - - if (this.inverse && !this.program) { - this.isInverse = true; - } - }; - - Handlebars.AST.ContentNode = function(string) { - this.type = "content"; - this.string = string; - }; - - Handlebars.AST.HashNode = function(pairs) { - this.type = "hash"; - this.pairs = pairs; - }; - - Handlebars.AST.IdNode = function(parts) { - this.type = "ID"; - this.original = parts.join("."); - - var dig = [], depth = 0; - - for(var i=0,l=parts.length; i<l; i++) { - var part = parts[i]; - - if(part === "..") { depth++; } - else if(part === "." || part === "this") { this.isScoped = true; } - else { dig.push(part); } - } - - this.parts = dig; - this.string = dig.join('.'); - this.depth = depth; - - // 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.DataNode = function(id) { - this.type = "DATA"; - this.id = id; - }; - - Handlebars.AST.StringNode = function(string) { - this.type = "STRING"; - this.string = string; - }; - - Handlebars.AST.IntegerNode = function(integer) { - this.type = "INTEGER"; - this.integer = integer; - }; - - Handlebars.AST.BooleanNode = function(bool) { - this.type = "BOOLEAN"; - this.bool = bool; - }; - - Handlebars.AST.CommentNode = function(comment) { - this.type = "comment"; - this.comment = comment; - }; - -})(); + +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); + + // a mustache is an eligible helper if: + // * its id is simple (a single part, not `this` or `..`) + var eligibleHelper = this.eligibleHelper = id.isSimple; + + // 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); + + // 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. +}; + +Handlebars.AST.PartialNode = function(id, context) { + this.type = "partial"; + + // TODO: disallow complex IDs + + this.id = id; + this.context = context; +}; + +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) { + verifyMatch(mustache.id, close); + this.type = "block"; + this.mustache = mustache; + this.program = program; + this.inverse = inverse; + + if (this.inverse && !this.program) { + this.isInverse = true; + } +}; + +Handlebars.AST.ContentNode = function(string) { + this.type = "content"; + this.string = string; +}; + +Handlebars.AST.HashNode = function(pairs) { + this.type = "hash"; + this.pairs = pairs; +}; + +Handlebars.AST.IdNode = function(parts) { + this.type = "ID"; + this.original = parts.join("."); + + var dig = [], depth = 0; + + for(var i=0,l=parts.length; i<l; i++) { + var part = parts[i]; + + if(part === "..") { depth++; } + else if(part === "." || part === "this") { this.isScoped = true; } + else { dig.push(part); } + } + + this.parts = dig; + this.string = dig.join('.'); + this.depth = depth; + + // 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.DataNode = function(id) { + this.type = "DATA"; + this.id = id; +}; + +Handlebars.AST.StringNode = function(string) { + this.type = "STRING"; + this.string = string; +}; + +Handlebars.AST.IntegerNode = function(integer) { + this.type = "INTEGER"; + this.integer = integer; +}; + +Handlebars.AST.BooleanNode = function(bool) { + this.type = "BOOLEAN"; + this.bool = bool; +}; + +Handlebars.AST.CommentNode = function(comment) { + this.type = "comment"; + this.comment = comment; +}; + // END(BROWSER) +return Handlebars; +}; + + + diff --git a/lib/handlebars/compiler/base.js b/lib/handlebars/compiler/base.js index 4bb8735..6f8fdc5 100644 --- a/lib/handlebars/compiler/base.js +++ b/lib/handlebars/compiler/base.js @@ -1,7 +1,9 @@ var handlebars = require("./parser").parser; -var Handlebars = require("../base"); + +exports.attach = function(Handlebars) { // BEGIN(BROWSER) + Handlebars.Parser = handlebars; Handlebars.parse = function(string) { @@ -24,4 +26,5 @@ Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); }; // END(BROWSER) -module.exports = Handlebars; +return Handlebars; +}; diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 7578dd2..6dc43d6 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -1,4 +1,8 @@ -var Handlebars = require("./base"); +var compilerbase = require("./base"); + +exports.attach = function(Handlebars) { + +compilerbase.attach(Handlebars); // BEGIN(BROWSER) @@ -1062,5 +1066,11 @@ Handlebars.compile = function(string, options) { }; }; + // END(BROWSER) +return Handlebars; + +}; + + diff --git a/lib/handlebars/compiler/index.js b/lib/handlebars/compiler/index.js index e62c768..6ed8d21 100644 --- a/lib/handlebars/compiler/index.js +++ b/lib/handlebars/compiler/index.js @@ -1,7 +1,16 @@ // Each of these module will augment the Handlebars object as it loads. No need to perform addition operations -module.exports = require("./base"); -require("./visitor"); -require("./printer"); +module.exports.attach = function(Handlebars) { -require("./ast"); -require("./compiler"); +var visitor = require("./visitor"), + printer = require("./printer"), + ast = require("./ast"), + compiler = require("./compiler"); + +visitor.attach(Handlebars); +printer.attach(Handlebars); +ast.attach(Handlebars); +compiler.attach(Handlebars); + +return Handlebars; + +}; diff --git a/lib/handlebars/compiler/printer.js b/lib/handlebars/compiler/printer.js index 7a42a66..8c4bdd7 100644 --- a/lib/handlebars/compiler/printer.js +++ b/lib/handlebars/compiler/printer.js @@ -1,6 +1,7 @@ -var Handlebars = require("./base"); +exports.attach = function(Handlebars) { // BEGIN(BROWSER) + Handlebars.PrintVisitor = function() { this.padding = 0; }; Handlebars.PrintVisitor.prototype = new Handlebars.Visitor(); @@ -124,4 +125,6 @@ Handlebars.PrintVisitor.prototype.comment = function(comment) { }; // END(BROWSER) -exports.PrintVisitor = Handlebars.PrintVisitor; +return Handlebars; +}; + diff --git a/lib/handlebars/compiler/visitor.js b/lib/handlebars/compiler/visitor.js index a557dd3..5d07314 100644 --- a/lib/handlebars/compiler/visitor.js +++ b/lib/handlebars/compiler/visitor.js @@ -1,4 +1,4 @@ -var Handlebars = require("./base"); +exports.attach = function(Handlebars) { // BEGIN(BROWSER) @@ -9,5 +9,10 @@ Handlebars.Visitor.prototype = { return this[object.type](object); } }; + // END(BROWSER) +return Handlebars; +}; + + |