alopex_sql/ast/
span.rs

1/// A location in source text (1-based line/column).
2#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
3pub struct Location {
4    /// Line number (1-based). Zero represents an unknown location.
5    pub line: u64,
6    /// Column number (1-based). Zero represents an unknown location.
7    pub column: u64,
8}
9
10impl Location {
11    /// Returns an unknown/empty location.
12    pub const fn empty() -> Self {
13        Self { line: 0, column: 0 }
14    }
15
16    /// Creates a new location with the given line/column.
17    pub const fn new(line: u64, column: u64) -> Self {
18        Self { line, column }
19    }
20}
21
22/// A span covering a start and end location (inclusive).
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24pub struct Span {
25    pub start: Location,
26    pub end: Location,
27}
28
29impl Span {
30    /// Returns an unknown/empty span.
31    pub const fn empty() -> Self {
32        Self {
33            start: Location::empty(),
34            end: Location::empty(),
35        }
36    }
37
38    /// Creates a new span from start and end locations.
39    pub const fn new(start: Location, end: Location) -> Self {
40        Self { start, end }
41    }
42
43    /// Returns the minimal span covering both spans.
44    pub fn union(&self, other: &Span) -> Span {
45        if self.start.line == 0 {
46            return *other;
47        }
48        if other.start.line == 0 {
49            return *self;
50        }
51        Span {
52            start: core::cmp::min(self.start, other.start),
53            end: core::cmp::max(self.end, other.end),
54        }
55    }
56}
57
58/// Trait for AST nodes that can report their source span.
59pub trait Spanned {
60    fn span(&self) -> Span;
61}