Skip to content

Commit 65b4e79

Browse files
Copilothzhangxyz
andcommitted
Refactor BNF package: 1:1 match JS/Python, add rollup, clean dependencies
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
1 parent 0648afc commit 65b4e79

7 files changed

Lines changed: 139 additions & 257 deletions

File tree

bnf/apyds_bnf/__init__.py

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,91 @@
1-
"""
2-
BNF Parser and Unparsers for DS
1+
from antlr4 import InputStream, CommonTokenStream
32

4-
This package provides bidirectional conversion between:
5-
- Ds: The lisp-like syntax currently used in DS
6-
- Dsp: A traditional readable syntax
7-
"""
3+
from .DspLexer import DspLexer
4+
from .DspParser import DspParser
5+
from .DspVisitor import DspVisitor
6+
from .DsLexer import DsLexer
7+
from .DsParser import DsParser
8+
from .DsVisitor import DsVisitor
89

9-
from .unparse import unparse
10-
from .parse import parse
1110

12-
__all__ = ["unparse", "parse"]
11+
class ParseVisitor(DspVisitor):
12+
def visitRule_pool(self, ctx):
13+
return "\n\n".join(self.visit(r) for r in ctx.rule_())
14+
15+
def visitRule(self, ctx):
16+
result = [self.visit(t) for t in ctx.term()]
17+
if len(result) == 1:
18+
return f"----\n{result[0]}"
19+
else:
20+
conclusion = result.pop()
21+
length = max(len(premise) for premise in result)
22+
result.append("-" * max(length, 4))
23+
result.append(conclusion)
24+
return "\n".join(result)
25+
26+
def visitSymbol(self, ctx):
27+
return ctx.SYMBOL().getText()
28+
29+
def visitParentheses(self, ctx):
30+
return self.visit(ctx.term())
31+
32+
def visitSubscript(self, ctx):
33+
return f"(subscript {' '.join(self.visit(t) for t in ctx.term())})"
34+
35+
def visitFunction(self, ctx):
36+
return f"(function {' '.join(self.visit(t) for t in ctx.term())})"
37+
38+
def visitUnary(self, ctx):
39+
return f"(unary {ctx.getChild(0).getText()} {self.visit(ctx.term())})"
40+
41+
def visitBinary(self, ctx):
42+
return f"(binary {ctx.getChild(1).getText()} {self.visit(ctx.term(0))} {self.visit(ctx.term(1))})"
43+
44+
45+
class UnparseVisitor(DsVisitor):
46+
def visitRule_pool(self, ctx):
47+
return "\n".join(self.visit(r) for r in ctx.rule_())
48+
49+
def visitRule(self, ctx):
50+
result = [self.visit(t) for t in ctx.term()]
51+
conclusion = result.pop()
52+
length = max(len(premise) for premise in result)
53+
return ", ".join(result) + " -> " + conclusion
54+
55+
def visitSymbol(self, ctx):
56+
return ctx.SYMBOL().getText()
57+
58+
def visitSubscript(self, ctx):
59+
terms = ctx.term()
60+
return f"{self.visit(terms[0])}[{', '.join(self.visit(t) for t in terms[1:])}]"
61+
62+
def visitFunction(self, ctx):
63+
terms = ctx.term()
64+
return f"{self.visit(terms[0])}({', '.join(self.visit(t) for t in terms[1:])})"
65+
66+
def visitUnary(self, ctx):
67+
return f"({ctx.getChild(0).getText()} {self.visit(ctx.term())})"
68+
69+
def visitBinary(self, ctx):
70+
return f"({self.visit(ctx.term(0))} {ctx.getChild(1).getText()} {self.visit(ctx.term(1))})"
71+
72+
73+
def parse(input):
74+
chars = InputStream(input)
75+
lexer = DspLexer(chars)
76+
tokens = CommonTokenStream(lexer)
77+
parser = DspParser(tokens)
78+
tree = parser.rule_pool()
79+
visitor = ParseVisitor()
80+
return visitor.visit(tree)
81+
82+
83+
def unparse(input):
84+
chars = InputStream(input)
85+
lexer = DsLexer(chars)
86+
tokens = CommonTokenStream(lexer)
87+
parser = DsParser(tokens)
88+
tree = parser.rule_pool()
89+
visitor = UnparseVisitor()
90+
return visitor.visit(tree)
91+

bnf/apyds_bnf/parse.py

Lines changed: 0 additions & 103 deletions
This file was deleted.

bnf/apyds_bnf/unparse.py

Lines changed: 0 additions & 82 deletions
This file was deleted.

bnf/package.json

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
{
22
"name": "atsds-bnf",
3-
"version": "0.1.0",
43
"description": "BNF parser and unparsers for DS - conversion between lisp-like and traditional syntax",
54
"author": "Hao Zhang <hzhangxyz@outlook.com>",
65
"license": "AGPL-3.0-or-later",
7-
"type": "module",
8-
"exports": {
9-
".": "./atsds_bnf/index.js"
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/USTC-KnowledgeComputingLab/ds.git"
109
},
11-
"main": "atsds_bnf/index.js",
12-
"module": "atsds_bnf/index.js",
10+
"type": "module",
11+
"exports": "./dist/bnf.mjs",
12+
"main": "dist/bnf.mjs",
13+
"module": "dist/bnf.mjs",
1314
"files": [
14-
"atsds_bnf/**/*.js"
15+
"dist/bnf.mjs"
1516
],
1617
"scripts": {
17-
"ds": "antlr4 -Dlanguage=JavaScript grammars/Ds.g4 -visitor -no-listener -o atsds_bnf/generated",
18-
"dsp": "antlr4 -Dlanguage=JavaScript grammars/Dsp.g4 -visitor -no-listener -o atsds_bnf/generated",
18+
"ds": "antlr4 -Dlanguage=JavaScript grammars/Ds.g4 -visitor -no-listener -o atsds_bnf",
19+
"dsp": "antlr4 -Dlanguage=JavaScript grammars/Dsp.g4 -visitor -no-listener -o atsds_bnf",
1920
"prepare": "npm-run-all ds dsp",
20-
"clean": "rm -rf atsds_bnf/generated"
21+
"rollup": "rollup --config rollup.config.mjs",
22+
"build": "npm-run-all prepare rollup"
2123
},
2224
"dependencies": {
2325
"antlr4": "^4.13.2"
2426
},
2527
"devDependencies": {
26-
"npm-run-all": "^4.1.5"
28+
"@rollup/plugin-node-resolve": "^16.0.3",
29+
"@rollup/plugin-terser": "^0.4.4",
30+
"npm-run-all": "^4.1.5",
31+
"rollup": "^4.53.3"
2732
}
2833
}

bnf/pyproject.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["setuptools>=61.0", "wheel", "antlr4-tools>=0.2.1", "setuptools-scm>=8.0"]
2+
requires = ["setuptools>=61.0", "setuptools-scm>=8.0"]
33
build-backend = "setuptools.build_meta"
44

55
[project]
@@ -14,10 +14,8 @@ dependencies = [
1414
"antlr4-python3-runtime>=4.13.2",
1515
]
1616

17-
[project.optional-dependencies]
18-
dev = [
19-
"antlr4-tools>=0.2.1",
20-
]
17+
[project.urls]
18+
Repository = "https://github.com/USTC-KnowledgeComputingLab/ds.git"
2119

2220
[tool.setuptools.packages.find]
2321
where = ["."]

bnf/rollup.config.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import terser from "@rollup/plugin-terser";
2+
import nodeResolve from "@rollup/plugin-node-resolve";
3+
4+
export default [
5+
{
6+
input: "atsds_bnf/index.js",
7+
output: {
8+
file: "dist/bnf.mjs",
9+
format: "es",
10+
},
11+
plugins: [
12+
terser(),
13+
nodeResolve(),
14+
],
15+
},
16+
];

0 commit comments

Comments
 (0)