npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-pegjs

v4.2.1

Published

TS target for peggy parser generator

Downloads

181,768

Readme

TS PEG.js

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

Build Status Known Vulnerabilities npm version

NPM

Requirements

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:

var peggy = require('peggy');
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

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
{
    "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:

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

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

Changelog

Changelog.

Acknowledgments

Thanks to:

License

The MIT License (MIT)


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