diff options
Diffstat (limited to 'src/compiler/error_helper')
-rw-r--r-- | src/compiler/error_helper/error_helper.c | 22 | ||||
-rw-r--r-- | src/compiler/error_helper/error_helper.h | 3 |
2 files changed, 20 insertions, 5 deletions
diff --git a/src/compiler/error_helper/error_helper.c b/src/compiler/error_helper/error_helper.c index fd648e4..2a9b209 100644 --- a/src/compiler/error_helper/error_helper.c +++ b/src/compiler/error_helper/error_helper.c @@ -3,18 +3,32 @@ #include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> -void printError(char const *error, SourceCode code, char const *begin, +void printError(char const *error, SourceCode *sourceCode, char const *begin, char const *end, ...) { va_list args; va_start(args, end); char *errorStr; vasprintf(&errorStr, error, args); - char const *lineBegin = code; + const Code *code = NULL; + + for (size_t i = 0; i < sourceCode->size; ++i) { + const Code *source = sourceCode->codes[i]; + if (source->code <= begin && strlen(source->code)+source->code >= end) { + code = source; + } + } + if (code == NULL) { + fprintf(stderr, "Bad Error: the error is in no source code"); + return; + } + + char const *lineBegin = code->code; int line = 1; - char const *iter = code; + char const *iter = code->code; for (; iter < begin; ++iter) { const char c = *iter; switch (c) { @@ -54,7 +68,7 @@ void printError(char const *error, SourceCode code, char const *begin, } AFTER_LOOP: - fprintf(stderr, "Error: %s at line %d\n", errorStr, line); + fprintf(stderr, "Error: %s at %s:%d\n", errorStr, code->filePath, line); int printed = 0; for (iter = lineBegin; iter < lineEnd; ++iter) { diff --git a/src/compiler/error_helper/error_helper.h b/src/compiler/error_helper/error_helper.h index 3a78c7c..c118fa9 100644 --- a/src/compiler/error_helper/error_helper.h +++ b/src/compiler/error_helper/error_helper.h @@ -1,6 +1,7 @@ #pragma once +#include <compiler/source_code/source_code.h> #include <utils/types.h> -extern void printError(char const *error, SourceCode code, char const *begin, +extern void printError(char const *error, SourceCode *code, char const *begin, char const *end, ...); |