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; }