1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::fmt;

/// A file position for human composed of the line (starting at 1), and column (starting a 0)
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Position {
    pub line: usize,
    pub col: usize,
}

impl Default for Position {
    fn default() -> Self {
        Self { line: 1, col: 0 }
    }
}

impl fmt::Debug for Position {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.line, self.col)
    }
}

impl fmt::Display for Position {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.line, self.col)
    }
}

impl Position {
    pub fn advance_line(&mut self) {
        self.line += 1;
        self.col = 0;
    }

    pub fn advance_col(&mut self) {
        self.col += 1;
    }

    pub fn advance(&mut self, c: char) {
        if c == '\n' {
            self.advance_line()
        } else {
            self.advance_col()
        }
    }
}

/// Span defined by 2 positions, defining a range between start and end
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Span {
    pub start: Position,
    pub end: Position,
}

impl Span {
    pub fn extend(&self, other: &Self) -> Self {
        Self {
            start: self.start,
            end: other.end,
        }
    }

    pub fn on_line(line: usize, start_col: usize, end_col: usize) -> Self {
        Self {
            start: Position {
                line,
                col: start_col,
            },
            end: Position { line, col: end_col },
        }
    }
}

impl fmt::Debug for Span {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}-{}", self.start, self.end)
    }
}

impl fmt::Display for Span {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}-{}", self.start, self.end)
    }
}

/// A type with the span (start and end positions) associated
#[derive(Clone, Debug)]
pub struct Spanned<T> {
    pub span: Span,
    pub inner: T,
}