diff options
Diffstat (limited to 'lib/handlebars/compiler')
-rw-r--r-- | lib/handlebars/compiler/ast.js | 50 | ||||
-rw-r--r-- | lib/handlebars/compiler/base.js | 11 | ||||
-rw-r--r-- | lib/handlebars/compiler/helpers.js | 41 |
3 files changed, 56 insertions, 46 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index e388e54..5a3057f 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -1,6 +1,6 @@ import Exception from "../exception"; -function LocationInfo(locInfo){ +function LocationInfo(locInfo) { locInfo = locInfo || {}; this.firstLine = locInfo.first_line; this.firstColumn = locInfo.first_column; @@ -9,38 +9,11 @@ function LocationInfo(locInfo){ } var AST = { - ProgramNode: function(statements, inverseStrip, inverse, locInfo) { - var inverseLocationInfo, firstInverseNode; - if (arguments.length === 3) { - locInfo = inverse; - inverse = null; - } else if (arguments.length === 2) { - locInfo = inverseStrip; - inverseStrip = null; - } - + ProgramNode: function(statements, strip, locInfo) { LocationInfo.call(this, locInfo); this.type = "program"; this.statements = statements; - this.strip = {}; - - if(inverse) { - firstInverseNode = inverse[0]; - if (firstInverseNode) { - inverseLocationInfo = { - first_line: firstInverseNode.firstLine, - last_line: firstInverseNode.lastLine, - last_column: firstInverseNode.lastColumn, - first_column: firstInverseNode.firstColumn - }; - this.inverse = new AST.ProgramNode(inverse, inverseStrip, inverseLocationInfo); - } else { - this.inverse = new AST.ProgramNode(inverse, inverseStrip); - } - this.strip.right = inverseStrip.left; - } else if (inverseStrip) { - this.strip.left = inverseStrip.right; - } + this.strip = strip; }, MustacheNode: function(rawParams, hash, open, strip, locInfo) { @@ -106,25 +79,14 @@ var AST = { this.strip = strip; }, - BlockNode: function(mustache, program, inverse, close, locInfo) { + BlockNode: function(mustache, program, inverse, strip, locInfo) { LocationInfo.call(this, locInfo); - if(mustache.sexpr.id.original !== close.path.original) { - throw new Exception(mustache.sexpr.id.original + " doesn't match " + close.path.original, this); - } - this.type = 'block'; this.mustache = mustache; this.program = program; this.inverse = inverse; - - this.strip = { - left: mustache.strip.left, - right: close.strip.right - }; - - (program || inverse).strip.left = mustache.strip.right; - (inverse || program).strip.right = close.strip.left; + this.strip = strip; if (inverse && !program) { this.isInverse = true; @@ -142,7 +104,7 @@ var AST = { this.type = 'block'; this.mustache = mustache; - this.program = new AST.ProgramNode([content], locInfo); + this.program = new AST.ProgramNode([content], {}, locInfo); }, ContentNode: function(string, locInfo) { diff --git a/lib/handlebars/compiler/base.js b/lib/handlebars/compiler/base.js index 722f09a..7ab1843 100644 --- a/lib/handlebars/compiler/base.js +++ b/lib/handlebars/compiler/base.js @@ -1,12 +1,19 @@ import parser from "./parser"; import AST from "./ast"; +import { stripFlags, prepareBlock } from "./helpers"; export { parser }; export function parse(input) { // Just return if an already-compile AST was passed in. - if(input.constructor === AST.ProgramNode) { return input; } + if (input.constructor === AST.ProgramNode) { return input; } + + for (var key in AST) { + parser.yy[key] = AST[key]; + } + + parser.yy.stripFlags = stripFlags; + parser.yy.prepareBlock = prepareBlock; - parser.yy = AST; return parser.parse(input); } diff --git a/lib/handlebars/compiler/helpers.js b/lib/handlebars/compiler/helpers.js new file mode 100644 index 0000000..1a2bd26 --- /dev/null +++ b/lib/handlebars/compiler/helpers.js @@ -0,0 +1,41 @@ +import Exception from "../exception"; +import AST from "./ast"; + +export function stripFlags(open, close) { + return { + left: open.charAt(2) === '~', + right: close.charAt(close.length-3) === '~' + }; +} + +export function prepareBlock(mustache, program, inverseAndProgram, close, inverted, locInfo) { + if (mustache.sexpr.id.original !== close.path.original) { + throw new Exception(mustache.sexpr.id.original + " doesn't match " + close.path.original, mustache); + } + + var inverse, strip; + + strip = { + left: mustache.strip.left, + right: close.strip.right + }; + + if (inverseAndProgram) { + inverse = inverseAndProgram.program; + var inverseStrip = inverseAndProgram.strip; + + program.strip.left = mustache.strip.right; + program.strip.right = inverseStrip.left; + inverse.strip.left = inverseStrip.right; + inverse.strip.right = close.strip.left; + } else { + program.strip.left = mustache.strip.right; + program.strip.right = close.strip.left; + } + + if (inverted) { + return new AST.BlockNode(mustache, inverse, program, strip, locInfo); + } else { + return new AST.BlockNode(mustache, program, inverse, strip, locInfo); + } +} |