-
Notifications
You must be signed in to change notification settings - Fork 0
/
PgnReader-0.1.tm
461 lines (391 loc) · 11.8 KB
/
PgnReader-0.1.tm
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
if {[catch {package require Itcl}]} {
if {[file exists [file join [file dirname [info script]] .. tcl++]]} {
lappend auto_path [file join [file dirname [info script]] ..]
} elseif {[file exists [file join [file dirname [info script]] tcl++]]} {
lappend auto_path [file dirname [info script]]
}
package require tcl++
interp alias {} itcl::class {} tcl++::class
}
itcl::class PgnReader {
public variable case 1
public variable verbose 0
public variable verbout stderr
private variable yyistcl83 -1
public variable yydata ""
public variable yyindex 0
private variable yymatches
public variable yystate "INITIAL"
public variable yytext
public variable yystart
public variable yyend
private common yyregexs
private common yyigncase
private common yystates
array set yyregexs {
1 {\[[A-Za-z]+[^\]]+\]\+?}
2 {[^0-9]1\.}
3 {[-0-2/]{3,}}
4 {[0ORNBQKa-h][-Oa-h0-8x\+]+}
5 {[\{]}
6 {[\}]}
7 {[^\}]}
}
array set yyigncase {
1 0
2 0
3 0
4 0
5 0
6 0
7 0
}
array set yystates {
COMMENT {
6
7
}
GAME {
3
4
5
}
__all__ {
1
2
}
}
private {
variable nesting 0
variable headers
variable moves
variable comments
variable ccomm ;# current comment
variable PGN
variable HM 0
}
public {
method comment {number {color ""}} {
if {$color ne ""} {
set col [string tolower [string range $color 0 0]]
set hm [expr {($number -1)*2}]
incr hm 1
if {$col eq "b"} {
incr hm 1
}
} else {
set hm $number
}
if {[dict exists $comments $hm]} {
return [dict get $comments $hm]
} else {
return ""
}
}
}
public {
method comments {} {
return $comments
}
}
public {
method getGame {filename n} {
set pgn ""
if [catch {open $filename r} infh] {
puts stderr "Cannot open $filename: $infh"
exit
} else {
set x 0
while {[gets $infh line] >= 0} {
if {[regexp {^\[Event } $line]} {
incr x
if {$x == $n} {
set pgn "$line\n"
} elseif {$x > $n} {
break
}
} elseif {$x == $n} {
append pgn "$line\n"
}
}
close $infh
return $pgn
}
}
}
public {
method header {htype} {
if {[info exists headers($htype)]} {
return $headers($htype)
} else {
return "*"
}
}
}
public {
method move {number {color ""}} {
set hm [expr {$number*2-2}]
if {$color eq ""} {
set color w
}
set col [string tolower [string range $color 0 0]]
if {$col eq "b"} {
incr hm 1
}
return [lindex $moves $hm]
}
}
public {
method moves {} {
return $moves
}
}
public {
method scanPGNFile {filename {n 1}} {
set pgn [$this getGame $filename $n]
set PGN $pgn
array set headers [list]
set moves [list]
set nesting 0
$this start $pgn
$this run
return [llength moves]
}
}
public {
method scanPGNText {pgntext} {
set PGN $pgntext
array set headers [list]
set moves [list]
set nesting 0
$this start $pgntext
$this run
return [llength moves]
}
}
constructor {args} {
eval $this configure $args
array set headers [list]
set comments [dict create 0 None]
}
public method reset {} {
if {$verbose} {
puts $verbout "PgnReader: reset, entering state INITIAL"
}
set yyindex 0
set yystate "INITIAL"
catch {unset yymatches}
}
public method start {yynewstr} {
reset
set yydata $yynewstr
}
protected method yyerror {yyerrmsg} {
puts $verbout "PgnReader: $yyerrmsg"
}
private method yyupdate {} {
if {$yyistcl83 == -1} {
if {[package vcompare [info tclversion] 8.3] >= 0} {
set yyistcl83 1
} else {
set yyistcl83 0
}
}
set yybestmatch -1
set yybestbegin [string length $yydata]
set yybestend $yybestbegin
if {$verbose >= 2} {
set yycurdata [string range $yydata $yyindex [expr {$yyindex + 16}]]
set yycurout [string map {\r \\r \n \\n} $yycurdata]
if {$yyindex + 16 < [string length $yydata]} {
append yycurout "..."
}
puts $verbout "PgnReader: looking for match at position $yyindex: `$yycurout'"
}
if {[info exists yystates($yystate)]} {
set yyruleset [concat $yystates($yystate) $yystates(__all__)]
} else {
set yyruleset $yystates(__all__)
}
if {[llength $yyruleset] == 0} {
if {$verbose} {
puts $verbout "PgnReader: no active rules in state $yystate"
}
}
foreach yyruleno $yyruleset {
#
# if the last match is in the past, rerun regexp
#
if {![info exists yymatches($yyruleno)] || [lindex [lindex $yymatches($yyruleno) 0] 0] < $yyindex} {
if {$yyistcl83} {
if {!$case || $yyigncase($yyruleno)} {
set yyfound [regexp -nocase -start $yyindex -inline -indices -- $yyregexs($yyruleno) $yydata]
} else {
set yyfound [regexp -start $yyindex -inline -indices -- $yyregexs($yyruleno) $yydata]
}
} else {
if {!$case || $yyigncase($yyruleno)} {
set yyres [regexp -nocase -start $yyindex -indices -- $yyregexs($yyruleno) $yydata yyfound]
} else {
set yyres [regexp -start $yyindex -indices -- $yyregexs($yyruleno) $yydata yyfound]
}
if {!$yyres} {
set yyfound [list]
} else {
set yyfound [list $yyfound]
}
}
if {[llength $yyfound] > 0} {
set yymatches($yyruleno) $yyfound
} else {
set yymatches($yyruleno) [list [string length $yydata] [string length $yydata]]
}
if {$verbose >= 3 && [llength $yymatches($yyruleno)] > 0} {
set yymatchbegin [lindex [lindex $yymatches($yyruleno) 0] 0]
set yymatchend [lindex [lindex $yymatches($yyruleno) 0] 1]
if {$yymatchbegin != [string length $yydata]} {
set yymatchdata [string range $yydata $yymatchbegin $yymatchend]
set yymatchout [string map {\r "\\r" \n "\\n"} $yymatchdata]
if {[string length $yymatchout] > 20} {
set yymatchout [string range $yymatchout 0 16]
append yymatchout "..."
}
puts $verbout "PgnReader: next match for rule $yyruleno is $yymatchbegin-$yymatchend: `$yymatchout'"
} else {
puts $verbout "PgnReader: rule $yyruleno does not match anwhere in remaining text"
}
}
}
#
# see if the match is better than the one we already have
#
set yymatchbegin [lindex [lindex $yymatches($yyruleno) 0] 0]
set yymatchend [lindex [lindex $yymatches($yyruleno) 0] 1]
if {($yymatchbegin < $yybestbegin) || ($yymatchbegin == $yybestbegin && $yymatchend > $yybestend)} {
set yybestmatch $yyruleno
set yybestbegin $yymatchbegin
set yybestend $yymatchend
if {$verbose >= 2} {
set yybestdata [string range $yydata $yybestbegin $yybestend]
set yymatchout [string map {\r "\\r" \n "\\n"} $yybestdata]
if {[string length $yymatchout] > 20} {
set yymatchout [string range $yymatchout 0 16]
append yymatchout "..."
}
puts $verbout "PgnReader: new best match rule $yyruleno, $yybestbegin-$yybestend: `$yymatchout'"
}
}
}
return $yybestmatch
}
public method step {} {
if {$yyindex >= [string length $yydata]} {
if {$verbose} {
puts $verbout "PgnReader: scanner at EOF"
}
return [list -1 1 ""]
}
set yyruleno [yyupdate]
if {$yyruleno == -1} {
if {$verbose} {
puts $verbout "PgnReader: no further match until EOF"
}
set yyindex [string length $yydata]
return [list -1 1 ""]
}
set yymatch $yymatches($yyruleno)
set yyindices [lindex $yymatch 0]
set yystart [lindex $yyindices 0]
set yyend [lindex $yyindices 1]
set yytext [string range $yydata $yystart $yyend]
if {$verbose} {
set yymatchout [string map {\r \\r \n \\n} $yytext]
if {[string length $yymatchout] > 20} {
set yymatchout [string range $yymatchout 0 16]
append yymatchout "..."
}
puts $verbout "PgnReader: best match at $yyindex using rule $yyruleno, $yystart-$yyend: `$yymatchout'"
}
set yyindex [expr {$yyend + 1}]
for {set yyi 0} {$yyi < [llength $yymatch]} {incr yyi} {
set yysubidxs [lindex $yymatch $yyi]
set yysubbegin [lindex $yysubidxs 0]
set yysubend [lindex $yysubidxs 1]
set $yyi [string range $yydata $yysubbegin $yysubend]
}
set yyoldstate $yystate
set yyretcode [catch {
switch -- $yyruleno {
1 {
regexp {\[([A-Za-z]+).+?"(.+)"} $yytext -> header value
#puts $yytext
#puts "$header: $value"
set headers($header) $value
}
2 {
set yystate GAME
set moves [list]
set HM 0
}
3 {
#puts "result: $yytext"
set yystate ""
}
4 {
#puts "move $yytext"
lappend moves $yytext
incr HM 1
}
5 {
incr nesting
set ccomm ""
set yystate COMMENT
}
6 {
incr nesting -1
if {$nesting == 0} {
dict set comments $HM $ccomm
set ccomm ""
set yystate GAME
}
}
7 {
append ccomm $yytext
}
}
} yyretdata]
if {$verbose} {
if {![string match $yyoldstate $yystate]} {
puts $verbout "PgnReader: leaving state $yyoldstate, entering $yystate"
}
}
if {$yyretcode == 1} {
global errorInfo
set yyerrmsg "script for rule # $yyruleno failed: $yyretdata"
yyerror $yyerrmsg
append errorInfo "\n while executing script for rule # $yyruleno"
error $yyerrmsg $errorInfo
}
if {$yyretcode == 2} {
return [list $yyruleno 1 $yyretdata]
}
return [list $yyruleno 0 ""]
}
public method next {} {
while {42} {
set yysd [step]
if {[lindex $yysd 1] == 1} {
return [lindex $yysd 2]
}
}
}
public method run {} {
set yyresult [list]
set yysd [next]
while {$yysd != ""} {
lappend yyresult $yysd
set yysd [next]
}
return $yyresult
}
}