Module chomp::types::numbering [] [src]

Module containing tools for working with position aware parsers.

use chomp::types::{Input, ParseResult};
use chomp::types::numbering::{InputPosition, LineNumber, Numbering};
use chomp::combinators::many;
use chomp::parsers::{Error, any, take_while1, string};
use chomp::run_parser;

// Let's count some lines
let i = InputPosition::new(&b"test a\ntest b\n\ntest c\n"[..], LineNumber::new());

// We could use a concrete type P too if we want to restrict to a
// certain position-aware implementation
fn parser<I: Input<Token=u8>, P: Numbering<Token=u8>>(i: InputPosition<I, P>)
  -> ParseResult<InputPosition<I, P>, (char, P), Error<u8>> {
    parse!{i;
                     string(b"test");
                     take_while1(|c| c == b' ' || c == b'\t');
        let t_name = any();
        i -> {
            // Obtain current parser position
            let p = i.position();

            i.ret((t_name as char, p))
            // We skip the take while below, because we want to determine the line right
            // after reading the t_name
        } <* take_while1(|c| c == b'\n');
    }
}

let r = run_parser(i, |i| many(i, parser)).1;

assert_eq!(r, Ok(vec![('a', LineNumber(0)),
                      ('b', LineNumber(1)),
                      // Note the two linebreaks in a row
                      ('c', LineNumber(3))]));

Structs

InputPosition

Wrapper around an Input implementation providing numbering support.

LineNumber

Struct counting the number of newlines (b'\n').

Traits

Numbering

Trait for managing some kind of numbering over the parsed data.