From e9c82a39aad94334190edafa40a5c14b08f31472 Mon Sep 17 00:00:00 2001 From: hl-valdemar Date: Sat, 19 Sep 2026 17:39:28 +0200 Subject: [PATCH] feat: add shared source diagnostics --- source/diagnostics/diagnostics.hon | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 source/diagnostics/diagnostics.hon diff --git a/source/diagnostics/diagnostics.hon b/source/diagnostics/diagnostics.hon new file mode 100644 index 0000000..e2f5cc7 --- /dev/null +++ b/source/diagnostics/diagnostics.hon @@ -0,0 +1,50 @@ +import "@std/mem" +import "@std/arraylist" + +Span :: struct { + start u32 + end u32 +} + +DiagnosticId :: distinct u32 +NO_DIAGNOSTIC :: maxval!(DiagnosticId) + +Code :: enum { + invalid_character, + float_must_end_with_digit, + unterminated_string, + unexpected_token, +} + +Diagnostic :: struct { + code Code + span Span +} + +Store :: struct { items arraylist.ArrayList(Diagnostic) } + +CapacityError :: enum { diagnostic_limit_reached } + +init proc(allocator mem.Allocator) Store { + return Store{ items = arraylist.init(allocator) } +} + +deinit proc(store @mut Store) void { + arraylist.deinit(&store.items) +} + +add proc(store @mut Store, code Code, span Span) DiagnosticId ! (mem.AllocError | CapacityError) { + if (store.items.items.len >= usize(NO_DIAGNOSTIC)) return .diagnostic_limit_reached + id :: DiagnosticId(store.items.items.len) + try arraylist.append(&store.items, Diagnostic{ code = code, span = span }) + return id +} + +get proc(store @Store, id DiagnosticId) ?Diagnostic { + if (usize(id) >= store.items.items.len) return null + return store.items.items[usize(id)] +} + +len proc(store @Store) usize { + return store.items.items.len +}