#proc-macro #parser #procedural #callback #intended #token-stream #syn

moisture

Moisture is a Rust-based parsing library intended for use with procedural macros!

1 unstable release

0.1.0 Apr 15, 2022

#2362 in Procedural macros

Download history 260/week @ 2023-11-20 1512/week @ 2023-11-27 736/week @ 2023-12-04 373/week @ 2023-12-11 614/week @ 2023-12-18 325/week @ 2023-12-25 204/week @ 2024-01-01 401/week @ 2024-01-08 357/week @ 2024-01-15 577/week @ 2024-01-22 287/week @ 2024-01-29 254/week @ 2024-02-05 415/week @ 2024-02-12 362/week @ 2024-02-19 362/week @ 2024-02-26 243/week @ 2024-03-04

1,412 downloads per month
Used in 5 crates (via goldberg)

GPL-3.0 license

89KB
2K SLoC

moisture

Moisture is a Rust syntax parsing library intended for use with procedural macros. It is based on syn, quote and proc-macro2. It's primary function is to crawl the syntax tree provided by the syn, process the individual items provided via registered callbacks handling the items, then return the token stream to the given objects. Moisture is intended to be a procedural macro solution for complex handling of the Rust syntax tree.

The changelog can be found here and further documentation can be found here.


lib.rs:

Moisture is a Rust code parsing library with a callback interface. It is based on syn, quote and proc-macro2 and is intended to be used with procedural macros.

What Moisture does specifically is parse the entire syntax tree provided by a token stream and issue callbacks to certain objects for potential modification. A basic example:

use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use syn::{LitStr, Result, parse2};
use syn::spanned::Spanned;

use moisture::*;

fn str_callback(moisture: &Moisture, context: &Context, tokens: TokenStream) -> Result<TokenStream> {
   let lit_str = parse2::<LitStr>(tokens.clone())?;

   if lit_str.value() == "foo" { Ok(LitStr::new("bar", lit_str.span()).to_token_stream()) }
   else { let result = moisture.lit_str(context, tokens)?; Ok(result) }
}

let mut moisture = Moisture::new();
moisture.register_callback(CallbackType::LitStr, str_callback);

let foo_stream = quote! { "foo" };
let bar_stream = run_moisture!(moisture, CallbackType::LitStr, foo_stream);
let bar_lit = parse2::<LitStr>(bar_stream).unwrap();

assert_eq!(bar_lit.value(), "bar");

let baz_stream = quote! { "baz" };
let same_stream = run_moisture!(moisture, CallbackType::LitStr, baz_stream);
let baz_lit = parse2::<LitStr>(same_stream).unwrap();

assert_eq!(baz_lit.value(), "baz");

A significant chunk of syn types are supported. See CallbackType for a list of supported syn types.

Dependencies

~1.5MB
~33K SLoC