m4rw3r / chomp

A fast monadic-style parser combinator designed to work on stable Rust.
Apache License 2.0
244 stars 19 forks source link

Request: readable str error messages #39

Closed MarkSwanson closed 8 years ago

MarkSwanson commented 8 years ago

My errors print out as:

FAIL: Error([10, 97, 99, 108, ...], Unexpected)

So I have to manually decode each number using the ascii table. This makes for a long/painful edit/compile/test cycle.

I need the ParseErrors to be printed as a human readable strings. If I can get access to the underlying &[u8] I'm happy to convert to a &str myself (ParseError does not provide access?)

Example code:

match parse_only(test, TEST_SIMPLE.as_bytes()) {
    Ok(p) => assert_eq!(p, r),
    Err(e) => panic!("FAIL: {:?}", e)
};

I enabled verbose_error, but I still only see encoded bytes.

[dependencies.chomp] features = ["verbose_error"] version = "*"

Any thoughts / suggestions on how I can use chomp better would be much appreciated. Thanks!

m4rw3r commented 8 years ago

verbose_error is enabled by default, so there is no need to activate it manually.

The ParseError::Error contains the failing slice and the error, a u8 slice can easily be printed using String::from_utf8_lossy. The error chomp::parsers::Error::Unexpected is caused by a failing satisfy, satisfy_with, not_token, take_while1 (failure on first match) or eof. When this error occurs the first token of the error slice is token which fails the predicate.

You should be able to destructure it like usual though:

match parse_only(test, TEST_SIMPLE.as_bytes()) {
    Ok(p)                          => assert_eq!(p, r),
    Err(ParseError::Error(b, e))   => panic!("FAIL: {:?} at \"{}\"", e, String::from_utf8_lossy(b)),
    Err(ParseError::Incomplete(n)) => panic!("FAIL: Requested {} additional bytes", n),
};

For a much more verbose error (well, debug really) I have this #23 which is intended to be used when building a parser as it will have a huge performance impact.

MarkSwanson commented 8 years ago

That works. Thanks!