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
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 Ident {
    pub fn new(loc: Loc, text: String) -> Self {
        Self { loc, text }
    }
}

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

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;

    #[allow(clippy::suspicious_arithmetic_impl)]
    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)
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
/// Typed label
pub struct Labelled<Expr> {
    /// Label is an identifier
    pub label: Ident,
    /// The thing attached on this label
    pub expr: Expr,
}

impl<Expr> Labelled<Expr> {
    pub fn new(label: Ident, expr: Expr) -> Self {
        Self { label, expr }
    }

    pub fn map_expr<Abs>(self, f: impl FnOnce(Expr) -> Abs) -> Labelled<Abs> {
        Labelled {
            label: self.label,
            expr: f(self.expr),
        }
    }
}