Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "nom-parse-macros"
description = "Procedural macros for generating parser functions for the nom libary"
version = "0.4.0"
version = "0.4.1"
license = "MIT/Apache-2.0"
keywords = ["nom", "parser", "parsable"]
categories = ["parsing"]
Expand Down
8 changes: 3 additions & 5 deletions examples/extra_where_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ where
{
move |mut input: I| {
let mut array = [T::default(); D];
for ix in 0..D {
let (rest, _) = prefix.parse(input)?;
let (rest, value) = T::parse(rest)?;
input = rest;
array[ix] = value;
for val in &mut array {
(input, _) = prefix.parse(input)?;
(input, *val) = T::parse(input)?;
}
Ok((input, array))
}
Expand Down
1 change: 1 addition & 0 deletions src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl Fields {
.collect()
}

#[allow(clippy::collapsible_else_if)]
pub fn create_instance_expr(&self, variant_name: Option<&Ident>) -> TokenStream {
let creation_names = self.get_creation_names();

Expand Down
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
//! don't need a ton of use statements in your code. But there are also a couple of special cases:
//!
//! - `{}` or `()` will be replaced with a [`nom_parse_trait::ParseFrom::parse`] call for the
//! corresponding field. This is useful when you are using types that have implemented the
//! `ParseFrom` trait already.
//! corresponding field. This is useful when you are using types that have implemented the
//! `ParseFrom` trait already.
//! - Strings, bytes strings and characters will be translated to match the input verbatim using
//! the [`nom::bytes::complete::tag`] function.
//! the [`nom::bytes::complete::tag`] function.
//!
//! # Input types that are supported
//!
Expand All @@ -34,13 +34,13 @@
//! # Known limitations
//!
//! - When your try to use a custom parser combinator, the nom function parser will try to change
//! all parameters to be nom parsers. This is useful in many cases, but when you need to pass in
//! a normal string for example, it won't work. In these cases, you can define a separate function
//! to wrap the call. I'm not sure how to fix that right now, but I'm open to suggestions.
//! all parameters to be nom parsers. This is useful in many cases, but when you need to pass in
//! a normal string for example, it won't work. In these cases, you can define a separate function
//! to wrap the call. I'm not sure how to fix that right now, but I'm open to suggestions.
//!
//! - Since the generated input type is very generic, all functions that you want to use in the
//! nom expression should also be very generic. In the future I might add a way to specify if you
//! want to generate a specific input type, but for now it's not possible.
//! nom expression should also be very generic. In the future I might add a way to specify if you
//! want to generate a specific input type, but for now it's not possible.

extern crate proc_macro;
mod fields;
Expand Down Expand Up @@ -146,7 +146,6 @@ use quote::ToTokens;
/// end: [N; D],
/// }
/// ```

#[proc_macro_attribute]
pub fn parse_from(attrs: TokenStream, object: TokenStream) -> TokenStream {
let parse_format = syn::parse_macro_input!(attrs as ParseFormat);
Expand Down
4 changes: 2 additions & 2 deletions src/nom_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ fn parse_call(call: &mut ExprCall) -> Result<()> {
if ident == "tuple" || ident == "alt" {
let args = call.args.clone();
if args.len() != 1 {
call.args = Punctuated::from(Punctuated::new());
call.args = Punctuated::new();
call.args.push(Expr::Tuple(ExprTuple {
attrs: vec![],
paren_token: Default::default(),
Expand All @@ -228,7 +228,7 @@ fn parse_call(call: &mut ExprCall) -> Result<()> {
update_nom_expression(arg)?;
}
// Nom functions without parameters should not be called, but referenced directly
} else if parameters.len() == 0 {
} else if parameters.is_empty() {
if ident != "fail" {
return Err(syn::Error::new_spanned(
call.func.clone(),
Expand Down
16 changes: 8 additions & 8 deletions src/parser_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ impl ToTokens for ParserGenerator {

tokens.extend(object.to_token_stream());

let (mapping_names, mappings): (Vec<_>, Vec<_>) = variants
.iter()
.map(|variant| generate_variant(variant))
.unzip();
let (mapping_names, mappings): (Vec<_>, Vec<_>) =
variants.iter().map(generate_variant).unzip();

generate_parser(
tokens,
Expand All @@ -78,7 +76,7 @@ impl ToTokens for ParserGenerator {

fn generate_variant(variant: &ParsedVariant) -> (Ident, TokenStream) {
let mapping_name = Ident::new(
&format!("map_{}", variant.name.to_string().to_lowercase()),
&format!("parse_{}", variant.name.to_string().to_lowercase()),
Span::call_site(),
);
let format_expr = variant.format.to_token_stream();
Expand Down Expand Up @@ -113,14 +111,16 @@ fn generate_parser(
token_stream: &mut TokenStream,
name: &Ident,
generics: &Generics,
extra_where_clauses: &Vec<WherePredicate>,
extra_where_clauses: &[WherePredicate],
content: impl ToTokens,
) {
let (_, type_generics, _) = generics.split_for_impl();
let parser_generics = parser_generics(&generics, extra_where_clauses);
let parser_generics = parser_generics(generics, extra_where_clauses);
let (impl_generics, _, where_statement) = parser_generics.split_for_impl();

token_stream.extend(quote! {
#[automatically_derived]
#[allow(unused)]
impl #impl_generics nom_parse_trait::ParseFrom<I, E> for #name #type_generics
#where_statement
{
Expand All @@ -134,7 +134,7 @@ fn generate_parser(
});
}

fn parser_generics(generics: &Generics, extra_where_clauses: &Vec<WherePredicate>) -> Generics {
fn parser_generics(generics: &Generics, extra_where_clauses: &[WherePredicate]) -> Generics {
let mut generics = generics.clone();

// If there are no generics, start a new one
Expand Down
Loading