-
Notifications
You must be signed in to change notification settings - Fork 3
/
igo-sgf-parser.el
523 lines (436 loc) · 17.2 KB
/
igo-sgf-parser.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
;;; igo-sgf-parser.el --- SGF Parser -*- lexical-binding: t; -*-
;; Copyright (C) 2020 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei
;; Keywords: games
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'cl-lib)
(require 'subr-x)
;;
;; Input Stream Functions:
;;
;; - (igo-sgf-scan strm)
;; - (igo-sgf-get strm)
;; - (igo-sgf-discard strm)
;; - (igo-sgf-strm-pos strm)
;; - (igo-sgf-error strm message)
(defun igo-sgf-scan (_strm)
(char-after))
(defun igo-sgf-get (_strm)
(when (< (point) (point-max))
(forward-char)
(char-before)))
(defun igo-sgf-discard (_strm)
(if (< (point) (point-max))
(forward-char)))
(defun igo-sgf-strm-pos (_strm)
(point))
(defun igo-sgf-error (strm message)
(error "%s: %s" (igo-sgf-strm-pos strm) message))
;;
;; Character Classification
;;
(defun igo-sgf-ws-p (c)
(and
(integerp c)
(or (= c ? )
(= c ?\t)
(= c ?\v)
(= c ?\n)
(= c ?\r))))
(defun igo-sgf-ucletter-p (c)
(and (integerp c) (>= c ?A) (<= c ?Z)))
;;
;; Stream Utilities
;;
(defun igo-sgf-skip-ws (strm)
(let (ch wss)
(while (igo-sgf-ws-p (setq ch (igo-sgf-scan strm)))
(push ch wss)
(igo-sgf-discard strm))
(concat (nreverse wss))))
(defun igo-sgf-match-char (strm ch)
(let ((strm-ch (igo-sgf-scan strm)))
(if (not (equal strm-ch ch))
(igo-sgf-error strm (format "Unexpected %s (expecting '%c')" (if strm-ch (concat "'" (char-to-string strm-ch) "'") "end of SGF") ch))
(igo-sgf-discard strm))))
(defun igo-sgf-scan-ucletters (strm)
"Scan [A-Z]* from strm."
(let (ch chars)
(while (igo-sgf-ucletter-p (setq ch (igo-sgf-scan strm)))
(push ch chars)
(igo-sgf-discard strm))
(concat (nreverse chars))))
;;
;; Parse SGF
;;
;; Syntax:
;;
;; Collection = GameTree+
;; GameTree = "(" Sequence GameTree* ")"
;; Sequence = Node+
;; Node = ";" Property*
;; Property = PropIdent PropValue+
;; PropIdent = UcLetter+
;; PropValue = "[" CValueType "]"
;; CValueType = (ValueType | Compose)
;; ValueType = (None | Number | Real | Double | Color | SimpleText |
;; Text | Point | Move | Stone)
;; (see: https:www.red-bean.com/sgf/sgf4.html )
;;
;; Functions corresponding to non-terminal symbols:
;; - igo-sgf-parse-tree-list :: GameTree*
;; - igo-sgf-parse-tree :: GameTree
;; - igo-sgf-parse-node-list :: Node*
;; - igo-sgf-parse-node :: Node
;; - igo-sgf-parse-property-list :: Property*
;; - igo-sgf-parse-property :: Property
;; - igo-sgf-parse-prop-value :: PropValue
(defun igo-sgf-parse-tree-list (strm)
"Parse 0 or more GameTree and return list of tree object."
;; Tree*
;; (...) (...) (...)
(let (trees wss)
(while (progn (setq wss (igo-sgf-skip-ws strm)) (equal (igo-sgf-scan strm) ?\())
(push (igo-sgf-parse-tree strm wss) trees))
(nreverse trees)))
(defun igo-sgf-parse-tree (strm &optional leading-wss)
"Parse GameTree and return a tree object [leading-wss begin-pos
nodes subtrees end-pos]."
(setq leading-wss (concat leading-wss (igo-sgf-skip-ws strm)))
;; ( Node+ Tree* )
;; ( ;... ;... ;... (...) (...) (...) )
(let (begin-pos nodes subtrees end-pos)
;; (
(setq begin-pos (igo-sgf-strm-pos strm))
(igo-sgf-match-char strm ?\()
;; Node+
(setq nodes (igo-sgf-parse-node-list strm))
(if (null nodes)
(igo-sgf-error strm "GameTree requires one or more Nodes"))
;; Tree*
(setq subtrees (igo-sgf-parse-tree-list strm))
;; )
;;;@todo skip white spaces?
(igo-sgf-match-char strm ?\))
(setq end-pos (igo-sgf-strm-pos strm))
(vector leading-wss begin-pos nodes subtrees end-pos)))
(defun igo-sgf-parse-node-list (strm) ;;Sequence
"Parse 0 or more Node and return list of node object."
;; Node*
;; ;... ;... ;...
(let (nodes wss)
(while (progn (setq wss (igo-sgf-skip-ws strm)) (equal (igo-sgf-scan strm) ?\;))
(push (igo-sgf-parse-node strm wss) nodes))
(nreverse nodes)))
(defun igo-sgf-parse-node (strm &optional leading-wss)
"Parse Node and return a node object [leading-wss begin-pos properties end-pos]."
(setq leading-wss (concat leading-wss (igo-sgf-skip-ws strm)))
;; ; Property*
;; ; PID[PValue][PValue][PValue]PID[PValue][PValue][PValue]...
(let (begin-pos properties end-pos)
;; ;
(setq begin-pos (igo-sgf-strm-pos strm))
(igo-sgf-match-char strm ?\;)
;; Property*
(setq properties (igo-sgf-parse-property-list strm))
(setq end-pos (igo-sgf-strm-pos strm))
(vector leading-wss begin-pos properties end-pos)))
(defun igo-sgf-parse-property-list (strm)
;; Property*
;; PID[PValue][PValue][PValue]PID[PValue][PValue][PValue]...
(let (properties wss)
(while (progn (setq wss (igo-sgf-skip-ws strm)) (igo-sgf-ucletter-p (igo-sgf-scan strm)))
(push (igo-sgf-parse-property strm wss) properties))
(nreverse properties)))
(defun igo-sgf-parse-property (strm &optional leading-wss)
"Return property object [leading-wss begin-pos id values end-pos]"
(setq leading-wss (concat leading-wss (igo-sgf-skip-ws strm)))
;; PropIdent PropValue+
(let (begin-pos id values end-pos)
(setq begin-pos (igo-sgf-strm-pos strm))
;; PropIdent
(setq id (igo-sgf-scan-ucletters strm))
(if (string-empty-p id)
(igo-sgf-error strm "No property ID"))
;; PropValue+
(let (wss)
(while (progn (setq wss (igo-sgf-skip-ws strm)) (equal (igo-sgf-scan strm) ?\[))
(push (igo-sgf-parse-prop-value strm wss) values)))
(setq values (nreverse values))
(if (null values)
(igo-sgf-error strm "Property requires one or more PropValues"))
(setq end-pos (igo-sgf-strm-pos strm))
(vector leading-wss begin-pos id values end-pos)))
(defun igo-sgf-parse-prop-value (strm &optional leading-wss)
(setq leading-wss (concat leading-wss (igo-sgf-skip-ws strm)))
(let (begin-pos ch chars end-pos)
(setq begin-pos (igo-sgf-strm-pos strm))
(igo-sgf-match-char strm ?\[)
(while (not (equal (setq ch (igo-sgf-get strm)) ?\]))
;; end of strm
(if (null ch)
(igo-sgf-error strm "Unexpected end of SGF in Property Value"))
;; push ch
(push ch chars)
;; NOTE: Do not resolve compose value here(colon separation). Depends on property type.
;; skip \], convert \r \n\r \r\n to \n
(if (= ch ?\\)
(let ((escaped-ch (igo-sgf-get strm)))
(cond
;; convert \\\n\r and \\\r\n to single \n
((or (and (= escaped-ch ?\n) (equal (igo-sgf-scan strm) ?\r))
(and (= escaped-ch ?\r) (equal (igo-sgf-scan strm) ?\n)))
(igo-sgf-discard strm)
(push ?\n chars))
;; convert single \r to \n
((or (= escaped-ch ?\r))
(push ?\n chars))
;; ], \, :, \n, spaces, etc...
(t (push escaped-ch chars))))))
(setq end-pos (igo-sgf-strm-pos strm))
;; return value
(igo-sgf-make-prop-value leading-wss begin-pos (concat (nreverse chars)) end-pos)))
;;
;; Syntax Tree Accessors
;;
(defun igo-sgf-format-location (begin end)
(cond
((and begin end) (format "%s-%s: " begin end))
(begin (format "%s: " begin))
(end (format "%s: " end))
(t "")))
;; Tree
(defun igo-sgf-tree-leading-white-spaces (tree) (aref tree 0))
(defun igo-sgf-tree-begin (tree) (aref tree 1))
(defun igo-sgf-tree-nodes (tree) (aref tree 2))
(defun igo-sgf-tree-subtrees (tree) (aref tree 3))
(defun igo-sgf-tree-end (tree) (aref tree 4))
(defun igo-sgf-tree-each-nodes (tree func)
(dolist (node (igo-sgf-tree-nodes tree))
(funcall func node tree))
(dolist (subtree (igo-sgf-tree-subtrees tree))
(igo-sgf-tree-each-nodes subtree func)))
(defun igo-sgf-tree-root-node (tree)
(car (igo-sgf-tree-nodes tree)))
;; Node
(defun igo-sgf-node-leading-white-spaces (node) (aref node 0))
(defun igo-sgf-node-begin (node) (aref node 1))
(defun igo-sgf-node-properties (node) (aref node 2))
(defun igo-sgf-node-end (node) (aref node 3))
(defun igo-sgf-node-get-property (node id)
(seq-find (lambda (prop) (string= (igo-sgf-property-id prop) id))
(igo-sgf-node-properties node)))
(defun igo-sgf-node-get-property-value (node id &optional default)
(let* ((property (igo-sgf-node-get-property node id)))
(if (null property)
(igo-sgf-make-prop-value "" nil default nil)
(igo-sgf-property-ensure-one-value property)
(let* ((values (igo-sgf-property-values property)))
(car values)))))
(defun igo-sgf-node-location-string (node)
(igo-sgf-format-location
(igo-sgf-node-begin node)
(igo-sgf-node-end node)))
;; Root Node
(defun igo-sgf-node-get-board-size (node)
(let* ((sz-value (igo-sgf-node-get-property-value node "SZ" "19"))
(sz (igo-sgf-split-compose sz-value))
(w (string-to-number (igo-sgf-prop-value-content (if (consp sz) (car sz) sz))))
(h (string-to-number (igo-sgf-prop-value-content (if (consp sz) (cdr sz) sz)))))
(if (not (and (>= w 1) (<= w 52) (>= h 1) (<= h 52)))
(error "%sInvalid board size(w=%s h=%s)." (igo-sgf-prop-value-location-string sz-value) w h))
(cons w h)))
(defun igo-sgf-node-check-game-type (node)
(let* ((gm-value (igo-sgf-node-get-property-value node "GM" "1"))
(gm-str (igo-sgf-prop-value-content gm-value)))
(if (not (string= gm-str "1"))
(error "%sGame type is not Go(GM[1])." (igo-sgf-prop-value-location-string gm-value))
1)))
;; Property
(defun igo-sgf-property-leading-white-spaces (prop) (aref prop 0))
(defun igo-sgf-property-begin (prop) (aref prop 1))
(defun igo-sgf-property-id (prop) (aref prop 2))
(defun igo-sgf-property-values (prop) (aref prop 3))
(defun igo-sgf-property-end (prop) (aref prop 4))
(defun igo-sgf-property-location-string (prop)
(igo-sgf-format-location
(igo-sgf-property-begin prop)
(igo-sgf-property-end prop)))
(defun igo-sgf-property-ensure-one-value (prop)
(if (/= (length (igo-sgf-property-values prop)) 1)
(error "%sProperty %s has only one value."
(igo-sgf-property-location-string prop)
(igo-sgf-property-id prop))))
;; Value
(defun igo-sgf-make-prop-value (leading-wss begin-pos value-str end-pos)
(vector leading-wss begin-pos value-str end-pos))
(defun igo-sgf-prop-value-leading-white-spaces (prop-value) (aref prop-value 0))
(defun igo-sgf-prop-value-begin (prop-value) (aref prop-value 1))
(defun igo-sgf-prop-value-content (prop-value) (aref prop-value 2))
(defun igo-sgf-prop-value-end (prop-value) (aref prop-value 3))
(defun igo-sgf-prop-value-location-string (prop-value)
(igo-sgf-format-location
(igo-sgf-prop-value-begin prop-value)
(igo-sgf-prop-value-end prop-value)))
(defun igo-sgf-split-compose (value)
"Split string VALUE with a colon.
(pv \"ABCD\") => (pv \"ABCD\")
(pv \"AB:CD\") => ((pv \"AB\") . (pv \"CD\"))
(pv \"AB\\:):CD\") => ((pv \"AB:)\") . (pv \"CD\"))
(pv \"AB:CD:EF\") => error
NOTE: (pv str)=(igo-sgf-make-prop-value \"\" nil str nil)
"
(let* ((value-str (igo-sgf-prop-value-content value))
(value-length (length value-str))
colon-pos
(i 0))
(while (< i value-length)
(let ((ch (elt value-str i)))
(cond
;; skip next escaped char
((= ch ?\\)
(setq i (1+ i)))
;; colon
((= ch ?:)
(if colon-pos
(error "%sToo many colons in compose value : %s"
(igo-sgf-prop-value-location-string value)
value-str))
(setq colon-pos i)))
(setq i (1+ i))))
(if (null colon-pos)
value
(let ((value-begin (igo-sgf-prop-value-begin value)))
(cons (igo-sgf-make-prop-value
(igo-sgf-prop-value-leading-white-spaces value)
value-begin
(substring value-str 0 colon-pos)
(if (integerp value-begin) (+ value-begin colon-pos)))
(igo-sgf-make-prop-value
""
(if (integerp value-begin) (+ value-begin colon-pos 1))
(substring value-str (1+ colon-pos))
(igo-sgf-prop-value-end value)) )))))
(defun igo-sgf-value-as-text (value)
(if (vectorp value) (setq value (igo-sgf-prop-value-content value)))
;; Delete escaped line-break
(setq value (replace-regexp-in-string "\\\\\\(\n\r?\\|\r\n?\\)" "" value))
;; Escape character
(setq value (replace-regexp-in-string "\\\\\\(.\\)" "\\1" value))
;; Convert Spaces
(setq value (replace-regexp-in-string "\t\\|\v" " " value))
value)
(defun igo-sgf-value-as-simple-text (value)
(if (vectorp value) (setq value (igo-sgf-prop-value-content value)))
;; Delete escaped line-break
(setq value (replace-regexp-in-string "\\\\\\(\n\r?\\|\r\n?\\)" "" value))
;; Escape character
(setq value (replace-regexp-in-string "\\\\\\(.\\)" "\\1" value))
;; Convert Spaces
(setq value (replace-regexp-in-string "\t\\|\v\\|\n\r?\\|\r\n?" " " value))
value)
(defun igo-sgf-value-as-color (value)
(let ((value-str (igo-sgf-prop-value-content value)))
(cond
((string= value-str "B") 'black)
((string= value-str "W") 'white)
(t (error "%sNot a color '%s'"
(igo-sgf-prop-value-location-string value)
value-str)))))
(defun igo-sgf-point-letter-to-number (ch pos)
"Convert a point type(same as move type) letter to an integer.
?a-?z ?A-?Z to 0-25 26-51"
(cond
((and (>= ch ?a) (<= ch ?z)) (- ch ?a))
((and (>= ch ?A) (<= ch ?Z)) (+ (- ch ?A) 26))
(t (error "%s: Invalid point letter '%c'" pos ch))))
(defun igo-sgf-value-as-point-xy (value &optional w h)
"Convert point type(same as move type) value to (x . y).
ex: (igo-sgf-value-as-point-xy (igo-sgf-make-prop-value \"\" nil \"da\" nil))
=> (3 . 0)"
(let ((value-str (igo-sgf-prop-value-content value)))
(if (/= (length value-str) 2)
(error "%sPoint value is not two letters." (igo-sgf-prop-value-location-string value)))
(let ((x (igo-sgf-point-letter-to-number (elt value-str 0) (+ (igo-sgf-prop-value-begin value) 1)))
(y (igo-sgf-point-letter-to-number (elt value-str 1) (+ (igo-sgf-prop-value-begin value) 2))))
(if (not (and (>= x 0) (>= y 0) (or (null w) (< x w)) (or (null h) (< y h))))
(error "%sPoint value %s is out of board (x=%s y=%s)" (igo-sgf-prop-value-location-string value) value-str x y))
(cons x y))))
(defun igo-sgf-expand-compressed-point (value &optional w h)
"https://www.red-bean.com/sgf/sgf4.html#3.5.1
ex:
(igo-sgf-expand-compressed-point (igo-sgf-make-prop-value \"\" nil \"jk\" nil)) => ((9 . 10))
(igo-sgf-expand-compressed-point (igo-sgf-make-prop-value \"\" nil \"jk:lm\" nil) => ((9 . 10) (10 . 10) (11 . 10) (9 . 11) (10 . 11) (11 . 11) (9 . 12) (10 . 12) (11 . 12))
"
;;(if (vectorp value) (setq value (igo-sgf-prop-value-content value)))
(let ((values (igo-sgf-split-compose value)))
(if (vectorp values)
;; not composed
(list (igo-sgf-value-as-point-xy values w h))
;; composed (rectangle)
(let ((lt (igo-sgf-value-as-point-xy (car values) w h))
(rb (igo-sgf-value-as-point-xy (cdr values) w h))
points)
(cl-loop for y from (cdr lt) to (cdr rb) do
(cl-loop for x from (car lt) to (car rb) do
(push (cons x y) points)))
(nreverse points)))))
;; Game Info Properties
(defconst igo-sgf-game-info-properties
'(
("CP" "Copyright")
("US" "Enterer Name")
("AN" "Annotator Name")
("SO" "Source")
("EV" "Event Name")
("GN" "Game Name")
("RO" "Round Number")
("DT" "Date")
("PC" "Place")
("BT" "Black Team" :player black)
("PB" "Black Player" :player black)
("BR" "Black Rank" :player black)
("WT" "White Team" :player white)
("PW" "White Player" :player white)
("WR" "White Rank" :player white)
("RU" "Rule")
("OT" "Overtime Method")
("TM" "Time Limit" :type real)
("HA" "Handicap Stones" :type number)
("KM" "Komi" :type real)
("RE" "Result")
("ON" "Opening Moves")
("GC" "Comment" :type text)
))
(defun igo-sgf-game-info-prop-id (prop)
(car prop))
(defun igo-sgf-game-info-prop-title (prop)
(cadr prop))
(defun igo-sgf-game-info-prop-type (prop)
(or (plist-get (cddr prop) :type) 'simpletext))
;; Encode Text
(defun igo-sgf-text (text &optional compose-type)
;; see: https://www.red-bean.com/sgf/sgf4.html#text
(if compose-type
(replace-regexp-in-string "\\([]:\\\\]\\)" "\\\\\\1" text) ;;escape :
(replace-regexp-in-string "\\([]\\\\]\\)" "\\\\\\1" text)))
(defun igo-sgf-simple-text (text &optional compose-type)
;;@todo same as igo-sgf-text?
;; see: https://www.red-bean.com/sgf/sgf4.html#text
(if compose-type
(replace-regexp-in-string "\\([]:\\\\]\\)" "\\\\\\1" text) ;;escape :
(replace-regexp-in-string "\\([]\\\\]\\)" "\\\\\\1" text)))
(provide 'igo-sgf-parser)
;;; igo-sgf-parser.el ends here