Sergey
Sergey

Reputation: 21221

How to get the output for several sequential nom parsers when the input is a &str?

This question is almost identical to Capture the entire contiguous matched input with nom, but I have to parse UTF-8 text as input (&str) not just bytes (&[u8]). I am trying to get the whole match for several parsers:

named!(parse <&str, &str>,
           recognize!(
               chain!(
                   is_not_s!(".") ~
                       tag_s!(".")    ~
                       is_not_s!( "./ \r\n\t" ),
                   || {}
               )
           )
);

And it causes this error:

no method named "offset" found for type "&str" in the current scope

Is the only way to do this to switch to &[u8] as input and then do map_res!?

Upvotes: 0

Views: 225

Answers (1)

G&#233;al
G&#233;al

Reputation: 1482

there's an Offset trait implementation for &str that will be available in the next version of nom. There is no planned release date yet for nom 2.0, so in the meantime, you can copy the implementation in your code:

use nom::Offset;

impl Offset for str {
    fn offset(&self, second: &Self) -> usize {
        let fst = self.as_ptr();
        let snd = second.as_ptr();

        snd as usize - fst as usize
    }
}

Upvotes: 2

Related Questions