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
use crate::ast;
use crate::{
Parse, ParseError, Parser, Peek, Peeker, Resolve, ResolveError, ResolveErrorKind, ResolveOwned,
Spanned, Storage, ToTokens,
};
use runestick::{Source, Span};
use std::borrow::Cow;
#[derive(Debug, Clone, Copy, PartialEq, Eq, ToTokens, Spanned)]
pub struct Label {
pub token: ast::Token,
#[rune(skip)]
pub source: ast::StringSource,
}
impl Label {
pub fn new(label: &str) -> Self {
crate::macros::current_context(|ctx| Self::new_with(label, ctx.macro_span(), ctx.storage()))
}
pub fn new_with(label: &str, span: Span, storage: &Storage) -> Self {
let id = storage.insert_str(label);
let source = ast::StringSource::Synthetic(id);
ast::Label {
token: ast::Token {
span,
kind: ast::Kind::Label(source),
},
source,
}
}
}
impl Parse for Label {
fn parse(p: &mut Parser<'_>) -> Result<Self, ParseError> {
let token = p.next()?;
match token.kind {
K!['label(source)] => Ok(Self { token, source }),
_ => Err(ParseError::expected(&token, "label")),
}
}
}
impl Peek for Label {
fn peek(p: &mut Peeker<'_>) -> bool {
matches!(p.nth(0), K!['label])
}
}
impl<'a> Resolve<'a> for Label {
type Output = Cow<'a, str>;
fn resolve(&self, storage: &Storage, source: &'a Source) -> Result<Cow<'a, str>, ResolveError> {
let span = self.token.span();
match self.source {
ast::StringSource::Text => {
let span = self.token.span();
let ident = source
.source(span.trim_start(1))
.ok_or_else(|| ResolveError::new(span, ResolveErrorKind::BadSlice))?;
Ok(Cow::Borrowed(ident))
}
ast::StringSource::Synthetic(id) => {
let ident = storage.get_string(id).ok_or_else(|| {
ResolveError::new(span, ResolveErrorKind::BadSyntheticId { kind: "ident", id })
})?;
Ok(Cow::Owned(ident))
}
ast::StringSource::BuiltIn(builtin) => Ok(Cow::Borrowed(builtin.as_str())),
}
}
}
impl ResolveOwned for Label {
type Owned = String;
fn resolve_owned(
&self,
storage: &Storage,
source: &Source,
) -> Result<Self::Owned, ResolveError> {
Ok(self.resolve(storage, source)?.into_owned())
}
}