Skip to main content

rune/macros/
macro_context.rs

1//! Context for a macro.
2
3use core::fmt;
4
5use crate::alloc;
6use crate::alloc::Vec;
7use crate::ast;
8use crate::ast::{OptionSpanned, Span};
9use crate::compile::{self, ErrorKind, ItemMeta};
10use crate::grammar::ws;
11use crate::indexing::Indexer;
12use crate::internal_macros::resolve_context;
13use crate::macros::{IntoLit, ToTokens, TokenStream};
14use crate::parse::{Parse, Parser, Resolve};
15use crate::runtime::Value;
16use crate::{Source, SourceId};
17
18/// Construct an empty macro context which can be used for testing.
19///
20/// # Examples
21///
22/// ```
23/// use rune::ast;
24/// use rune::macros;
25///
26/// macros::test(|cx| {
27///     let lit = cx.lit("hello world")?;
28///     assert!(matches!(lit, ast::Lit::Str(..)));
29///     Ok(())
30/// })?;
31/// # Ok::<_, rune::support::Error>(())
32/// ```
33#[cfg(all(feature = "std", feature = "anyhow"))]
34#[cfg_attr(rune_docsrs, doc(cfg(all(feature = "std", feature = "anyhow"))))]
35pub fn test<F, O>(f: F) -> crate::support::Result<O>
36where
37    F: FnOnce(&mut MacroContext<'_, '_, '_>) -> crate::support::Result<O>,
38{
39    use rust_alloc::rc::Rc;
40
41    use crate::compile::{NoopCompileVisitor, NoopSourceLoader, Pool, Prelude, UnitBuilder};
42    use crate::hir;
43    use crate::indexing::{IndexItem, Items, Scopes};
44    use crate::macros::Storage;
45    use crate::query::Query;
46    use crate::shared::{Consts, Gen};
47    use crate::support::Context as _;
48    use crate::{Context, Diagnostics, Item, Options, Sources};
49
50    let mut unit = UnitBuilder::default();
51    let prelude = Prelude::default();
52    let gen = Gen::default();
53    let const_arena = hir::Arena::new();
54    let mut consts = Consts::default();
55    let mut storage = Storage::default();
56    let mut sources = Sources::default();
57    let mut pool = Pool::new().context("Failed to allocate pool")?;
58    let mut visitor = NoopCompileVisitor::new();
59    let mut diagnostics = Diagnostics::default();
60    let mut source_loader = NoopSourceLoader::default();
61    let options = Options::from_default_env()?;
62    let context = Context::default();
63    let mut inner = Default::default();
64
65    let mut query = Query::new(
66        &mut unit,
67        &prelude,
68        &const_arena,
69        &mut consts,
70        &mut storage,
71        &mut sources,
72        &mut pool,
73        &mut visitor,
74        &mut diagnostics,
75        &mut source_loader,
76        &options,
77        &[],
78        &gen,
79        &context,
80        &mut inner,
81    );
82
83    let source_id = SourceId::empty();
84
85    let (root_id, root_mod_id) = query
86        .insert_root_mod(source_id, Span::empty())
87        .context("Failed to inserted root module")?;
88
89    let item_meta = query
90        .item_for("root item", root_id)
91        .context("Just inserted item meta does not exist")?;
92
93    let tree = Rc::default();
94
95    let mut idx = Indexer {
96        q: query.borrow(),
97        source_id,
98        items: Items::new(Item::new()).context("Failed to construct items")?,
99        scopes: Scopes::new().context("Failed to build indexer scopes")?,
100        item: IndexItem::new(root_mod_id, root_id),
101        nested_item: None,
102        macro_depth: 0,
103        root: None,
104        queue: None,
105        loaded: None,
106        tree: &tree,
107    };
108
109    let mut cx = MacroContext {
110        macro_span: Span::empty(),
111        input_span: Span::empty(),
112        item_meta,
113        idx: &mut idx,
114    };
115
116    f(&mut cx)
117}
118
119/// Context for a running macro.
120pub struct MacroContext<'a, 'b, 'arena> {
121    /// Macro span of the full macro call.
122    pub(crate) macro_span: Span,
123    /// Macro span of the input.
124    pub(crate) input_span: Span,
125    /// The item where the macro is being evaluated.
126    pub(crate) item_meta: ItemMeta,
127    /// Indexer.
128    pub(crate) idx: &'a mut Indexer<'b, 'arena>,
129}
130
131impl<'a, 'b, 'arena> MacroContext<'a, 'b, 'arena> {
132    /// Construct a parser over a token stream, bounded by the compiler options
133    /// this macro is being expanded under.
134    ///
135    /// The syntax tree this parser produces is walked by recursing over it, so
136    /// how deep it is allowed to get is bounded by the `max-ast-depth` option.
137    /// A parser built with [`Parser::from_token_stream`] instead uses that
138    /// option's default, since it has no way of seeing what the compiler was
139    /// configured with.
140    ///
141    /// A macro which only needs to know where each of its arguments ends does
142    /// not need a tree at all - see [`MacroContext::exprs`], which splits an
143    /// input without either recursing over it or holding it to that bound.
144    ///
145    /// `span` is the span to use if the stream is empty - typically
146    /// [`MacroContext::input_span`].
147    ///
148    /// # Examples
149    ///
150    /// ```
151    /// # use rune::support::*;
152    /// use rune::ast;
153    /// use rune::macros::{self, quote};
154    ///
155    /// macros::test(|cx| {
156    ///     let stream = quote!(1 + 2).into_token_stream(cx)?;
157    ///
158    ///     let mut p = cx.parser(&stream, cx.input_span());
159    ///     let expr = p.parse_all::<ast::Expr>()?;
160    ///     let value = cx.eval(&expr)?;
161    ///
162    ///     let integer = value.as_integer::<u32>().context("Expected integer")?;
163    ///     assert_eq!(3, integer);
164    ///     Ok(())
165    /// })?;
166    /// # Ok::<_, rune::support::Error>(())
167    /// ```
168    pub fn parser<'s>(&self, token_stream: &'s TokenStream, span: Span) -> Parser<'s> {
169        Parser::from_token_stream(token_stream, span)
170            .with_max_depth(self.idx.q.options.max_ast_depth)
171    }
172
173    /// Split a token stream into the comma separated expressions it is made
174    /// of, each one being the tokens it was written as.
175    ///
176    /// This is what a macro which takes a list of arguments uses to find where
177    /// each one ends. The split is done by the same parser the compiler uses,
178    /// which walks its input over an explicit stack, and each expression is
179    /// handed back as tokens rather than as a syntax tree - so a macro built
180    /// out of this neither recurses over its own input nor holds it to the
181    /// much smaller `max-ast-depth` which bounds [`MacroContext::parser`].
182    ///
183    /// What comes back is where each argument ends rather than what it means:
184    /// the tokens are handed on as they were written, and whatever they turn
185    /// out to say is reported where the macro puts them, since that is where
186    /// they are lowered.
187    ///
188    /// # Examples
189    ///
190    /// ```
191    /// # use rune::support::*;
192    /// use rune::macros::{self, quote};
193    ///
194    /// macros::test(|cx| {
195    ///     let stream = quote!("Hello {}", 1 + 2).into_token_stream(cx)?;
196    ///
197    ///     let exprs = cx.exprs(&stream)?;
198    ///     assert_eq!(exprs.len(), 2);
199    ///
200    ///     let value = cx.eval_stream(&exprs[1])?;
201    ///     assert_eq!(value.as_integer::<u32>()?, 3);
202    ///     Ok(())
203    /// })?;
204    /// # Ok::<_, rune::support::Error>(())
205    /// ```
206    pub fn exprs(&mut self, stream: &TokenStream) -> compile::Result<Vec<TokenStream>> {
207        let span = self.stream_span(stream);
208
209        let tree = crate::grammar::token_stream(stream)
210            .max_nesting(self.idx.q.options.max_depth)
211            .exprs(ast::Kind::Comma)?;
212
213        let Some([root]) = tree.nodes() else {
214            return Err(compile::Error::msg(span, "expected a single root"));
215        };
216
217        let mut exprs = Vec::new();
218
219        // Whether the expression read most recently is still waiting for the
220        // separator which ends it, which is what tells a missing separator
221        // apart from a missing expression.
222        let mut separated = true;
223
224        for node in root.children() {
225            if node.is_empty() {
226                match node.kind() {
227                    ws!() => {}
228                    ast::Kind::Comma if !separated => {
229                        separated = true;
230                    }
231                    _ => {
232                        return Err(compile::Error::msg(node.span(), "expected an expression"));
233                    }
234                }
235
236                continue;
237            }
238
239            if !separated {
240                return Err(compile::Error::msg(node.span(), "expected `,`"));
241            }
242
243            let mut expr = TokenStream::new();
244
245            for token in node.walk_tokens() {
246                expr.push(token)?;
247            }
248
249            exprs.try_push(expr)?;
250            separated = false;
251        }
252
253        Ok(exprs)
254    }
255
256    /// Evaluate the tokens of an expression as a constant.
257    ///
258    /// This is [`MacroContext::eval`] over the tokens an expression was
259    /// written as, which is what a macro that split its input with
260    /// [`MacroContext::exprs`] holds.
261    ///
262    /// # Examples
263    ///
264    /// ```
265    /// # use rune::support::*;
266    /// use rune::macros::{self, quote};
267    ///
268    /// macros::test(|cx| {
269    ///     let stream = quote!(1 + 2).into_token_stream(cx)?;
270    ///
271    ///     let value = cx.eval_stream(&stream)?;
272    ///     assert_eq!(value.as_integer::<u32>()?, 3);
273    ///     Ok(())
274    /// })?;
275    /// # Ok::<_, rune::support::Error>(())
276    /// ```
277    pub fn eval_stream(&mut self, stream: &TokenStream) -> compile::Result<Value> {
278        self.eval_stream_as(stream, "Expression")
279    }
280
281    /// Evaluate a token stream as a constant expression, as
282    /// [`MacroContext::eval_stream`] does, describing it as `what` if it turns
283    /// out not to be constant.
284    pub(crate) fn eval_stream_as(
285        &mut self,
286        stream: &TokenStream,
287        what: &'static str,
288    ) -> compile::Result<Value> {
289        let span = self.stream_span(stream);
290        crate::compile::const_eval::eval_stream(self, stream, span, what)
291    }
292
293    /// The span of a token stream, which is the span of the input the macro
294    /// was called with if the stream is empty.
295    pub(crate) fn stream_span(&self, stream: &TokenStream) -> Span {
296        stream.option_span().unwrap_or(self.input_span)
297    }
298
299    /// Evaluate the given target as a constant expression.
300    ///
301    /// # Panics
302    ///
303    /// This will panic if it's called outside of a macro context.
304    ///
305    /// # Examples
306    ///
307    /// ```
308    /// # use rune::support::*;
309    /// use rune::ast;
310    /// use rune::macros::{self, quote};
311    /// use rune::parse::{Parser};
312    ///
313    /// macros::test(|cx| {
314    ///     let stream = quote!(1 + 2).into_token_stream(cx)?;
315    ///
316    ///     let mut p = Parser::from_token_stream(&stream, cx.input_span());
317    ///     let expr = p.parse_all::<ast::Expr>()?;
318    ///     let value = cx.eval(&expr)?;
319    ///
320    ///     let integer = value.as_integer::<u32>().context("Expected integer")?;
321    ///     assert_eq!(3, integer);
322    ///     Ok(())
323    /// })?;
324    /// # Ok::<_, rune::support::Error>(())
325    /// ```
326    pub fn eval(&mut self, target: &ast::Expr) -> compile::Result<Value> {
327        crate::compile::const_eval::eval_ast(self, target)
328    }
329
330    /// Construct a new literal from within a macro context.
331    ///
332    /// # Examples
333    ///
334    /// ```
335    /// use rune::ast;
336    /// use rune::macros;
337    ///
338    /// macros::test(|cx| {
339    ///     let lit = cx.lit("hello world")?;
340    ///     assert!(matches!(lit, ast::Lit::Str(..)));
341    ///     Ok(())
342    /// })?;
343    /// # Ok::<_, rune::support::Error>(())
344    /// ```
345    pub fn lit<T>(&mut self, lit: T) -> alloc::Result<ast::Lit>
346    where
347        T: IntoLit,
348    {
349        T::into_lit(lit, self)
350    }
351
352    /// Construct a new identifier from the given string from inside of a macro
353    /// context.
354    ///
355    /// # Examples
356    ///
357    /// ```
358    /// use rune::ast;
359    /// use rune::macros;
360    ///
361    /// macros::test(|cx| {
362    ///     let lit = cx.ident("foo")?;
363    ///     assert!(matches!(lit, ast::Ident { .. }));
364    ///     Ok(())
365    /// })?;
366    /// # Ok::<_, rune::support::Error>(())
367    /// ```
368    pub fn ident(&mut self, ident: &str) -> alloc::Result<ast::Ident> {
369        let span = self.macro_span();
370        let id = self.idx.q.storage.insert_str(ident)?;
371        let source = ast::LitSource::Synthetic(id);
372        Ok(ast::Ident { span, source })
373    }
374
375    /// Construct a new label from the given string. The string should be
376    /// specified *without* the leading `'`, so `"foo"` instead of `"'foo"`.
377    ///
378    /// This constructor does not panic when called outside of a macro context
379    /// but requires access to a `span` and `storage`.
380    ///
381    /// # Examples
382    ///
383    /// ```
384    /// use rune::ast;
385    /// use rune::macros;
386    ///
387    /// macros::test(|cx| {
388    ///     let lit = cx.label("foo")?;
389    ///     assert!(matches!(lit, ast::Label { .. }));
390    ///     Ok(())
391    /// })?;
392    /// # Ok::<_, rune::support::Error>(())
393    /// ```
394    pub fn label(&mut self, label: &str) -> alloc::Result<ast::Label> {
395        let span = self.macro_span();
396        let id = self.idx.q.storage.insert_str(label)?;
397        let source = ast::LitSource::Synthetic(id);
398        Ok(ast::Label { span, source })
399    }
400
401    /// Stringify the token stream.
402    pub fn stringify<T>(&mut self, tokens: &T) -> alloc::Result<Stringify<'_, 'a, 'b, 'arena>>
403    where
404        T: ToTokens,
405    {
406        let mut stream = TokenStream::new();
407        tokens.to_tokens(self, &mut stream)?;
408        Ok(Stringify { cx: self, stream })
409    }
410
411    /// Resolve the value of a token.
412    pub fn resolve<'r, T>(&'r self, item: T) -> compile::Result<T::Output>
413    where
414        T: Resolve<'r>,
415    {
416        item.resolve(resolve_context!(self.idx.q))
417    }
418
419    /// Access a literal source as a string.
420    pub(crate) fn literal_source(&self, source: ast::LitSource, span: Span) -> Option<&str> {
421        match source {
422            ast::LitSource::Text(source_id) => self.idx.q.sources.source(source_id, span),
423            ast::LitSource::Synthetic(id) => self.idx.q.storage.get_string(id),
424            ast::LitSource::BuiltIn(builtin) => Some(builtin.as_str()),
425        }
426    }
427
428    /// Insert the given source so that it has a [SourceId] that can be used in
429    /// combination with parsing functions such as
430    /// [parse_source][MacroContext::parse_source].
431    pub fn insert_source(&mut self, name: &str, source: &str) -> alloc::Result<SourceId> {
432        self.idx.q.sources.insert(Source::new(name, source)?)
433    }
434
435    /// Parse the given input as the given type that implements
436    /// [Parse][crate::parse::Parse].
437    pub fn parse_source<T>(&self, id: SourceId) -> compile::Result<T>
438    where
439        T: Parse,
440    {
441        let source = self.idx.q.sources.get(id).ok_or_else(|| {
442            compile::Error::new(Span::empty(), ErrorKind::MissingSourceId { source_id: id })
443        })?;
444
445        crate::parse::parse_all(source.as_str(), id, false)
446    }
447
448    /// The span of the macro call including the name of the macro.
449    ///
450    /// If the macro call was `stringify!(a + b)` this would refer to the whole
451    /// macro call.
452    pub fn macro_span(&self) -> Span {
453        self.macro_span
454    }
455
456    /// The span of the macro stream (the argument).
457    ///
458    /// If the macro call was `stringify!(a + b)` this would refer to `a + b`.
459    pub fn input_span(&self) -> Span {
460        self.input_span
461    }
462}
463
464pub struct Stringify<'cx, 'a, 'b, 'arena> {
465    cx: &'cx MacroContext<'a, 'b, 'arena>,
466    stream: TokenStream,
467}
468
469impl fmt::Display for Stringify<'_, '_, '_, '_> {
470    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
471        let mut it = self.stream.iter();
472        let last = it.next_back();
473
474        for token in it {
475            token.token_fmt(self.cx, f)?;
476            write!(f, " ")?;
477        }
478
479        if let Some(last) = last {
480            last.token_fmt(self.cx, f)?;
481        }
482
483        Ok(())
484    }
485}