Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pub(crate) support #566

Merged
merged 1 commit into from
Oct 6, 2018
Merged
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
85 changes: 84 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ macro_rules! closure (
/// // will use &[u8] as input type (use this if the compiler
/// // complains about lifetime issues
/// named!(my_function<&[u8]>, tag!("abcd"));
/// //prefix them with 'pub' to make the functions public
/// // prefix them with 'pub' to make the functions public
/// named!(pub my_function, tag!("abcd"));
/// // prefix them with 'pub(crate)' to make the functions public within the crate
/// named!(pub(crate) my_function, tag!("abcd"));
/// ```
#[macro_export]
macro_rules! named (
Expand Down Expand Up @@ -157,6 +159,36 @@ macro_rules! named (
$submac!(i, $($args)*)
}
);
(pub(crate) $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => (
#[allow(unused_variables)]
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i,$o, u32> {
$submac!(i, $($args)*)
}
);
(pub(crate) $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => (
#[allow(unused_variables)]
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> {
$submac!(i, $($args)*)
}
);
(pub(crate) $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => (
#[allow(unused_variables)]
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> {
$submac!(i, $($args)*)
}
);
(pub(crate) $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => (
#[allow(unused_variables)]
pub(crate) fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, u32> {
$submac!(i, $($args)*)
}
);
(pub(crate) $name:ident, $submac:ident!( $($args:tt)* )) => (
#[allow(unused_variables)]
pub(crate) fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], u32> {
$submac!(i, $($args)*)
}
);
);

/// Makes a function from a parser combination with arguments.
Expand All @@ -172,6 +204,16 @@ macro_rules! named_args {
$submac!(input, $($args)*)
}
};
(pub(crate) $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
pub(crate) fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> {
$submac!(input, $($args)*)
}
};
(pub(crate) $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
pub(crate) 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)*)
}
};
($func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> {
$submac!(input, $($args)*)
Expand Down Expand Up @@ -260,6 +302,36 @@ macro_rules! named_attr (
$submac!(i, $($args)*)
}
);
($(#[$attr:meta])*, pub(crate) $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => (
$(#[$attr])*
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i,$o, u32> {
$submac!(i, $($args)*)
}
);
($(#[$attr:meta])*, pub(crate) $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => (
$(#[$attr])*
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> {
$submac!(i, $($args)*)
}
);
($(#[$attr:meta])*, pub(crate) $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => (
$(#[$attr])*
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> {
$submac!(i, $($args)*)
}
);
($(#[$attr:meta])*, pub(crate) $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => (
$(#[$attr])*
pub(crate) fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, u32> {
$submac!(i, $($args)*)
}
);
($(#[$attr:meta])*, pub(crate) $name:ident, $submac:ident!( $($args:tt)* )) => (
$(#[$attr])*
pub(crate) fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], u32> {
$submac!(i, $($args)*)
}
);
);

/// Used to wrap common expressions and function as macros
Expand Down Expand Up @@ -1228,6 +1300,17 @@ mod tests {
assert_eq!(res, Done(&b""[..], a));
}

mod pub_crate_named_mod {
named!(pub(crate) tst, tag!("abcd"));
}

#[test]
fn pub_crate_named_test() {
let a = &b"abcd"[..];
let res = pub_crate_named_mod::tst(a);
assert_eq!(res, Done(&b""[..], a));
}

#[test]
fn apply_test() {
fn sum2(a:u8, b:u8) -> u8 { a + b }
Expand Down