Swiftlet - Text parsing library
Swiftlet is a high-performance text-parsing library for Rust, inspired by Python’s Lark.
It accepts a context-free grammar (CFG) as input and generates an efficient parser capable of analyzing and validating any text that conforms to the specified grammar. The parser constructs a well-structured Abstract Syntax Tree (AST), enabling further semantic analysis, transformation, or code generation.
Designed with Rust’s performance, and safety in mind, Swiftlet aims to provide a robust, extensible, and developer-friendly framework for building custom parsers, interpreters, and compilers with minimal boilerplate.
Features
- Earley parser, capable to parse any context free grammar (CFG).
- Use cache, to further optimize parsing.
- Canonical-LR (CLR) parser, less powerful than Earley parser, but it's fast and memory optimize.
- EBNF base grammar
- Builds a parse-tree (AST) based on the grammar.
- Display each step of parser, when debug set true.
- Line and column tracking.
- Common terminals are added, list mention below.
Example
use ;
use Arc;
Output
AST: Tree("start", [Tree("expr", [Tree("sub", [Tree("expr", [Tree("add", [Tree("expr", [Tree("sub", [Tree("expr", ["10"]), "-", "2"])]), "+", "5"])]), "-", "2"])])])
Total: 11
For more examples, please check examples folder.
Grammar reference
Click here