diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-08-23 15:38:26 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-08-24 15:33:18 +0200 |
commit | 8e8dae0eb0f90dea3cb4b244c79f7fa855219f92 (patch) | |
tree | 9adbcdd3197f27039a76e0f8cd0411946bd39272 /compiler.c | |
parent | 476f02bfa397959ba8a553a36cf5eaffc82b8f07 (diff) |
lib: introduce helper function for indenting error messages
Factor out the nested syntax error message indentation logic into a
separate helper procedure for reuse in other places.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'compiler.c')
-rw-r--r-- | compiler.c | 36 |
1 files changed, 7 insertions, 29 deletions
@@ -172,8 +172,8 @@ uc_compiler_syntax_error(uc_compiler_t *compiler, size_t off, const char *fmt, . uc_source_t *source = uc_compiler_current_source(compiler); uc_stringbuf_t *buf = compiler->parser->error; size_t line = 0, byte = 0, len = 0; - char *s, *p, *nl; va_list ap; + char *s; if (compiler->parser->synchronizing) return; @@ -195,32 +195,8 @@ uc_compiler_syntax_error(uc_compiler_t *compiler, size_t off, const char *fmt, . va_end(ap); ucv_stringbuf_append(buf, "Syntax error: "); - - p = strstr(s, "\nSyntax error: "); - - if (!p) { - ucv_stringbuf_addstr(buf, s, len); - ucv_stringbuf_append(buf, "\n"); - } - else { - ucv_stringbuf_printf(buf, "%.*s\n\n", (int)(p - s), s); - - while (len > 0 && s[len-1] == '\n') - s[--len] = 0; - - for (p++, nl = strchr(p, '\n'); p != NULL; - p = nl ? nl + 1 : NULL, nl = p ? strchr(p, '\n') : NULL) - { - if (!nl) - ucv_stringbuf_printf(buf, " | %s", p); - else if (nl != p) - ucv_stringbuf_printf(buf, " | %.*s\n", (int)(nl - p), p); - else - ucv_stringbuf_append(buf, " |\n"); - } - - ucv_stringbuf_append(buf, "\n\n"); - } + ucv_stringbuf_addstr(buf, s, len); + ucv_stringbuf_append(buf, "\n"); free(s); @@ -3413,9 +3389,11 @@ uc_compiler_compile_module(uc_compiler_t *compiler, const char *name, uc_value_t err = NULL; res = uc_compiler_compile_module_source(compiler, source, imports, &err); - if (!res) + if (!res) { + uc_error_message_indent(&err); uc_compiler_syntax_error(compiler, compiler->parser->curr.pos, - "Unable to compile module '%s':\n%s", source->filename, err); + "Unable to compile module '%s':\n\n%s", source->filename, err); + } free(err); } |