A top-down parsing library for Rust.
```
extern crate topdown;
extern crate topdown; use topdown::{CharSeq, skip, Parser, wrap, choice, ParserResult, re, chainl}; use topdown::ParserResult::{Succ, Error, Fail};
fn op1<'a>(cs: &mut CharSeq) -> ParserResult<|int, int|:'a->int> { return runparser!( cs >> &re("\+|-") -> |o| match o.asslice() { "+" => |a:int, b:int| a+b, "-" => |a:int, b:int| a-b, _ => panic!(o) } ) }
fn op2<'a>(cs: &mut CharSeq) -> ParserResult<|int, int|:'a->int> { return runparser!( cs >> &re("\*|/|%") -> |o| match o.asslice() { "" => |a:int, b:int| ab, "/" => |a:int, b:int| a/b, "%" => |a:int, b:int| a%b, _ => panic!(o) } ) }
fn digit(cs: &mut CharSeq) -> ParserResult
fn factor(cs: &mut CharSeq) -> ParserResult
fn term(cs: &mut CharSeq) -> ParserResult
fn expr(cs: &mut CharSeq) -> ParserResult
fn calc(e: &str) { let mut cs = CharSeq::new(e, ""); let skip = skip(" "); cs.add_hook(&skip); match cs.accept(&wrap(expr)) { Succ(x) => { if !cs.eof() { println!("error: {}", e); } else { println!("{}={}", e, x) } }, Fail(m, l) => println!("error: {}", e), Error(m, l) => println!("error: {}", e) } }
fn main() { calc("1 + 2 - 3 * 4"); calc("1 + (2 - 3) * 4"); calc("1 + (2 - 3)) * 4"); calc("1 + (2 - 3 * 4"); } ```
MIT