Crates.io | teleparse |
lib.rs | teleparse |
version | 0.0.4 |
source | src |
created_at | 2024-05-28 03:08:44.908724 |
updated_at | 2024-08-05 02:32:21.854604 |
description | teleparse |
homepage | |
repository | https://github.com/Pistonite/teleparse |
max_upload_size | |
id | 1253930 |
size | 251,812 |
working in progress - Proc-macro powered LL(1) parsing library
This library is comparable to serde
for parsing - All you need is define the syntax
as data types and call parse()
on the root type.
Features:
#[test]
to ensure the grammar is LL(1), or fail at runtimeCredits:
Progress:
tp
tp
Traditionally recursive grammar can also be simplified with built-in syntax types.
// with recursion
E => T E'
E' => + T E' | ε
T => F T'
T' => * F T' | ε
F => ( E ) | id
// simplified
E => T ( + T )*
T => F ( * F )*
F => ( E ) | id
Which can then be implemented as:
use teleparse::prelude::*;
#[derive_lexicon]
#[teleparse(ignore(r"\s+"))]
pub enum TokenType {
#[teleparse(regex(r"\w+"), terminal(Ident))]
Ident,
#[teleparse(terminal(
OpAdd = "+",
OpMul = "*",
))]
Op,
/// Parentheses
#[teleparse(terminal(
ParenOpen = "(",
ParenClose = ")"
))]
Paren,
}
#[derive_syntax]
#[teleparse(root)]
struct E(tp::Split<T, OpAdd>); // E -> T ( + T )*
#[derive_syntax]
struct T(tp::Split<F, OpMul>); // T -> F ( * F )*
#[derive_syntax]
enum F {
Ident(Ident),
Paren((ParenOpen, Box<E>, ParenClose)),
}
fn main() -> Result<(), teleparse::GrammarError> {
let source = "(a+b)*(c+d)";
let _expr = E::parse(source)?;
Ok(())
}