summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2013-07-29 21:37:04 -0500
committerkpdecker <kpdecker@gmail.com>2013-07-29 21:37:25 -0500
commit03310df95e91880ea647f38805dc9694a3258a65 (patch)
tree2ec52d349b0f61c5c53c4e62d91be8ad88318228 /src
parent3bc7d7b9989a504148f875f4ef6bd786066f4299 (diff)
downloadhandlebars.js-03310df95e91880ea647f38805dc9694a3258a65.zip
handlebars.js-03310df95e91880ea647f38805dc9694a3258a65.tar.gz
handlebars.js-03310df95e91880ea647f38805dc9694a3258a65.tar.bz2
Add strip lex helper method
Diffstat (limited to 'src')
-rw-r--r--src/handlebars.l24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/handlebars.l b/src/handlebars.l
index d13e111..018096b 100644
--- a/src/handlebars.l
+++ b/src/handlebars.l
@@ -1,6 +1,15 @@
%x mu emu com
+%{
+
+function strip(start, end) {
+ return yytext = yytext.substr(start, yyleng-end);
+}
+
+%}
+
+
/*
ID is the inverse of control characters.
Control characters ranges:
@@ -17,7 +26,7 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]
"\\\\"/("{{") yytext = "\\"; return 'CONTENT';
[^\x00]*?/("{{") {
if(yytext.slice(-1) !== "\\") this.begin("mu");
- if(yytext.slice(-1) === "\\") yytext = yytext.substr(0,yyleng-1), this.begin("emu");
+ if(yytext.slice(-1) === "\\") strip(0,1), this.begin("emu");
if(yytext) return 'CONTENT';
}
@@ -25,11 +34,11 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]
<emu>[^\x00]{2,}?/("{{"|<<EOF>>) {
if(yytext.slice(-1) !== "\\") this.popState();
- if(yytext.slice(-1) === "\\") yytext = yytext.substr(0,yyleng-1);
+ if(yytext.slice(-1) === "\\") strip(0,1);
return 'CONTENT';
}
-<com>[\s\S]*?"--}}" yytext = yytext.substr(0, yyleng-4); this.popState(); return 'COMMENT';
+<com>[\s\S]*?"--}}" strip(0,4); this.popState(); return 'COMMENT';
<mu>"{{>" return 'OPEN_PARTIAL';
<mu>"{{#" return 'OPEN_BLOCK';
@@ -39,7 +48,7 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]
<mu>"{{{" return 'OPEN_UNESCAPED';
<mu>"{{&" return 'OPEN';
<mu>"{{!--" this.popState(); this.begin('com');
-<mu>"{{!"[\s\S]*?"}}" yytext = yytext.substr(3,yyleng-5); this.popState(); return 'COMMENT';
+<mu>"{{!"[\s\S]*?"}}" strip(3,5); this.popState(); return 'COMMENT';
<mu>"{{" return 'OPEN';
<mu>"=" return 'EQUALS';
@@ -49,8 +58,8 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]
<mu>\s+ /*ignore whitespace*/
<mu>"}}}" this.popState(); return 'CLOSE_UNESCAPED';
<mu>"}}" this.popState(); return 'CLOSE';
-<mu>'"'("\\"["]|[^"])*'"' yytext = yytext.substr(1,yyleng-2).replace(/\\"/g,'"'); return 'STRING';
-<mu>"'"("\\"[']|[^'])*"'" yytext = yytext.substr(1,yyleng-2).replace(/\\'/g,"'"); return 'STRING';
+<mu>'"'("\\"["]|[^"])*'"' yytext = strip(1,2).replace(/\\"/g,'"'); return 'STRING';
+<mu>"'"("\\"[']|[^'])*"'" yytext = strip(1,2).replace(/\\'/g,"'"); return 'STRING';
<mu>"@" return 'DATA';
<mu>"true"/[}\s] return 'BOOLEAN';
<mu>"false"/[}\s] return 'BOOLEAN';
@@ -58,8 +67,7 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]
<mu>{ID} return 'ID';
-<mu>'['[^\]]*']' yytext = yytext.substr(1, yyleng-2); return 'ID';
+<mu>'['[^\]]*']' yytext = strip(1,2); return 'ID';
<mu>. return 'INVALID';
<INITIAL,mu><<EOF>> return 'EOF';
-