All Projects → metadevpro → Ts Pegjs

metadevpro / Ts Pegjs

Licence: mit
Plugin for pegjs to generate TypeScript parsers.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Ts Pegjs

Npeg
PEGs for Nim, another take
Stars: ✭ 163 (+114.47%)
Mutual labels:  parser-generator, peg
Cpp Peglib
A single file C++ header-only PEG (Parsing Expression Grammars) library
Stars: ✭ 435 (+472.37%)
Mutual labels:  parser-generator, peg
Rust Peg
Parsing Expression Grammar (PEG) parser generator for Rust
Stars: ✭ 836 (+1000%)
Mutual labels:  parser-generator, peg
peg
Import of Ian Piumarta's peg/leg recursive-descent parser generators for C
Stars: ✭ 41 (-46.05%)
Mutual labels:  parser-generator, peg
kiuatan
A parser library for Pony.
Stars: ✭ 15 (-80.26%)
Mutual labels:  parser-generator, peg
Pegparser
💡 Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.
Stars: ✭ 164 (+115.79%)
Mutual labels:  parser-generator, peg
Lug
Parsing expression grammar (PEG) embedded domain specific language and parsing machine for C++17
Stars: ✭ 44 (-42.11%)
Mutual labels:  parser-generator, peg
Pegjs
PEG.js: Parser generator for JavaScript
Stars: ✭ 4,176 (+5394.74%)
Mutual labels:  parser-generator, peg
pe
Fastest general-purpose parsing library for Python with a familiar API
Stars: ✭ 21 (-72.37%)
Mutual labels:  parser-generator, peg
Pigeon
Command pigeon generates parsers in Go from a PEG grammar.
Stars: ✭ 603 (+693.42%)
Mutual labels:  parser-generator, peg
Rascal
The implementation of the Rascal meta-programming language (including interpreter, type checker, parser generator, compiler and JVM based run-time system)
Stars: ✭ 284 (+273.68%)
Mutual labels:  parser-generator
Pom
PEG parser combinators using operator overloading without macros.
Stars: ✭ 310 (+307.89%)
Mutual labels:  peg
Owl
A parser generator for visibly pushdown languages.
Stars: ✭ 645 (+748.68%)
Mutual labels:  parser-generator
gpkeditor
markdown editor with peg.js
Stars: ✭ 15 (-80.26%)
Mutual labels:  peg
inmemantlr
ANTLR as a libray for JVM based languages
Stars: ✭ 87 (+14.47%)
Mutual labels:  parser-generator
3bmd
markdown processor in CL using esrap parser
Stars: ✭ 58 (-23.68%)
Mutual labels:  peg
Myna Parser
Myna Parsing Library
Stars: ✭ 69 (-9.21%)
Mutual labels:  peg
intellij-javacc
JavaCC and JJTree grammar support for the IntelliJ Platform
Stars: ✭ 31 (-59.21%)
Mutual labels:  parser-generator
JavaScript
JavaScript version ( Browser and Node.js ) of SimplePEG
Stars: ✭ 14 (-81.58%)
Mutual labels:  peg
Foundatio.parsers
A lucene style query parser that is extensible and allows modifying the query.
Stars: ✭ 39 (-48.68%)
Mutual labels:  peg

TS PEG.js

TS PEG.js is a TS code generation plugin for PEG.js.

Build Status Dependency Status Known Vulnerabilities npm version

NPM

Requirements

Installation

Node.js

Install PEG.js with ts-pegjs plugin

$ npm install ts-pegjs

Usage

Generating a Parser from JS code

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

var pegjs = require("pegjs");
var tspegjs = require("ts-pegjs");

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

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

  • 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

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
{
    "tspegjs": {
        "customHeader": "// import lib\nimport { Lib } from 'mylib';"
    }
}

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:

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

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

Changelog

Changelog.

Acknowledgments

Thanks to:

License

The MIT License (MIT)


(c) 2017-2021, Pedro J. Molina at metadev.pro

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].