3

A simple error handler added to nom

The first one compiles with no errors, the second one errors out

use nom::*;
use std::str;
// This works
named!(
    field<&str>,
    map!(
        complete!(add_return_error!(
            ErrorKind::Custom(1),
            preceded!(
                tag!("."),
                recognize!(many1!(
                    alt!(alphanumeric => { |_| true } | char!('_') => {|_| true})
                ))
            )
        )),
        |v| str::from_utf8(v).unwrap()
    )
);

// This doesn't compile
named!(
    entity<&str>,
    add_return_error!(
        ErrorKind::Custom(2),
        map!(
            recognize!(many1!(
                alt!(alphanumeric => { |_| true } | char!('_') => {|_| true})
            )),
            |v| str::from_utf8(v).unwrap()
        )
    )
);

Error is

cargo build
   Compiling pdl_parser v0.1.0 (file:///H:/parser)
error[E0282]: type annotations needed
  --> src\pdl_parser.rs:72:1
   |
72 | / named!(
73 | |     entity<&str>,
74 | |     add_return_error!(
75 | |         ErrorKind::Custom(2),
...  |
82 | |     )
83 | | );
   | |__^ cannot infer type for `E`
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

Why does the first one work and second one not? And what can I do to fix it? I tried changing the signature of the second one to entity<&[u8],&str,ErrorKind> and then to i32 and u32 but no success.

Delta_Fore
  • 3,079
  • 4
  • 26
  • 46

1 Answers1

0

The solution to the cannot infer type for E error is to separate out the parsing steps so the compiler does not get so confused.

Rewriting the parser-combinator as

named!(
    entity_name<Vec<u8>>,
    many1!(alt!(alphanumeric => { |x : &[u8]| x[0] } | char!('_') => {|_| b'_'}))
);
named!(
    entity<&[u8],&str,i32>,
    add_return_error!(
        ErrorKind::Custom(2),
        map!(recognize!(entity_name),|v| str::from_utf8(v).unwrap())
    )
);

And the corresponding test

#[test]
fn parse_pdl_error_test() {
    assert_eq!(entity(b"_error_stack").to_result(), Ok("_error_stack"));
    assert_eq!(entity(b"[];']").to_result(), Err(ErrorKind::Custom(2)));
}
Delta_Fore
  • 3,079
  • 4
  • 26
  • 46