diff options
Diffstat (limited to 'lib/handlebars/compiler')
-rw-r--r-- | lib/handlebars/compiler/ast.js | 41 | ||||
-rw-r--r-- | lib/handlebars/compiler/base.js | 25 | ||||
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 55 | ||||
-rw-r--r-- | lib/handlebars/compiler/index.js | 18 | ||||
-rw-r--r-- | lib/handlebars/compiler/javascript-compiler.js | 36 | ||||
-rw-r--r-- | lib/handlebars/compiler/printer.js | 42 | ||||
-rw-r--r-- | lib/handlebars/compiler/visitor.js | 15 |
7 files changed, 89 insertions, 143 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index 567e297..b61fa0e 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -1,15 +1,14 @@ -exports.attach = function(Handlebars) { +import { Exception } from "../utils"; -// BEGIN(BROWSER) -Handlebars.AST = {}; +var exports = {}; -Handlebars.AST.ProgramNode = function(statements, inverse) { +exports.ProgramNode = function ProgramNode(statements, inverse) { this.type = "program"; this.statements = statements; - if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); } + if(inverse) { this.inverse = new ProgramNode(inverse); } }; -Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) { +exports.MustacheNode = function(rawParams, hash, unescaped) { this.type = "mustache"; this.escaped = !unescaped; this.hash = hash; @@ -31,15 +30,15 @@ Handlebars.AST.MustacheNode = function(rawParams, hash, unescaped) { // pass or at runtime. }; -Handlebars.AST.PartialNode = function(partialName, context) { +exports.PartialNode = function(partialName, context) { this.type = "partial"; this.partialName = partialName; this.context = context; }; -Handlebars.AST.BlockNode = function(mustache, program, inverse, close) { +exports.BlockNode = function(mustache, program, inverse, close) { if(mustache.id.original !== close.original) { - throw new Handlebars.Exception(mustache.id.original + " doesn't match " + close.original); + throw new Exception(mustache.id.original + " doesn't match " + close.original); } this.type = "block"; @@ -52,17 +51,17 @@ Handlebars.AST.BlockNode = function(mustache, program, inverse, close) { } }; -Handlebars.AST.ContentNode = function(string) { +exports.ContentNode = function(string) { this.type = "content"; this.string = string; }; -Handlebars.AST.HashNode = function(pairs) { +exports.HashNode = function(pairs) { this.type = "hash"; this.pairs = pairs; }; -Handlebars.AST.IdNode = function(parts) { +exports.IdNode = function(parts) { this.type = "ID"; var original = "", @@ -93,43 +92,39 @@ Handlebars.AST.IdNode = function(parts) { this.stringModeValue = this.string; }; -Handlebars.AST.PartialNameNode = function(name) { +exports.PartialNameNode = function(name) { this.type = "PARTIAL_NAME"; this.name = name.original; }; -Handlebars.AST.DataNode = function(id) { +exports.DataNode = function(id) { this.type = "DATA"; this.id = id; }; -Handlebars.AST.StringNode = function(string) { +exports.StringNode = function(string) { this.type = "STRING"; this.original = this.string = this.stringModeValue = string; }; -Handlebars.AST.IntegerNode = function(integer) { +exports.IntegerNode = function(integer) { this.type = "INTEGER"; this.original = this.integer = integer; this.stringModeValue = Number(integer); }; -Handlebars.AST.BooleanNode = function(bool) { +exports.BooleanNode = function(bool) { this.type = "BOOLEAN"; this.bool = bool; this.stringModeValue = bool === "true"; }; -Handlebars.AST.CommentNode = function(comment) { +exports.CommentNode = function(comment) { this.type = "comment"; this.comment = comment; }; -// END(BROWSER) - -return Handlebars; -}; - +export default exports; diff --git a/lib/handlebars/compiler/base.js b/lib/handlebars/compiler/base.js index 7594451..adbf4f2 100644 --- a/lib/handlebars/compiler/base.js +++ b/lib/handlebars/compiler/base.js @@ -1,21 +1,12 @@ -var handlebars = require("./parser"); +import parser from "./parser"; +import AST from "./ast"; -exports.attach = function(Handlebars) { - -// BEGIN(BROWSER) - -Handlebars.Parser = handlebars; - -Handlebars.parse = function(input) { +export var parser = parser; +export function parse(input) { // Just return if an already-compile AST was passed in. - if(input.constructor === Handlebars.AST.ProgramNode) { return input; } - - Handlebars.Parser.yy = Handlebars.AST; - return Handlebars.Parser.parse(input); -}; - -// END(BROWSER) + if(input.constructor === AST.ProgramNode) { return input; } -return Handlebars; -}; + parser.yy = AST; + return parser.parse(input); +} diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 98e12b1..6656a3f 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -1,13 +1,12 @@ -var compilerbase = require("./base"); - -exports.attach = function(Handlebars) { - -compilerbase.attach(Handlebars); - -// BEGIN(BROWSER) +import { Exception } from "../utils"; +import { template } from "../runtime"; +import { parse } from "./base"; +import { JavaScriptCompiler } from "./javascript-compiler"; +import AST from "./ast"; /*jshint eqnull:true*/ -var Compiler = Handlebars.Compiler = function() {}; + +export function Compiler() {}; // the foundHelper register will disambiguate helper lookup from finding a // function in a context. This is necessary for mustache compatibility, which @@ -40,6 +39,7 @@ Compiler.prototype = { return out.join("\n"); }, + equals: function(other) { var len = this.opcodes.length; if (other.opcodes.length !== len) { @@ -301,7 +301,7 @@ Compiler.prototype = { DATA: function(data) { this.options.data = true; if (data.id.isScoped || data.id.depth) { - throw new Handlebars.Exception('Scoped data references are not supported: ' + data.original); + throw new Exception('Scoped data references are not supported: ' + data.original); } this.opcode('lookupData'); @@ -415,49 +415,46 @@ Compiler.prototype = { } }; -Handlebars.precompile = function(input, options) { - if (input == null || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) { - throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.precompile. You passed " + input); +export function precompile(input, options) { + if (input == null || (typeof input !== 'string' && input.constructor !== AST.ProgramNode)) { + throw new Exception("You must pass a string or Handlebars AST to Handlebars.precompile. You passed " + input); } options = options || {}; if (!('data' in options)) { options.data = true; } - var ast = Handlebars.parse(input); + + var ast = parse(input); var environment = new Compiler().compile(ast, options); - return new Handlebars.JavaScriptCompiler().compile(environment, options); + return new JavaScriptCompiler().compile(environment, options); }; -Handlebars.compile = function(input, options) { - if (input == null || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) { - throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input); +export function compile(input, options) { + if (input == null || (typeof input !== 'string' && input.constructor !== AST.ProgramNode)) { + throw new Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input); } options = options || {}; + if (!('data' in options)) { options.data = true; } + var compiled; - function compile() { - var ast = Handlebars.parse(input); + + function compileInput() { + var ast = parse(input); var environment = new Compiler().compile(ast, options); - var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true); - return Handlebars.template(templateSpec); + var templateSpec = new JavaScriptCompiler().compile(environment, options, undefined, true); + return template(templateSpec, options.env || Handlebars, compile); } // Template is only compiled on first use and cached after that point. return function(context, options) { if (!compiled) { - compiled = compile(); + compiled = compileInput(); } return compiled.call(this, context, options); }; }; - - -// END(BROWSER) - -return Handlebars; - -}; diff --git a/lib/handlebars/compiler/index.js b/lib/handlebars/compiler/index.js deleted file mode 100644 index 0cc5c01..0000000 --- a/lib/handlebars/compiler/index.js +++ /dev/null @@ -1,18 +0,0 @@ -// Each of these module will augment the Handlebars object as it loads. No need to perform addition operations -module.exports.attach = function(Handlebars) { - -var visitor = require("./visitor"), - printer = require("./printer"), - ast = require("./ast"), - compiler = require("./compiler"), - javascriptCompiler = require("./javascript-compiler"); - -visitor.attach(Handlebars); -printer.attach(Handlebars); -ast.attach(Handlebars); -compiler.attach(Handlebars); -javascriptCompiler.attach(Handlebars); - -return Handlebars; - -}; diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js index 0bb1816..d3bd258 100644 --- a/lib/handlebars/compiler/javascript-compiler.js +++ b/lib/handlebars/compiler/javascript-compiler.js @@ -1,18 +1,10 @@ -var compilerbase = require("./base"); +import { COMPILER_REVISION, REVISION_CHANGES } from "../base"; -exports.attach = function(Handlebars) { - -compilerbase.attach(Handlebars); - -// BEGIN(BROWSER) -/*jshint eqnull:true*/ - -var Literal = function(value) { +function Literal(value) { this.value = value; }; - -var JavaScriptCompiler = Handlebars.JavaScriptCompiler = function() {}; +export function JavaScriptCompiler() {}; JavaScriptCompiler.prototype = { // PUBLIC API: You can override these methods in a subclass to provide @@ -51,7 +43,9 @@ JavaScriptCompiler.prototype = { this.environment = environment; this.options = options || {}; - Handlebars.log(Handlebars.logger.DEBUG, this.environment.disassemble() + "\n\n"); + // TODO: a module-compatible logger + // Handlebars.log(Handlebars.logger.DEBUG, this.environment.disassemble() + "\n\n"); + log(this.environment.disassemble() + "\n\n"); this.name = this.environment.name; this.isChild = !!context; @@ -162,8 +156,8 @@ JavaScriptCompiler.prototype = { var source = this.mergeSource(); if (!this.isChild) { - var revision = Handlebars.COMPILER_REVISION, - versions = Handlebars.REVISION_CHANGES[revision]; + var revision = COMPILER_REVISION, + versions = REVISION_CHANGES[revision]; source = "this.compilerInfo = ["+revision+",'"+versions+"'];\n"+source; } @@ -173,7 +167,11 @@ JavaScriptCompiler.prototype = { return Function.apply(this, params); } else { var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + source + '}'; - Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n"); + + // TODO: a module-compatible logger + //Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n"); + log(functionSource + "\n\n"); + return functionSource; } }, @@ -849,8 +847,6 @@ JavaScriptCompiler.isValidJavaScriptVariableName = function(name) { return false; }; -// END(BROWSER) - -return Handlebars; - -}; +function log(string) { + //console.log(string); +} diff --git a/lib/handlebars/compiler/printer.js b/lib/handlebars/compiler/printer.js index d9eb7a5..0280960 100644 --- a/lib/handlebars/compiler/printer.js +++ b/lib/handlebars/compiler/printer.js @@ -1,15 +1,13 @@ -exports.attach = function(Handlebars) { +import Visitor from "./visitor"; -// BEGIN(BROWSER) - -Handlebars.print = function(ast) { - return new Handlebars.PrintVisitor().accept(ast); +export function print(ast) { + return new PrintVisitor().accept(ast); }; -Handlebars.PrintVisitor = function() { this.padding = 0; }; -Handlebars.PrintVisitor.prototype = new Handlebars.Visitor(); +export function PrintVisitor() { this.padding = 0; }; +PrintVisitor.prototype = new Handlebars.Visitor(); -Handlebars.PrintVisitor.prototype.pad = function(string, newline) { +PrintVisitor.prototype.pad = function(string, newline) { var out = ""; for(var i=0,l=this.padding; i<l; i++) { @@ -22,7 +20,7 @@ Handlebars.PrintVisitor.prototype.pad = function(string, newline) { return out; }; -Handlebars.PrintVisitor.prototype.program = function(program) { +PrintVisitor.prototype.program = function(program) { var out = "", statements = program.statements, inverse = program.inverse, @@ -37,7 +35,7 @@ Handlebars.PrintVisitor.prototype.program = function(program) { return out; }; -Handlebars.PrintVisitor.prototype.block = function(block) { +PrintVisitor.prototype.block = function(block) { var out = ""; out = out + this.pad("BLOCK:"); @@ -76,13 +74,13 @@ Handlebars.PrintVisitor.prototype.mustache = function(mustache) { return this.pad("{{ " + this.accept(mustache.id) + " " + params + hash + " }}"); }; -Handlebars.PrintVisitor.prototype.partial = function(partial) { +PrintVisitor.prototype.partial = function(partial) { var content = this.accept(partial.partialName); if(partial.context) { content = content + " " + this.accept(partial.context); } return this.pad("{{> " + content + " }}"); }; -Handlebars.PrintVisitor.prototype.hash = function(hash) { +PrintVisitor.prototype.hash = function(hash) { var pairs = hash.pairs; var joinedPairs = [], left, right; @@ -95,19 +93,19 @@ Handlebars.PrintVisitor.prototype.hash = function(hash) { return "HASH{" + joinedPairs.join(", ") + "}"; }; -Handlebars.PrintVisitor.prototype.STRING = function(string) { +PrintVisitor.prototype.STRING = function(string) { return '"' + string.string + '"'; }; -Handlebars.PrintVisitor.prototype.INTEGER = function(integer) { +PrintVisitor.prototype.INTEGER = function(integer) { return "INTEGER{" + integer.integer + "}"; }; -Handlebars.PrintVisitor.prototype.BOOLEAN = function(bool) { +PrintVisitor.prototype.BOOLEAN = function(bool) { return "BOOLEAN{" + bool.bool + "}"; }; -Handlebars.PrintVisitor.prototype.ID = function(id) { +PrintVisitor.prototype.ID = function(id) { var path = id.parts.join("/"); if(id.parts.length > 1) { return "PATH:" + path; @@ -116,23 +114,19 @@ Handlebars.PrintVisitor.prototype.ID = function(id) { } }; -Handlebars.PrintVisitor.prototype.PARTIAL_NAME = function(partialName) { +PrintVisitor.prototype.PARTIAL_NAME = function(partialName) { return "PARTIAL:" + partialName.name; }; -Handlebars.PrintVisitor.prototype.DATA = function(data) { +PrintVisitor.prototype.DATA = function(data) { return "@" + this.accept(data.id); }; -Handlebars.PrintVisitor.prototype.content = function(content) { +PrintVisitor.prototype.content = function(content) { return this.pad("CONTENT[ '" + content.string + "' ]"); }; -Handlebars.PrintVisitor.prototype.comment = function(comment) { +PrintVisitor.prototype.comment = function(comment) { return this.pad("{{! '" + comment.comment + "' }}"); }; -// END(BROWSER) - -return Handlebars; -}; diff --git a/lib/handlebars/compiler/visitor.js b/lib/handlebars/compiler/visitor.js index 5d07314..29ea82c 100644 --- a/lib/handlebars/compiler/visitor.js +++ b/lib/handlebars/compiler/visitor.js @@ -1,18 +1,9 @@ -exports.attach = function(Handlebars) { +export function Visitor() {}; -// BEGIN(BROWSER) +Visitor.prototype = { + constructor: Visitor, -Handlebars.Visitor = function() {}; - -Handlebars.Visitor.prototype = { accept: function(object) { return this[object.type](object); } }; - -// END(BROWSER) - -return Handlebars; -}; - - |