Make the ID node capable of handling paths
TODO: handle invalid paths in the tokenizer or parser
TODO: invalidate "{{ foo/ bar }}"
This commit is contained in:
@@ -33,6 +33,11 @@ Handlebars.HandlebarsLexer.prototype.lex = function() {
|
|||||||
if(lookahead === "") return;
|
if(lookahead === "") return;
|
||||||
|
|
||||||
if(this.state == "MUSTACHE") {
|
if(this.state == "MUSTACHE") {
|
||||||
|
if(this.peek() === "/") {
|
||||||
|
this.getchar();
|
||||||
|
return "SEP";
|
||||||
|
}
|
||||||
|
|
||||||
// chomp optional whitespace
|
// chomp optional whitespace
|
||||||
while(this.peek() === " ") { this.ignorechar(); }
|
while(this.peek() === " ") { this.ignorechar(); }
|
||||||
|
|
||||||
@@ -64,7 +69,7 @@ Handlebars.HandlebarsLexer.prototype.lex = function() {
|
|||||||
// All other cases are IDs or errors
|
// All other cases are IDs or errors
|
||||||
} else {
|
} else {
|
||||||
// grab alphanumeric characters
|
// grab alphanumeric characters
|
||||||
while(this.peek().match(/[0-9A-Za-z]/)) { this.getchar() }
|
while(this.peek().match(/[0-9A-Za-z\.]/)) { this.getchar() }
|
||||||
|
|
||||||
// if any characters were grabbed => ID
|
// if any characters were grabbed => ID
|
||||||
if(this.yytext.length) { return "ID" }
|
if(this.yytext.length) { return "ID" }
|
||||||
|
|||||||
@@ -79,7 +79,12 @@ Handlebars.PrintVisitor.prototype.STRING = function(string) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Handlebars.PrintVisitor.prototype.ID = function(id) {
|
Handlebars.PrintVisitor.prototype.ID = function(id) {
|
||||||
return "ID:" + id.id;
|
var path = id.id.join("/");
|
||||||
|
if(id.id.length > 1) {
|
||||||
|
return "PATH:" + path;
|
||||||
|
} else {
|
||||||
|
return "ID:" + path;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Handlebars.PrintVisitor.prototype.content = function(content) {
|
Handlebars.PrintVisitor.prototype.content = function(content) {
|
||||||
|
|||||||
@@ -36,6 +36,12 @@ describe "Tokenizer" do
|
|||||||
result[1].should be_token("ID", "foo")
|
result[1].should be_token("ID", "foo")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "tokenizes a path as 'OPEN ID CLOSE'" do
|
||||||
|
result = tokenize("{{../foo/bar}}")
|
||||||
|
result.should match_tokens(%w(OPEN ID SEP ID SEP ID CLOSE))
|
||||||
|
result[1].should be_token("ID", "..")
|
||||||
|
end
|
||||||
|
|
||||||
it "tokenizes a simple mustahe with spaces as 'OPEN ID CLOSE'" do
|
it "tokenizes a simple mustahe with spaces as 'OPEN ID CLOSE'" do
|
||||||
result = tokenize("{{ foo }}")
|
result = tokenize("{{ foo }}")
|
||||||
result.should match_tokens(%w(OPEN ID CLOSE))
|
result.should match_tokens(%w(OPEN ID CLOSE))
|
||||||
|
|||||||
+12
-7
@@ -29,7 +29,7 @@ openBlock
|
|||||||
;
|
;
|
||||||
|
|
||||||
closeBlock
|
closeBlock
|
||||||
: OPEN_ENDBLOCK id CLOSE { }
|
: OPEN_ENDBLOCK path CLOSE { }
|
||||||
;
|
;
|
||||||
|
|
||||||
mustache
|
mustache
|
||||||
@@ -37,7 +37,7 @@ mustache
|
|||||||
;
|
;
|
||||||
|
|
||||||
partial
|
partial
|
||||||
: OPEN_PARTIAL id CLOSE { $$ = new yy.PartialNode($2) }
|
: OPEN_PARTIAL path CLOSE { $$ = new yy.PartialNode($2) }
|
||||||
;
|
;
|
||||||
|
|
||||||
simpleInverse
|
simpleInverse
|
||||||
@@ -45,8 +45,8 @@ simpleInverse
|
|||||||
;
|
;
|
||||||
|
|
||||||
inMustache
|
inMustache
|
||||||
: id params { $$ = [$1].concat($2) }
|
: path params { $$ = [$1].concat($2) }
|
||||||
| id { $$ = [$1] }
|
| path { $$ = [$1] }
|
||||||
;
|
;
|
||||||
|
|
||||||
params
|
params
|
||||||
@@ -55,11 +55,16 @@ params
|
|||||||
;
|
;
|
||||||
|
|
||||||
param
|
param
|
||||||
: id { $$ = $1 }
|
: path { $$ = $1 }
|
||||||
| STRING { $$ = new yy.StringNode($1) }
|
| STRING { $$ = new yy.StringNode($1) }
|
||||||
;
|
;
|
||||||
|
|
||||||
id
|
path
|
||||||
: ID { $$ = new yy.IdNode($1) }
|
: pathSegments { $$ = new yy.IdNode($1) }
|
||||||
|
;
|
||||||
|
|
||||||
|
pathSegments
|
||||||
|
: pathSegments SEP ID { $1.push($3); $$ = $1; }
|
||||||
|
| ID { $$ = [$1] }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ require.paths.push(__dirname + "/lib");
|
|||||||
|
|
||||||
var Handlebars = require("handlebars").Handlebars;
|
var Handlebars = require("handlebars").Handlebars;
|
||||||
|
|
||||||
var string = "foo {{ bar baz \"baz\" }}baz{{! foo bar baz }}{{#foo}} bar {{^}} baz {{/foo}}{{> partial }}{{# bar }}part1 {{^}} part2{{> foo }}{{/bar}}zomg"
|
var string = "foo {{ bar ../baz/bat/bam \"baz\" }}baz{{! foo bar baz }}{{#foo}} bar {{^}} baz {{/foo}}{{> partial }}{{# bar }}part1 {{^}} part2{{> foo }}{{/bar}}zomg"
|
||||||
|
|
||||||
var ast = Handlebars.parse(string);
|
var ast = Handlebars.parse(string);
|
||||||
require("sys").print(Handlebars.print(ast));
|
require("sys").print(Handlebars.print(ast));
|
||||||
|
|||||||
Reference in New Issue
Block a user