@echecs/pgn 2.1.4 → 3.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +98 -190
- package/dist/grammar.cjs +382 -0
- package/dist/grammar.cjs.map +1 -0
- package/dist/{grammar.d.ts → grammar.d.cts} +3 -3
- package/dist/index.d.ts +11 -6
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -0
- package/package.json +33 -26
- package/dist/grammar.js +0 -428
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# PGN
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
`PGN` is a parser that is part of the **ECHECS** project, designed to interpret
|
|
4
|
+
the
|
|
5
|
+
[PGN (Portable Game Notation) specification](http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm).
|
|
5
6
|
|
|
6
7
|
## Installation
|
|
7
8
|
|
|
@@ -9,228 +10,135 @@
|
|
|
9
10
|
npm install --save-dev @echecs/pgn
|
|
10
11
|
```
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
The `parse` function takes a PGN formatted string as input and returns an array
|
|
16
|
+
of parsed PGN objects.
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
parse(input: string): PGN[]
|
|
14
20
|
```
|
|
15
21
|
|
|
16
|
-
|
|
22
|
+
### PGN Object Format
|
|
17
23
|
|
|
18
|
-
|
|
24
|
+
Here’s the structure of the `PGN` object:
|
|
19
25
|
|
|
20
|
-
|
|
26
|
+
#### PGN Object
|
|
21
27
|
|
|
22
|
-
```
|
|
28
|
+
```json
|
|
23
29
|
{
|
|
24
|
-
meta: Meta,
|
|
25
|
-
moves: Moves,
|
|
26
|
-
result: 1-0 // 1-0
|
|
30
|
+
"meta": Meta,
|
|
31
|
+
"moves": Moves,
|
|
32
|
+
"result": "1-0" // possible values: "1-0", "0-1", "1/2-1/2", "?"
|
|
27
33
|
}
|
|
28
34
|
```
|
|
29
35
|
|
|
30
|
-
|
|
36
|
+
#### Meta Object
|
|
31
37
|
|
|
32
|
-
|
|
38
|
+
The `meta` object contains metadata about the chess game.
|
|
39
|
+
|
|
40
|
+
```json
|
|
33
41
|
{
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
// plus any other additional tags with `key` string
|
|
42
|
+
"Event": "name of the tournament or match event",
|
|
43
|
+
"Site": "location of the event",
|
|
44
|
+
"Date": "starting date of the game",
|
|
45
|
+
"Round": "playing round ordinal of the game",
|
|
46
|
+
"White": "player of the white pieces",
|
|
47
|
+
"Black": "player of the black pieces",
|
|
48
|
+
"Result": "result of the game",
|
|
49
|
+
// Any other additional tags
|
|
43
50
|
[key]: "string"
|
|
44
51
|
}
|
|
45
52
|
```
|
|
46
53
|
|
|
47
|
-
|
|
54
|
+
#### Moves Array
|
|
48
55
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
56
|
+
`Moves` is an array representing the sequence of moves in the game. Each element
|
|
57
|
+
is an array containing the move number, the white move, and the black move.
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
[
|
|
61
|
+
moveNumber,
|
|
62
|
+
Move,
|
|
63
|
+
Move
|
|
64
|
+
]
|
|
52
65
|
```
|
|
53
66
|
|
|
54
|
-
|
|
67
|
+
Note: Half moves are included for variations or in cases where the last move was
|
|
68
|
+
made by white.
|
|
55
69
|
|
|
56
|
-
|
|
70
|
+
#### Move Object
|
|
57
71
|
|
|
58
|
-
|
|
72
|
+
Each move is represented by the following structure:
|
|
73
|
+
|
|
74
|
+
```json
|
|
59
75
|
{
|
|
60
|
-
annotations: ["!", "$126"], //
|
|
61
|
-
capture: false, //
|
|
62
|
-
castling: true, //
|
|
63
|
-
check: false, //
|
|
64
|
-
checkmate: false, //
|
|
65
|
-
comment:
|
|
66
|
-
from:
|
|
67
|
-
piece:
|
|
68
|
-
promotion: Piece
|
|
69
|
-
to:
|
|
70
|
-
variants: [...] // moves following Moves format
|
|
76
|
+
"annotations": ["!", "$126"], // optional, annotations for the move
|
|
77
|
+
"capture": false, // optional, indicates if any piece was captured
|
|
78
|
+
"castling": true, // optional, indicates if the move was castling
|
|
79
|
+
"check": false, // optional, indicates if the move put the rival king in check
|
|
80
|
+
"checkmate": false, // optional, indicates if it is a checkmate
|
|
81
|
+
"comment": "Some comment", // optional, comment about the move
|
|
82
|
+
"from": "e", // optional, disambiguation of the move
|
|
83
|
+
"piece": "K", // required, type of piece (P, R, N, B, Q, K)
|
|
84
|
+
"promotion": "Piece", // optional, promotion piece (R, N, B, Q)
|
|
85
|
+
"to": "g1", // required, ending square of the move
|
|
86
|
+
"variants": [...] // optional, array of moves for variations following Moves format
|
|
71
87
|
}
|
|
72
88
|
```
|
|
73
89
|
|
|
74
|
-
|
|
90
|
+
### Example
|
|
91
|
+
|
|
92
|
+
Here's a sample usage of the `PGN` parser:
|
|
75
93
|
|
|
76
94
|
```js
|
|
77
95
|
import { readFileSync } from 'fs';
|
|
78
96
|
import parse from '@echecs/pgn';
|
|
79
97
|
|
|
80
|
-
function readFile(path
|
|
98
|
+
function readFile(path) {
|
|
81
99
|
const filename = require.resolve(path);
|
|
82
|
-
|
|
83
100
|
return readFileSync(filename, 'utf8');
|
|
84
101
|
}
|
|
85
102
|
|
|
86
103
|
const pgn = parse(readFile('./games/file.pgn'));
|
|
87
|
-
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
// "to": "c5",
|
|
121
|
-
// },
|
|
122
|
-
// ],
|
|
123
|
-
// [
|
|
124
|
-
// 3,
|
|
125
|
-
// {
|
|
126
|
-
// "piece": "N",
|
|
127
|
-
// "to": "f3",
|
|
128
|
-
// },
|
|
129
|
-
// {
|
|
130
|
-
// "capture": true,
|
|
131
|
-
// "from": "c",
|
|
132
|
-
// "piece": "P",
|
|
133
|
-
// "to": "d4",
|
|
134
|
-
// },
|
|
135
|
-
// ],
|
|
136
|
-
// ...
|
|
137
|
-
// [
|
|
138
|
-
// 6,
|
|
139
|
-
// {
|
|
140
|
-
// "capture": true,
|
|
141
|
-
// "from": "c",
|
|
142
|
-
// "piece": "P",
|
|
143
|
-
// "to": "d5",
|
|
144
|
-
// },
|
|
145
|
-
// {
|
|
146
|
-
// "piece": "B",
|
|
147
|
-
// "to": "c5",
|
|
148
|
-
// },
|
|
149
|
-
// ],
|
|
150
|
-
// [
|
|
151
|
-
// 7,
|
|
152
|
-
// {
|
|
153
|
-
// "from": "5",
|
|
154
|
-
// "piece": "N",
|
|
155
|
-
// "to": "c3",
|
|
156
|
-
// },
|
|
157
|
-
// {
|
|
158
|
-
// "castling": true,
|
|
159
|
-
// "piece": "K",
|
|
160
|
-
// "to": "g8",
|
|
161
|
-
// },
|
|
162
|
-
// ],
|
|
163
|
-
// ...
|
|
164
|
-
// [
|
|
165
|
-
// 21,
|
|
166
|
-
// {
|
|
167
|
-
// "capture": true,
|
|
168
|
-
// "piece": "N",
|
|
169
|
-
// "to": "e4",
|
|
170
|
-
// },
|
|
171
|
-
// {
|
|
172
|
-
// "piece": "B",
|
|
173
|
-
// "to": "b7",
|
|
174
|
-
// },
|
|
175
|
-
// ],
|
|
176
|
-
// [
|
|
177
|
-
// 22,
|
|
178
|
-
// {
|
|
179
|
-
// "capture": true,
|
|
180
|
-
// "piece": "Q",
|
|
181
|
-
// "to": "d7",
|
|
182
|
-
// },
|
|
183
|
-
// {
|
|
184
|
-
// "capture": true,
|
|
185
|
-
// "piece": "B",
|
|
186
|
-
// "to": "e4",
|
|
187
|
-
// },
|
|
188
|
-
// ],
|
|
189
|
-
// [
|
|
190
|
-
// 23,
|
|
191
|
-
// {
|
|
192
|
-
// "piece": "R",
|
|
193
|
-
// "to": "h2",
|
|
194
|
-
// },
|
|
195
|
-
// {
|
|
196
|
-
// "capture": true,
|
|
197
|
-
// "piece": "B",
|
|
198
|
-
// "to": "d6",
|
|
199
|
-
// },
|
|
200
|
-
// ],
|
|
201
|
-
// ...
|
|
202
|
-
// [
|
|
203
|
-
// 29,
|
|
204
|
-
// {
|
|
205
|
-
// "capture": true,
|
|
206
|
-
// "check": true,
|
|
207
|
-
// "piece": "Q",
|
|
208
|
-
// "to": "e6",
|
|
209
|
-
// },
|
|
210
|
-
// {
|
|
211
|
-
// "piece": "K",
|
|
212
|
-
// "to": "h8",
|
|
213
|
-
// },
|
|
214
|
-
// ],
|
|
215
|
-
// [
|
|
216
|
-
// 30,
|
|
217
|
-
// {
|
|
218
|
-
// "piece": "Q",
|
|
219
|
-
// "to": "e7",
|
|
220
|
-
// },
|
|
221
|
-
// {
|
|
222
|
-
// "piece": "B",
|
|
223
|
-
// "to": "c7",
|
|
224
|
-
// },
|
|
225
|
-
// ],
|
|
226
|
-
// ],
|
|
227
|
-
// "result": 0,
|
|
228
|
-
// },
|
|
229
|
-
// ];
|
|
104
|
+
|
|
105
|
+
// Output example of parsed `PGN`
|
|
106
|
+
console.log(pgn);
|
|
107
|
+
/*
|
|
108
|
+
[
|
|
109
|
+
{
|
|
110
|
+
"meta": {
|
|
111
|
+
"Event": "Some Tournament",
|
|
112
|
+
"Site": "Some Location",
|
|
113
|
+
"Date": "2023.10.04",
|
|
114
|
+
"Round": "1",
|
|
115
|
+
"White": "Player1",
|
|
116
|
+
"Black": "Player2",
|
|
117
|
+
"Result": "1-0",
|
|
118
|
+
// additional tags...
|
|
119
|
+
},
|
|
120
|
+
"moves": [
|
|
121
|
+
[
|
|
122
|
+
1,
|
|
123
|
+
{ "piece": "P", "to": "e4" },
|
|
124
|
+
{ "piece": "P", "to": "e5" }
|
|
125
|
+
],
|
|
126
|
+
[
|
|
127
|
+
2,
|
|
128
|
+
{ "piece": "N", "to": "f3" },
|
|
129
|
+
{ "piece": "N", "to": "c6" }
|
|
130
|
+
],
|
|
131
|
+
// more moves...
|
|
132
|
+
],
|
|
133
|
+
"result": "1-0"
|
|
134
|
+
}
|
|
135
|
+
];
|
|
136
|
+
*/
|
|
230
137
|
```
|
|
231
138
|
|
|
232
|
-
##
|
|
139
|
+
## Important Notes
|
|
233
140
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
141
|
+
- `PGN` is a parser and does not verify the validity of the PGN games. It only
|
|
142
|
+
parses the provided content.
|
|
143
|
+
- For game validation, use **@echecs/game** as it is responsible for verifying
|
|
144
|
+
game correctness as part of the **ECHECS** project.
|