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 LEXEMCHAR [a-zA-Z0-9_]
64 yylval.str = strlower(mcnstrdup(lexContext, yytext, yyleng));
65 return getIdent(yylval.str);
68 <xVAR>\|\| { return OR_P; }
71 yylval.punct = *yytext;
82 strbuf = mcalloc(lexContext, total);
88 <xQUOTED>\\(.|\n) { addchar(yytext[1]); }
97 <xQUOTED>[^\"\\]+ { addstring(yytext, yyleng); }
100 /* This is only needed for \ just before EOF */
104 <xEXPR>{LEXEMCHAR}+ {
105 yylval.str = strlower(mcnstrdup(lexContext, yytext, yyleng));
106 return getIdent(yylval.str);
115 yylval.str = mcnstrdup(lexContext, yytext, yyleng);
121 return INCLUDE_CLOSE;
128 <xVAR,xINCLUDE,xEXPR,xCOMMENT>\n+ ;
130 <xVAR,xINCLUDE,xEXPR,xCOMMENT>[\r\t ]+ ;
134 <xVAR,xINCLUDE,xEXPR>. {
135 return yyerror("Syntax error in template tag", yylineno);
138 <xVAR,xQUOTED,xINCLUDE,xEXPR,xCOMMENT><<EOF>> {
139 return yyerror("unterminated template tag", yylineno);
143 yylval.str = mcnstrdup(lexContext, yytext, yyleng);
147 <INITIAL>([^\<]*\<[^\&\#\@\%][^\<]*)+ {
148 yylval.str = mcnstrdup(lexContext, yytext, yyleng);
154 /* This is only needed for < just before EOF */
155 yylval.str = mcnstrdup(lexContext, yytext, yyleng);
165 typedef struct KeyWord {
171 static KeyWord keywords[] = {
172 { "H", 0, HTMLESCAPE },
173 { "U", 0, URLESCAPE },
176 { "ELSE", 0, ELSE_P },
177 { "LOOP", 0, LOOP_P },
178 { "ENDIF", 0, ENDIF_P },
179 { "DEFINED", 0, DEFINED_P },
180 { "ENDLOOP", 0, ENDLOOP_P }
184 startTemplateLex(Template tmpl, FILE *in) {
185 if ( keywords->len == 0 ) {
187 for(i=0; i<sizeof(keywords)/sizeof(KeyWord);i++)
188 keywords[i].len = strlen(keywords[i].word);
191 lexContext = tmpl->templateContext;
198 getIdent(char *word) {
199 int len = strlen(word);
202 if ( len > keywords[ sizeof(keywords)/sizeof(KeyWord)-1 ].len )
205 for(i=0; i<sizeof(keywords)/sizeof(KeyWord) && len >= keywords[i].len; i++)
206 if ( len == keywords[i].len && strcasecmp(word, keywords[i].word) == 0 )
207 return keywords[i].id;
213 yyerror(char *s, int line) {
215 tlog(TL_CRIT,"template error at line %d in '%s': %s", line, fileToParse, s);
221 addstring(char *s, int l) {
222 while( length + l + 1 >= total ) {
224 strbuf=mcrealloc(strbuf, total);
227 memcpy( strbuf+length, s, l);
229 strbuf[length] = '\0';
234 if( length + 2 >= total ) {
236 strbuf=mcrealloc(strbuf, total);
239 strbuf[ length++ ] = s;
240 strbuf[length] = '\0';