Module rune::ast[][src]

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::macros::{quote, MacroContext, TokenStream};
use rune::parse::Parser;
use std::sync::Arc;

fn ident_to_string(ctx: &mut MacroContext<'_>, stream: &TokenStream) -> rune::Result<TokenStream> {
    let mut p = Parser::from_token_stream(stream, ctx.stream_span());
    let ident = p.parse_all::<ast::Ident>()?;
    let ident = ctx.resolve(ident)?.to_owned();
    let string = ctx.lit(&ident);
    Ok(quote!(#string).into_token_stream(ctx))
}

let mut m = Module::new();
m.macro_(&["ident_to_string"], 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::from_value(value)?;

assert_eq!(value, "hello");

Macros

Helper macro to reference a specific token kind, or short sequence of kinds.

Helper macro to reference a specific token.

Structs

This file has been generated from assets\tokens.yaml DO NOT modify by hand! The abstract keyword.

The alignof keyword.

&.

&&.

&=.

Parse something bracketed, that is separated by <(T, S?)*>.

->.

The as keyword.

The async keyword.

@.

Attribute like #[derive(Debug)]

The await keyword.

!.

!=.

The become keyword.

A block of expressions.

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.

The enum keyword.

=.

==.

An assign expression a = b.

A return statement <expr>.await.

A binary expression.

A block expression.

A break statement: break [expr].

A function call <expr>(<args>).

A closure expression.

A continue statement: continue [label].

An else branch of an if expression.

An else branch of an if expression.

A prioritized expression group without delimiters <expr>.

A field access <expr>.<field>.

A for loop over an iterator: for i in [1, 2, 3] {}.

A prioritized expression group (<expr>).

An if statement: if cond { true } else { false }

An index get operation <target>[<index>].

A let expression let <name> = <expr>;

A literal expression. With the addition of being able to receive attributes, this is identical to ast::Lit.

A loop expression: loop { ... }.

A match expression.

A match branch.

Parse an object expression.

A range expression a .. b or a ..= b.

A return expression return [expr].

A select expression that selects over a collection of futures.

A try expression <expr>?.

An expression to construct a literal tuple.

A unary expression.

A literal vector.

A while loop: while [expr] { ... }.

A yield [expr] 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 literal object field.

A parsed file.

The final keyword.

The fn keyword.

The for keyword.

Helper to force an expression to have a specific semi-colon policy.

>.

>=.

>>.

>>=.

An identifier, like foo or Hello.“.

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 item.

A struct item.

A use item.

A single use declaration path, like foo::bar::{baz::*, biz}.

An enum variant.

A label, like 'foo

The let keyword.

The unit literal ().

A byte literal.

A string literal.

A character literal.

A number literal.

A string literal.

A local variable declaration let <pattern> = <expr>;

The loop keyword.

<.

<=.

<<.

<<=.

The macro keyword.

A function call <expr>!(<args>).

The match keyword.

The mod keyword.

The move keyword.

The not keyword.

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.

A literal pattern.

An object pattern.

A tuple pattern.

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.

An error with an associated span.

*.

*=.

The static keyword.

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

A binary operation.

A built-in identifiers that do not have a source.

An if condition.

The source of an item that implements Copy.

A delimiter, {, {, or [.

A rune expression.

Things that we can break on.

The field being accessed.

The limits of the specified range.

A single selection branch.

A single argument in a closure.

A declaration.

An item body.

Parsing an item or an expression.

AST for a struct body.

A use component.

An item body declaration.

The kind of the token.

A literal value,

The kind of the identifier.

A resolved number literal.

The kind of a number literal.

The source of a number.

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: pub(self) or pub(super) or pub(crate) or pub(in some::module).

Traits

Types for which we can optionally get a span.

Types for which we can get a span.