//return 1 if name was processed as special variable, 0 otherwise int dag_parse_process_special_variable(struct lexer_book *bk, struct dag_node *n, int nodeid, char *name, const char *value) { struct dag *d = bk->d; int special = 0; if(strcmp(RESOURCES_CATEGORY, name) == 0) { special = 1; /* If we have never seen this label, then create * a new category, otherwise retrieve the category. */ struct dag_task_category *category = dag_task_category_lookup_or_create(d, value); /* If we are parsing inside a node, make category * the category of the node, but do not update * the global task_category. Else, update the * global task category. */ if(n) { /* Remove node from previous category...*/ list_pop_tail(n->category->nodes); n->category = category; /* and add it to the new one */ list_push_tail(n->category->nodes, n); debug(D_DEBUG, "Updating category '%s' for rule %d.\n", value, n->nodeid); } else bk->category = category; } /* else if some other special variable .... */ /* ... */ return special; }
int dag_parse(struct dag *d, FILE * dag_stream) { char *line = NULL; struct lexer_book *bk = calloc(1, sizeof(struct lexer_book)); //Taking advantage that calloc zeroes memory bk->d = d; bk->stream = dag_stream; bk->category = dag_task_category_lookup_or_create(d, "default"); while((line = dag_parse_readline(bk, NULL)) != NULL) { if(strlen(line) == 0 || line[0] == '#') { /* Skip blank lines and comments */ free(line); continue; } if(strncmp(line, "export ", 7) == 0) { if(!dag_parse_export(bk, line)) { dag_parse_error(bk, "export"); goto failure; } } else if(strchr(line, '=')) { if(!dag_parse_variable(bk, NULL, line)) { dag_parse_error(bk, "variable"); goto failure; } } else if(strstr(line, ":")) { if(!dag_parse_node(bk, line)) { dag_parse_error(bk, "node"); goto failure; } } else { dag_parse_error(bk, "syntax"); goto failure; } free(line); } //ok dag_close_over_environment(d); dag_compile_ancestors(d); free(bk); return 1; failure: free(line); free(bk); return 0; }
static int dag_parse(struct dag *d, FILE *stream) { struct lexer *bk = lexer_create(STREAM, stream, 1, 1); bk->d = d; bk->stream = stream; bk->category = dag_task_category_lookup_or_create(d, "default"); struct dag_variable_lookup_set s = { d, NULL, NULL, NULL }; bk->environment = &s; struct token *t; while((t = lexer_peek_next_token(bk))) { s.category = bk->category; s.node = NULL; s.table = NULL; switch (t->type) { case TOKEN_NEWLINE: case TOKEN_SPACE: /* Skip newlines, spaces at top level. */ lexer_free_token(lexer_next_token(bk)); break; case TOKEN_SYNTAX: dag_parse_syntax(bk); break; case TOKEN_FILES: dag_parse_node(bk); break; case TOKEN_VARIABLE: dag_parse_variable(bk, NULL); break; default: lexer_report_error(bk, "Unexpected token. Expected one of NEWLINE, SPACE, SYNTAX, FILES, or VARIABLE, but got: %s\n:", lexer_print_token(t)); break; } } dag_close_over_environment(d); dag_compile_ancestors(d); free(bk); return 1; }