/* parser_get_token */ int parser_get_token(Parser * parser, Token ** token) { int ret = 0; /* XXX not sure */ size_t i; ParserCallbackData * pcd; if((*token = token_new(parser->filename, parser->line, parser->col)) == NULL) return -1; if(parser->last == EOF && parser_scan_filter(parser) == EOF && parser->error != 0) { token_delete(*token); *token = NULL; return -1; } #ifdef DEBUG fprintf(stderr, "DEBUG: %s() %c\n", __func__, parser->last); #endif for(i = 0; i < parser->callbacks_cnt; i++) { pcd = &parser->callbacks[i]; if((ret = pcd->callback(parser, *token, parser->last, pcd->data)) <= 0) break; } if(ret == 0 && i != parser->callbacks_cnt) return 0; /* there is a token and no error */ token_delete(*token); *token = NULL; return (ret >= 0 && parser->last == EOF) ? 0 : -1; }
static int _parser_scan(State * state) { int ret; TokenCode code; char const * string; if(state->token != NULL) token_delete(state->token); if((ret = _scan_skip_meta(state)) != 0 || state->token == NULL) return ret; code = token_get_code(state->token); string = token_get_string(state->token); if(code == AS_CODE_WORD) { if(string != NULL && string[0] == '$') token_set_code(state->token, AS_CODE_IMMEDIATE); } else if(code == AS_CODE_OPERATOR_MODULO) { /* FIXME ugly workaround */ if((ret = _scan_skip_meta(state)) != 0) return ret; if(_parser_is_code(state, AS_CODE_WORD)) token_set_code(state->token, AS_CODE_REGISTER); } return 0; }
/* parser_string */ int parser_string(AsmPrefs * ap, AsmCode * code, char const * string) { CppPrefs prefs; State state; size_t i; memset(&prefs, 0, sizeof(prefs)); #if 0 prefs.filename = infile; #endif prefs.filters = CPP_FILTER_COMMENT; memset(&state, 0, sizeof(state)); state.code = code; if((state.cpp = cpp_new(&prefs)) == NULL) return -1; if(ap != NULL) for(i = 0; i < ap->defines_cnt; i++) /* FIXME check errors */ cpp_define_add(state.cpp, ap->defines[i], NULL); if(_parser_scan(&state) != 0) return _parser_error(&state, "%s", error_get()); if(_program(&state) != 0) error_set_code(1, "%s%u%s%u%s", "Compilation failed with ", state.error_cnt, " error(s) and ", state.warning_cnt, " warning(s)"); if(state.token != NULL) token_delete(state.token); return state.error_cnt; }
/* parser */ int parser(AsmPrefs * ap, AsmCode * code, char const * infile) { CppPrefs prefs; State state; memset(&prefs, 0, sizeof(prefs)); prefs.filename = infile; prefs.filters = CPP_FILTER_COMMENT; memset(&state, 0, sizeof(state)); state.code = code; if((state.cpp = cpp_new(&prefs)) == NULL) return -1; if(_parser_defines(&state, ap) != 0) { cpp_delete(state.cpp); return -1; } if(_parser_scan(&state) != 0) { cpp_delete(state.cpp); return _parser_error(&state, "%s", error_get()); } if(_program(&state) != 0) error_set_code(1, "%s%s%u%s%u%s", infile, ": Compilation failed with ", state.error_cnt, " error(s) and ", state.warning_cnt, " warning(s)"); if(state.token != NULL) token_delete(state.token); return state.error_cnt; }
void token_list_delete(t_token_list *list, int delete_tokens) { if (!list) return ; if (delete_tokens) token_delete(list->token); token_list_delete(list->next, delete_tokens); free(list); }
//------------------------------------------------------------------------------ // pp_skip_to_else_elif_endif function // // Skip to next #else, #elif and #endif token at same nesting level. // void pp_skip_to_else_elif_endif () { slcc_string* s; slcc_token token; skipping_code = true; for (;;) { token = lex_get_next_token (true); if (token.type == TT_PREPROCESSOR) { if (pp_check_preprocessor_cmd ( token.value.preprocessor, DirectiveIF ) ) current_nesting++; else if (pp_check_preprocessor_cmd ( token.value.preprocessor, DirectiveELIF ) && current_nesting == 0) break; else if (pp_check_preprocessor_cmd ( token.value.preprocessor, DirectiveELSE ) && current_nesting == 0) break; else if (pp_check_preprocessor_cmd ( token.value.preprocessor, DirectiveENDIF ) ) { if (current_nesting == 0) break; else current_nesting--; } } } /* Process the encountered preprocessor token */ skipping_code = false; s = str_new_with_data (token.value.preprocessor); pp_process_directive (s); token_delete (&token); }
static int _cpp_do(Prefs * prefs, FILE * fp, char const * filename) { int ret; CppPrefs cppprefs; Cpp * cpp; size_t i; size_t j; size_t k; Token * token; memset(&cppprefs, 0, sizeof(cppprefs)); cppprefs.filename = filename; cppprefs.filters = CPP_FILTER_COMMENT; if(prefs->flags & PREFS_t) cppprefs.filters |= CPP_FILTER_TRIGRAPH; if(prefs->flags & PREFS_w) cppprefs.filters |= CPP_FILTER_WHITESPACE; if((cpp = cpp_new(&cppprefs)) == NULL) return _cpp_error(); for(i = 0; i < prefs->paths_cnt; i++) if(cpp_path_add(cpp, prefs->paths[i]) != 0) break; for(j = 0; j < prefs->defines_cnt; j++) if(cpp_define_add(cpp, prefs->defines[j], NULL) != 0) break; for(k = 0; k < prefs->undefines_cnt; k++) if(cpp_define_remove(cpp, prefs->undefines[k]) != 0) break; if(i != prefs->paths_cnt || j != prefs->defines_cnt || k != prefs->undefines_cnt) { cpp_delete(cpp); return 1; } while((ret = cpp_scan(cpp, &token)) == 0) { if(token == NULL) /* end of file */ break; _do_print_token(fp, token); token_delete(token); } if(ret != 0) _cpp_error(); cpp_delete(cpp); return ret; }
static int _scan_skip_meta(State * state) { int ret = 0; TokenCode code; while(cpp_scan(state->cpp, &state->token) == 0) { if(state->token == NULL) return ret; if((code = token_get_code(state->token)) < AS_CODE_META_FIRST || code > AS_CODE_META_LAST) return ret; if(code == AS_CODE_META_ERROR) ret |= _parser_error(state, "%s", token_get_string( state->token)); else if(code == AS_CODE_META_WARNING) _parser_warning(state, "%s", token_get_string( state->token)); token_delete(state->token); } return 1; }