Code Monkey home page Code Monkey logo

ts-pegjs's Introduction

TS PEG.js

Greenkeeper badge

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

Build Status Dependency Status bitHound Overall Score 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')+", {
    plugins: [tspegjs]
    "tspegjs": {
        "noTslint": false,
        "tslintIgnores": "rule1,rule2",
        "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

  • no-tslint — Excludes the default tslint rules exclusions (defaults to false).
  • tslint-ignores — A custom list of tslint rules to be excluded (comma separated, if no provided, a default tslint rule exclusion list will be applied).
  • 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.

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": {
        "noTslint": false,
        "tslintIgnores": "rule1,rule2",
        "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
        // [...]
    }

Acknowledgments

Thanks to:

License

The MIT License (MIT)


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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.