extern crate combine;
use combine::*;
use combine::primitives::{SourcePosition, Stream};
#[derive(PartialEq, Debug)]
struct Word {
word: String,
pos: SourcePosition,
}
fn w(s: &str, l: i32, c: i32) -> Word {
Word {
word: s.into(),
pos: SourcePosition {
line: l,
column: c,
},
}
}
fn word<I>(input: State<I>) -> ParseResult<Word, I>
where I: Stream<Item = char>
{
let p = input.position;
many1(alpha_num()).map(|s| Word { word: s, pos: p }).parse_state(input)
}
fn words<I>(input: State<I>) -> ParseResult<Vec<Word>, I>
where I: Stream<Item = char>
{
(spaces(), sep_by(parser(word), spaces::<I>()), spaces()).map(|r| r.1).parse_state(input)
}
#[test]
fn test() {
assert_eq!(parser(words)
.parse("little red riding hood\n damsel in distress\n \n \
mario"),
Ok((vec![
w("little", 1, 1),
w("red", 1, 8),
w("riding", 1, 12),
w("hood", 1, 19),
w("damsel", 2, 7),
w("in", 2, 17),
w("distress", 2, 27),
w("mario", 4, 2),
],
"")));
}
fn main() {}