From b8a9f7264d3b6ac48514272bf35291736cedad00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Mu=C3=B1oz?= Date: Tue, 11 Nov 2014 21:35:10 -0500 Subject: [PATCH] Add parser support for block params --- lib/handlebars/compiler/ast.js | 3 ++- lib/handlebars/compiler/helpers.js | 4 +++- lib/handlebars/compiler/printer.js | 9 +++++++++ spec/ast.js | 2 +- spec/compiler.js | 4 ++-- spec/parser.js | 10 +++++++++- spec/tokenizer.js | 14 ++++++++++++++ src/handlebars.l | 4 +++- src/handlebars.yy | 13 ++++++++----- 9 files changed, 51 insertions(+), 12 deletions(-) diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index 35a60dbed..0bc70e93a 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -9,10 +9,11 @@ function LocationInfo(locInfo) { } var AST = { - ProgramNode: function(statements, strip, locInfo) { + ProgramNode: function(statements, blockParams, strip, locInfo) { LocationInfo.call(this, locInfo); this.type = "program"; this.statements = statements; + this.blockParams = blockParams; this.strip = strip; }, diff --git a/lib/handlebars/compiler/helpers.js b/lib/handlebars/compiler/helpers.js index d9b7b14cf..50a3c53a3 100644 --- a/lib/handlebars/compiler/helpers.js +++ b/lib/handlebars/compiler/helpers.js @@ -23,7 +23,7 @@ export function prepareRawBlock(openRawBlock, content, close, locInfo) { throw new Exception(openRawBlock.sexpr.id.original + " doesn't match " + close, errorNode); } - var program = new this.ProgramNode([content], {}, locInfo); + var program = new this.ProgramNode([content], null, {}, locInfo); return new this.BlockNode(openRawBlock.sexpr, program, undefined, undefined, locInfo); } @@ -40,6 +40,8 @@ export function prepareBlock(openBlock, program, inverseAndProgram, close, inver throw new Exception(openBlock.sexpr.id.original + ' doesn\'t match ' + close.path.original, errorNode); } + program.blockParams = openBlock.blockParams; + // Safely handle a chained inverse that does not have a non-conditional inverse // (i.e. both inverseAndProgram AND close are undefined) if (!close) { diff --git a/lib/handlebars/compiler/printer.js b/lib/handlebars/compiler/printer.js index c329373ea..e93652c88 100644 --- a/lib/handlebars/compiler/printer.js +++ b/lib/handlebars/compiler/printer.js @@ -26,6 +26,15 @@ PrintVisitor.prototype.program = function(program) { statements = program.statements, i, l; + if (program.blockParams) { + var blockParams = "BLOCK PARAMS: ["; + for(i=0, l=program.blockParams.length; i@\[-\^`\{-~]+/{LOOKAHEAD} "true"/{LITERAL_LOOKAHEAD} return 'BOOLEAN'; "false"/{LITERAL_LOOKAHEAD} return 'BOOLEAN'; \-?[0-9]+(?:\.[0-9]+)?/{LITERAL_LOOKAHEAD} return 'NUMBER'; +"as"\s+"|" return 'OPEN_BLOCK_PARAMS'; +"|" return 'CLOSE_BLOCK_PARAMS'; {ID} return 'ID'; diff --git a/src/handlebars.yy b/src/handlebars.yy index 5448966f7..775d5ca0f 100644 --- a/src/handlebars.yy +++ b/src/handlebars.yy @@ -9,7 +9,7 @@ root ; program - : statement* -> new yy.ProgramNode(yy.prepareProgram($1), {}, @$) + : statement* -> new yy.ProgramNode(yy.prepareProgram($1), null, {}, @$) ; statement @@ -39,11 +39,11 @@ block ; openBlock - : OPEN_BLOCK sexpr CLOSE -> { sexpr: $2, strip: yy.stripFlags($1, $3) } + : OPEN_BLOCK sexpr blockParams? CLOSE -> { sexpr: $2, blockParams: $3, strip: yy.stripFlags($1, $4) } ; openInverse - : OPEN_INVERSE sexpr CLOSE -> { sexpr: $2, strip: yy.stripFlags($1, $3) } + : OPEN_INVERSE sexpr blockParams? CLOSE -> { sexpr: $2, blockParams: $3, strip: yy.stripFlags($1, $4) } ; openInverseChain @@ -57,7 +57,7 @@ inverseAndProgram inverseChain : openInverseChain program inverseChain? { var inverse = yy.prepareBlock($1, $2, $3, $3, false, @$), - program = new yy.ProgramNode(yy.prepareProgram([inverse]), {}, @$); + program = new yy.ProgramNode(yy.prepareProgram([inverse]), null, {}, @$); program.inverse = inverse; @@ -104,6 +104,10 @@ hashSegment : ID EQUALS param -> [$1, $3] ; +blockParams + : OPEN_BLOCK_PARAMS ID+ CLOSE_BLOCK_PARAMS -> $2 + ; + partialName : path -> new yy.PartialNameNode($1, @$) | STRING -> new yy.PartialNameNode(new yy.StringNode($1, @$), @$) @@ -122,4 +126,3 @@ pathSegments : pathSegments SEP ID { $1.push({part: $3, separator: $2}); $$ = $1; } | ID -> [{part: $1}] ; -