Expand description
Abstract syntax trees for the Rune language.
These are primarily made available for use in macros, where the input to the macro needs to be parsed so that it can be processed.
Below we define a macro capable of taking identifiers like hello
, and
turning them into literal strings like "hello"
.
use rune::{Context, FromValue, Module, Vm};
use rune::ast;
use rune::compile;
use rune::macros::{quote, MacroContext, TokenStream};
use rune::parse::Parser;
use rune::alloc::prelude::*;
use std::sync::Arc;
#[rune::macro_]
fn ident_to_string(cx: &mut MacroContext<'_, '_, '_>, stream: &TokenStream) -> compile::Result<TokenStream> {
let mut p = Parser::from_token_stream(stream, cx.input_span());
let ident = p.parse_all::<ast::Ident>()?;
let ident = cx.resolve(ident)?.try_to_owned()?;
let string = cx.lit(&ident)?;
Ok(quote!(#string).into_token_stream(cx)?)
}
let mut m = Module::new();
m.macro_meta(ident_to_string)?;
let mut context = Context::new();
context.install(m)?;
let runtime = Arc::new(context.runtime()?);
let mut sources = rune::sources! {
entry => {
pub fn main() {
ident_to_string!(hello)
}
}
};
let unit = rune::prepare(&mut sources)
.with_context(&context)
.build()?;
let unit = Arc::new(unit);
let mut vm = Vm::new(runtime, unit);
let value = vm.call(["main"], ())?;
let value: String = rune::from_value(value)?;
assert_eq!(value, "hello");
Re-exports§
pub use self::spanned::OptionSpanned;
pub use self::spanned::Spanned;
Macros§
- Helper macro to reference a specific token kind, or short sequence of kinds.
- Helper macro to reference a specific token.
Structs§
- The
abstract
keyword. - The
alignof
keyword. &
.&&
.&=
.- Parse something bracketed, that is separated by
<(T, S?)*>
. ->
.- The
as
keyword. - The
async
keyword. @
.- Attributes like:
- The
await
keyword. !
.!=
.- The
become
keyword. - A block of statements.
- Parse something braced, that is separated by
{(T, S?)*}
. - Parse something bracketed, that is separated by
[(T, S?)*]
. - The
break
keyword. - A single index in a Span, like the start or ending index.
^
.^=
.- closing brace
- closing bracket
- closing marker
- closing parenthesis
:
.::
.,
.- The
const
keyword. - The
continue
keyword. - The
crate
keyword. -
.-=
.- The
default
keyword. /
.- The
do
keyword. $
..
...
...=
.- The
else
keyword. - A block of statements.
- The
enum
keyword. =
.==
.- An
= ...
e.g. inside an attribute#[doc = ...]
. - An assign expression.
- An await expression.
- A binary expression.
- A block expression.
- A break expression.
- A call expression.
- A closure expression.
- A
continue
statement. - A single selection branch.
- An else branch of an if expression.
- An else branch of an if expression.
- A prioritized expression group without delimiters
<expr>
. - A field access.
- A
for
loop over an iterator. - A prioritized expression group.
- A conditional
if
expression. - An index get operation.
- A let expression.
- A literal expression. With the addition of being able to receive attributes, this is identical to ast::Lit.
- A
loop
expression. - A match expression.
- A match branch.
- An object expression.
- A range expression.
- A return expression.
- A
select
expression that selects over a collection of futures. - A single selection branch.
- A try expression.
- An expression to construct a literal tuple.
- A unary expression.
- A literal vector.
- A
while
loop. - A
yield
expression to return a value from a generator. - The
extern
keyword. - The
false
keyword. - A field as part of a struct or a tuple body.
- A single field assignment in an object expression.
- A rune file.
- The
final
keyword. - The
fn
keyword. - The
for
keyword. - Parses
[{( ... )}]
ensuring that the delimiter is balanced. >
.>=
.>>
.>>=
.- An identifier, like
foo
orHello
. - The
if
keyword. - The
impl
keyword. - The
in
keyword. - The
is
keyword. - The composite
is not
operation. - A const declaration.
- An enum item.
- A function item.
- An impl item.
- A module declaration.
- A module item.
- A struct item.
- A
use
item. - A single use declaration path.
- An enum variant.
- A label, like
'foo
. - The
let
keyword. - The boolean literal.
- A byte literal.
- A string literal.
- A character literal.
- A number literal.
- A string literal.
- A local variable declaration.
- The
loop
keyword. <
.<=
.<<
.<<=
.- The
macro
keyword. - A macro call.
- The
match
keyword. - The
mod
keyword. - The
move
keyword. - The
mut
keyword. - The
not
keyword. - A resolved number literal.
- Configuration of a text number.
- The
offsetof
keyword. - opening brace
- opening bracket
- opening marker
- opening parenthesis
- The
override
keyword. - Parse something parenthesis, that is separated by
((T, S?)*)
. - An object item.
- An ignore pattern.
- A literal pattern.
- An object pattern.
- A path pattern.
- The rest pattern
..
and associated attributes. - A tuple pattern.
- An array pattern.
- A path, where each element is separated by a
::
. - Used to parse an expression without supporting an immediate binary expression.
%
.%=
.|
.- |=`.
||
.+
.+=
.#
.- The
priv
keyword. - The
proc
keyword. - The
pub
keyword. - The
pure
keyword. ?
.- The
ref
keyword. - The
return
keyword. =>
.- The
select
keyword. - The
Self
keyword. - The
self
keyword. ;
.- The shebang of a file.
- The
sizeof
keyword. /=
.- A span corresponding to a range in the source file being parsed.
*
.*=
.- The
static
keyword. - A semi-terminated expression.
- Configuration for a literal string.
- The
struct
keyword. - The
super
keyword. ~
.- A single token encountered during parsing.
- The
true
keyword. - The
typeof
keyword. _
.- The
unsafe
keyword. - The
use
keyword. - The
virtual
keyword. - The
while
keyword. - The
yield
keyword.
Enums§
- Whether or not the attribute is an outer
#!
or inner#
attribute - A binary operation.
- A built-in identifiers that do not have a source.
- The condition in an if statement.
- The source of an item that implements Copy.
- A delimiter,
{
,{
, or[
. - A rune expression.
- Representation of closure arguments.
- The field being accessed.
- The limits of the specified range.
- A single selection branch.
- An item body declaration.
- A single argument in a closure.
- A declaration.
- An item body.
- Parsing an item or an expression.
- A use component.
- The kind of the token.
- A literal value,
- The kind of the identifier.
- The kind of a number literal.
- The literal size of a number.
- The source of a number.
- The suffix of a number.
- The value of a number literal.
- A literal object identifier.
- Possible literal object keys.
- A pattern match.
- An identified path kind.
- Part of a
::
separated path. - A statement within a block.
- Key used to stort a statement into its processing order.
- The source of the literal string. This need to be treated separately from LitSource because it might encompass special things like quoting and escaping.
- A unary operation.
- Visibility level restricted to some path.
Traits§
- Types for which we can optionally get a span.
- Types for which we can get a span.