diff options
-rw-r--r-- | lib/handlebars/compiler/ast.js | 34 | ||||
-rw-r--r-- | src/handlebars.yy | 31 |
2 files changed, 47 insertions, 18 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index db7063c..f6229e2 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -1,16 +1,25 @@ import Exception from "../exception"; -export function ProgramNode(statements, inverse) { +export function ProgramNode(statements, inverseStrip, inverse) { this.type = "program"; this.statements = statements; - if(inverse) { this.inverse = new ProgramNode(inverse); } + this.strip = {}; + + if(inverse) { + this.inverse = new ProgramNode(inverse, inverseStrip); + this.strip.right = inverseStrip.left; + } else if (inverseStrip) { + this.strip.left = inverseStrip.right; + } } -export function MustacheNode(rawParams, hash, open) { +export function MustacheNode(rawParams, hash, open, strip) { this.type = "mustache"; this.hash = hash; + this.strip = strip; - this.escaped = open[2] !== '{' && open[2] !== '&'; + var escapeFlag = open[3] || open[2]; + this.escaped = escapeFlag !== '{' && escapeFlag !== '&'; var id = this.id = rawParams[0]; var params = this.params = rawParams.slice(1); @@ -29,15 +38,16 @@ export function MustacheNode(rawParams, hash, open) { // pass or at runtime. } -export function PartialNode(partialName, context) { +export function PartialNode(partialName, context, strip) { this.type = "partial"; this.partialName = partialName; this.context = context; + this.strip = strip; } export function BlockNode(mustache, program, inverse, close) { - if(mustache.id.original !== close.original) { - throw new Exception(mustache.id.original + " doesn't match " + close.original); + if(mustache.id.original !== close.path.original) { + throw new Exception(mustache.id.original + " doesn't match " + close.path.original); } this.type = "block"; @@ -45,7 +55,15 @@ export function BlockNode(mustache, program, inverse, close) { this.program = program; this.inverse = inverse; - if (this.inverse && !this.program) { + this.strip = { + left: mustache.strip.left, + right: close.strip.right + }; + + (program || inverse).strip.left = mustache.strip.right; + (inverse || program).strip.right = close.strip.left; + + if (inverse && !program) { this.isInverse = true; } } diff --git a/src/handlebars.yy b/src/handlebars.yy index 96a5029..6ee2207 100644 --- a/src/handlebars.yy +++ b/src/handlebars.yy @@ -2,6 +2,17 @@ %ebnf +%{ + +function stripFlags(open, close) { + return { + left: open[2] === '(', + right: close[0] === ')' || close[1] === ')' + }; +} + +%} + %% root @@ -9,9 +20,9 @@ root ; program - : simpleInverse statements -> new yy.ProgramNode([], $2) - | statements simpleInverse statements -> new yy.ProgramNode($1, $3) - | statements simpleInverse -> new yy.ProgramNode($1, []) + : simpleInverse statements -> new yy.ProgramNode([], $1, $2) + | statements simpleInverse statements -> new yy.ProgramNode($1, $2, $3) + | statements simpleInverse -> new yy.ProgramNode($1, $2, []) | statements -> new yy.ProgramNode($1) | simpleInverse -> new yy.ProgramNode([]) | "" -> new yy.ProgramNode([]) @@ -32,31 +43,31 @@ statement ; openBlock - : OPEN_BLOCK inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1) + : OPEN_BLOCK inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3)) ; openInverse - : OPEN_INVERSE inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1) + : OPEN_INVERSE inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3)) ; closeBlock - : OPEN_ENDBLOCK path CLOSE -> $2 + : OPEN_ENDBLOCK path CLOSE -> {path: $2, strip: stripFlags($1, $3)} ; mustache // Parsing out the '&' escape token at AST level saves ~500 bytes after min due to the removal of one parser node. // This also allows for handler unification as all mustache node instances can utilize the same handler - : OPEN inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1) - | OPEN_UNESCAPED inMustache CLOSE_UNESCAPED -> new yy.MustacheNode($2[0], $2[1], $1) + : OPEN inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3)) + | OPEN_UNESCAPED inMustache CLOSE_UNESCAPED -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3)) ; partial - : OPEN_PARTIAL partialName path? CLOSE -> new yy.PartialNode($2, $3) + : OPEN_PARTIAL partialName path? CLOSE -> new yy.PartialNode($2, $3, stripFlags($1, $4)) ; simpleInverse - : OPEN_INVERSE CLOSE { } + : OPEN_INVERSE CLOSE -> stripFlags($1, $2) ; inMustache |