Newbie Question: parse a 4 digit year? #628
-
Running the following code: use chumsky::prelude::*;
#[derive(Debug)]
enum Decl {
Year(u64),
}
fn parser() -> impl Parser<char, Decl, Error = Simple<char>> {
let digits = text::digits::<_, Simple<char>>(10);
let year = digits.repeated().exactly(4);
year.then_ignore(end()).map(|c| Decl::Year(0000))
}
fn main() {
let src = std::fs::read_to_string(std::env::args().nth(1).unwrap()).unwrap();
match parser().parse(src) {
Ok(ast) => println!("{:?}", ast),
Err(parse_errs) => parse_errs
.into_iter()
.for_each(|e| println!("Parse error: {}", e)),
}
} produces the following output: It's my understanding that the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Edit: Actually, which version of Chumsky are you using? I think it’s 0.9, but I’m not 100%. If you are on the 1.0.x, lazy-per-default with |
Beta Was this translation helpful? Give feedback.
digits
parses as many digits as it can in one go: so the very first time it's invoked, it's consuming every digit and then the remaining 3 attemts at consuming digits have nothing left to consume, hence 'found end of input'. Consider using the following instead: