MCPcopy Index your code
hub / github.com/Y-jiji/peggen

github.com/Y-jiji/peggen @v0.2.6-release

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.2.6-release ↗ · + Follow
98 symbols 153 edges 23 files 23 documented · 23% updated 25d agov0.2.6-release · 2024-09-22★ 33

Browse by type

Functions 71 Types & classes 27
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Peggen

A parser generator for parsing expression grammar (PEG) that use inline macros to specify PEG operations.

How is it different from (...)?

/ Conceptual User Experience Performance Error Handling
PEST PEST only annotates text.

Peggen generates AST directly from your text. | In most cases, you still want rust enums for your AST, which is directly provided by Peggen, but you have to manually create enums from PEST rules. | PEST use an optimizer to memorize your grammar rules, and use memorization for better performance; Peggen doesn't use memorization, arguably this gives better performance over memorization for most grammars. | / | | Chumsky | Chumsky provides parser combinators. Peggen is a parser generator. | Both Chumsky and Peggen provides ast directly. However, Peggen supports arena allocation. | Chumsky deallocates successful sub-rules when a rule fails; Peggen uses a internal representation to eliminate deallocation. | / | | LALRPOP | Peggen is PEG-based; LALRPOP uses LR(1) grammar. | Peggen is more intuitive to use than LALRPOP; LR(1) grammar is hard to extend and debug. | LALRPOP has better performance over Peggen. | LR(1) grammar can report errors far away from normally percepted cause; Peggen allows you to capture errors from customary cause. |

Performance

I roughly tested the peggen on a sample json file against chumsky.

CPU Model: Intel(R) Core(TM) i7-14700HX

Suprisingly, Peggen is faster than Chumsky.

Here are some numbers: - Peggen : 867913 ns/iter - Chumsky: 1464737 ns/iter

Example: Json Parser

You can write a json parser in the following several lines:

#[derive(Debug, ParseImpl, Space, Num, EnumAstImpl)]
pub enum Json {
    #[rule(r"null")]
    Null,
    #[rule(r"{0:`false|true`}")]
    Bool(bool),
    #[rule(r"{0:`-?(0|[1-9][0-9]*)\.([0-9]+)`}")]
    Flt(f32),
    #[rule("{0:`0|-?[1-9][0-9]*`}")]
    Num(i32),
    #[rule(r#""{0:`[^"]*`}""#)]
    Str(String),
    #[rule(r#"\{ [*0: "{0:`[^"]*`}" : {1} , ][?0: "{0:`[^"]*`}" : {1} ] \}"#)]
    Obj(RVec<(String, Json)>),
    #[rule(r"\[ [*0: {0} , ][?0: {0} ] \]")]
    Arr(RVec<Json>)
}

Roadmap

  • Optimizations:
  • Rule dispatch: filter rules by the first symbol, instead of trying each of them.
  • Thinner tag: currently each tag in internal representation is 3-pointers wide, I want to make them thinner.
  • Error Handling:
  • Custom error handlers when error handlers fail.

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Method 55
Function 16
Class 10
Interface 10
Enum 7

Languages

Rust100%

Modules by API surface

peggen-macs/src/fmtparser.rs21 symbols
peggen-core/src/lib.rs9 symbols
peggen-macs/src/lib.rs8 symbols
peggen-core/src/span.rs8 symbols
peggen-impl/src/rvec.rs6 symbols
peggen-macs/src/builder.rs5 symbols
peggen-impl/src/bump.rs5 symbols
peggen-core/src/prepend.rs4 symbols
peggen-core/src/parser.rs4 symbols
peggen-macs/src/builder/rules_impl_build.rs3 symbols
peggen-macs/src/builder/parse_impl_build.rs3 symbols
peggen-macs/src/builder/ast_impl_build.rs3 symbols

For agents

$ claude mcp add peggen \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page