Struct rune::macros::MacroContext

source ·
pub struct MacroContext<'a, 'b, 'arena> { /* private fields */ }
Expand description

Context for a running macro.

Implementations§

source§

impl<'a, 'b, 'arena> MacroContext<'a, 'b, 'arena>

source

pub fn eval(&mut self, target: &Expr) -> Result<Value>

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::{self, quote};
use rune::parse::{Parser};

macros::test(|cx| {
    let stream = quote!(1 + 2).into_token_stream(cx)?;

    let mut p = Parser::from_token_stream(&stream, cx.input_span());
    let expr = p.parse_all::<ast::Expr>()?;
    let value = cx.eval(&expr)?;

    let integer = value.into_integer::<u32>().context("Expected integer")?;
    assert_eq!(3, integer);
    Ok(())
})?;
source

pub fn lit<T>(&mut self, lit: T) -> Result<Lit>
where T: IntoLit,

Construct a new literal from within a macro context.

§Examples
use rune::ast;
use rune::macros;

macros::test(|cx| {
    let lit = cx.lit("hello world")?;
    assert!(matches!(lit, ast::Lit::Str(..)));
    Ok(())
})?;
source

pub fn ident(&mut self, ident: &str) -> Result<Ident>

Construct a new identifier from the given string from inside of a macro context.

§Examples
use rune::ast;
use rune::macros;

macros::test(|cx| {
    let lit = cx.ident("foo")?;
    assert!(matches!(lit, ast::Ident { .. }));
    Ok(())
})?;
source

pub fn label(&mut self, label: &str) -> Result<Label>

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;

macros::test(|cx| {
    let lit = cx.label("foo")?;
    assert!(matches!(lit, ast::Label { .. }));
    Ok(())
})?;
source

pub fn stringify<T>( &mut self, tokens: &T ) -> Result<Stringify<'_, 'a, 'b, 'arena>>
where T: ToTokens,

Stringify the token stream.

source

pub fn resolve<'r, T>(&'r self, item: T) -> Result<T::Output>
where T: Resolve<'r>,

Resolve the value of a token.

source

pub fn insert_source(&mut self, name: &str, source: &str) -> Result<SourceId>

Insert the given source so that it has a SourceId that can be used in combination with parsing functions such as parse_source.

source

pub fn parse_source<T>(&self, id: SourceId) -> Result<T>
where T: Parse,

Parse the given input as the given type that implements Parse.

source

pub fn macro_span(&self) -> Span

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.

source

pub fn input_span(&self) -> Span

The span of the macro stream (the argument).

If the macro call was stringify!(a + b) this would refer to a + b.

Auto Trait Implementations§

§

impl<'a, 'b, 'arena> !RefUnwindSafe for MacroContext<'a, 'b, 'arena>

§

impl<'a, 'b, 'arena> !Send for MacroContext<'a, 'b, 'arena>

§

impl<'a, 'b, 'arena> !Sync for MacroContext<'a, 'b, 'arena>

§

impl<'a, 'b, 'arena> Unpin for MacroContext<'a, 'b, 'arena>

§

impl<'a, 'b, 'arena> !UnwindSafe for MacroContext<'a, 'b, 'arena>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more