Skip to content

Commit

Permalink
remove named_args macro and its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nickelc authored and Geal committed Aug 5, 2021
1 parent f546168 commit 9601840
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 199 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ required-features = ["alloc"]
name = "multiline"
required-features = ["alloc"]

[[test]]
name = "named_args"

[[test]]
name = "overflow"

Expand Down
61 changes: 0 additions & 61 deletions src/combinator/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,67 +131,6 @@ macro_rules! named (
);
);

/// Makes a function from a parser combination with arguments.
///
/// ```ignore
/// //takes [`&[u8]`] as input
/// named_args!(tagged(open_tag: &[u8], close_tag: &[u8])<&str>,
/// delimited!(tag!(open_tag), map_res!(take!(4), str::from_utf8), tag!(close_tag))
/// );
/// //takes `&str` as input
/// named_args!(tagged(open_tag: &str, close_tag: &str)<&str, &str>,
/// delimited!(tag!(open_tag), take!(4), tag!(close_tag))
/// );
/// ```
///
/// Note: If using arguments that way gets hard to read, it is always
/// possible to write the equivalent parser definition manually, like
/// this:
///
/// ```ignore
/// fn tagged(input: &[u8], open_tag: &[u8], close_tag: &[u8]) -> IResult<&[u8], &str> {
/// // the first combinator in the tree gets the input as argument. It is then
/// // passed from one combinator to the next through macro rewriting
/// delimited!(input,
/// tag!(open_tag), take!(4), tag!(close_tag)
/// )
/// );
/// ```
///
#[macro_export(local_inner_macros)]
macro_rules! named_args {
($vis:vis $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
$vis fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> {
$submac!(input, $($args)*)
}
};

($vis:vis $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
$vis fn $func_name<'this_is_probably_unique_i_hope_please, 'a>(
input: &'this_is_probably_unique_i_hope_please [u8], $( $arg : $typ ),*) ->
$crate::IResult<&'this_is_probably_unique_i_hope_please [u8], $return_type>
{
$submac!(input, $($args)*)
}
};

($vis:vis $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $input_type:ty, $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
$vis fn $func_name(input: $input_type, $( $arg : $typ ),*) -> $crate::IResult<$input_type, $return_type> {
$submac!(input, $($args)*)
}
};

($vis:vis $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $input_type:ty, $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
$vis fn $func_name<'a>(
input: $input_type, $( $arg : $typ ),*)
-> $crate::IResult<$input_type, $return_type>
{
$submac!(input, $($args)*)
}
};
}

/// Makes a function from a parser combination, with attributes.
///
/// The usage of this macro is almost identical to `named!`, except that
Expand Down
122 changes: 0 additions & 122 deletions tests/named_args.rs

This file was deleted.

39 changes: 26 additions & 13 deletions tests/reborrow_fold.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
#![allow(dead_code)]
#![allow(unused_variables)]

#[macro_use]
extern crate nom;
// #![allow(unused_variables)]

use std::str;

named_args!(atom<'a>(tomb: &'a mut ())<String>,
map!(map_res!(is_not!(" \t\r\n()"), str::from_utf8), ToString::to_string));
use nom::bytes::complete::is_not;
use nom::character::complete::char;
use nom::combinator::{map, map_res};
use nom::multi::fold_many0;
use nom::sequence::delimited;
use nom::IResult;

fn atom<'a>(_tomb: &'a mut ()) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], String> {
move |input| {
map(
map_res(is_not(" \t\r\n"), str::from_utf8),
ToString::to_string,
)(input)
}
}

/*FIXME: should we support the use case of borrowing data mutably in a parser?
named_args!(list<'a>(tomb: &'a mut ())<String>,
delimited!(
char!('('),
fold_many0!(call!(atom, tomb), "".to_string(), |acc: String, next: String| acc + next.as_str()),
char!(')')));
*/
// FIXME: should we support the use case of borrowing data mutably in a parser?
fn list<'a>(i: &'a [u8], tomb: &'a mut ()) -> IResult<&'a [u8], String> {
delimited(
char('('),
fold_many0(atom(tomb), String::new(), |acc: String, next: String| {
acc + next.as_str()
}),
char(')'),
)(i)
}

0 comments on commit 9601840

Please sign in to comment.