{"id":27974279,"url":"https://github.com/metadevpro/ts-pegjs","last_synced_at":"2025-05-15T09:07:20.356Z","repository":{"id":22565496,"uuid":"96700110","full_name":"metadevpro/ts-pegjs","owner":"metadevpro","description":"Plugin for pegjs to generate TypeScript parsers.","archived":false,"fork":false,"pushed_at":"2025-01-22T06:35:49.000Z","size":477,"stargazers_count":159,"open_issues_count":14,"forks_count":38,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-08T00:13:44.460Z","etag":null,"topics":["parser-generator","peg","pegjs","pegjs-plugin","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/metadevpro.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-07-09T17:32:27.000Z","updated_at":"2025-04-17T15:40:22.000Z","dependencies_parsed_at":"2023-02-19T14:45:30.474Z","dependency_job_id":"2ec5be0b-cbc9-4883-b92d-4cc755413efd","html_url":"https://github.com/metadevpro/ts-pegjs","commit_stats":{"total_commits":121,"total_committers":24,"mean_commits":5.041666666666667,"dds":0.3471074380165289,"last_synced_commit":"2b605b7a72717bf5e205e580cdb86e0f02491c7a"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metadevpro%2Fts-pegjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metadevpro%2Fts-pegjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metadevpro%2Fts-pegjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metadevpro%2Fts-pegjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/metadevpro","download_url":"https://codeload.github.com/metadevpro/ts-pegjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254310515,"owners_count":22049469,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["parser-generator","peg","pegjs","pegjs-plugin","typescript"],"created_at":"2025-05-08T00:13:39.234Z","updated_at":"2025-05-15T09:07:15.341Z","avatar_url":"https://github.com/metadevpro.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TS PEG.js\n\nTS PEG.js is a TS code generation plugin for [peggy](https://www.npmjs.com/package/peggy).\n\n[![Build Status](https://travis-ci.org/metadevpro/ts-pegjs.svg?branch=master)](https://travis-ci.org/metadevpro/ts-pegjs)\n[![Known Vulnerabilities](https://snyk.io/test/github/metadevpro/ts-pegjs/badge.svg)](https://snyk.io/test/github/metadevpro/ts-pegjs)\n[![npm version](https://badge.fury.io/js/ts-pegjs.svg)](http://badge.fury.io/js/ts-pegjs)\n\n[![NPM](https://nodei.co/npm/ts-pegjs.png?downloads=true\u0026downloadRank=true\u0026stars=true)](https://nodei.co/npm/ts-pegjs/)\n\n## Requirements\n\n-   [peggy](https://www.npmjs.com/package/peggy) (previous versions use: [pegjs](https://pegjs.org))\n\n## Installation\n\n### Node.js\n\nInstalls ts-pegjs + peggy\n\n    $ npm install ts-pegjs\n\n## Usage\n\n### Generating a Parser from JS code\n\nIn Node.js, require both the peggy parser generator and the ts-pegjs plugin:\n\n```typescript\nvar peggy = require('peggy');\nvar tspegjs = require('ts-pegjs');\n```\n\nTo generate a TS parser, pass to `pegjs.generate` ts-pegjs plugin and your grammar:\n\n```typescript\nvar parser = pegjs.generate(\"start = ('a' / 'b')+\", {\n    output: 'source',\n    format: 'commonjs',\n    plugins: [tspegjs],\n    tspegjs: {\n        customHeader: \"// import lib\\nimport { Lib } from 'mylib';\"\n    }\n});\n```\n\nThe method will return source code of generated parser as a string.\n\nSupported options of `pegjs.generate`:\n\n-   `cache` — if `true`, makes the parser cache results, avoiding exponential\n    parsing time in pathological cases but making the parser slower (default:\n    `false`). This is strongly recommended for big grammars\n    (like javascript.pegjs or css.pegjs in example folder)\n-   `allowedStartRules` — rules the parser will be allowed to start parsing from\n    (default: the first rule in the grammar)\n\n### Plugin options\n\n**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`.\n\n-   `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.\n-   `customHeaderFile` — A header file to include.\n-   `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`.\n-   `returnTypes` — An object containing rule names as keys and a valid TS return type as string.\n-   `skipTypeComputation` — Boolean. If `true`, `ts-pegjs` will not try to use TS to infer types based on your grammar rules.\n-   `onlyGenerateGrammarTypes` — Boolean. If `true`, only types for your grammar rules (and no parser) will be generated. Cannot be used with `skipTypeComputation`.\n-   `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.\n\n### Generating a Parser from CLI\n\nSample usage:\n\n```\npeggy --plugin ./src/tspegjs -o examples/arithmetics.ts --cache examples/arithmetics.pegjs\n```\n\n(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`.)\n\nIt will generarate the parser in the TS flavour.\n\nIf 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:\n\n```\npeggy --plugin ./src/tspegjs --extra-options-file pegconfig.json -o examples/arithmetics.ts --cache examples/arithmetics.pegjs\n```\n\n```json\n{\n    \"tspegjs\": {\n        \"customHeader\": \"// import lib\\nimport { Lib } from 'mylib';\"\n    },\n    \"returnTypes\": {\n        \"Integer\": \"number\",\n        \"Expression\": \"number\",\n    }\n}\n```\n\u003e For rules not listed in `returnTypes` object `any` type is declared by default.\n\n\u003e 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.\n\n## Using the Parser\n\n1. Save parser generated by `pegjs.generate` to a file or use the one generated from the CLI tool.\n\n2. In client TS code:\n\n```typescript\nimport { PeggySyntaxError, parse } from './arithmetics';\n\ntry {\n    const sampleOutput = parse('my sample...');\n} catch (ex: PeggySyntaxError) {\n    // Handle parsing error\n    // [...]\n}\n```\n\n## Changelog\n\n[Changelog](./Changelog.md).\n\n## Acknowledgments\n\nThanks to:\n\n-   [David Majda](https://github.com/dmajda) for creating pegjs\n-   [Elantcev Mikhail](https://github.com/Nordth) for providing the pegjs PHP plugin, inspiration on this one.\n\n## License\n\n[The MIT License (MIT)](http://opensource.org/licenses/MIT)\n\n---\n\n(c) 2017-2023, [Pedro J. Molina](https://github.com/pjmolina) at [metadev.pro](https://metadev.pro)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetadevpro%2Fts-pegjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmetadevpro%2Fts-pegjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetadevpro%2Fts-pegjs/lists"}