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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::{span::Span, Spanned};

use std::{
    cmp::{Ord, Ordering},
    fmt,
    hash::{Hash, Hasher},
};

/// An [Ident] is an _identifier_ with a corresponding `span` from which it was derived.
#[derive(Debug, Clone)]
pub struct Ident {
    name_override_opt: Option<&'static str>,
    span: Span,
    is_raw_ident: bool,
}

// custom implementation of Hash so that namespacing isn't reliant on the span itself, which will
// often be different.
impl Hash for Ident {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.as_str().hash(state);
    }
}

impl PartialEq for Ident {
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl Ord for Ident {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_str().cmp(other.as_str())
    }
}

impl PartialOrd for Ident {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Eq for Ident {}

impl Spanned for Ident {
    fn span(&self) -> Span {
        self.span.clone()
    }
}

impl fmt::Display for Ident {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "{}", self.as_str())
    }
}

impl Ident {
    pub fn as_str(&self) -> &str {
        self.name_override_opt.unwrap_or_else(|| self.span.as_str())
    }

    pub fn is_raw_ident(&self) -> bool {
        self.is_raw_ident
    }

    pub fn new(span: Span) -> Ident {
        let span = span.trim();
        Ident {
            name_override_opt: None,
            span,
            is_raw_ident: false,
        }
    }

    pub fn new_no_trim(span: Span) -> Ident {
        Ident {
            name_override_opt: None,
            span,
            is_raw_ident: false,
        }
    }

    pub fn new_with_raw(span: Span, is_raw_ident: bool) -> Ident {
        let span = span.trim();
        Ident {
            name_override_opt: None,
            span,
            is_raw_ident,
        }
    }

    pub fn new_with_override(name_override: &'static str, span: Span) -> Ident {
        Ident {
            name_override_opt: Some(name_override),
            span,
            is_raw_ident: false,
        }
    }

    pub fn new_no_span(name: &'static str) -> Ident {
        Ident {
            name_override_opt: Some(name),
            span: Span::dummy(),
            is_raw_ident: false,
        }
    }
}