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
use derive_more::From;

use crate::code_point::CodePoint;
use crate::structure::Located;
use crate::structure::rlt::Context;
use crate::structure::rlt::new_types::{Identifier, TypeName};

#[derive(Debug, Clone, PartialEq, From)]
pub enum Term {
    Reference(Reference),
    Contextual(ContextualReference)
}

#[derive(Debug, Clone, PartialEq)]
pub enum Reference {
    Type(TypeName),
    Identifier(Identifier),
}

#[derive(Debug, Clone, PartialEq)]
pub struct ContextualReference {
    pub context: Context,
    pub inner: Reference
}

impl Located for Term {
    fn location(&self) -> CodePoint {
        match self {
            Term::Reference(x) => x.location(),
            Term::Contextual(x) => x.location()
        }
    }
}

impl Located for ContextualReference {
    fn location(&self) -> CodePoint {
        let (_, unfolded) = self.context.clone().unfold();
        let first = unfolded.first().unwrap_or(&self.inner);
        let last = &self.inner;
        let length = last.location().offset + last.location().length - first.location().offset;
        CodePoint::new(length, first.location().offset)
    }
}

impl Located for Reference {
    fn location(&self) -> CodePoint {
        match self {
            Reference::Type(x) => x.location(),
            Reference::Identifier(x) => x.location(),
        }
    }
}