Skip to content

Commit 9e4aa26

Browse files
Copilothzhangxyz
andcommitted
Add TypeScript declaration files to bnf package
Co-authored-by: hzhangxyz <[email protected]>
1 parent 3c57815 commit 9e4aa26

File tree

6 files changed

+234
-6
lines changed

6 files changed

+234
-6
lines changed

bnf/atsds_bnf/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*
2-
!index.js
2+
!index.mjs
3+
!index.mts

bnf/atsds_bnf/index.mts

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { InputStream, CommonTokenStream } from "antlr4";
2+
import DspLexer from "./DspLexer.js";
3+
import DspParser from "./DspParser.js";
4+
import DspVisitor from "./DspVisitor.js";
5+
import DsLexer from "./DsLexer.js";
6+
import DsParser from "./DsParser.js";
7+
import DsVisitor from "./DsVisitor.js";
8+
9+
class ParseVisitor extends DspVisitor {
10+
visitRule_pool(ctx: any): string {
11+
return ctx
12+
.rule_()
13+
.map((r: any) => this.visit(r))
14+
.join("\n\n");
15+
}
16+
17+
visitRule(ctx: any): string {
18+
const result = ctx.term().map((t: any) => this.visit(t));
19+
if (result.length === 1) {
20+
return `----\n${result[0]}`;
21+
} else {
22+
const conclusion = result.pop();
23+
const length = Math.max(...result.map((premise: string) => premise.length));
24+
result.push("-".repeat(Math.max(length, 4)));
25+
result.push(conclusion);
26+
return result.join("\n");
27+
}
28+
}
29+
30+
visitSymbol(ctx: any): string {
31+
return ctx.SYMBOL().getText();
32+
}
33+
34+
visitParentheses(ctx: any): string {
35+
return this.visit(ctx.term());
36+
}
37+
38+
visitSubscript(ctx: any): string {
39+
return `(subscript ${ctx
40+
.term()
41+
.map((t: any) => this.visit(t))
42+
.join(" ")})`;
43+
}
44+
45+
visitFunction(ctx: any): string {
46+
return `(function ${ctx
47+
.term()
48+
.map((t: any) => this.visit(t))
49+
.join(" ")})`;
50+
}
51+
52+
visitUnary(ctx: any): string {
53+
return `(unary ${ctx.getChild(0).getText()} ${this.visit(ctx.term())})`;
54+
}
55+
56+
visitBinary(ctx: any): string {
57+
return `(binary ${ctx.getChild(1).getText()} ${this.visit(ctx.term(0))} ${this.visit(ctx.term(1))})`;
58+
}
59+
}
60+
61+
class UnparseVisitor extends DsVisitor {
62+
visitRule_pool(ctx: any): string {
63+
return ctx
64+
.rule_()
65+
.map((r: any) => this.visit(r))
66+
.join("\n");
67+
}
68+
69+
visitRule(ctx: any): string {
70+
const result = ctx.term().map((t: any) => this.visit(t));
71+
const conclusion = result.pop();
72+
return result.join(", ") + " -> " + conclusion;
73+
}
74+
75+
visitSymbol(ctx: any): string {
76+
return ctx.SYMBOL().getText();
77+
}
78+
79+
visitSubscript(ctx: any): string {
80+
return `${this.visit(ctx.term(0))}[${ctx
81+
.term()
82+
.slice(1)
83+
.map((t: any) => this.visit(t))
84+
.join(", ")}]`;
85+
}
86+
87+
visitFunction(ctx: any): string {
88+
return `${this.visit(ctx.term(0))}(${ctx
89+
.term()
90+
.slice(1)
91+
.map((t: any) => this.visit(t))
92+
.join(", ")})`;
93+
}
94+
95+
visitUnary(ctx: any): string {
96+
return `(${ctx.getChild(1).getText()} ${this.visit(ctx.term())})`;
97+
}
98+
99+
visitBinary(ctx: any): string {
100+
return `(${this.visit(ctx.term(0))} ${ctx.getChild(1).getText()} ${this.visit(ctx.term(1))})`;
101+
}
102+
}
103+
104+
/**
105+
* Parse Dsp format (human-readable) to Ds format (internal S-expression)
106+
* @param input - The Dsp format string to parse
107+
* @returns The Ds format string
108+
*/
109+
export function parse(input: string): string {
110+
const chars = new InputStream(input);
111+
const lexer = new DspLexer(chars);
112+
const tokens = new CommonTokenStream(lexer);
113+
const parser = new DspParser(tokens);
114+
const tree = parser.rule_pool();
115+
const visitor = new ParseVisitor();
116+
return visitor.visit(tree);
117+
}
118+
119+
/**
120+
* Unparse Ds format (internal S-expression) to Dsp format (human-readable)
121+
* @param input - The Ds format string to unparse
122+
* @returns The Dsp format string
123+
*/
124+
export function unparse(input: string): string {
125+
const chars = new InputStream(input);
126+
const lexer = new DsLexer(chars);
127+
const tokens = new CommonTokenStream(lexer);
128+
const parser = new DsParser(tokens);
129+
const tree = parser.rule_pool();
130+
const visitor = new UnparseVisitor();
131+
return visitor.visit(tree);
132+
}

bnf/package-lock.json

Lines changed: 70 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bnf/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"main": "dist/index.mjs",
1414
"module": "dist/index.mjs",
1515
"browser": "dist/index.mjs",
16+
"types": "dist/index.d.mts",
1617
"files": [
18+
"dist/index.d.mts",
1719
"dist/index.mjs"
1820
],
1921
"scripts": {
@@ -33,10 +35,14 @@
3335
"@rollup/plugin-json": "^6.1.0",
3436
"@rollup/plugin-node-resolve": "^16.0.3",
3537
"@rollup/plugin-terser": "^0.4.4",
38+
"@rollup/plugin-typescript": "^12.3.0",
3639
"@types/jest": "^30.0.0",
3740
"cross-env": "^10.1.0",
3841
"jest": "^30.2.0",
3942
"npm-run-all": "^4.1.5",
40-
"rollup": "^4.53.3"
43+
"rollup": "^4.53.3",
44+
"rollup-plugin-dts": "^6.3.0",
45+
"tslib": "^2.8.1",
46+
"typescript": "^5.9.3"
4147
}
4248
}

bnf/rollup.config.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import commonjs from "@rollup/plugin-commonjs";
22
import json from "@rollup/plugin-json";
33
import nodeResolve from "@rollup/plugin-node-resolve";
44
import terser from "@rollup/plugin-terser";
5+
import typescript from "@rollup/plugin-typescript";
6+
import { dts } from "rollup-plugin-dts";
57

68
export default [
79
{
8-
input: "atsds_bnf/index.mjs",
10+
input: "atsds_bnf/index.mts",
911
output: {
1012
file: "dist/index.mjs",
1113
format: "es",
1214
},
1315
plugins: [
16+
typescript(),
1417
nodeResolve({
1518
browser: true,
1619
}),
@@ -19,4 +22,12 @@ export default [
1922
terser(),
2023
],
2124
},
25+
{
26+
input: "atsds_bnf/index.mts",
27+
output: {
28+
file: "dist/index.d.mts",
29+
format: "es",
30+
},
31+
plugins: [dts()],
32+
},
2233
];

bnf/tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "esnext",
5+
"strict": true,
6+
"esModuleInterop": true,
7+
"isolatedModules": true,
8+
"skipLibCheck": true
9+
},
10+
"include": ["atsds_bnf/*"]
11+
}

0 commit comments

Comments
 (0)