Skip to content

Commit

Permalink
use local_inner_macros annotation to support rust 2018
Browse files Browse the repository at this point in the history
since macro import is now done through `use nom::the_macro`
instead of `#[macro_use]`, macros using other internal macros
result in the compiler complaining about missing imports.
One of the solutions would be to use them through
`$crate::internal_macro`, but it does not work for the comon case of
`call!`:

A lot of nom parsers use this pattern:

```
macro_rules! my_parser (
  ($i:expr, $submac:ident!( $($args:tt)* )) => ({
    //stuff
  });
  ($i:expr, $f:expr) => (
    my_parser!($i, call!($f))
  )
);
```

if I replace `call!` with `$crate::call!` rustc complains about infinite
recursion because `$crate::call!($f)` is an expression. `$f` has to be
an expr, because it could be a variable name, or a closure that’s
written right there. And I cannot adapt `$submac:ident!` to take a path
instead, because somehow module path matching does not accept a `!`
right after.

The other solution uses `#[macro_export(local_inner_macros)]`
( https://rust-lang-nursery.github.io/edition-guide/rust-2018/macros/macro-changes.html?highlight=macros#macros-using-local_inner_macros)
to indicate some macros use other macros from the same crate, but it requires
local wrappers to libstd's macros (mainly for the `dbg` and `dbg_dmp`
combinators
  • Loading branch information
Geal committed Dec 24, 2018
1 parent 415a689 commit e3fa8e7
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 172 deletions.
16 changes: 8 additions & 8 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
///
/// assert_eq!(take_4_bits( sl ), Ok( (&sl[1..], 0xA) ));
/// # }
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! bits (
($i:expr, $submac:ident!( $($args:tt)* )) => (
bits_impl!($i, $submac!($($args)*));
Expand All @@ -41,7 +41,7 @@ macro_rules! bits (
#[cfg(feature = "verbose-errors")]
/// Internal parser, do not use directly
#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! bits_impl (
($i:expr, $submac:ident!( $($args:tt)* )) => (
{
Expand Down Expand Up @@ -86,7 +86,7 @@ macro_rules! bits_impl (
#[cfg(not(feature = "verbose-errors"))]
/// Internal parser, do not use directly
#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! bits_impl (
($i:expr, $submac:ident!( $($args:tt)* )) => (
{
Expand Down Expand Up @@ -141,7 +141,7 @@ macro_rules! bits_impl (
///
/// assert_eq!(parse( input ), Ok(( &[][..], (0xd, 0xea, &[0xbe, 0xaf][..]) )));
/// # }
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! bytes (
($i:expr, $submac:ident!( $($args:tt)* )) => (
bytes_impl!($i, $submac!($($args)*));
Expand All @@ -154,7 +154,7 @@ macro_rules! bytes (
#[cfg(feature = "verbose-errors")]
/// Internal parser, do not use directly
#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! bytes_impl (
($macro_i:expr, $submac:ident!( $($args:tt)* )) => (
{
Expand Down Expand Up @@ -214,7 +214,7 @@ macro_rules! bytes_impl (
#[cfg(not(feature = "verbose-errors"))]
/// Internal parser, do not use directly
#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! bytes_impl (
($macro_i:expr, $submac:ident!( $($args:tt)* )) => (
{
Expand Down Expand Up @@ -269,7 +269,7 @@ macro_rules! bytes_impl (
/// assert_eq!(take_pair( &sl[1..] ), Ok((&sl[2..], (0xC, 0xD))) );
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_bits (
($i:expr, $t:ty, $count:expr) => (
{
Expand Down Expand Up @@ -340,7 +340,7 @@ macro_rules! take_bits (
/// assert_eq!(take_a( sl ), Ok((&sl[1..], 0xA)) );
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! tag_bits (
($i:expr, $t:ty, $count:expr, $p: pat) => (
{
Expand Down
18 changes: 9 additions & 9 deletions src/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
/// or empty input (End Of File). If none of them work, `preceded!` will fail and
/// "ef" will be tested.
///
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! alt (
(__impl $i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)* ) => (
compile_error!("alt uses '|' as separator, not ',':
Expand Down Expand Up @@ -277,7 +277,7 @@ macro_rules! alt (
/// alt_complete!(parser_1 | parser_2 | ... | parser_n)
/// ```
/// **For more in depth examples, refer to the documentation of `alt!`**
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! alt_complete (
// Recursive rules (must include `complete!` around the head)

Expand Down Expand Up @@ -435,7 +435,7 @@ macro_rules! alt_complete (
/// );
/// ```
///
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! switch (
(__impl $i:expr, $submac:ident!( $($args:tt)* ), $( $($p:pat)|+ => $subrule:ident!( $($args2:tt)* ))|* ) => (
{
Expand Down Expand Up @@ -559,7 +559,7 @@ macro_rules! switch (
/// assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4))));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! permutation (
($i:expr, $($rest:tt)*) => (
{
Expand Down Expand Up @@ -602,7 +602,7 @@ macro_rules! permutation (
);

#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! permutation_init (
((), $e:ident?, $($rest:tt)*) => (
permutation_init!(($crate::lib::std::option::Option::None), $($rest)*)
Expand Down Expand Up @@ -651,7 +651,7 @@ macro_rules! permutation_init (
);

#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! succ (
(0, $submac:ident ! ($($rest:tt)*)) => ($submac!(1, $($rest)*));
(1, $submac:ident ! ($($rest:tt)*)) => ($submac!(2, $($rest)*));
Expand Down Expand Up @@ -679,7 +679,7 @@ macro_rules! succ (
// permutation_unwrap. This is a bit ugly, but it will have no
// impact on the generated code
#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! acc (
(0, $tup:expr) => ($tup.0);
(1, $tup:expr) => ($tup.1);
Expand All @@ -705,7 +705,7 @@ macro_rules! acc (
);

#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! permutation_unwrap (
($it:tt, (), $res:ident, $e:ident?, $($rest:tt)*) => (
succ!($it, permutation_unwrap!((acc!($it, $res)), $res, $($rest)*));
Expand Down Expand Up @@ -781,7 +781,7 @@ macro_rules! permutation_unwrap (
);

#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! permutation_iterator (
($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident?, $($rest:tt)*) => (
permutation_iterator!($it, $i, $all_done, $needed, $res, call!($e), $($rest)*);
Expand Down
44 changes: 22 additions & 22 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// assert_eq!(r, Ok((&b"efgh"[..], &b"abcd"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! tag (
($i:expr, $tag: expr) => (
{
Expand Down Expand Up @@ -57,7 +57,7 @@ macro_rules! tag (
/// assert_eq!(r, Ok((&b"efgh"[..], &b"aBCd"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! tag_no_case (
($i:expr, $tag: expr) => (
{
Expand Down Expand Up @@ -96,7 +96,7 @@ macro_rules! tag_no_case (
/// assert_eq!(r, Ok((&b"\nijkl"[..], &b"abcdefgh"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! is_not (
($input:expr, $arr:expr) => (
{
Expand Down Expand Up @@ -125,7 +125,7 @@ macro_rules! is_not (
/// assert_eq!(r2, Ok((&b"efgh"[..], &b"dcba"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! is_a (
($input:expr, $arr:expr) => (
{
Expand Down Expand Up @@ -156,7 +156,7 @@ macro_rules! is_a (
/// assert_eq!(esc(&b"ab\\\"cd;"[..]), Ok((&b";"[..], &b"ab\\\"cd"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! escaped (
// Internal parser, do not use directly
(__impl $i: expr, $normal:ident!( $($args:tt)* ), $control_char: expr, $escapable:ident!( $($args2:tt)* )) => (
Expand Down Expand Up @@ -289,7 +289,7 @@ macro_rules! escaped (
/// assert_eq!(transform(&b"ab\\\"cd"[..]), Ok((&b""[..], String::from("ab\"cd"))));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! escaped_transform (
// Internal parser, do not use directly
(__impl $i: expr, $normal:ident!( $($args:tt)* ), $control_char: expr, $transform:ident!( $($args2:tt)* )) => (
Expand Down Expand Up @@ -417,7 +417,7 @@ macro_rules! escaped_transform (
/// assert_eq!(r, Ok((&b"\nefgh"[..], &b"abcd"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_while (
($input:expr, $submac:ident!( $($args:tt)* )) => (
{
Expand Down Expand Up @@ -450,7 +450,7 @@ macro_rules! take_while (
/// assert_eq!(r, Err(Err::Error(error_position!(&b"\nefgh"[..], ErrorKind::TakeWhile1))));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_while1 (
($input:expr, $submac:ident!( $($args:tt)* )) => (
{
Expand Down Expand Up @@ -483,7 +483,7 @@ macro_rules! take_while1 (
/// assert_eq!(r, Ok((&b"\nefgh"[..], &b"abcd"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_while_m_n (
($input:expr, $m:expr, $n:expr, $submac:ident!( $($args:tt)* )) => (
{
Expand Down Expand Up @@ -560,7 +560,7 @@ macro_rules! take_while_m_n (
/// assert_eq!(r2, Ok((&b":abcdefgh"[..], &b""[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_till (
($input:expr, $submac:ident!( $($args:tt)* )) => (
{
Expand Down Expand Up @@ -593,7 +593,7 @@ macro_rules! take_till (
/// assert_eq!(r2, Err(Err::Error(error_position!(&b":abcdefgh"[..], ErrorKind::TakeTill1))));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_till1 (
($input:expr, $submac:ident!( $($args:tt)* )) => (
{
Expand Down Expand Up @@ -622,7 +622,7 @@ macro_rules! take_till1 (
/// assert_eq!(take5(&a[..]), Ok((&b"fgh"[..], &b"abcde"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take (
($i:expr, $count:expr) => (
{
Expand Down Expand Up @@ -659,7 +659,7 @@ macro_rules! take (
/// assert_eq!(take5(&a[..]), Ok((&b"fgh"[..], "abcde")));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_str (
( $i:expr, $size:expr ) => (
{
Expand All @@ -685,7 +685,7 @@ macro_rules! take_str (
/// assert_eq!(r, Ok((&b" efgh"[..], &b"abcd "[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_until_and_consume (
($i:expr, $substr:expr) => (
{
Expand Down Expand Up @@ -726,7 +726,7 @@ macro_rules! take_until_and_consume (
/// assert_eq!(r, Ok((&b" efgh"[..], &b"abcd "[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_until_and_consume1 (
($i:expr, $substr:expr) => (
{
Expand Down Expand Up @@ -771,7 +771,7 @@ macro_rules! take_until_and_consume1 (
/// assert_eq!(r, Ok((&b"foo efgh"[..], &b"abcd "[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_until (
($i:expr, $substr:expr) => (
{
Expand Down Expand Up @@ -814,7 +814,7 @@ macro_rules! take_until (
/// assert_eq!(r, Ok((&b"foo efgh"[..], &b"abcd "[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_until1 (
($i:expr, $substr:expr) => (
{
Expand Down Expand Up @@ -858,7 +858,7 @@ macro_rules! take_until1 (
/// assert_eq!(r, Ok((&b"efgh"[..], &b"abcd"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_until_either_and_consume (
($input:expr, $arr:expr) => (
{
Expand Down Expand Up @@ -917,7 +917,7 @@ macro_rules! take_until_either_and_consume (
/// assert_eq!(r, Ok((&b"efgh"[..], &b"abcd"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_until_either_and_consume1 (
($input:expr, $arr:expr) => (
{
Expand Down Expand Up @@ -978,7 +978,7 @@ macro_rules! take_until_either_and_consume1 (
/// assert_eq!(r, Ok((&b"2efgh"[..], &b"abcd"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_until_either (
($input:expr, $arr:expr) => (
{
Expand Down Expand Up @@ -1021,7 +1021,7 @@ macro_rules! take_until_either (
/// assert_eq!(r, Ok((&b"2efgh"[..], &b"abcd"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! take_until_either1 (
($input:expr, $arr:expr) => (
{
Expand Down Expand Up @@ -1063,7 +1063,7 @@ macro_rules! take_until_either1 (
/// assert_eq!(r, Ok((&b"fgh"[..], &b"abcde"[..])));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! length_bytes(
($i:expr, $submac:ident!( $($args:tt)* )) => (
{
Expand Down
6 changes: 3 additions & 3 deletions src/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use traits::{need_more, AtEof};
/// assert_eq!(a_or_b("汉jiosfe"), Ok(("jiosfe", '汉')));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! one_of (
($i:expr, $inp: expr) => (
{
Expand Down Expand Up @@ -57,7 +57,7 @@ macro_rules! one_of (
/// assert_eq!(err_on_single_quote(b"'jiosfe"), Err(Err::Error(error_position!(&b"'jiosfe"[..], ErrorKind::NoneOf))));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! none_of (
($i:expr, $inp: expr) => (
{
Expand Down Expand Up @@ -95,7 +95,7 @@ macro_rules! none_of (
/// assert_eq!(match_letter_a(b"123cdef"), Err(Err::Error(error_position!(&b"123cdef"[..], ErrorKind::Char))));
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! char (
($i:expr, $c: expr) => (
{
Expand Down
Loading

0 comments on commit e3fa8e7

Please sign in to comment.