rune/ast/
spanned.rs

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use crate::alloc;
use crate::ast::Span;
use crate::parse::NonZeroId;

/// Helper derive to implement [`Spanned`].
pub use rune_macros::Spanned;

/// Helper derive to implement [`OptionSpanned`].
pub use rune_macros::OptionSpanned;

/// Defer building a span from a function.
pub(crate) fn from_fn<F>(function: F) -> FromFn<F> {
    FromFn { function }
}

/// Function used to build a [`Span`].
#[derive(Clone, Copy)]
pub(crate) struct FromFn<F> {
    function: F,
}

impl<F> Spanned for FromFn<F>
where
    F: Fn() -> Span,
{
    #[inline]
    fn span(&self) -> Span {
        (self.function)()
    }
}

/// Types for which we can get a span.
pub trait Spanned {
    /// Get the span of the type.
    fn span(&self) -> Span;
}

impl Spanned for syntree::Span<u32> {
    fn span(&self) -> Span {
        Span::new(self.start, self.end)
    }
}

impl<A, B> Spanned for (A, B)
where
    A: Spanned,
    B: OptionSpanned,
{
    fn span(&self) -> Span {
        if let Some(end) = self.1.option_span() {
            self.0.span().join(end)
        } else {
            self.0.span()
        }
    }
}

impl Spanned for Span {
    fn span(&self) -> Span {
        *self
    }
}

#[cfg(feature = "alloc")]
impl<T> Spanned for ::rust_alloc::boxed::Box<T>
where
    T: Spanned,
{
    fn span(&self) -> Span {
        Spanned::span(&**self)
    }
}

impl<T> Spanned for alloc::Box<T>
where
    T: Spanned,
{
    fn span(&self) -> Span {
        Spanned::span(&**self)
    }
}

impl<T> Spanned for &T
where
    T: ?Sized + Spanned,
{
    fn span(&self) -> Span {
        Spanned::span(*self)
    }
}

impl<T> Spanned for &mut T
where
    T: ?Sized + Spanned,
{
    fn span(&self) -> Span {
        Spanned::span(*self)
    }
}

impl<S> Spanned for (S, NonZeroId)
where
    S: Spanned,
{
    fn span(&self) -> Span {
        self.0.span()
    }
}

/// Types for which we can optionally get a span.
pub trait OptionSpanned {
    /// Get the optional span of the type.
    fn option_span(&self) -> Option<Span>;
}

/// Take the span of a vector of spanned.
/// Provides the span between the first and the last element.
impl<T> OptionSpanned for [T]
where
    T: Spanned,
{
    fn option_span(&self) -> Option<Span> {
        let span = self.first()?.span();

        if let Some(last) = self.last() {
            Some(span.join(last.span()))
        } else {
            Some(span)
        }
    }
}

impl<T> OptionSpanned for Option<T>
where
    T: Spanned,
{
    fn option_span(&self) -> Option<Span> {
        Some(self.as_ref()?.span())
    }
}

#[cfg(feature = "alloc")]
impl<T> OptionSpanned for ::rust_alloc::boxed::Box<T>
where
    T: OptionSpanned,
{
    fn option_span(&self) -> Option<Span> {
        OptionSpanned::option_span(&**self)
    }
}

impl<T> OptionSpanned for alloc::Box<T>
where
    T: OptionSpanned,
{
    fn option_span(&self) -> Option<Span> {
        OptionSpanned::option_span(&**self)
    }
}