Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript Converstion #97

Merged
merged 5 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Sample usage:
peggy --plugin ./src/tspegjs -o examples/arithmetics.ts --cache examples/arithmetics.pegjs
```

(Note `./src/tspegjs` is the path to `tspegjs.js` in the project. If you installed ts-pegjs using npm, it should probably be `./node_modules/ts-pegjs/src/tspegjs`.)
(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.

Expand Down
516 changes: 260 additions & 256 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@
"pretest": "npm run clean",
"build": "vite build",
"test": "vite build && vitest run",
"lint": "eslint src/",
"lint": "eslint src/ --ext .ts",
"release": "npm test && npm publish"
},
"peerDependencies": {
"peggy": "^2.0.1"
"peggy": "^3.0.2"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"eslint": "^8.36.0",
"@types/node": "^18.15.11",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.57.0",
"eslint": "^8.37.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.8.7",
"rimraf": "^3.0.2",
"typescript": "^5.0.2",
"typescript": "^5.0.3",
"vite": "^4.2.1",
"vite-tsconfig-paths": "^4.0.7",
"vitest": "^0.29.7"
"vitest": "^0.29.8"
},
"dependencies": {},
"prettier": {
"tabWidth": 2,
"semi": true,
Expand Down
18 changes: 14 additions & 4 deletions src/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"parserOptions": {
"sourceType": "module"
},
Expand All @@ -15,13 +19,19 @@
"off",
"unix"
],
"quotes": [
"error",
"double"
],
"quotes": 0,
"semi": [
"error",
"always"
],
"no-unused-vars": 0,
"@typescript-eslint/no-unused-vars": [
"warn",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
}
}
85 changes: 0 additions & 85 deletions src/cli.js

This file was deleted.

94 changes: 94 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env node

import * as fs from 'node:fs';
import * as process from 'node:process';
import peggy, { ParserBuildOptions } from 'peggy';
import tspegjs from './tspegjs';
import { version } from '../package.json';
import { TsPegjsParserBuildOptions } from './types';

const generate = peggy.generate;

let args = process.argv;
args.shift();
args.shift();

const needHelp = args.find((a) => a === '-h');

if (args.length === 0 || needHelp) {
showHelp();
process.exit(0);
}

const inFile = args[args.length - 1];
let outFile = inFile.replace('.pegjs', '.ts');
args.forEach((arg, index) => {
if (arg === '-o') {
outFile = args[index + 1];
}
});
let allowedStartRules = null;
let customHeaderFile = null;
let customHeader = null;

args.forEach((arg, index) => {
if (arg === '--allowed-start-rules') {
allowedStartRules = (args[index + 1] || '').split(',');
}
if (arg === '--custom-header') {
customHeader = args[index + 1];
}
if (arg === '--custom-header-file') {
customHeaderFile = args[index + 1];
}
});

const trace = args.find((a) => a === '--trace') ? true : false;
const cache = args.find((a) => a === '--cache') ? true : false;

function showHelp() {
/* eslint-disable no-console */
console.log('tspegjs v.' + version + ' TS target for pegjs');
console.log('Usage:');
console.log(
' tspegjs [-o outFile.ts] [--allowed-start-rules <rule1,rule2>] [--trace] [--cache] [--no-tslint] [--tslint-ignores <rule1,rule2>] [--custom-header <header>] [--custom-header-file <headerFile>] <inGrammar.pegjs>'
);
}

function generateParser(
input_file: string,
output_file: string,
trace: boolean,
cache: boolean,
allowedStartRules: null | string[],
customHeader?: null | string,
customHeaderFile?: null | string
) {
fs.readFile(input_file, function (err, data) {
if (err) throw err;

if (customHeaderFile && !customHeader) {
customHeader = fs.readFileSync(customHeaderFile).toString();
}

const opts: TsPegjsParserBuildOptions = {
output: 'source',
trace: trace,
cache: cache,
plugins: [tspegjs],
tspegjs: {
customHeader
}
};
if (allowedStartRules) {
opts.allowedStartRules = allowedStartRules;
}

// We must cast `opts` as a workaround for https://github.com/peggyjs/peggy/issues/403
// Remove when issue fixed
let parser = generate(data.toString(), opts as ParserBuildOptions);
fs.writeFileSync(output_file, parser.toString());
});
}

generateParser(inFile, outFile, trace, cache, allowedStartRules, customHeader, customHeaderFile);
Loading