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 rune::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::try_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::try_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;
Macros§
- K
- Helper macro to reference a specific token kind, or short sequence of kinds.
- T
- Helper macro to reference a specific token.
Structs§
- Abstract
- The
abstractkeyword. - AlignOf
- The
alignofkeyword. - Amp
&.- AmpAmp
&&.- AmpEq
&=.- Angle
Bracketed - Parse something bracketed, that is separated by
<(T, S?)*>. - Arrow
->.- As
- The
askeyword. - Async
- The
asynckeyword. - At
@.- Attribute
- Attributes like:
- Await
- The
awaitkeyword. - Bang
!.- BangEq
!=.- Become
- The
becomekeyword. - Block
- A block of statements.
- Braced
- Parse something braced, that is separated by
{(T, S?)*}. - Bracketed
- Parse something bracketed, that is separated by
[(T, S?)*]. - Break
- The
breakkeyword. - Byte
Index - A single index in a Span, like the start or ending index.
- Caret
^.- CaretEq
^=.- Close
Brace - closing brace
- Close
Bracket - closing bracket
- Close
Empty - closing marker
- Close
Paren - closing parenthesis
- Colon
:.- Colon
Colon ::.- Comma
,.- Const
- The
constkeyword. - Continue
- The
continuekeyword. - Crate
- The
cratekeyword. - Dash
-.- DashEq
-=.- Default
- The
defaultkeyword. - Div
/.- Do
- The
dokeyword. - Dollar
$.- Dot
..- DotDot
...- DotDot
Eq ..=.- Else
- The
elsekeyword. - Empty
Block - A block of statements.
- Enum
- The
enumkeyword. - Eq
=.- EqEq
==.- EqValue
- An
= ...e.g. inside an attribute#[doc = ...]. - Expr
Assign - An assign expression.
- Expr
Await - An await expression.
- Expr
Binary - A binary expression.
- Expr
Block - A block expression.
- Expr
Break - A break expression.
- Expr
Call - A call expression.
- Expr
Closure - A closure expression.
- Expr
Continue - A
continuestatement. - Expr
Default Branch - A single selection branch.
- Expr
Else - An else branch of an if expression.
- Expr
Else If - An else branch of an if expression.
- Expr
Empty - A prioritized expression group without delimiters
<expr>. - Expr
Field Access - A field access.
- ExprFor
- A
forloop over an iterator. - Expr
Group - A prioritized expression group.
- ExprIf
- A conditional
ifexpression. - Expr
Index - An index get operation.
- ExprLet
- A let expression.
- ExprLit
- A literal expression. With the addition of being able to receive attributes, this is identical to ast::Lit.
- Expr
Loop - A
loopexpression. - Expr
Match - A match expression.
- Expr
Match Branch - A match branch.
- Expr
Object - An object expression.
- Expr
Range - A range expression.
- Expr
Return - A return expression.
- Expr
Select - A
selectexpression that selects over a collection of futures. - Expr
Select PatBranch - A single selection branch.
- ExprTry
- A try expression.
- Expr
Tuple - An expression to construct a literal tuple.
- Expr
Unary - A unary expression.
- ExprVec
- A literal vector.
- Expr
While - A
whileloop. - Expr
Yield - A
yieldexpression to return a value from a generator. - Extern
- The
externkeyword. - False
- The
falsekeyword. - Field
- A field as part of a struct or a tuple body.
- Field
Assign - A single field assignment in an object expression.
- File
- A rune file.
- Final
- The
finalkeyword. - Fn
- The
fnkeyword. - For
- The
forkeyword. - Group
- Parses
[{( ... )}]ensuring that the delimiter is balanced. - Gt
>.- GtEq
>=.- GtGt
>>.- GtGtEq
>>=.- Ident
- An identifier, like
fooorHello. - If
- The
ifkeyword. - Impl
- The
implkeyword. - In
- The
inkeyword. - Is
- The
iskeyword. - IsNot
- The composite
is notoperation. - Item
Const - A const declaration.
- Item
Enum - An enum item.
- ItemFn
- A function item.
- Item
Impl - An impl item.
- Item
Inline Body - A module declaration.
- ItemMod
- A module item.
- Item
Struct - A struct item.
- ItemUse
- A
useitem. - Item
UsePath - A single use declaration path.
- Item
Variant - An enum variant.
- Label
- A label, like
'foo. - Let
- The
letkeyword. - LitBool
- The boolean literal.
- LitByte
- A byte literal.
- LitByte
Str - A string literal.
- LitChar
- A character literal.
- LitNumber
- A number literal.
- LitStr
- A string literal.
- Local
- A local variable declaration.
- Loop
- The
loopkeyword. - Lt
<.- LtEq
<=.- LtLt
<<.- LtLtEq
<<=.- Macro
- The
macrokeyword. - Macro
Call - A macro call.
- Match
- The
matchkeyword. - Mod
- The
modkeyword. - Move
- The
movekeyword. - Mut
- The
mutkeyword. - Not
- The
notkeyword. - Number
- A resolved number literal.
- Number
Text - Configuration of a text number.
- Offset
Of - The
offsetofkeyword. - Open
Brace - opening brace
- Open
Bracket - opening bracket
- Open
Empty - opening marker
- Open
Paren - opening parenthesis
- Override
- The
overridekeyword. - Parenthesized
- Parse something parenthesis, that is separated by
((T, S?)*). - PatBinding
- An object item.
- PatIgnore
- An ignore pattern.
- PatLit
- A literal pattern.
- PatObject
- An object pattern.
- PatPath
- A path pattern.
- PatRest
- The rest pattern
..and associated attributes. - PatTuple
- A tuple pattern.
- PatVec
- An array pattern.
- Path
- A path, where each element is separated by a
::. - Path
Segment Expr - Used to parse an expression without supporting an immediate binary expression.
- Perc
%.- PercEq
%=.- Pipe
|.- PipeEq
- |=`.
- Pipe
Pipe ||.- Plus
+.- PlusEq
+=.- Pound
#.- Priv
- The
privkeyword. - Proc
- The
prockeyword. - Pub
- The
pubkeyword. - Pure
- The
purekeyword. - Question
Mark ?.- Ref
- The
refkeyword. - Return
- The
returnkeyword. - Rocket
=>.- Select
- The
selectkeyword. - Self
Type - The
Selfkeyword. - Self
Value - The
selfkeyword. - Semi
Colon ;.- Shebang
- The shebang of a file.
- SizeOf
- The
sizeofkeyword. - SlashEq
/=.- Span
- A span corresponding to a range in the source file being parsed.
- Star
*.- StarEq
*=.- Static
- The
statickeyword. - Stmt
Semi - A semi-terminated expression.
- StrText
- Configuration for a literal string.
- Struct
- The
structkeyword. - Super
- The
superkeyword. - Tilde
~.- Token
- A single token encountered during parsing.
- True
- The
truekeyword. - TypeOf
- The
typeofkeyword. - Underscore
_.- Unsafe
- The
unsafekeyword. - Use
- The
usekeyword. - Virtual
- The
virtualkeyword. - While
- The
whilekeyword. - Yield
- The
yieldkeyword.
Enums§
- Attr
Style - Whether or not the attribute is an outer
#!or inner#attribute - BinOp
- A binary operation.
- BuiltIn
- A built-in identifiers that do not have a source.
- Condition
- The condition in an if statement.
- Copy
Source - The source of an item that implements Copy.
- Delimiter
- A delimiter,
{,{, or[. - Expr
- A rune expression.
- Expr
Closure Args - Representation of closure arguments.
- Expr
Field - The field being accessed.
- Expr
Range Limits - The limits of the specified range.
- Expr
Select Branch - A single selection branch.
- Fields
- An item body declaration.
- FnArg
- A single argument in a closure.
- Item
- A declaration.
- Item
ModBody - An item body.
- Item
OrExpr - Parsing an item or an expression.
- Item
UseSegment - A use component.
- Kind
- The kind of the token.
- Lit
- A literal value,
- LitSource
- The kind of the identifier.
- Number
Base - The kind of a number literal.
- Number
Size - The literal size of a number.
- Number
Source - The source of a number.
- Number
Suffix - The suffix of a number.
- Number
Value - The value of a number literal.
- Object
Ident - A literal object identifier.
- Object
Key - Possible literal object keys.
- Pat
- A pattern match.
- Path
Kind - An identified path kind.
- Path
Segment - Part of a
::separated path. - Stmt
- A statement within a block.
- Stmt
Sort Key - Key used to stort a statement into its processing order.
- StrSource
- The source of the literal string. This need to be treated separately from LitSource because it might encompass special things like quoting and escaping.
- Type
- A type, used for static typing.
- UnOp
- A unary operation.
- Visibility
- Visibility level restricted to some path.
Traits§
- Option
Spanned - Types for which we can optionally get a span.
- Spanned
- Types for which we can get a span.