8 #define yylval tmpl_yylval
11 extern char* fileToParse;
13 static int yyerror(char *s, int line);
14 static int getIdent(char *word);
16 static char *strbuf = NULL;
19 static void addstring(char *s, int l);
20 static void addchar(char s);
21 static MemoryContext *lexContext;
26 %option never-interactive
30 %option prefix="tmpl_yy"
39 LEXEMCHARSTART [a-zA-Z_]
40 LEXEMCHAR [a-zA-Z0-9_]
66 yylval.punct = *yytext;
77 strbuf = mcalloc(lexContext, total);
83 <xQUOTED>\\(.|\n) { addchar(yytext[1]); }
92 <xQUOTED>[^\"\\]+ { addstring(yytext, yyleng); }
95 /* This is only needed for \ just before EOF */
99 <xEXPR,xVAR>{DIGIT}+ {
100 yylval.intval = atoi(yytext);
104 <xEXPR,xVAR>{LEXEMCHARSTART}{LEXEMCHAR}* {
105 yylval.str = strlower(mcnstrdup(lexContext, yytext, yyleng));
106 return getIdent(yylval.str);
109 <xEXPR,xVAR>\|\| { return OR_P; }
111 <xEXPR,xVAR>\&\& { return AND_P; }
113 <xEXPR,xVAR>\! { return NOT_P; }
115 <xEXPR,xVAR>[\^\%\+\*\/\-\(\)\?\:] {
116 yylval.punct = *yytext;
120 <xEXPR,xVAR>(\<|\<=|\>=|\>|==|\!=|\<\>) {
121 yylval.str = mcnstrdup(lexContext, yytext, yyleng);
125 <xEXPR,xVAR>{DIGIT}+"."{DIGIT}+ {
126 yylval.floatval = atof(yytext);
130 <xEXPR,xVAR>{DIGIT}+[eE]["+""-"]?{DIGIT}+ {
131 yylval.floatval = atof(yytext);
135 <xEXPR,xVAR>{DIGIT}+"."{DIGIT}+[eE]["+""-"]?{DIGIT}+ {
136 yylval.floatval = atof(yytext);
146 yylval.str = mcnstrdup(lexContext, yytext, yyleng);
152 return INCLUDE_CLOSE;
159 <xVAR,xINCLUDE,xEXPR,xCOMMENT>\n+ ;
161 <xVAR,xINCLUDE,xEXPR,xCOMMENT>[\r\t ]+ ;
165 <xVAR,xINCLUDE,xEXPR>. {
166 return yyerror("Syntax error in template tag", yylineno);
169 <xVAR,xQUOTED,xINCLUDE,xEXPR,xCOMMENT><<EOF>> {
170 return yyerror("unterminated template tag", yylineno);
174 yylval.str = mcnstrdup(lexContext, yytext, yyleng);
178 <INITIAL>([^\<]*\<[^\&\#\@\%][^\<]*)+ {
179 yylval.str = mcnstrdup(lexContext, yytext, yyleng);
185 /* This is only needed for < just before EOF */
186 yylval.str = mcnstrdup(lexContext, yytext, yyleng);
196 typedef struct KeyWord {
202 static KeyWord keywords[] = {
203 { "H", 0, HTMLESCAPE },
204 { "U", 0, URLESCAPE },
206 { "ELSE", 0, ELSE_P },
207 { "LOOP", 0, LOOP_P },
208 { "ENDIF", 0, ENDIF_P },
209 { "ENDLOOP", 0, ENDLOOP_P }
213 startTemplateLex(Template tmpl, FILE *in) {
214 if ( keywords->len == 0 ) {
216 for(i=0; i<sizeof(keywords)/sizeof(KeyWord);i++)
217 keywords[i].len = strlen(keywords[i].word);
220 lexContext = tmpl->templateContext;
227 getIdent(char *word) {
228 int len = strlen(word);
231 if ( len > keywords[ sizeof(keywords)/sizeof(KeyWord)-1 ].len )
234 for(i=0; i<sizeof(keywords)/sizeof(KeyWord) && len >= keywords[i].len; i++)
235 if ( len == keywords[i].len && strcasecmp(word, keywords[i].word) == 0 )
236 return keywords[i].id;
242 yyerror(char *s, int line) {
244 tlog(TL_CRIT,"template error at line %d in '%s': %s", line, fileToParse, s);
250 addstring(char *s, int l) {
251 while( length + l + 1 >= total ) {
253 strbuf=mcrealloc(strbuf, total);
256 memcpy( strbuf+length, s, l);
258 strbuf[length] = '\0';
263 if( length + 2 >= total ) {
265 strbuf=mcrealloc(strbuf, total);
268 strbuf[ length++ ] = s;
269 strbuf[length] = '\0';