commit 2f626af47071623ff393a568fdf765526342fb27 from: Murilo Ijanc date: Tue Aug 5 17:20:00 2025 UTC Add span and error types Span tracks byte positions in source code for error reporting. OlangError carries a message and span. commit - c916ec471c406a8caef7d57af437fa182632e120 commit + 2f626af47071623ff393a568fdf765526342fb27 blob - /dev/null blob + aa69daf3d651955eac5e92fe1316dc2a7d997967 (mode 644) --- /dev/null +++ src/error.rs @@ -0,0 +1,47 @@ +// vim: set tw=79 cc=80 ts=4 sw=4 sts=4 et : +// +// Copyright (c) 2025-2026 Murilo Ijanc' +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// + +use std::fmt; + +use crate::span::Span; + +#[derive(Debug, Clone)] +pub struct OlangError { + pub message: String, + pub span: Span, +} + +impl OlangError { + pub fn new( + message: impl Into, + span: Span, + ) -> Self { + Self { + message: message.into(), + span, + } + } +} + +impl fmt::Display for OlangError { + fn fmt( + &self, + f: &mut fmt::Formatter<'_>, + ) -> fmt::Result { + write!(f, "error: {}", self.message) + } +} blob - /dev/null blob + 5a454a528486b4073ba49646dfbb0384bb8001f4 (mode 644) --- /dev/null +++ src/span.rs @@ -0,0 +1,28 @@ +// vim: set tw=79 cc=80 ts=4 sw=4 sts=4 et : +// +// Copyright (c) 2025-2026 Murilo Ijanc' +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// + +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Span { + pub start: usize, + pub end: usize, +} + +impl Span { + pub fn new(start: usize, end: usize) -> Self { + Self { start, end } + } +}