Peggy

Parser generator for JavaScript using parsing expression grammars (PEG)

Tool
npm
v5.1.0
1,177stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
51/100Fair
Development Activity8
Maintenance32
Community64
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
72/100Good
Architecture75
Code Quality78
Innovation68
Learning Curve65

Peggy is a parser generator for JavaScript that compiles a parsing expression grammar (PEG) into a fast, standalone parser with excellent built-in error reporting. It is the actively maintained successor to PEG.js, offering near drop-in API compatibility for anyone migrating.

Peggy integrates lexical and syntactic analysis into a single grammar, letting developers process complex data formats or design custom languages — building interpreters, compilers, config-file parsers, and other language tools — without hand-writing a tokenizer and parser separately. It’s usable from the command line, as a JavaScript/TypeScript API, or directly in the browser.

What You Get

  • A peggy CLI (bin/peggy.js) that compiles .peggy/.pegjs grammar files into standalone parser modules
  • A JavaScript/TypeScript API (peggy.generate()) for compiling grammars programmatically at build time or runtime
  • PEG (parsing expression grammar) semantics — more expressive and predictable than traditional LL(k)/LR(k) parser generators
  • Source map support for debugging generated parsers back to the original grammar
  • A browser-ready bundle (browser/peggy.min.js) for client-side grammar compilation

Common Use Cases

  • Parsing custom domain-specific languages (DSLs), config formats, or query languages in a Node.js or browser app
  • Replacing hand-rolled regex-based parsers with a maintainable, testable formal grammar
  • Migrating existing PEG.js-based parsers to actively maintained tooling with minimal code changes
  • Building compilers, interpreters, or linters that need a robust parsing front-end with rich error messages

Under The Hood

Architecture: Peggy’s own grammar parser is bootstrapped from a src/parser.pegjs grammar file (compiled by Peggy itself) into lib/parser.js, and the lib/compiler/ directory implements a multi-pass compiler pipeline that walks the parsed grammar AST, performs semantic checks (via grammar-error.js for structured error reporting with source locations), and emits generated parser JavaScript. The CLI (bin/peggy.js) and the public peg.js API both funnel into this shared compiler core, so command-line and programmatic usage produce identical output. Tech Stack: Built with a pnpm workspace, TypeScript type declarations (peg.d.ts) ship alongside the JS output via tsup, and the build pipeline (npm run build) chains grammar self-compilation, TS type generation, docs, and a browser bundle target (browser/peggy.min.js) distributed via unpkg/jsDelivr. Code Quality: The test suite is large relative to the implementation — roughly 12,000 lines of tests (test/api, test/behavior, test/cli, test/unit) against about 9,900 lines of lib/ source — covering the CLI, the generate API, and the behavior of generated parsers themselves; CI runs via GitHub Actions with Codecov coverage tracking. API Design: peggy.generate(grammar, options) is a single clear entry point returning either a parser module or generated source, and the CLI mirrors the same option surface, so switching between build-time codegen and runtime compilation requires no relearning — a deliberate design carried over from PEG.js to ease migration.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search