From e0aa705f71ec745f1444c6a05aa9b49a408b8435 Mon Sep 17 00:00:00 2001 From: tomhuda Date: Thu, 3 Mar 2011 21:33:28 -0800 Subject: [PATCH] Add support for hash args in the tokenizer and parser --- lib/handlebars/ast.js | 5 +++++ lib/handlebars/printer.js | 13 +++++++++++++ spec/parser_spec.rb | 22 ++++++++++++++++++++++ spec/tokenizer_spec.rb | 24 ++++++++++++++++++++++-- src/handlebars.l | 3 ++- src/handlebars.yy | 18 +++++++++++++++++- 6 files changed, 81 insertions(+), 4 deletions(-) diff --git a/lib/handlebars/ast.js b/lib/handlebars/ast.js index 8966fd65..ccaef511 100644 --- a/lib/handlebars/ast.js +++ b/lib/handlebars/ast.js @@ -52,6 +52,11 @@ var Handlebars = require("handlebars"); 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("/"); diff --git a/lib/handlebars/printer.js b/lib/handlebars/printer.js index 549e56b6..86a67ef7 100644 --- a/lib/handlebars/printer.js +++ b/lib/handlebars/printer.js @@ -89,6 +89,19 @@ Handlebars.PrintVisitor.prototype.partial = function(partial) { return this.pad("{{> " + content + " }}"); }; +Handlebars.PrintVisitor.prototype.hash = function(hash) { + var pairs = hash.pairs; + var joinedPairs = [], left, right; + + for(var i=0, l=pairs.length; i"{{!".*?"}}" { yytext = yytext.substr(3,yyleng-5); this.begin("INITIAL"); return 'COMMENT'; } "{{" { return 'OPEN'; } +"=" { return 'EQUALS'; } "."/[} ] { return 'ID'; } ".." { return 'ID'; } [/.] { return 'SEP'; } @@ -23,7 +24,7 @@ "}}}" { this.begin("INITIAL"); return 'CLOSE'; } "}}" { this.begin("INITIAL"); return 'CLOSE'; } '"'("\\"["]|[^"])*'"' { yytext = yytext.substr(1,yyleng-2).replace(/\\"/g,'"'); return 'STRING'; } -[a-zA-Z0-9_]+/[} /.] { return 'ID'; } +[a-zA-Z0-9_]+/[=} /.] { return 'ID'; } . { return 'INVALID'; } <> { return 'EOF'; } diff --git a/src/handlebars.yy b/src/handlebars.yy index 7d2dcb01..e9a0c13c 100644 --- a/src/handlebars.yy +++ b/src/handlebars.yy @@ -54,7 +54,9 @@ simpleInverse ; inMustache - : path params { $$ = [$1].concat($2) } + : path params hash { $$ = [$1].concat($2).concat([$3]) } + | path params { $$ = [$1].concat($2) } + | path hash { $$ = [$1].concat([$2]) } | path { $$ = [$1] } ; @@ -68,6 +70,20 @@ param | STRING { $$ = new yy.StringNode($1) } ; +hash + : hashSegments { $$ = new yy.HashNode($1) } + ; + +hashSegments + : hashSegments hashSegment { $1.push($2); $$ = $1 } + | hashSegment { $$ = [$1] } + ; + +hashSegment + : ID EQUALS path { $$ = [$1, $3] } + | ID EQUALS STRING { $$ = [$1, new yy.StringNode($3)] } + ; + path : pathSegments { $$ = new yy.IdNode($1) } ;