diff options
author | tomhuda <tomhuda@tilde.io> | 2012-05-28 19:06:26 -0700 |
---|---|---|
committer | tomhuda <tomhuda@tilde.io> | 2012-05-28 19:06:26 -0700 |
commit | 0afc8b58d218d6220d5efdd2509754d13d9e1c55 (patch) | |
tree | 30c6bc9ddc743c13bbe69295f09bef9c1df5ca3a | |
parent | 1082ec2414e7d9c0cd5dcafa0dfa5510e3b1ed60 (diff) | |
download | handlebars.js-0afc8b58d218d6220d5efdd2509754d13d9e1c55.zip handlebars.js-0afc8b58d218d6220d5efdd2509754d13d9e1c55.tar.gz handlebars.js-0afc8b58d218d6220d5efdd2509754d13d9e1c55.tar.bz2 |
Clean up parser tests and AST printer
-rw-r--r-- | lib/handlebars/compiler/ast.js | 4 | ||||
-rw-r--r-- | lib/handlebars/compiler/printer.js | 44 | ||||
-rw-r--r-- | spec/parser_spec.rb | 62 |
3 files changed, 49 insertions, 61 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index 8381ca5..d61377e 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -54,6 +54,10 @@ var Handlebars = require('./base'); this.mustache = mustache; this.program = program; this.inverse = inverse; + + if (this.inverse && !this.program) { + this.isInverse = true; + } }; Handlebars.AST.ContentNode = function(string) { diff --git a/lib/handlebars/compiler/printer.js b/lib/handlebars/compiler/printer.js index b7485b7..8157190 100644 --- a/lib/handlebars/compiler/printer.js +++ b/lib/handlebars/compiler/printer.js @@ -18,31 +18,17 @@ Handlebars.PrintVisitor.prototype.pad = function(string, newline) { }; Handlebars.PrintVisitor.prototype.program = function(program) { - var out = this.pad("PROGRAM:"), + var out = "", statements = program.statements, inverse = program.inverse, i, l; - this.padding++; - for(i=0, l=statements.length; i<l; i++) { out = out + this.accept(statements[i]); } this.padding--; - if(inverse) { - out = out + this.pad("{{^}}"); - - this.padding++; - - for(i=0, l=inverse.statements.length; i<l; i++) { - out = out + this.accept(inverse.statements[i]); - } - } - - this.padding--; - return out; }; @@ -52,25 +38,25 @@ Handlebars.PrintVisitor.prototype.block = function(block) { out = out + this.pad("BLOCK:"); this.padding++; out = out + this.accept(block.mustache); - out = out + this.accept(block.program); - this.padding--; - - return out; -}; - -Handlebars.PrintVisitor.prototype.inverse = function(block) { - var out = ""; - - out = out + this.pad("INVERSE:"); - this.padding++; - out = out + this.accept(block.mustache); - out = out + this.accept(block.program); + if (block.program) { + out = out + this.pad("PROGRAM:"); + this.padding++; + out = out + this.accept(block.program); + this.padding--; + } + if (block.inverse) { + if (block.program) { this.padding++; } + out = out + this.pad("{{^}}"); + this.padding++; + out = out + this.accept(block.inverse); + this.padding--; + if (block.program) { this.padding--; } + } this.padding--; return out; }; - Handlebars.PrintVisitor.prototype.mustache = function(mustache) { var params = mustache.params, paramStrings = [], hash; diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index e23361e..8dd13a4 100644 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -7,11 +7,9 @@ describe "Parser" do @compiles = true end - def program(&block) + def root(&block) ASTBuilder.build do - program do - instance_eval(&block) - end + instance_eval(&block) end end @@ -118,109 +116,109 @@ describe "Parser" do end it "parses simple mustaches" do - ast_for("{{foo}}").should == program { mustache id("foo") } + ast_for("{{foo}}").should == root { mustache id("foo") } end it "parses mustaches with paths" do - ast_for("{{foo/bar}}").should == program { mustache path("foo", "bar") } + ast_for("{{foo/bar}}").should == root { mustache path("foo", "bar") } end it "parses mustaches with this/foo" do - ast_for("{{this/foo}}").should == program { mustache id("foo") } + ast_for("{{this/foo}}").should == root { mustache id("foo") } end it "parses mustaches with - in a path" do - ast_for("{{foo-bar}}").should == program { mustache id("foo-bar") } + ast_for("{{foo-bar}}").should == root { mustache id("foo-bar") } end it "parses mustaches with parameters" do - ast_for("{{foo bar}}").should == program { mustache id("foo"), [id("bar")] } + ast_for("{{foo bar}}").should == root { mustache id("foo"), [id("bar")] } end it "parses mustaches with hash arguments" do - ast_for("{{foo bar=baz}}").should == program do + ast_for("{{foo bar=baz}}").should == root do mustache id("foo"), [], hash(["bar", id("baz")]) end - ast_for("{{foo bar=1}}").should == program do + ast_for("{{foo bar=1}}").should == root do mustache id("foo"), [], hash(["bar", integer("1")]) end - ast_for("{{foo bar=true}}").should == program do + ast_for("{{foo bar=true}}").should == root do mustache id("foo"), [], hash(["bar", boolean("true")]) end - ast_for("{{foo bar=false}}").should == program do + ast_for("{{foo bar=false}}").should == root do mustache id("foo"), [], hash(["bar", boolean("false")]) end - ast_for("{{foo bar=baz bat=bam}}").should == program do + ast_for("{{foo bar=baz bat=bam}}").should == root do mustache id("foo"), [], hash(["bar", "ID:baz"], ["bat", "ID:bam"]) end - ast_for("{{foo bar=baz bat=\"bam\"}}").should == program do + ast_for("{{foo bar=baz bat=\"bam\"}}").should == root do mustache id("foo"), [], hash(["bar", "ID:baz"], ["bat", "\"bam\""]) end - ast_for("{{foo omg bar=baz bat=\"bam\"}}").should == program do + ast_for("{{foo omg bar=baz bat=\"bam\"}}").should == root do mustache id("foo"), [id("omg")], hash(["bar", id("baz")], ["bat", string("bam")]) end - ast_for("{{foo omg bar=baz bat=\"bam\" baz=1}}").should == program do + ast_for("{{foo omg bar=baz bat=\"bam\" baz=1}}").should == root do mustache id("foo"), [id("omg")], hash(["bar", id("baz")], ["bat", string("bam")], ["baz", integer("1")]) end - ast_for("{{foo omg bar=baz bat=\"bam\" baz=true}}").should == program do + ast_for("{{foo omg bar=baz bat=\"bam\" baz=true}}").should == root do mustache id("foo"), [id("omg")], hash(["bar", id("baz")], ["bat", string("bam")], ["baz", boolean("true")]) end - ast_for("{{foo omg bar=baz bat=\"bam\" baz=false}}").should == program do + ast_for("{{foo omg bar=baz bat=\"bam\" baz=false}}").should == root do mustache id("foo"), [id("omg")], hash(["bar", id("baz")], ["bat", string("bam")], ["baz", boolean("false")]) end end it "parses mustaches with string parameters" do - ast_for("{{foo bar \"baz\" }}").should == program { mustache id("foo"), [id("bar"), string("baz")] } + ast_for("{{foo bar \"baz\" }}").should == root { mustache id("foo"), [id("bar"), string("baz")] } end it "parses mustaches with INTEGER parameters" do - ast_for("{{foo 1}}").should == program { mustache id("foo"), [integer("1")] } + ast_for("{{foo 1}}").should == root { mustache id("foo"), [integer("1")] } end it "parses mustaches with BOOLEAN parameters" do - ast_for("{{foo true}}").should == program { mustache id("foo"), [boolean("true")] } - ast_for("{{foo false}}").should == program { mustache id("foo"), [boolean("false")] } + ast_for("{{foo true}}").should == root { mustache id("foo"), [boolean("true")] } + ast_for("{{foo false}}").should == root { mustache id("foo"), [boolean("false")] } end it "parses contents followed by a mustache" do - ast_for("foo bar {{baz}}").should == program do + ast_for("foo bar {{baz}}").should == root do content "foo bar " mustache id("baz") end end it "parses a partial" do - ast_for("{{> foo }}").should == program { partial id("foo") } + ast_for("{{> foo }}").should == root { partial id("foo") } end it "parses a partial with context" do - ast_for("{{> foo bar}}").should == program { partial id("foo"), id("bar") } + ast_for("{{> foo bar}}").should == root { partial id("foo"), id("bar") } end it "parses a comment" do - ast_for("{{! this is a comment }}").should == program do + ast_for("{{! this is a comment }}").should == root do comment " this is a comment " end end it "parses a multi-line comment" do - ast_for("{{!\nthis is a multi-line comment\n}}").should == program do + ast_for("{{!\nthis is a multi-line comment\n}}").should == root do multiline_comment "this is a multi-line comment" end end it "parses an inverse section" do - ast_for("{{#foo}} bar {{^}} baz {{/foo}}").should == program do + ast_for("{{#foo}} bar {{^}} baz {{/foo}}").should == root do block do mustache id("foo") @@ -236,11 +234,11 @@ describe "Parser" do end it "parses a standalone inverse section" do - ast_for("{{^foo}}bar{{/foo}}").should == program do - inverted_block do + ast_for("{{^foo}}bar{{/foo}}").should == root do + block do mustache id("foo") - program do + inverse do content "bar" end end |