rune/modules/macros/
builtin.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
//! Built-in macros.

use crate as rune;
use crate::compile;
use crate::macros::{quote, MacroContext, TokenStream};
use crate::parse::Parser;
use crate::{ContextError, Module};

/// Built-in macros.
#[rune::module(::std::macros::builtin)]
pub fn module() -> Result<Module, ContextError> {
    let mut builtins = Module::from_meta(self::module_meta)?.with_unique("std::macros::builtin");
    builtins.macro_meta(file)?;
    builtins.macro_meta(line)?;
    Ok(builtins)
}

/// Return the line in the current file.
///
/// # Examples
///
/// ```rune
/// println!("{}:{}: Something happened", file!(), line!());
/// ```
#[rune::macro_]
pub(crate) fn line(
    cx: &mut MacroContext<'_, '_, '_>,
    stream: &TokenStream,
) -> compile::Result<TokenStream> {
    use crate as rune;

    let mut parser = Parser::from_token_stream(stream, cx.input_span());
    parser.eof()?;

    let stream = quote!(
        #[builtin]
        line!()
    );

    Ok(stream.into_token_stream(cx)?)
}

/// Return the name of the current file.
///
/// # Examples
///
/// ```rune
/// println!("{}:{}: Something happened", file!(), line!());
/// ```
#[rune::macro_]
pub(crate) fn file(
    cx: &mut MacroContext<'_, '_, '_>,
    stream: &TokenStream,
) -> compile::Result<TokenStream> {
    use crate as rune;

    let mut parser = Parser::from_token_stream(stream, cx.input_span());
    parser.eof()?;

    let stream = quote!(
        #[builtin]
        file!()
    );

    Ok(stream.into_token_stream(cx)?)
}