From 98567779006f83316f4f1871875d91194d46d24e Mon Sep 17 00:00:00 2001 From: khamidou Date: Mon, 2 Mar 2009 11:18:29 +0100 Subject: [PATCH] implemented foreach (at last) --- croma.c | 7 +++++++ lexer.l | 12 ++++++------ macro.c | 10 ++++++++++ parse.c | 59 ++++++++++++++++++++++++++++++++++++++------------------ tokens.h | 2 +- 5 files changed, 64 insertions(+), 26 deletions(-) diff --git a/croma.c b/croma.c index 1305f45..48238fd 100644 --- a/croma.c +++ b/croma.c @@ -11,6 +11,13 @@ extern FILE *yyin; 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(); diff --git a/lexer.l b/lexer.l index e637c5d..164aaf2 100644 --- a/lexer.l +++ b/lexer.l @@ -14,18 +14,18 @@ int yywrap() %} -%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; } -[ \t\n]+ { return SPACES; } -[ \t\n]+ /* otherwise eat up spaces */ { } diff --git a/macro.c b/macro.c index d73d34d..8a37531 100644 --- a/macro.c +++ b/macro.c @@ -15,3 +15,13 @@ struct croma_block* alloc_and_insert_block(void) 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); +} diff --git a/parse.c b/parse.c index 93b2d5c..ab273a4 100644 --- a/parse.c +++ b/parse.c @@ -3,6 +3,8 @@ 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", @@ -12,16 +14,24 @@ char *token_list[] = { "left 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); } } @@ -34,16 +44,17 @@ void init_parser(void) 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); + } } } @@ -51,6 +62,7 @@ void yyparse(void) 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); @@ -82,6 +94,8 @@ void foreach_expr(void) p++; } + + free_block(b); } @@ -111,16 +125,23 @@ void parse_arglist(struct croma_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"; diff --git a/tokens.h b/tokens.h index dc87c6d..ac89f9d 100644 --- a/tokens.h +++ b/tokens.h @@ -2,7 +2,7 @@ #define __TOKENS_H__ enum { - LPAREN = 0, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, COMMA, + LPAREN = 0, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, COMMA, QUOTE, FOREACH, }; -- 2.43.0