Uses old Rust 2015
0.1.1 |
|
---|---|
0.1.0 |
|
#401 in #ast
9KB
AST Coercion Macros
This crate provides the AST coercion macros described by Daniel Keep in The Little Book of Rust Macros.
The Rust parser is not very robust in the face of
tt
substitutions. Problems can arise when the parser is expecting a particular grammar construct and instead finds a lump of substitutedtt
tokens. Rather than attempt to parse them, it will often just give up. In these cases, it is necessary to employ an AST coercion.macro_rules! as_expr { ($e:expr) => {$e} } macro_rules! as_item { ($i:item) => {$i} } macro_rules! as_pat { ($p:pat) => {$p} } macro_rules! as_stmt { ($s:stmt) => {$s} }
These coercions are often used with push-down accumulation macros in order to get the parser to treat the final
tt
sequence as a particular kind of grammar construct.Note that this specific set of macros is determined by what macros are allowed to expand to, not what they are able to capture. That is, because macros cannot appear in type position (see issue #27245), you cannot have an
as_ty!
macro.
Installation
This crate is available on crates.io. Use
the following in Cargo.toml
:
[dependencies]
reparse = "0.1"
As this crate provides macros, it must be imported with the #[macro_use]
attribute:
#[macro_use]
extern crate reparse;
License
Licensed under either of
- Creative Commons Attribution-ShareAlike 4.0 International License (LICENSE-CC or https://creativecommons.org/licenses/by-sa/4.0/legalcode)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.