| Crates.io | sipha-pratt |
| lib.rs | sipha-pratt |
| version | 0.3.0 |
| created_at | 2025-11-19 17:57:10.542604+00 |
| updated_at | 2025-11-22 09:50:47.262825+00 |
| description | Pratt parser for operator precedence parsing in sipha |
| homepage | |
| repository | https://github.com/NyalephTheCat/sipha |
| max_upload_size | |
| id | 1940500 |
| size | 29,649 |
Pratt parser implementation for operator precedence in sipha.
sipha-pratt provides the core logic for Pratt parsing, which is used to handle operator precedence and associativity in expressions.
Add sipha-pratt to your Cargo.toml:
[dependencies]
sipha-pratt = "0.1.1"
use sipha_pratt::{PrattParser, Precedence, Associativity, BinaryRule, PrattRuleKind};
use sipha_core::traits::{TokenKind, RuleId};
use std::collections::HashMap;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
enum Token { Plus, Star }
impl TokenKind for Token { fn is_trivia(&self) -> bool { false } }
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
enum Rule { Add, Mul }
impl RuleId for Rule {}
struct MyParser {
pratt_rules: HashMap<Token, PrattRuleKind<Token, Rule>>,
}
impl PrattParser<Token, Rule> for MyParser {
fn get_rule(&self, kind: &Token) -> Option<&PrattRuleKind<Token, Rule>> {
self.pratt_rules.get(kind)
}
}
let mut parser = MyParser {
pratt_rules: HashMap::new(),
};
parser.pratt_rules.insert(
Token::Plus,
PrattRuleKind::Binary(BinaryRule::create(Rule::Add, Precedence(10), Associativity::Left)),
);
parser.pratt_rules.insert(
Token::Star,
PrattRuleKind::Binary(BinaryRule::create(Rule::Mul, Precedence(20), Associativity::Left)),
);
This project is licensed under the MIT License - see the LICENSE file for details.