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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! Context for a macro.

use std::fmt;

use crate::ast;
use crate::ast::Span;
use crate::compile::{
    IrCompiler, IrError, IrEval, IrEvalContext, IrValue, ItemMeta, NoopCompileVisitor, Pool,
    Prelude, UnitBuilder,
};
use crate::macros::{IntoLit, Storage, ToTokens, TokenStream};
use crate::parse::{Parse, ParseError, ParseErrorKind, Resolve, ResolveError};
use crate::query::Query;
use crate::shared::{Consts, Gen};
use crate::{Source, SourceId, Sources};

/// Context for a running macro.
pub struct MacroContext<'a> {
    /// Macro span of the full macro call.
    pub(crate) macro_span: Span,
    /// Macro span of the stream.
    pub(crate) stream_span: Span,
    /// The item where the macro is being evaluated.
    pub(crate) item_meta: ItemMeta,
    /// Accessible query required to run the IR interpreter and has access to
    /// storage.
    pub(crate) q: Query<'a>,
}

impl<'a> MacroContext<'a> {
    /// Construct an empty macro context which can be used for testing.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::macros::MacroContext;
    ///
    /// MacroContext::test(|ctx| ());
    /// ```
    pub fn test<F, O>(f: F) -> O
    where
        F: FnOnce(&mut MacroContext<'_>) -> O,
    {
        let mut unit = UnitBuilder::default();
        let prelude = Prelude::default();
        let gen = Gen::default();
        let mut consts = Consts::default();
        let mut storage = Storage::default();
        let mut sources = Sources::default();
        let mut pool = Pool::default();
        let mut visitor = NoopCompileVisitor::new();
        let mut inner = Default::default();

        let mut query = Query::new(
            &mut unit,
            &prelude,
            &mut consts,
            &mut storage,
            &mut sources,
            &mut pool,
            &mut visitor,
            &gen,
            &mut inner,
        );

        let mut ctx = MacroContext {
            macro_span: Span::empty(),
            stream_span: Span::empty(),
            item_meta: Default::default(),
            q: query.borrow(),
        };

        f(&mut ctx)
    }

    /// Evaluate the given target as a constant expression.
    ///
    /// # Panics
    ///
    /// This will panic if it's called outside of a macro context.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::ast;
    /// use rune::macros::{MacroContext, quote};
    /// use rune::parse::{Parser};
    ///
    /// // Note: should only be used for testing.
    /// MacroContext::test(|ctx| {
    ///     let stream = quote!(1 + 2).into_token_stream(ctx);
    ///
    ///     let mut p = Parser::from_token_stream(&stream, ctx.stream_span());
    ///     let expr = p.parse_all::<ast::Expr>().unwrap();
    ///     let value = ctx.eval(&expr).unwrap();
    ///
    ///     assert_eq!(3, value.into_integer::<u32>().unwrap());
    /// });
    /// ```
    pub fn eval<T>(&mut self, target: &T) -> Result<IrValue, IrError>
    where
        T: IrEval,
    {
        let mut ctx = IrEvalContext {
            c: IrCompiler {
                source_id: self.item_meta.location.source_id,
                q: self.q.borrow(),
            },
            item: &self.item_meta,
        };

        target.eval(&mut ctx)
    }

    /// Construct a new literal from within a macro context.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::ast;
    /// use rune::macros::MacroContext;
    ///
    /// MacroContext::test(|ctx| {
    ///     let lit = ctx.lit("hello world");
    ///     assert!(matches!(lit, ast::Lit::Str(..)))
    /// });
    /// ```
    pub fn lit<T>(&mut self, lit: T) -> ast::Lit
    where
        T: IntoLit,
    {
        T::into_lit(lit, self)
    }

    /// Construct a new identifier from the given string from inside of a macro
    /// context.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::ast;
    /// use rune::macros::MacroContext;
    ///
    /// MacroContext::test(|ctx| {
    ///     let lit = ctx.ident("foo");
    ///     assert!(matches!(lit, ast::Ident { .. }))
    /// });
    /// ```
    pub fn ident(&mut self, ident: &str) -> ast::Ident {
        let span = self.macro_span();
        let id = self.q.storage.insert_str(ident);
        let source = ast::LitSource::Synthetic(id);
        ast::Ident { span, source }
    }

    /// Construct a new label from the given string. The string should be
    /// specified *without* the leading `'`, so `"foo"` instead of `"'foo"`.
    ///
    /// This constructor does not panic when called outside of a macro context
    /// but requires access to a `span` and `storage`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::ast;
    /// use rune::macros::MacroContext;
    ///
    /// MacroContext::test(|ctx| {
    ///     let lit = ctx.label("foo");
    ///     assert!(matches!(lit, ast::Label { .. }))
    /// });
    /// ```
    pub fn label(&mut self, label: &str) -> ast::Label {
        let span = self.macro_span();
        let id = self.q.storage.insert_str(label);
        let source = ast::LitSource::Synthetic(id);
        ast::Label { span, source }
    }

    /// Stringify the token stream.
    pub fn stringify<T>(&mut self, tokens: &T) -> Stringify<'_, 'a>
    where
        T: ToTokens,
    {
        let mut stream = TokenStream::new();
        tokens.to_tokens(self, &mut stream);
        Stringify { ctx: self, stream }
    }

    /// Resolve the value of a token.
    pub fn resolve<'r, T>(&'r self, item: T) -> Result<T::Output, ResolveError>
    where
        T: Resolve<'r>,
    {
        item.resolve(resolve_context!(self.q))
    }

    /// Access a literal source as a string.
    pub(crate) fn literal_source(&self, source: ast::LitSource, span: Span) -> Option<&str> {
        match source {
            ast::LitSource::Text(source_id) => self.q.sources.source(source_id, span),
            ast::LitSource::Synthetic(id) => self.q.storage.get_string(id),
            ast::LitSource::BuiltIn(builtin) => Some(builtin.as_str()),
        }
    }

    /// Insert the given source so that it has a [SourceId] that can be used in
    /// combination with parsing functions such as
    /// [parse_source][MacroContext::parse_source].
    pub fn insert_source(&mut self, name: &str, source: &str) -> SourceId {
        self.q.sources.insert(Source::new(name, source))
    }

    /// Parse the given input as the given type that implements
    /// [Parse][crate::parse::Parse].
    pub fn parse_source<T>(&self, id: SourceId) -> Result<T, ParseError>
    where
        T: Parse,
    {
        let source = self.q.sources.get(id).ok_or_else(|| {
            ParseError::new(
                Span::empty(),
                ParseErrorKind::MissingSourceId { source_id: id },
            )
        })?;

        crate::parse::parse_all(source.as_str(), id, false)
    }

    /// The span of the macro call including the name of the macro.
    ///
    /// If the macro call was `stringify!(a + b)` this would refer to the whole
    /// macro call.
    pub fn macro_span(&self) -> Span {
        self.macro_span
    }

    /// The span of the macro stream (the argument).
    ///
    /// If the macro call was `stringify!(a + b)` this would refer to `a + b`.
    pub fn stream_span(&self) -> Span {
        self.stream_span
    }
}

pub struct Stringify<'ctx, 'a> {
    ctx: &'ctx MacroContext<'a>,
    stream: TokenStream,
}

impl fmt::Display for Stringify<'_, '_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut it = self.stream.iter();
        let last = it.next_back();

        for token in it {
            token.token_fmt(self.ctx, f)?;
            write!(f, " ")?;
        }

        if let Some(last) = last {
            last.token_fmt(self.ctx, f)?;
        }

        Ok(())
    }
}