")" { return RPAREN; }
"{{" { return LBRACKET; }
"}}" { return RBRACKET; }
-"," { return COMMA; }
"'"|"\\" { return QUOTE; }
"foreach" { return FOREACH; }
"define" { return DEFINE; }
"$$" { return DOLDOL; }
[ \t\n]+ { return SPACES; }
-[a-zA-Z0-9,?!%*:;$^]+ { return WORD; }
-
+[a-zA-Z0-9,?!%*:,;#$^]+ { return WORD; }
+<<EOF>> { return -1; }
free(a->value);
free(a);
}
+
+struct croma_block *lookup_symbol(char *name)
+{
+ struct croma_block *b;
+ TAILQ_FOREACH(b, &blocks_head, blocks) {
+ if (strcmp(name, b->name) == 0)
+ return b;
+ }
+
+ return NULL;
+}
+
+struct croma_arg *lookup_arg(struct croma_block *b, char *name)
+{
+ if (b == NULL)
+ return NULL;
+
+ struct croma_arg *a;
+
+ TAILQ_FOREACH(a, &b->args_head, params) {
+ if (strcmp(name, a->name) == 0)
+ return a;
+ }
+
+ return NULL;
+
+}
void yyparse(void)
{
- while(!feof(yyin)) {
- int t = yylex();
+ int t;
+ while((t = yylex()) != -1) {
+
switch(t) {
case FOREACH:
default:
printf("%s", yytext);
+ continue;
+ break;
}
}
}
void foreach_expr(void)
{
struct croma_block *b = alloc_and_insert_block();
+ b->name = strdup("Another anonymous foreach block");
TAILQ_INIT(&b->args_head);
expect(LPAREN);
return;
}
+
+/*
+ This routine extracts a word from a string, without modifying it.
+ */
+char *extract_word(char *s)
+{
+ while(*s == ' ' || *s == '\t' || *s == '\n')
+ s++;
+
+ char *wend = s;
+
+ while(*wend != ' ' || *wend != '\t' || wend != '\n')
+ wend++;
+
+ char *r = malloc((wend - s) * sizeof(char));
+
+ wend = s;
+
+ /* Copy the contents of the word in r. */
+ while(*wend != ' ' || *wend != '\t' || wend != '\n')
+ *r++ = *wend++;
+
+ return r;
+}
+
+/*
+ Replace arguments replaces all the references to the arguments
+ of b in t by their values.
+ */
+
+char *replace_arguments(struct croma_block *b, char *t)
+{
+ if (b == NULL)
+ return;
+
+ char *buf = calloc(8192, sizeof(char));
+ char *s = strdup(t);
+ char *ss = s; /* Used only to compute the number of elements copied in buf. */
+ char *argname;
+ int buflen = s - ss;
+ struct croma_arg * arg;
+
+ while(*s != '\0' && buflen < 8192) {
+
+ while(*s == ' ' || *s == '\t' || *s == '\n')
+ *buf++ = *s++;
+
+ argname = extract_word(s);
+ arg = lookup_arg(b, argname);
+
+ if (arg != NULL && arg->value != NULL) {
+
+ strncat(buf, arg->value, 8192 - buflen);
+
+ /* Don't forget to increment the pointers by the number
+ of bytes copied
+ */
+
+ s += strlen(arg->value);
+ buf += strlen(arg->value);
+
+ } else {
+ /* If the value is not found, copy the word into the buffer. */
+ while(*s != ' ' || *s != '\t' || *s != '\n')
+ *buf++ = *s++;
+ }
+
+ buflen = s - ss;
+ }
+
+}
+
+char *expand_macro(char *text)
+{
+ if (text == NULL)
+ return NULL;
+
+ char *buf = malloc(8192);
+ char *s = strdup(text);
+ char *p = strtok(s," \t\n");
+
+ struct croma_block *b;
+
+ int i = 0;
+
+ if (b == NULL)
+ fail("Unable to allocate memory");
+
+
+ while(p != NULL) {
+ b = lookup_symbol(p);
+ /* FIXME : implement argument list parsing */
+ p = strtok(NULL, " \t\n");
+ }
+
+
+}
#define __TOKENS_H__
enum {
- LPAREN = 0, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES, COMMA, QUOTE,
- FOREACH, DEFINE,
+ LPAREN = 0, RPAREN, LBRACKET, RBRACKET, WORD, DOLDOL, SPACES,
+ QUOTE, FOREACH, DEFINE,
};