rune/macros/macro_context.rs
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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
//! Context for a macro.
use core::fmt;
use crate::alloc;
use crate::ast;
use crate::ast::Span;
use crate::compile::{self, ErrorKind, ItemMeta};
use crate::indexing::Indexer;
use crate::macros::{IntoLit, ToTokens, TokenStream};
use crate::parse::{Parse, Resolve};
use crate::runtime::Value;
use crate::{Source, SourceId};
/// Construct an empty macro context which can be used for testing.
///
/// # Examples
///
/// ```
/// use rune::ast;
/// use rune::macros;
///
/// macros::test(|cx| {
/// let lit = cx.lit("hello world")?;
/// assert!(matches!(lit, ast::Lit::Str(..)));
/// Ok(())
/// })?;
/// # Ok::<_, rune::support::Error>(())
/// ```
#[cfg(feature = "std")]
#[cfg_attr(rune_docsrs, doc(cfg(feature = "std")))]
pub fn test<F, O>(f: F) -> crate::support::Result<O>
where
F: FnOnce(&mut MacroContext<'_, '_, '_>) -> crate::support::Result<O>,
{
use rust_alloc::rc::Rc;
use crate::compile::{NoopCompileVisitor, NoopSourceLoader, Pool, Prelude, UnitBuilder};
use crate::hir;
use crate::indexing::{IndexItem, Items, Scopes};
use crate::macros::Storage;
use crate::query::Query;
use crate::shared::{Consts, Gen};
use crate::support::Context as _;
use crate::{Context, Diagnostics, Item, Options, Sources};
let mut unit = UnitBuilder::default();
let prelude = Prelude::default();
let gen = Gen::default();
let const_arena = hir::Arena::new();
let mut consts = Consts::default();
let mut storage = Storage::default();
let mut sources = Sources::default();
let mut pool = Pool::new().context("Failed to allocate pool")?;
let mut visitor = NoopCompileVisitor::new();
let mut diagnostics = Diagnostics::default();
let mut source_loader = NoopSourceLoader::default();
let options = Options::from_default_env()?;
let context = Context::default();
let mut inner = Default::default();
let mut query = Query::new(
&mut unit,
&prelude,
&const_arena,
&mut consts,
&mut storage,
&mut sources,
&mut pool,
&mut visitor,
&mut diagnostics,
&mut source_loader,
&options,
&gen,
&context,
&mut inner,
);
let source_id = SourceId::empty();
let (root_id, root_mod_id) = query
.insert_root_mod(source_id, Span::empty())
.context("Failed to inserted root module")?;
let item_meta = query
.item_for("root item", root_id)
.context("Just inserted item meta does not exist")?;
let tree = Rc::default();
let mut idx = Indexer {
q: query.borrow(),
source_id,
items: Items::new(Item::new()).context("Failed to construct items")?,
scopes: Scopes::new().context("Failed to build indexer scopes")?,
item: IndexItem::new(root_mod_id, root_id),
nested_item: None,
macro_depth: 0,
root: None,
queue: None,
loaded: None,
tree: &tree,
};
let mut cx = MacroContext {
macro_span: Span::empty(),
input_span: Span::empty(),
item_meta,
idx: &mut idx,
};
f(&mut cx)
}
/// Context for a running macro.
pub struct MacroContext<'a, 'b, 'arena> {
/// Macro span of the full macro call.
pub(crate) macro_span: Span,
/// Macro span of the input.
pub(crate) input_span: Span,
/// The item where the macro is being evaluated.
pub(crate) item_meta: ItemMeta,
/// Indexer.
pub(crate) idx: &'a mut Indexer<'b, 'arena>,
}
impl<'a, 'b, 'arena> MacroContext<'a, 'b, 'arena> {
/// Evaluate the given target as a constant expression.
///
/// # Panics
///
/// This will panic if it's called outside of a macro context.
///
/// # Examples
///
/// ```
/// # use rune::support::*;
/// 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.as_integer::<u32>().context("Expected integer")?;
/// assert_eq!(3, integer);
/// Ok(())
/// })?;
/// # Ok::<_, rune::support::Error>(())
/// ```
pub fn eval(&mut self, target: &ast::Expr) -> compile::Result<Value> {
target.eval(self)
}
/// 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(())
/// })?;
/// # Ok::<_, rune::support::Error>(())
/// ```
pub fn lit<T>(&mut self, lit: T) -> alloc::Result<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;
///
/// macros::test(|cx| {
/// let lit = cx.ident("foo")?;
/// assert!(matches!(lit, ast::Ident { .. }));
/// Ok(())
/// })?;
/// # Ok::<_, rune::support::Error>(())
/// ```
pub fn ident(&mut self, ident: &str) -> alloc::Result<ast::Ident> {
let span = self.macro_span();
let id = self.idx.q.storage.insert_str(ident)?;
let source = ast::LitSource::Synthetic(id);
Ok(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;
///
/// macros::test(|cx| {
/// let lit = cx.label("foo")?;
/// assert!(matches!(lit, ast::Label { .. }));
/// Ok(())
/// })?;
/// # Ok::<_, rune::support::Error>(())
/// ```
pub fn label(&mut self, label: &str) -> alloc::Result<ast::Label> {
let span = self.macro_span();
let id = self.idx.q.storage.insert_str(label)?;
let source = ast::LitSource::Synthetic(id);
Ok(ast::Label { span, source })
}
/// Stringify the token stream.
pub fn stringify<T>(&mut self, tokens: &T) -> alloc::Result<Stringify<'_, 'a, 'b, 'arena>>
where
T: ToTokens,
{
let mut stream = TokenStream::new();
tokens.to_tokens(self, &mut stream)?;
Ok(Stringify { cx: self, stream })
}
/// Resolve the value of a token.
pub fn resolve<'r, T>(&'r self, item: T) -> compile::Result<T::Output>
where
T: Resolve<'r>,
{
item.resolve(resolve_context!(self.idx.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.idx.q.sources.source(source_id, span),
ast::LitSource::Synthetic(id) => self.idx.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) -> alloc::Result<SourceId> {
self.idx.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) -> compile::Result<T>
where
T: Parse,
{
let source = self.idx.q.sources.get(id).ok_or_else(|| {
compile::Error::new(Span::empty(), ErrorKind::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 input_span(&self) -> Span {
self.input_span
}
}
pub struct Stringify<'cx, 'a, 'b, 'arena> {
cx: &'cx MacroContext<'a, 'b, 'arena>,
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.cx, f)?;
write!(f, " ")?;
}
if let Some(last) = last {
last.token_fmt(self.cx, f)?;
}
Ok(())
}
}