%x mu c i h esc run %% [^#]*?/"$" { var _reg = /\\+$/; var _esc = yytext.match(_reg); var _num = _esc ? _esc[0].length: null; /*转义实现,非常恶心,暂时没有好的解决方案*/ if (!_num || !(_num % 2)) { this.begin("mu"); } else { yytext = yytext.replace(/\\$/, ''); this.begin('esc'); } if (_num > 1) yytext = yytext.replace(/(\\\\)+$/, '\\'); if(yytext) return 'CONTENT'; } [^\$]*?/"#" { var _reg = /\\+$/; var _esc = yytext.match(_reg); var _num = _esc ? _esc[0].length: null; if (!_num || !(_num % 2)) { this.begin("h"); } else { yytext = yytext.replace(/\\$/, ''); this.begin('esc'); } if (_num > 1) yytext = yytext.replace(/(\\\\)+$/, '\\'); if(yytext) return 'CONTENT'; } [^\x00]+ { return 'CONTENT'; } "#"\*[\s\S]+?\*"#" { this.popState(); return 'COMMENT'; } "#"\[\[[\s\S]+?\]\]"#" { this.popState(); yytext = yytext.replace(/^#\[\[|\]\]#$/g, ''); return 'RAW'} "##"[^\n]* { this.popState(); return 'COMMENT'; } "#@" { return 'MACRO_BODY'; } "#"/[a-zA-Z{] { return 'HASH'; } "set"[ ]*/[^a-zA-Z0-9_]+ { return 'SET'; } "if"[ ]*/[^a-zA-Z0-9_]+ { return 'IF'; } "elseif"[ ]*/[^a-zA-Z0-9_]+ { return 'ELSEIF'; } "else" { this.popState(); return 'ELSE'; } "{else}" { this.popState(); return 'ELSE'; } "end" { this.popState(); return 'END'; } "{end}" { this.popState(); return 'END'; } "break" { this.popState(); return 'BREAK'; } "return"[ ]*/[^a-zA-Z0-9_]+ { return 'RETURN'; } "foreach"[ ]*/[^a-zA-Z0-9_]+ { return 'FOREACH'; } "noescape"/[^a-zA-Z0-9_]+ { return 'NOESCAPE'; } "define"[ ]*/[^a-zA-Z0-9_]+ { return 'DEFINE'; } "macro"[ ]*/[^a-zA-Z0-9_]+ { return 'MACRO'; } "in" { return 'IN'; } [%\+\-\*/] { return yytext; } "<=" { return yytext; } ">=" { return yytext; } [><] { return yytext; } "==" { return yytext; } ">" { return yytext; } "<" { return yytext; } "||" { return yytext; } "or" { return '||'; } "&&" { return yytext; } "and" { return '&&'; } "!=" { return yytext; } "not" { return '!'; } "$!"/[{a-zA-Z_] { return 'DOLLAR'; } "$"/[{a-zA-Z_] { return 'DOLLAR'; } "!" { return yytext; } "=" { return 'EQUAL'; } [ ]+/[^,] { var len = this.stateStackSize(); if (len >= 2 && this.topState() === 'c' && this.topState(1) === 'run') { return 'SPACE'; } } \s+ { /*ignore whitespace*/ } "{" { return 'MAP_BEGIN'; } "}" { return 'MAP_END'; } ":"[\s]* { return 'MAP_SPLIT'; } "{" { yy.begin = true; return 'VAR_BEGIN'; } "}" { this.popState(); if (yy.begin === true) { yy.begin = false; return 'VAR_END';} else { return 'CONTENT'; } } "("[\s]*/[$'"\[\{\-0-9\w()!] { this.begin("c"); return 'PARENTHESIS'; } ")" { if (this.popState() === "c") { var len = this.stateStackSize(); if (this.topState() === 'run') { this.popState(); len = len - 1; } var tailStack = this.topState(len - 2); /** 遇到#set(a = b)括号结束后结束状态h*/ if (len === 2 && tailStack === "h"){ this.popState(); } else if (len === 3 && tailStack === "mu" && this.topState(len - 3) === "h") { // issue#7 $foo#if($a)...#end this.popState(); this.popState(); } return 'CLOSE_PARENTHESIS'; } else { return 'CONTENT'; } } "["[\s]*/[\-$"'0-9{\[\]]+ { this.begin("i"); return 'BRACKET'; } "]" { if (this.popState() === "i") { return 'CLOSE_BRACKET'; } else { return 'CONTENT'; } } ".." { return 'RANGE'; } "."/[a-zA-Z_] { return 'DOT'; } "."/[\d] { return 'DECIMAL_POINT'; } ","[ ]* { return 'COMMA'; } '"'(\\\"|[^\"])*'"' { yytext = yytext.substr(1, yyleng-2).replace(/\\"/g,'"'); return 'EVAL_STRING'; } "'"(\\\'|[^\'])*"'" { yytext = yytext.substr(1, yyleng-2).replace(/\\'/g,"'"); return 'STRING'; } "null" { return 'BOOL'; } "false" { return 'BOOL'; } "true" { return 'BOOL'; } [0-9]+ { return 'INTEGER'; } [_a-zA-Z][a-zA-Z0-9_-]* { /* return ID */ return 'ID'; } [_a-zA-Z][a-zA-Z0-9_\-]*[ ]*/"(" { this.begin("run"); return 'ID'; } "\\#" { this.popState(); yytext = '#'; return 'CONTENT'; } "#" { this.begin('h'); return 'HASH'; } . { this.popState(); return 'CONTENT'; } \s+ { this.popState(); return 'CONTENT'; } [\$#] { this.popState(); return 'CONTENT'; } <> { this.popState(); return 'EOF'; } <> { return 'EOF'; }