pegjs-util is a utility library for the Peggy parser generator (formerly PEG.js), currently at stable version 2.0.2. It enhances Peggy's core `parse` function by injecting convenient utilities directly into grammar actions. The library provides three main features: Parser Tree Token Unrolling, which simplifies common patterns of extracting tokens from repeated grammar rule matches; Abstract Syntax Tree (AST) Node Generation, which assists in building structured ASTs directly within grammar rules; and improved, "cooked" Error Reporting, offering more user-friendly diagnostics than Peggy's default output. Releases appear to follow the development of Peggy itself, with the latest versions published as needed. It differentiates itself by streamlining common parser generator tasks, reducing boilerplate in `.peggy` grammar files, and providing a more robust parsing and error reporting experience.
npm install pegjs-utilVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `pegjs-util` to parse a simple language, generate an Abstract Syntax Tree (AST) with `asty`, and benefit from enhanced error reporting. It shows the integration of `unroll` and `ast` helpers within a Peggy grammar, enabled by `PEGUtil.parse`.
Use `const PEGUtil = require('pegjs-util');` for importing the library in Node.js environments.Always use `PEGUtil.parse(parser, input, options)` to leverage the utility features within your Peggy grammars.
If using the AST generation features as demonstrated, ensure `asty` is installed: `npm install asty`.
Ensure your project uses `peggy` (version 1.x or newer) and update `require('pegjs')` or `import 'pegjs'` statements to `require('peggy')` or `import 'peggy'` respectively. Consult Peggy's migration guide for details.Examine the error message, specifically the expected tokens and the found character at the indicated line and column, and correct the input string or adjust the grammar definition.
Ensure that your parser execution uses `PEGUtil.parse(parser, input, options)` instead of Peggy's direct `parser.parse(input, options)`.
Install the `asty` package (`npm install asty`) and ensure it is correctly imported (`const ASTY = require('asty');`) in the file where the `makeAST` callback is defined.