Skip to content

Commit 1d53c6a

Browse files
Copilothzhangxyz
andcommitted
Restructure BNF package: move configs to root, rename to atsds-bnf/apyds-bnf
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
1 parent a0078b2 commit 1d53c6a

21 files changed

Lines changed: 220 additions & 279 deletions

bnf/.gitignore

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# Generated files
2-
javascript/src/generated/
3-
javascript/dist/
4-
javascript/node_modules/
5-
python/ds_bnf/generated/
6-
python/build/
7-
python/dist/
8-
python/*.egg-info/
2+
src/generated/
3+
dist/
4+
node_modules/
5+
apyds_bnf/generated/
6+
build/
7+
*.egg-info/
98

109
# Python cache
1110
__pycache__/

bnf/IMPLEMENTATION.md

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,29 @@ This document provides a complete summary of the BNF support package implementat
1212
bnf/
1313
├── README.md # Main package documentation
1414
├── .gitignore # Git ignore rules for generated files
15+
├── package.json # NPM package (atsds-bnf)
16+
├── pyproject.toml # Python package (apyds-bnf)
17+
├── setup.py # Python setup with ANTLR generation
1518
├── setup.sh # Setup script to generate ANTLR parsers
19+
├── tsconfig.json # TypeScript configuration
1620
├── grammars/ # ANTLR4 grammar definitions
1721
│ ├── Ds.g4 # Lisp-like syntax grammar
1822
│ ├── Dsp.g4 # Traditional syntax grammar
1923
│ └── README.md # Grammar design notes
20-
├── javascript/ # JavaScript/TypeScript implementation
21-
│ ├── package.json # NPM package configuration
22-
│ ├── tsconfig.json # TypeScript configuration
23-
│ ├── README.md # JavaScript-specific documentation
24-
│ ├── src/
25-
│ │ ├── index.ts # Main export file
26-
│ │ ├── unparse.ts # Ds → Dsp converter
27-
│ │ └── parse.ts # Dsp → Ds converter
28-
│ └── tests/
29-
│ └── conversion.test.js # Test suite
30-
├── python/ # Python implementation
31-
│ ├── pyproject.toml # Python package configuration
32-
│ ├── README.md # Python-specific documentation
33-
│ ├── ds_bnf/
34-
│ │ ├── __init__.py # Package initialization
35-
│ │ ├── unparse.py # Ds → Dsp converter
36-
│ │ ├── parse.py # Dsp → Ds converter
37-
│ │ └── cli.py # Command-line interface
38-
│ └── tests/
39-
│ ├── __init__.py
40-
│ └── test_conversion.py # Test suite
24+
├── src/ # TypeScript source files
25+
│ ├── index.ts # Main export file
26+
│ ├── unparse.ts # Ds → Dsp converter
27+
│ └── parse.ts # Dsp → Ds converter
28+
├── apyds_bnf/ # Python package
29+
│ ├── __init__.py # Package initialization
30+
│ ├── unparse.py # Ds → Dsp converter
31+
│ ├── parse.py # Dsp → Ds converter
32+
│ └── cli.py # Command-line interface
33+
├── tests/ # JavaScript tests
34+
│ └── conversion.test.js # Test suite
35+
├── py_tests/ # Python tests
36+
│ ├── __init__.py
37+
│ └── test_conversion.py # Test suite
4138
└── examples/ # Usage examples
4239
├── README.md # Examples documentation
4340
├── example.ds # Sample Ds file
@@ -116,41 +113,39 @@ Complete documentation including:
116113

117114
2. **JavaScript**:
118115
```bash
119-
cd javascript
120116
npm install
121117
npm run build
122118
npm test
123119
```
124120

125121
3. **Python**:
126122
```bash
127-
cd python
128123
pip install -e .
129-
pytest
124+
pytest py_tests/
130125
```
131126

132127
### API Examples
133128

134129
**JavaScript**:
135130
```javascript
136-
import { unparse, parse } from 'ds-bnf';
131+
import { unparse, parse } from 'atsds-bnf';
137132

138133
const dsp = unparse('(binary -> a b)'); // "(a -> b)"
139134
const ds = parse('a -> b'); // "(binary -> a b)"
140135
```
141136

142137
**Python**:
143138
```python
144-
from ds_bnf import unparse, parse
139+
from apyds_bnf import unparse, parse
145140

146141
dsp = unparse('(binary -> a b)') # "(a -> b)"
147142
ds = parse('a -> b') # "(binary -> a b)"
148143
```
149144

150145
**CLI**:
151146
```bash
152-
ds-unparse input.ds > output.dsp
153-
ds-parse input.dsp > output.ds
147+
apyds-unparse input.ds > output.dsp
148+
apyds-parse input.dsp > output.ds
154149
```
155150

156151
## Design Principles

bnf/README.md

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,52 @@ This package provides bidirectional conversion between two syntax formats for th
55
- **Ds**: The lisp-like syntax currently used in DS
66
- **Dsp**: A traditional readable syntax with infix operators
77

8+
## Installation
9+
10+
### JavaScript/TypeScript
11+
12+
```bash
13+
cd bnf
14+
npm install
15+
npm run build
16+
```
17+
18+
### Python
19+
20+
```bash
21+
cd bnf
22+
pip install -e .
23+
```
24+
25+
Or with development dependencies:
26+
27+
```bash
28+
pip install -e ".[dev]"
29+
```
30+
831
## Structure
932

1033
```
1134
bnf/
35+
├── package.json # JavaScript/TypeScript package (atsds-bnf)
36+
├── pyproject.toml # Python package (apyds-bnf)
37+
├── setup.py # Python setup with ANTLR generation
38+
├── tsconfig.json # TypeScript configuration
1239
├── grammars/ # ANTLR grammar files
1340
│ ├── Ds.g4 # Grammar for lisp-like syntax
1441
│ └── Dsp.g4 # Grammar for traditional syntax
15-
├── javascript/ # JavaScript/TypeScript implementation
16-
│ ├── package.json
17-
│ ├── tsconfig.json
18-
│ └── src/
19-
│ ├── index.ts
20-
│ ├── unparse.ts # Ds → Dsp conversion
21-
│ └── parse.ts # Dsp → Ds conversion
22-
└── python/ # Python implementation
23-
├── pyproject.toml
24-
└── ds_bnf/
25-
├── __init__.py
26-
├── unparse.py # Ds → Dsp conversion
27-
├── parse.py # Dsp → Ds conversion
28-
└── cli.py # Command-line interface
42+
├── src/ # TypeScript source files
43+
│ ├── index.ts
44+
│ ├── unparse.ts # Ds → Dsp conversion
45+
│ └── parse.ts # Dsp → Ds conversion
46+
├── apyds_bnf/ # Python package
47+
│ ├── __init__.py
48+
│ ├── unparse.py # Ds → Dsp conversion
49+
│ ├── parse.py # Dsp → Ds conversion
50+
│ └── cli.py # Command-line interface
51+
├── tests/ # JavaScript tests
52+
├── py_tests/ # Python tests
53+
└── examples/ # Usage examples
2954
```
3055

3156
## Syntax Examples
@@ -47,18 +72,22 @@ bnf/
4772

4873
## JavaScript/TypeScript Usage
4974

50-
### Installation
75+
### Building
76+
77+
The build process includes:
78+
79+
1. **Generate parsers**: Run ANTLR4 to generate lexer/parser from grammars
80+
2. **Compile TypeScript**: Transpile TypeScript to JavaScript
5181

5282
```bash
53-
cd bnf/javascript
54-
npm install
55-
npm run build
83+
npm run prepare # Generate ANTLR parsers (ds + dsp)
84+
npm run build # Full build (prepare + compile)
5685
```
5786

5887
### API
5988

6089
```javascript
61-
import { unparse, parse } from 'ds-bnf';
90+
import { unparse, parse } from 'atsds-bnf';
6291

6392
// Convert Ds to Dsp
6493
const dsp = unparse('(binary -> a b)');
@@ -69,37 +98,12 @@ const ds = parse('a -> b');
6998
console.log(ds); // "(binary -> a b)"
7099
```
71100

72-
### Building
73-
74-
The build process includes:
75-
76-
1. **Generate parsers**: Run ANTLR4 to generate lexer/parser from grammars
77-
2. **Compile TypeScript**: Transpile TypeScript to JavaScript
78-
79-
```bash
80-
npm run generate # Generate ANTLR parsers
81-
npm run build # Full build (generate + compile)
82-
```
83-
84101
## Python Usage
85102

86-
### Installation
87-
88-
```bash
89-
cd bnf/python
90-
pip install -e .
91-
```
92-
93-
Or with development dependencies:
94-
95-
```bash
96-
pip install -e ".[dev]"
97-
```
98-
99103
### API
100104

101105
```python
102-
from ds_bnf import unparse, parse
106+
from apyds_bnf import unparse, parse
103107

104108
# Convert Ds to Dsp
105109
dsp = unparse('(binary -> a b)')
@@ -116,21 +120,24 @@ After installation, two CLI commands are available:
116120

117121
```bash
118122
# Unparse: Ds → Dsp
119-
ds-unparse input.ds > output.dsp
120-
echo "(binary -> a b)" | ds-unparse
123+
apyds-unparse input.ds > output.dsp
124+
echo "(binary -> a b)" | apyds-unparse
121125

122126
# Parse: Dsp → Ds
123-
ds-parse input.dsp > output.ds
124-
echo "a -> b" | ds-parse
127+
apyds-parse input.dsp > output.ds
128+
echo "a -> b" | apyds-parse
125129
```
126130

127131
### Generating Parsers
128132

129-
To generate the ANTLR parsers for Python:
133+
The Python package automatically generates ANTLR parsers during installation using the custom `setup.py` build command. You can also generate them manually:
130134

131135
```bash
132-
cd bnf/python
133-
antlr4 -Dlanguage=Python3 -visitor -o ds_bnf/generated ../grammars/Ds.g4 ../grammars/Dsp.g4
136+
# Using antlr4 command
137+
antlr4 -Dlanguage=Python3 -visitor -no-listener -o apyds_bnf/generated grammars/Ds.g4 grammars/Dsp.g4
138+
139+
# Or using antlr4-tools
140+
python -m antlr4_tools -Dlanguage=Python3 -visitor -no-listener -o apyds_bnf/generated grammars/Ds.g4 grammars/Dsp.g4
134141
```
135142

136143
## Grammar Details
@@ -154,32 +161,20 @@ antlr4 -Dlanguage=Python3 -visitor -o ds_bnf/generated ../grammars/Ds.g4 ../gram
154161
- Subscript: `base[index1, index2]`
155162
- Function: `name(arg1, arg2)`
156163
- Unary: `op operand` (e.g., `! x`, `- y`)
157-
- Binary infix operators with precedence:
158-
- `::`, `.`
159-
- `.*`
160-
- `*`, `/`, `%`
161-
- `+`, `-`
162-
- `<<`, `>>`
163-
- `<`, `>`, `<=`, `>=`
164-
- `==`, `!=`
165-
- `&`, `^`, `|`
166-
- `&&`, `||`
167-
- `=`
164+
- Binary infix operators with precedence
168165

169166
## Testing
170167

171168
### JavaScript
172169

173170
```bash
174-
cd bnf/javascript
175171
npm test
176172
```
177173

178174
### Python
179175

180176
```bash
181-
cd bnf/python
182-
pytest
177+
pytest py_tests/
183178
```
184179

185180
## Development
@@ -189,12 +184,15 @@ This package follows the mono repo layout and is designed to be self-contained w
189184
### Prerequisites
190185

191186
- **JavaScript**: Node.js 20+, ANTLR4 CLI
192-
- **Python**: Python 3.10+, ANTLR4 CLI
187+
- **Python**: Python 3.10+, antlr4-tools or ANTLR4 CLI
193188

194189
### Installing ANTLR4
195190

196191
```bash
197-
# Using pip (for the runtime and CLI)
192+
# For JavaScript development
193+
npm install -g antlr4
194+
195+
# For Python development
198196
pip install antlr4-tools
199197

200198
# Or download from https://www.antlr.org/download.html

bnf/examples/README.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This directory contains example files demonstrating the BNF conversion.
2626
### JavaScript
2727

2828
```javascript
29-
import { unparse, parse } from 'ds-bnf';
29+
import { unparse, parse } from 'atsds-bnf';
3030
import { readFileSync } from 'fs';
3131

3232
// Read and convert Ds to Dsp
@@ -45,7 +45,7 @@ console.log(dsOutput);
4545
### Python
4646

4747
```python
48-
from ds_bnf import unparse, parse
48+
from apyds_bnf import unparse, parse
4949

5050
# Read and convert Ds to Dsp
5151
with open('example.ds') as f:
@@ -65,10 +65,7 @@ print(ds_output)
6565
### Command-line
6666

6767
```bash
68-
# Using JavaScript
69-
cat example.ds | node -e "import('ds-bnf').then(m => console.log(m.unparse(require('fs').readFileSync(0, 'utf-8'))))"
70-
71-
# Using Python
72-
ds-unparse example.ds
73-
ds-parse example.dsp
68+
# Using Python CLI
69+
apyds-unparse example.ds
70+
apyds-parse example.dsp
7471
```

0 commit comments

Comments
 (0)