Initial commit. Note that I'm using CommonJS modules and node purely to help me develop this. If this ends up being useful, I will likely distribute the entire package as a single JS file for easier consumption in the browser.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
if(require) {
|
||||
var Lexer = require("handlebars/jison_ext").Lexer
|
||||
}
|
||||
|
||||
var HandlebarsLexer = function() {
|
||||
this.state = "CONTENT";
|
||||
};
|
||||
HandlebarsLexer.prototype = new Lexer;
|
||||
|
||||
HandlebarsLexer.prototype.lex = function() {
|
||||
if(this.input === "") return;
|
||||
|
||||
this.setupLex();
|
||||
|
||||
var lookahead = this.peek(2);
|
||||
var result = '';
|
||||
|
||||
if(this.state == "MUSTACHE") {
|
||||
// chomp optional whitespace
|
||||
while(this.peek() === " ") { this.readchar(); }
|
||||
|
||||
if(this.peek(2) === "}}") {
|
||||
this.state = "CONTENT"
|
||||
this.getchar(2);
|
||||
|
||||
if(this.peek() == "}") this.getchar();
|
||||
return "CLOSE";
|
||||
} else if(this.peek() === '"') {
|
||||
this.getchar();
|
||||
while(this.peek() !== '"') { this.getchar() }
|
||||
this.getchar();
|
||||
return "STRING";
|
||||
} else {
|
||||
while(this.peek().match(/[A-Za-z]/)) { this.getchar() }
|
||||
return "ID"
|
||||
}
|
||||
} else if(lookahead == "{{") {
|
||||
this.state = "MUSTACHE";
|
||||
this.getchar(2);
|
||||
|
||||
var peek = this.peek();
|
||||
|
||||
if(peek === ">") {
|
||||
this.getchar();
|
||||
return "OPEN_PARTIAL";
|
||||
} else if(peek === "#") {
|
||||
this.getchar();
|
||||
return "OPEN_BLOCK";
|
||||
} else if(peek === "/") {
|
||||
this.getchar();
|
||||
return "OPEN_ENDBLOCK";
|
||||
} else if(peek === "^") {
|
||||
this.getchar();
|
||||
return "OPEN_INVERSE"
|
||||
} else if(peek === "!") {
|
||||
this.getchar();
|
||||
this.setupLex();
|
||||
while(this.peek(2) !== "}}") { this.getchar(); };
|
||||
this.readchar(2);
|
||||
this.state = "CONTENT"
|
||||
return "COMMENT";
|
||||
} else {
|
||||
return "OPEN";
|
||||
}
|
||||
} else {
|
||||
while(this.peek(2) !== "{{" && this.peek(2) !== "") { result = result + this.getchar(); }
|
||||
return "CONTENT"
|
||||
}
|
||||
};
|
||||
|
||||
if(exports) { exports.Lexer = HandlebarsLexer; }
|
||||
Reference in New Issue
Block a user