# ts-pegjs

> TS target for peggy parser generator

Latest version **4.2.3** (published 2026-10-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-pegjs
pnpm add ts-pegjs
yarn add ts-pegjs
bun add ts-pegjs
```

Provides the command `tspegjs`.

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.2.3 |
| Published | 2026-10-28 |
| First published | 2017-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 71.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 162 |
| Author | Pedro J. Molina |
| Maintainers | pjmolina |
| Keywords | peggy, pegjs, plugin, ts, typescript, parser, generator |

## Links

- npm: https://www.npmjs.com/package/ts-pegjs
- Repository: https://github.com/metadevpro/ts-pegjs
- Homepage: https://github.com/metadevpro/ts-pegjs#readme
- Issues: https://github.com/metadevpro/ts-pegjs/issues
- npm.io page: https://npm.io/package/ts-pegjs

## Dependencies (3)

- [synckit](https://npm.io/package/synckit.md) ^0.12.3
- [prettier](https://npm.io/package/prettier.md) ^3.9.10
- [ts-morph](https://npm.io/package/ts-morph.md) ^28.0.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 4.2.3 (latest) — 2026-10-28
- 4.2.2 — 2026-07-29
- 4.2.1 — 2023-06-05
- 4.2.0 — 2023-06-02
- 4.1.1 — 2023-05-21
- 4.1.0 — 2023-04-15
- 4.0.0 — 2023-04-10
- 3.1.0 — 2023-03-03
- 3.0.0 — 2023-01-02
- 2.2.1 — 2023-01-02
- 2.2.0 — 2023-01-02
- 2.1.0 — 2022-07-26
- 2.0.2 — 2022-07-05
- 2.0.1 — 2022-07-04
- 2.0.0 — 2022-07-04
- … 31 more at https://npm.io/package/ts-pegjs/versions

## README

# TS PEG.js

TS PEG.js is a TS code generation plugin for [peggy](https://www.npmjs.com/package/peggy).

[![Build Status](https://travis-ci.org/metadevpro/ts-pegjs.svg?branch=master)](https://travis-ci.org/metadevpro/ts-pegjs)
[![Known Vulnerabilities](https://snyk.io/test/github/metadevpro/ts-pegjs/badge.svg)](https://snyk.io/test/github/metadevpro/ts-pegjs)
[![npm version](https://badge.fury.io/js/ts-pegjs.svg)](http://badge.fury.io/js/ts-pegjs)

## Requirements

- [peggy](https://www.npmjs.com/package/peggy) (previous versions use: [pegjs](https://pegjs.org))

## Installation

### Node.js

Installs ts-pegjs + peggy

    npm install ts-pegjs

## Usage

### Generating a Parser from JS code

In Node.js, require both the peggy parser generator and the ts-pegjs plugin:

```typescript
var peggy = require('peggy');
var tspegjs = require('ts-pegjs');
```

To generate a TS parser, pass to `pegjs.generate` ts-pegjs plugin and your grammar:

```typescript
var parser = pegjs.generate("start = ('a' / 'b')+", {
  output: 'source',
  format: 'commonjs',
  plugins: [tspegjs],
  tspegjs: {
    customHeader: "// import lib\nimport { Lib } from 'mylib';"
  }
});
```

The method will return source code of generated parser as a string.

Supported options of `pegjs.generate`:

- `cache` — if `true`, makes the parser cache results, avoiding exponential
  parsing time in pathological cases but making the parser slower (default:
  `false`). This is strongly recommended for big grammars
  (like javascript.pegjs or css.pegjs in example folder)
- `allowedStartRules` — rules the parser will be allowed to start parsing from
  (default: the first rule in the grammar)

### Plugin options

**Note:** Options in CLI mode are written in POSIX (long names as kebab-case) convention e.g. `--custom-header` but with camelcase on JavaScript e.g. `customHeader`.

- `customHeader` — A string or an array of strings which are a valid TS code to be injected on the header of the output file. E.g. provides a convenient place for adding library imports.
- `customHeaderFile` — A header file to include.
- `errorName` — The name of the exported internal error class to override. The default value from version 3.0.0 is `PeggySyntaxError`. Previous one was `SyntaxError`.
- `returnTypes` — An object containing rule names as keys and a valid TS return type as string.
- `skipTypeComputation` — Boolean. If `true`, `ts-pegjs` will not try to use TS to infer types based on your grammar rules.
- `onlyGenerateGrammarTypes` — Boolean. If `true`, only types for your grammar rules (and no parser) will be generated. Cannot be used with `skipTypeComputation`.
- `doNotCamelCaseTypes` — Boolean. By default type names for grammar rules are converted to CamelCase. If `true`, this conversion is not done and type names will match the casing of your grammar rules.

### Generating a Parser from CLI

Sample usage:

```
peggy --plugin ./src/tspegjs -o examples/arithmetics.ts --cache examples/arithmetics.pegjs
```

(Note `./src/tspegjs` is the path to `tspegjs.ts` in the project. If you installed ts-pegjs using npm, it should probably be `./node_modules/ts-pegjs/src/tspegjs`.)

It will generarate the parser in the TS flavour.

If you need to pass specific plugin options you can use the option `--extra-options-file` provided by pegjs and pass it a filename (e.g. pegconfig.json) containing specific options like the following JSON sample:

```
peggy --plugin ./src/tspegjs --extra-options-file pegconfig.json -o examples/arithmetics.ts --cache examples/arithmetics.pegjs
```

```json
{
  "tspegjs": {
    "customHeader": "// import lib\nimport { Lib } from 'mylib';"
  },
  "returnTypes": {
    "Integer": "number",
    "Expression": "number"
  }
}
```

> For rules not listed in `returnTypes` object `any` type is declared by default.

> Make sure to pass any additional CLI options, like `--extra-options-file` before the parameter `-o` as these will otherwise be treated as arguments to that one.

## Using the Parser

1. Save parser generated by `pegjs.generate` to a file or use the one generated from the CLI tool.

2. In client TS code:

```typescript
import { PeggySyntaxError, parse } from './arithmetics';

try {
  const sampleOutput = parse('my sample...');
} catch (ex: PeggySyntaxError) {
  // Handle parsing error
  // [...]
}
```

## Changelog

[Changelog](./Changelog.md).

## Acknowledgments

Thanks to:

- [David Majda](https://github.com/dmajda) for creating pegjs
- [Elantcev Mikhail](https://github.com/Nordth) for providing the pegjs PHP plugin, inspiration on this one.

## License

[The MIT License (MIT)](http://opensource.org/licenses/MIT)

---

(c) 2017-2026, [Pedro J. Molina](https://github.com/pjmolina) at [metadev.pro](https://metadev.pro)

---
_Source: https://npm.io/package/ts-pegjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
