From 323ddffe325a4bffec89447c75cc27a81315abc1 Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Fri, 8 May 2026 17:05:01 +0300 Subject: Split Spanned from Span and expose fields Spanned wraps a value with a Span. Public fields enable destructuring in pattern matches. --- compiler/src/lexer/mod.rs | 6 ++-- compiler/src/lexer/tests.rs | 6 ++-- compiler/src/span.rs | 67 ++++++++++++++++++--------------------------- 3 files changed, 34 insertions(+), 45 deletions(-) (limited to 'compiler') diff --git a/compiler/src/lexer/mod.rs b/compiler/src/lexer/mod.rs index ff7d51d..464d88e 100644 --- a/compiler/src/lexer/mod.rs +++ b/compiler/src/lexer/mod.rs @@ -1,4 +1,4 @@ -use crate::span::{Pos, Span}; +use crate::span::{Pos, Span, Spanned}; #[cfg(test)] mod tests; @@ -113,7 +113,7 @@ impl<'a> Lexer<'a> { } impl<'a> Iterator for Lexer<'a> { - type Item = Span>; + type Item = Spanned>; fn next(&mut self) -> Option { loop { @@ -163,6 +163,6 @@ impl<'a> Iterator for Lexer<'a> { }; let end = Pos::new(self.line, self.column, self.cursor); - Some(Span::new(token, start, end)) + Some(Spanned::new(token, Span::new(start, end))) } } diff --git a/compiler/src/lexer/tests.rs b/compiler/src/lexer/tests.rs index 30be85a..2dce2e3 100644 --- a/compiler/src/lexer/tests.rs +++ b/compiler/src/lexer/tests.rs @@ -4,7 +4,7 @@ use super::Token::*; use super::*; fn tokenize<'a>(input: &'a str) -> Vec> { - Lexer::new(input).map(|s| s.into_inner()).collect() + Lexer::new(input).map(|s| s.inner).collect() } #[test] @@ -289,7 +289,9 @@ fn test_comments() { } fn spans(input: &str) -> Vec<(Pos, Pos)> { - Lexer::new(input).map(|s| (s.start(), s.end())).collect() + Lexer::new(input) + .map(|s| (s.span.start, s.span.end)) + .collect() } #[test] diff --git a/compiler/src/span.rs b/compiler/src/span.rs index 0644c1c..369c58c 100644 --- a/compiler/src/span.rs +++ b/compiler/src/span.rs @@ -1,61 +1,48 @@ #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Pos { - line: usize, - column: usize, - cursor: usize, + pub line: usize, + pub column: usize, + pub offset: usize, } impl Pos { - pub fn new(line: usize, column: usize, cursor: usize) -> Self { + pub fn new(line: usize, column: usize, offset: usize) -> Self { Self { line, column, - cursor, + offset, } } - - pub fn line(self) -> usize { - self.line - } - - pub fn column(self) -> usize { - self.column - } - - pub fn cursor(self) -> usize { - self.cursor - } } -#[derive(Clone, Debug)] -pub struct Span { - inner: T, - start: Pos, - end: Pos, +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Span { + pub start: Pos, + pub end: Pos, } -impl Span { - pub fn new(inner: T, start: Pos, end: Pos) -> Self { - Self { inner, start, end } - } - - pub fn inner(&self) -> &T { - &self.inner - } - - pub fn inner_mut(&mut self) -> &mut T { - &mut self.inner +impl Span { + pub fn new(start: Pos, end: Pos) -> Self { + Self { start, end } } +} - pub fn into_inner(self) -> T { - self.inner - } +#[derive(Clone, Copy, Debug)] +pub struct Spanned { + pub inner: T, + pub span: Span, +} - pub fn start(&self) -> Pos { - self.start +impl Spanned { + pub fn new(inner: T, span: Span) -> Self { + Self { inner, span } } - pub fn end(&self) -> Pos { - self.end + pub fn map(self, f: impl FnOnce(T) -> U) -> Spanned { + let Self { inner, span } = self; + Spanned { + inner: f(inner), + span, + } } } -- cgit v1.3