#ast #coercion #tlborm

yanked reparse

AST coercion macros described in The Little Book Of Rust Macros

Uses old Rust 2015

0.1.1 Apr 12, 2016
0.1.0 Apr 12, 2016

#364 in #ast

CC-BY-SA-4.0/MIT

9KB

AST Coercion Macros

Build Status Latest Version

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 substituted tt 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

at your option.

No runtime deps