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
use std::fmt::{Display, Error, Formatter};
use std::ops::Add;

/// Trivial information about the surface syntax items,
/// short for "Location".
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Default)]
pub struct Loc {
    pub start: usize,
    pub line: usize,
    pub end: usize,
    pub is_generated: bool,
}

/// Surface syntax tree element: Identifier.
/// Also used in other syntax trees.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Ident {
    pub loc: Loc,
    pub text: String,
}

impl ToLoc for Ident {
    fn loc(&self) -> Loc {
        self.loc.clone()
    }
}

pub fn merge_info(a: &impl ToLoc, b: &impl ToLoc) -> Loc {
    a.loc() + b.loc()
}

/// Something that holds a `Loc`.
pub trait ToLoc {
    /// Access the location information.
    fn loc(&self) -> Loc;
}

impl Add for Loc {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self::Output {
            line: self.line,
            start: self.start,
            end: rhs.end,
            is_generated: self.is_generated || rhs.is_generated,
        }
    }
}

impl Display for Loc {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "line {:?} ({:?}:{:?})", self.line, self.start, self.end)
    }
}