rune/ast/
mod.rs

1//! Abstract syntax trees for the Rune language.
2//!
3//! These are primarily made available for use in macros, where the input to the
4//! macro needs to be parsed so that it can be processed.
5//!
6//! Below we define a macro capable of taking identifiers like `hello`, and
7//! turning them into literal strings like `"hello"`.
8//!
9//! ```
10//! use rune::{Context, FromValue, Module, Vm};
11//! use rune::ast;
12//! use rune::compile;
13//! use rune::macros::{quote, MacroContext, TokenStream};
14//! use rune::parse::Parser;
15//! use rune::alloc::prelude::*;
16//!
17//! use std::sync::Arc;
18//!
19//! #[rune::macro_]
20//! fn ident_to_string(cx: &mut MacroContext<'_, '_, '_>, stream: &TokenStream) -> compile::Result<TokenStream> {
21//!     let mut p = Parser::from_token_stream(stream, cx.input_span());
22//!     let ident = p.parse_all::<ast::Ident>()?;
23//!     let ident = cx.resolve(ident)?.try_to_owned()?;
24//!     let string = cx.lit(&ident)?;
25//!     Ok(quote!(#string).into_token_stream(cx)?)
26//! }
27//!
28//! let mut m = Module::new();
29//! m.macro_meta(ident_to_string)?;
30//!
31//! let mut context = Context::new();
32//! context.install(m)?;
33//!
34//! let runtime = Arc::new(context.runtime()?);
35//!
36//! let mut sources = rune::sources! {
37//!     entry => {
38//!         pub fn main() {
39//!             ident_to_string!(hello)
40//!         }
41//!     }
42//! };
43//!
44//! let unit = rune::prepare(&mut sources)
45//!     .with_context(&context)
46//!     .build()?;
47//!
48//! let unit = Arc::new(unit);
49//!
50//! let mut vm = Vm::new(runtime, unit);
51//! let value = vm.call(["main"], ())?;
52//! let value: String = rune::from_value(value)?;
53//!
54//! assert_eq!(value, "hello");
55//! # Ok::<_, rune::support::Error>(())
56//! ```
57
58use crate as rune;
59use crate::alloc::prelude::*;
60use crate::macros::{MacroContext, ToTokens, TokenStream};
61use crate::parse::{Parse, Parser, Peek};
62
63#[cfg(test)]
64mod testing;
65
66#[macro_use]
67/// Generated modules.
68mod generated;
69pub use self::generated::*;
70
71macro_rules! expr_parse {
72    ($ty:ident, $local:ty, $expected:literal) => {
73        impl $crate::parse::Parse for $local {
74            fn parse(p: &mut $crate::parse::Parser<'_>) -> $crate::compile::Result<Self> {
75                let t = p.tok_at(0)?;
76
77                match $crate::ast::Expr::parse(p)? {
78                    $crate::ast::Expr::$ty(expr) => Ok(expr),
79                    _ => Err($crate::compile::Error::expected(t, $expected)),
80                }
81            }
82        }
83    };
84}
85
86macro_rules! item_parse {
87    ($ty:ident, $local:ty, $expected:literal) => {
88        impl $crate::parse::Parse for $local {
89            fn parse(p: &mut $crate::parse::Parser<'_>) -> $crate::compile::Result<Self> {
90                let t = p.tok_at(0)?;
91
92                match $crate::ast::Item::parse(p)? {
93                    $crate::ast::Item::$ty(item) => Ok(item),
94                    _ => Err($crate::compile::Error::expected(t, $expected)),
95                }
96            }
97        }
98    };
99}
100
101#[cfg(test)]
102mod tests;
103
104mod attribute;
105mod block;
106mod condition;
107mod expr;
108mod expr_assign;
109mod expr_await;
110mod expr_binary;
111mod expr_block;
112mod expr_break;
113mod expr_call;
114mod expr_closure;
115mod expr_continue;
116mod expr_empty;
117mod expr_field_access;
118mod expr_for;
119mod expr_group;
120mod expr_if;
121mod expr_index;
122mod expr_let;
123mod expr_lit;
124mod expr_loop;
125mod expr_match;
126mod expr_object;
127mod expr_range;
128mod expr_return;
129mod expr_select;
130mod expr_try;
131mod expr_tuple;
132mod expr_unary;
133mod expr_vec;
134mod expr_while;
135mod expr_yield;
136mod fields;
137mod file;
138mod fn_arg;
139mod grouped;
140mod ident;
141mod item;
142mod item_const;
143mod item_enum;
144mod item_fn;
145mod item_impl;
146mod item_mod;
147mod item_struct;
148mod item_use;
149mod label;
150mod lit;
151mod lit_bool;
152mod lit_byte;
153mod lit_byte_str;
154mod lit_char;
155mod lit_number;
156mod lit_str;
157mod local;
158mod macro_call;
159mod macro_utils;
160mod pat;
161mod path;
162mod prelude;
163mod rn_type;
164mod span;
165pub(crate) mod spanned;
166mod stmt;
167mod to_ast;
168mod token;
169pub(super) mod unescape;
170mod utils;
171mod vis;
172
173pub use self::attribute::{AttrStyle, Attribute};
174pub use self::block::{Block, EmptyBlock};
175pub use self::condition::Condition;
176pub use self::expr::Expr;
177pub use self::expr_assign::ExprAssign;
178pub use self::expr_await::ExprAwait;
179pub use self::expr_binary::{BinOp, ExprBinary};
180pub use self::expr_block::ExprBlock;
181pub use self::expr_break::ExprBreak;
182pub use self::expr_call::ExprCall;
183pub use self::expr_closure::{ExprClosure, ExprClosureArgs};
184pub use self::expr_continue::ExprContinue;
185pub use self::expr_empty::ExprEmpty;
186pub use self::expr_field_access::{ExprField, ExprFieldAccess};
187pub use self::expr_for::ExprFor;
188pub use self::expr_group::ExprGroup;
189pub use self::expr_if::{ExprElse, ExprElseIf, ExprIf};
190pub use self::expr_index::ExprIndex;
191pub use self::expr_let::ExprLet;
192pub use self::expr_lit::ExprLit;
193pub use self::expr_loop::ExprLoop;
194pub use self::expr_match::{ExprMatch, ExprMatchBranch};
195pub use self::expr_object::{ExprObject, FieldAssign, ObjectIdent, ObjectKey};
196pub use self::expr_range::{ExprRange, ExprRangeLimits};
197pub use self::expr_return::ExprReturn;
198pub use self::expr_select::{ExprDefaultBranch, ExprSelect, ExprSelectBranch, ExprSelectPatBranch};
199pub use self::expr_try::ExprTry;
200pub use self::expr_tuple::ExprTuple;
201pub use self::expr_unary::{ExprUnary, UnOp};
202pub use self::expr_vec::ExprVec;
203pub use self::expr_while::ExprWhile;
204pub use self::expr_yield::ExprYield;
205pub use self::fields::Fields;
206pub use self::file::{File, Shebang};
207pub use self::fn_arg::FnArg;
208pub use self::grouped::{AngleBracketed, Braced, Bracketed, Parenthesized};
209pub use self::ident::Ident;
210pub use self::item::Item;
211pub use self::item_const::ItemConst;
212pub use self::item_enum::{ItemEnum, ItemVariant};
213pub use self::item_fn::ItemFn;
214pub use self::item_impl::ItemImpl;
215pub use self::item_mod::{ItemInlineBody, ItemMod, ItemModBody};
216pub use self::item_struct::{Field, ItemStruct};
217pub use self::item_use::{ItemUse, ItemUsePath, ItemUseSegment};
218pub use self::label::Label;
219pub use self::lit::Lit;
220pub use self::lit_bool::LitBool;
221pub use self::lit_byte::LitByte;
222pub use self::lit_byte_str::LitByteStr;
223pub use self::lit_char::LitChar;
224pub use self::lit_number::LitNumber;
225pub use self::lit_str::LitStr;
226pub use self::local::Local;
227pub use self::macro_call::MacroCall;
228pub use self::macro_utils::{EqValue, Group};
229pub use self::pat::{
230    Pat, PatBinding, PatIgnore, PatLit, PatObject, PatPath, PatRest, PatTuple, PatVec,
231};
232pub use self::path::{Path, PathKind, PathSegment, PathSegmentExpr};
233use self::prelude::*;
234pub use self::rn_type::Type;
235pub use self::span::{ByteIndex, Span};
236pub use self::spanned::{OptionSpanned, Spanned};
237pub use self::stmt::{ItemOrExpr, Stmt, StmtSemi, StmtSortKey};
238pub(crate) use self::to_ast::ToAst;
239pub use self::token::{
240    BuiltIn, CopySource, Delimiter, LitSource, Number, NumberBase, NumberSize, NumberSource,
241    NumberSuffix, NumberText, NumberValue, StrSource, StrText, Token,
242};
243pub use self::vis::Visibility;
244
245macro_rules! decl_tokens {
246    ($(($parser:ident, $name:expr, $doc:expr, $($kind:tt)*),)*) => {
247        $(
248            #[doc = $doc]
249            #[derive(Debug, TryClone, Clone, Copy, PartialEq, Eq)]
250            #[try_clone(copy)]
251            pub struct $parser {
252                /// Associated token.
253                pub span: Span,
254            }
255
256            impl Spanned for $parser {
257                fn span(&self) -> Span {
258                    self.span
259                }
260            }
261
262            impl OptionSpanned for $parser {
263                fn option_span(&self) -> Option<Span> {
264                    Some(self.span)
265                }
266            }
267
268            impl Parse for $parser {
269                fn parse(parser: &mut Parser<'_>) -> $crate::compile::Result<Self> {
270                    let t = parser.next()?;
271
272                    match t.kind {
273                        $($kind)* => Ok(Self { span: t.span }),
274                        _ => Err($crate::compile::Error::expected(t, $name)),
275                    }
276                }
277            }
278
279            impl Peek for $parser {
280                fn peek(p: &mut $crate::parse::Peeker<'_>) -> bool {
281                    matches!(p.nth(0), $($kind)*)
282                }
283            }
284
285            impl ToTokens for $parser {
286                fn to_tokens(&self, _: &mut MacroContext<'_, '_, '_>, stream: &mut TokenStream) -> alloc::Result<()> {
287                    stream.push(Token { span: self.span, kind: $($kind)* })
288                }
289            }
290        )*
291    }
292}
293
294decl_tokens! {
295    (CloseBrace, "a closing brace `}`", "closing brace", Kind::Close(Delimiter::Brace)),
296    (CloseBracket, "a closing bracket `]`", "closing bracket", Kind::Close(Delimiter::Bracket)),
297    (CloseParen, "a closing parenthesis `)`", "closing parenthesis", Kind::Close(Delimiter::Parenthesis)),
298    (CloseEmpty, "an empty closing marker", "closing marker", Kind::Close(Delimiter::Empty)),
299    (OpenBrace, "an opening brace `{`", "opening brace", Kind::Open(Delimiter::Brace)),
300    (OpenBracket, "an open bracket `[`", "opening bracket", Kind::Open(Delimiter::Bracket)),
301    (OpenParen, "an opening parenthesis `(`", "opening parenthesis", Kind::Open(Delimiter::Parenthesis)),
302    (OpenEmpty, "an empty opening marker", "opening marker", Kind::Open(Delimiter::Empty)),
303}
304
305/// The composite `is not` operation.
306#[derive(Debug, TryClone, Clone, Copy, PartialEq, Eq, Hash, ToTokens, Spanned)]
307#[try_clone(copy)]
308#[non_exhaustive]
309pub struct IsNot {
310    /// The `is` token.
311    pub is: Is,
312    /// The `not` token.
313    pub not: Not,
314}