int main(int argc, char **argv)
{
yyin = stdin;
+
+ if (argc == 2) {
+ yyin = fopen(argv[1], "r");
+ if (yyin == NULL)
+ fail("Unable to open file : %s");
+ }
+
init_parser();
yyparse();
%}
-%s verbatim
%%
"(" { return LPAREN; }
")" { return RPAREN; }
-"{{" { BEGIN(verbatim); return LBRACKET; }
-"}}" { BEGIN(INITIAL); return RBRACKET; }
+"{{" { return LBRACKET; }
+"}}" { return RBRACKET; }
"," { return COMMA; }
+"'" { return QUOTE; }
"foreach" { return FOREACH; }
"$$" { return DOLDOL; }
+[ \t\n]+ { return SPACES; }
+[a-zA-Z0-9,?!%*$^]+ { return WORD; }
+
-[a-zA-Z0-9]+ { return WORD; }
-<verbatim>[ \t\n]+ { return SPACES; }
-[ \t\n]+ /* otherwise eat up spaces */ { }
return block;
}
+
+void free_block(struct croma_block *b)
+{
+ if (b == NULL)
+ return;
+
+ free(b->contents);
+ free(b->name);
+ TAILQ_REMOVE(&blocks_head, b, blocks);
+}
extern char *strndup (__const char *__string, size_t __n);
extern char *yytext;
+extern int yylineno;
+extern FILE *yyin;
char *token_list[] = { "left parenthesis",
"right parenthesis",
"$$ sign",
"spaces",
"comma",
+ "quote",
"foreach", NULL};
void expect(int t)
{
int c = yylex();
+
+ /* Eat up spaces during parsing. */
+ while(c == SPACES)
+ {
+ c = yylex();
+ }
+
if (c != t) {
- fail("Expected token %s in input, got %s (%s).\n",
- token_list[t], token_list[c], yytext);
- fail("unexpected token : %s \n", yytext);
+ printf("c %d, t %d, yytext %s\n", c, t, yytext);
+ fail("Expected token %s in input, got %s (at line %d).\n",
+ token_list+t, token_list+c, yylineno);
}
}
void yyparse(void)
{
- int t = yylex();
-
- switch(t) {
-
- case FOREACH:
- foreach_expr();
- break;
+ while(!feof(yyin)) {
+ int t = yylex();
+ switch(t) {
+
+ case FOREACH:
+ foreach_expr();
+ break;
- default:
- fail("garbage somewhere in input.\n");
+ default:
+ printf("%s", yytext);
+ }
}
}
void foreach_expr(void)
{
struct croma_block *b = alloc_and_insert_block();
+ b->name = strdup("foreach block"); /* FIXME : just to keep free_block() happy */
expect(LPAREN);
p++;
}
+
+ free_block(b);
}
fail("Unable to alloc memory for text %s\n", yytext);
int c = yylex();
- if (c == RPAREN)
+ while (c == SPACES)
+ c = yylex();
+
+ switch(c) {
+ case WORD:
+ continue;
+ break;
+
+ case RPAREN:
return;
- else if (c != COMMA) {
- puts("Expected a comma in parameter list");
- exit(-1);
- }
+ break;
- expect(WORD);
+ default:
+ fail("Unexpected token in argument list");
+ break;
+ }
-
}
// *p++ = "\0";
#define __TOKENS_H__
enum {
- LPAREN = 0, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, COMMA,
+ LPAREN = 0, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, COMMA, QUOTE,
FOREACH,
};