summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/handlebars_lexer.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/handlebars/handlebars_lexer.js')
-rw-r--r--lib/handlebars/handlebars_lexer.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/handlebars/handlebars_lexer.js b/lib/handlebars/handlebars_lexer.js
new file mode 100644
index 0000000..f92fc17
--- /dev/null
+++ b/lib/handlebars/handlebars_lexer.js
@@ -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; }