ketchup
A blazingly fast parser that can ketch - up with your parsing needs.
Freedom from Frameworks
The best parser is always a hand-written parser
I won't bore you to death with the details of the philosophy behind ketchup.
All you need to know to get started is that:
- ketchup is not a parsing framework, and it is not a complete be-all end-all solution.
- ketchup is library, that's designed in a way as to be used alongside other parsing techniques; it's designed to be embedded wherever it is deemed most effective to automate parsing that would be otherwise too arduous to hand-write and maintain.
- ketchup should not, and does not, define how your project is structured or works and it should never be used alone.
In short, ketchup gives you the freedom to choose.
Examples
for more complete examples, see the examples folder
A minimal maths demo
#[derive(Debug, Clone, PartialEq, Eq)]
enum Expr {
Number(i32),
Add,
Sub,
Mul,
Div,
Neg,
Pos,
}
impl ketchup::node::Node for Expr {
const MAX_PRECEDENCE: ketchup::Precedence = 2;
fn get_kind(&self) -> ketchup::node::NodeKind {
use ketchup::node::NodeKind;
match self {
Expr::Number(_) => NodeKind::Operand,
Expr::Pos => NodeKind::Unary,
Expr::Neg => NodeKind::Unary,
Expr::Add => NodeKind::Binary,
Expr::Sub => NodeKind::Binary,
Expr::Mul => NodeKind::Binary,
Expr::Div => NodeKind::Binary,
}
}
fn get_precedence(&self) -> ketchup::Precedence {
match self {
Expr::Number(_) => unreachable!(),
Expr::Pos => 2,
Expr::Neg => 2,
Expr::Mul => 1,
Expr::Div => 1,
Expr::Add => 0,
Expr::Sub => 0,
}
}
}
fn main() {
use ketchup::prelude::*;
let mut asa = VectorASA::<Expr>::new(Expr::MAX_PRECEDENCE);
let number = Expr::Number(12);
parse::operand(number, &mut asa).unwrap();
parse::binary_node(Expr::Add, true, &mut asa).unwrap();
let number = Expr::Number(4);
parse::operand(number, &mut asa).unwrap();
parse::binary_node(Expr::Mul, true, &mut asa).unwrap();
let number = Expr::Number(8);
parse::operand(number, &mut asa).unwrap();
assert!(*asa.is_complete()); assert_eq!( asa.vector[..],
[ Expr::Add,
Expr::Number(12), Expr::Mul,
Expr::Number(4), Expr::Number(8),
],
);
}