rune/ast/
label.rs

1use crate::ast::prelude::*;
2
3#[test]
4#[cfg(not(miri))]
5fn ast_parse() {
6    rt::<ast::Label>("'foo");
7    rt::<ast::Label>("'barify42");
8}
9
10/// A label, like `'foo`.
11///
12/// Custom labels are constructed in macros using
13/// [MacroContext::label][crate::macros::MacroContext::label].
14///
15/// ```
16/// use rune::ast;
17/// use rune::macros;
18///
19/// macros::test(|cx| {
20///     let lit = cx.label("foo")?;
21///     assert!(matches!(lit, ast::Label { .. }));
22///     Ok(())
23/// })?;
24/// # Ok::<_, rune::support::Error>(())
25/// ```
26#[derive(Debug, TryClone, Clone, Copy, PartialEq, Eq, Spanned)]
27#[try_clone(copy)]
28#[non_exhaustive]
29pub struct Label {
30    /// The token of the label.
31    pub span: Span,
32    /// The source of the label.
33    #[rune(skip)]
34    pub source: ast::LitSource,
35}
36
37impl ToAst for Label {
38    fn to_ast(span: Span, kind: ast::Kind) -> Result<Self> {
39        match kind {
40            K!['label(source)] => Ok(Self { span, source }),
41            _ => Err(compile::Error::expected(
42                ast::Token { span, kind },
43                Self::into_expectation(),
44            )),
45        }
46    }
47
48    #[inline]
49    fn matches(kind: &ast::Kind) -> bool {
50        matches!(kind, K!['label])
51    }
52
53    #[inline]
54    fn into_expectation() -> Expectation {
55        Expectation::Description("a label")
56    }
57}
58
59impl Parse for Label {
60    fn parse(p: &mut Parser<'_>) -> Result<Self> {
61        let t = p.next()?;
62        Self::to_ast(t.span, t.kind)
63    }
64}
65
66impl Peek for Label {
67    fn peek(p: &mut Peeker<'_>) -> bool {
68        matches!(p.nth(0), K!['label])
69    }
70}
71
72impl<'a> Resolve<'a> for Label {
73    type Output = &'a str;
74
75    fn resolve(&self, cx: ResolveContext<'a>) -> Result<&'a str> {
76        let span = self.span;
77
78        match self.source {
79            ast::LitSource::Text(source_id) => {
80                let ident = cx
81                    .sources
82                    .source(source_id, span.trim_start(1u32))
83                    .ok_or_else(|| compile::Error::new(span, ErrorKind::BadSlice))?;
84
85                Ok(ident)
86            }
87            ast::LitSource::Synthetic(id) => {
88                let ident = cx.storage.get_string(id).ok_or_else(|| {
89                    compile::Error::new(
90                        span,
91                        ErrorKind::BadSyntheticId {
92                            kind: SyntheticKind::Ident,
93                            id,
94                        },
95                    )
96                })?;
97
98                Ok(ident)
99            }
100            ast::LitSource::BuiltIn(builtin) => Ok(builtin.as_str()),
101        }
102    }
103}
104
105impl ToTokens for Label {
106    fn to_tokens(
107        &self,
108        _: &mut MacroContext<'_, '_, '_>,
109        stream: &mut TokenStream,
110    ) -> alloc::Result<()> {
111        stream.push(ast::Token {
112            span: self.span,
113            kind: ast::Kind::Label(self.source),
114        })
115    }
116}