PEG.js is a parser generator for JavaScript that produces fast parsers based on the Parsing Expression Grammar (PEG) formalism. It enables developers to define grammars for custom languages or complex data formats and generate a JavaScript parser function from them. The current stable version, 0.10.0, was released in August 2016. While the project's direct development on PEG.js itself has largely ceased, its successor, Peggy.js (npm: `peggy`), maintains API compatibility and active development. Key differentiators of PEG.js include its simple, expressive grammar syntax, excellent error reporting, and the ability to integrate both lexical and syntactical analysis into a single grammar. It can be used programmatically via a JavaScript API or through a command-line interface, generating parsers in multiple module formats like CommonJS (default), AMD, UMD, or global variables.
npm install pegjsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `pegjs` to define a simple arithmetic grammar, generate a parser at runtime, and then use that parser to evaluate expressions and catch parsing errors.
Update calls from `pegjs.buildParser` to `pegjs.generate`. For browser environments, use `peg` instead of `PEG` if relying on global exports.
Avoid direct manipulation of internal parser state or variables within generated code. If absolutely necessary, adapt to the `peg$` prefix, but this is discouraged.
Always specify the `format` option in `pegjs.generate` (e.g., `{ format: 'commonjs' }`) or via the `--format` CLI option to ensure the desired output module system.Evaluate migrating to Peggy.js (`npm install peggy`) for an actively maintained and compatible parser generator.
If using tracing features, be prepared for potential API changes. For v0.10.0, consult the specific documentation for its tracing implementation, if available.
Change `pegjs.buildParser(...)` to `pegjs.generate(...)`.
Review grammar rules for left recursion or other patterns that could cause infinite loops. Restructure rules to ensure progress is made with each match.
Use `peg` instead of `PEG` for the global variable. Ensure the generated parser script is included correctly and the format option (e.g., `globals`) is set appropriately during generation.
Debug the input string and grammar. Use the error's `location` property (offset, line, column) to pinpoint the exact mismatch. Consider adding more robust error handling or `error()` calls within grammar actions.
No dependency data recorded yet.