Skip to content

Instantly share code, notes, and snippets.

@avwhite
Created January 27, 2015 23:58
Show Gist options
  • Save avwhite/94a76e0718f095d5cfdc to your computer and use it in GitHub Desktop.
Save avwhite/94a76e0718f095d5cfdc to your computer and use it in GitHub Desktop.
extern crate "parser-combinators" as parser;
use parser::{Parser, ParserExt};
fn main() {
let good_test = "2 + 12 + 22";
let integer = parser::spaces().with(
parser::chars1(parser::digit()).map(|s| s.parse::<i32>().unwrap())
);
let plus = parser::spaces().with(parser::string("+"));
let mut sum = parser::chainl1(integer, plus.map(|str|
Box::new(|&mut: a:i32, b:i32| {a + b}) as Box<FnMut(i32,i32) -> i32>
));
match sum.parse(good_test) {
Ok((value, _)) => println!("Good! {}", value),
Err(err) => println!("{}", err)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment