diff options
-rw-r--r-- | compiler.c | 53 | ||||
-rw-r--r-- | tests/custom/04_modules/06_export_errors | 26 | ||||
-rw-r--r-- | tests/custom/04_modules/07_import_default | 2 | ||||
-rw-r--r-- | tests/custom/04_modules/08_import_list | 2 | ||||
-rw-r--r-- | tests/custom/04_modules/12_import_immutability | 2 |
5 files changed, 61 insertions, 24 deletions
@@ -169,10 +169,11 @@ uc_compiler_current_source(uc_compiler_t *compiler) __attribute__((format(printf, 3, 0))) static void 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; @@ -188,7 +189,7 @@ uc_compiler_syntax_error(uc_compiler_t *compiler, size_t off, const char *fmt, . if (off) { byte = off; - line = uc_source_get_line(uc_compiler_current_source(compiler), &byte); + line = uc_source_get_line(source, &byte); } va_start(ap, fmt); @@ -196,15 +197,53 @@ uc_compiler_syntax_error(uc_compiler_t *compiler, size_t off, const char *fmt, . va_end(ap); ucv_stringbuf_append(buf, "Syntax error: "); - ucv_stringbuf_addstr(buf, s, len); - ucv_stringbuf_append(buf, "\n"); + + 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"); + } free(s); - if (line) - ucv_stringbuf_printf(buf, "In line %zu, byte %zu:\n", line, byte); + if (line) { + ucv_stringbuf_append(buf, "In "); + + if (compiler->program->sources.count > 1) { + len = strlen(source->filename); + + if (len > 48) + ucv_stringbuf_printf(buf, "...%s", source->filename + len - 45); + else + ucv_stringbuf_addstr(buf, source->filename, len); + + ucv_stringbuf_append(buf, ", "); + } + + ucv_stringbuf_printf(buf, "line %zu, byte %zu:\n", line, byte); + } - if (uc_error_context_format(buf, uc_compiler_current_source(compiler), NULL, off)) + if (uc_error_context_format(buf, source, NULL, off)) ucv_stringbuf_append(buf, "\n\n"); } diff --git a/tests/custom/04_modules/06_export_errors b/tests/custom/04_modules/06_export_errors index c02a547..5c9f676 100644 --- a/tests/custom/04_modules/06_export_errors +++ b/tests/custom/04_modules/06_export_errors @@ -36,15 +36,14 @@ import "./files/test.uc"; -- Expect stderr -- Syntax error: Unable to compile module './files/test.uc': -Syntax error: Exports may only appear at top level of a module -In line 2, byte 2: - - ` export let x = 1;` - ^-- Near here - + | Syntax error: Exports may only appear at top level of a module + | In ./files/test.uc, line 2, byte 2: + | + | ` export let x = 1;` + | ^-- Near here -In line 1, byte 25: +In [stdin], line 1, byte 25: `import "./files/test.uc";` Near here --------------^ @@ -72,15 +71,14 @@ export { y as x }; -- Expect stderr -- Syntax error: Unable to compile module './files/test-duplicate.uc': -Syntax error: Duplicate export 'x' for module './files/test-duplicate.uc' -In line 4, byte 15: - - `export { y as x };` - Near here ----^ - + | Syntax error: Duplicate export 'x' for module './files/test-duplicate.uc' + | In ./files/test-duplicate.uc, line 4, byte 15: + | + | `export { y as x };` + | Near here ----^ -In line 1, byte 35: +In [stdin], line 1, byte 35: `import "./files/test-duplicate.uc";` Near here ------------------------^ diff --git a/tests/custom/04_modules/07_import_default b/tests/custom/04_modules/07_import_default index 7190a22..d9d08b5 100644 --- a/tests/custom/04_modules/07_import_default +++ b/tests/custom/04_modules/07_import_default @@ -39,7 +39,7 @@ export const x = "This is a non-default export"; -- Expect stderr -- Syntax error: Module ./files/test2.uc has no default export -In line 1, byte 20: +In [stdin], line 1, byte 20: `import defVal from "./files/test2.uc";` Near here ---------^ diff --git a/tests/custom/04_modules/08_import_list b/tests/custom/04_modules/08_import_list index 1a4f116..b55ddec 100644 --- a/tests/custom/04_modules/08_import_list +++ b/tests/custom/04_modules/08_import_list @@ -38,7 +38,7 @@ export const x = "This is a test"; -- Expect stderr -- Syntax error: Module ./files/test2.uc has no default export -In line 1, byte 15: +In [stdin], line 1, byte 15: `import y from "./files/test2.uc";` Near here ----^ diff --git a/tests/custom/04_modules/12_import_immutability b/tests/custom/04_modules/12_import_immutability index 37c0bc6..48a7fe2 100644 --- a/tests/custom/04_modules/12_import_immutability +++ b/tests/custom/04_modules/12_import_immutability @@ -16,7 +16,7 @@ export let a = 1; -- Expect stderr -- Syntax error: Invalid assignment to constant 'a' -In line 3, byte 5: +In [stdin], line 3, byte 5: `a = 2;` ^-- Near here |