Skip to content

Commit

Permalink
Merge pull request #60 from metadevpro/feature/support-for-peggy
Browse files Browse the repository at this point in the history
close #59. Upgrade to peggy. publish as 1.0.0
  • Loading branch information
pjmolina committed May 26, 2021
2 parents be0ca97 + b6ca98d commit a48e1c0
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 357 deletions.
12 changes: 9 additions & 3 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Changelog

## Version 1.0.0

2021.05.26

- Major change: [#59](https://github.com/metadevpro/ts-pegjs/issues/59). Changed dependency from [pegjs](https://github.com/pegjs/pegjs) (unmantained) in favour of [peggy](https://github.com/peggyjs/peggy) (a sensible mantained successor).

## Version 0.3.1

2021.02.09

- Fix [#53](https://github.com/metadevpro/ts-pegjs/issues/53). Errata in `--allowed-start-rules`.
- Fix [#53](https://github.com/metadevpro/ts-pegjs/issues/53). Errata in `--allowed-start-rules`.

## Version 0.3.0

2020.12.31

- Added ESLint for checking TS.
- Removed support for TSLint rules. Removed options: `noTslint` and `tslintIgnores`
- Added ESLint for checking TS.
- Removed support for TSLint rules. Removed options: `noTslint` and `tslintIgnores`
63 changes: 27 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TS PEG.js

TS PEG.js is a TS code generation plugin for [PEG.js](https://pegjs.org).
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)
[![Dependency Status](https://david-dm.org/metadevpro/ts-pegjs.svg)](https://david-dm.org/metadevpro/ts-pegjs)
Expand All @@ -9,40 +9,38 @@ TS PEG.js is a TS code generation plugin for [PEG.js](https://pegjs.org).

[![NPM](https://nodei.co/npm/ts-pegjs.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/ts-pegjs/)


## Requirements

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

## Installation

### Node.js

Install PEG.js with ts-pegjs plugin
Installs ts-pegjs + peggy

$ npm install ts-pegjs

Usage
-----
## Usage

### Generating a Parser from JS code

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

```typescript
var pegjs = require("pegjs");
var tspegjs = require("ts-pegjs");
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",
output: 'source',
format: 'commonjs',
plugins: [tspegjs],
"tspegjs": {
"customHeader": "// import lib\nimport { Lib } from 'mylib';"
tspegjs: {
customHeader: "// import lib\nimport { Lib } from 'mylib';"
}
});
```
Expand All @@ -51,32 +49,32 @@ 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
- `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
- `allowedStartRules` — rules the parser will be allowed to start parsing from
(default: the first rule in the grammar)

### Plugin options

* `custom-header` — A custom header of TS code to be injected on the header of the output file. E.g. provides a convenient place for adding library imports.
* `returnTypes` — An object containing rule names as keys and return type as string.
- `custom-header` — A custom header of TS code to be injected on the header of the output file. E.g. provides a convenient place for adding library imports.
- `returnTypes` — An object containing rule names as keys and return type as string.

### Generating a Parser from CLI

Sample usage:

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

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:

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

```json
Expand All @@ -89,45 +87,38 @@ pegjs --plugin ./src/tspegjs --extra-options-file pegconfig.json -o examples/ari

> 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
----------------
## Using the Parser

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

2) In client TS code:
2. In client TS code:

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

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

Changelog
---------------
## Changelog

[Changelog](./Changelog.md).

Acknowledgments
---------------
## 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.

- [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
-------
## License

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

---

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

0 comments on commit a48e1c0

Please sign in to comment.