Tamschi / salve

Greasy parsing extensions for `syn`, to calm down macro errors.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

salve

Lib.rs Crates.io Docs.rs

Rust 1.61 CI Crates.io - License

GitHub open issues open pull requests good first issues

crev reviews Zulip Chat

Greasy parsing extensions for syn, to soften proc macro errors.

Installation

Please use cargo-edit to always add the latest version of this library:

cargo add salve

Example

use catch::CatchExt;
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote_spanned;
use salve::ParseOrForgeExt;
use syn::{parse::ParseStream, Error, Token};

pub fn parse_triple(input: ParseStream, errors: &mut Vec<Error>) -> (Ident, Token![,], Ident) {
  (
    // Parsing will continue here regardless of errors, yielding dummy values iff needed.
    // Errors are collected for later use.
    input.parse_or_forge().catch_item(errors),
    input.parse_or_forge().catch_item(errors),
    input.parse_or_forge().catch_item(errors),
  )
}

pub fn macro_transform(input: ParseStream) -> TokenStream {
  let mut errors = vec![];
  let (first, _, second) = parse_triple(input, &mut errors);
  if !input.is_empty() {
    // Added last, since this is often incidental.
    errors.push(Error::new_spanned(
      input.parse::<TokenStream>().expect("infallible"),
      "Unexpected tokens.",
    ))
  }
  let errors = errors.into_iter().map(Error::into_compile_error);
  quote_spanned! {Span::mixed_site()=>
    #(#errors)* // Emit parsing errors first, for better visibility.

    // Even if `second` is unavailable, `first` may be present, reducing errors elsewhere.
    #[automatically_derived]
    struct #first;

    // Even if `first` is unavailable, `second` may be present, reducing errors elsewhere.
    #[automatically_derived]
    struct #second;
  }
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

See CONTRIBUTING for more information.

Code of Conduct

Changelog

Versioning

salve strictly follows Semantic Versioning 2.0.0 with the following exceptions:

  • The minor version will not reset to 0 on major version changes (except for v1).
    Consider it the global feature level.
  • The patch version will not reset to 0 on major or minor version changes (except for v0.1 and v1).
    Consider it the global patch level.

This includes the Rust version requirement specified above.
Earlier Rust versions may be compatible, but this can change with minor or patch releases.

Which versions are affected by features and patches can be determined from the respective headings in CHANGELOG.md.

Note that dependencies of this crate may have a more lenient MSRV policy! Please use cargo +nightly update -Z minimal-versions in your automation if you don't generate Cargo.lock manually (or as necessary) and require support for a compiler older than current stable.

About

Greasy parsing extensions for `syn`, to calm down macro errors.

License:Other


Languages

Language:Rust 100.0%