/
parser.go
1725 lines (1559 loc) · 39.8 KB
/
parser.go
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: src/github.com/Ken1JF/sgf/parser.go
* Project: abst-hier
*
* Created by Ken Friedenbach on 12/08/09.
* Copyright 2009-2014, all rights reserved.
*
* This package implements reading of SGF game trees.
*
*/
// Much of this logic is based on the scanner and parser for Go,
// whiich may be found in:
// ${GOROOT}/src/pkg/go/parser/
//
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sgf
import (
"bytes"
"fmt"
"github.com/Ken1JF/ah"
"os"
"strconv"
"strings"
"unicode"
)
type ParserMode uint8
// The mode parameter to the Parse* functions is a set of flags (or 0).
// They control the amount of source code parsed and other optional
// Parser functionality.
const (
ParseComments ParserMode = 1 << iota // parse comments and add them to tree
TraceParser // print a trace of parsed productions
ParserPlay // Do and Undo Board Moves while reading file
ParserGoGoD // apply GoGoD error checks
ParserDbStat // count DataBase statistics
ParserIgnoreUnknSGF // ignore the unknown SGF properties
)
const DefaultParserMode = ParseComments + ParserIgnoreUnknSGF
// The Parser structure holds the Parser's internal state,
// as well as the parse tree slice.
type Parser struct {
// Errors and Warnings
errors ah.ErrorList
warnings ah.ErrorList
UnknownProperty Property // most recent unknown property
// scanner state
scanner Scanner
// Tracing/debugging
mode ParserMode // parsing mode
trace bool // == (mode & TraceParser != 0)
play bool // == (mode & ParserPlay != 0)
dbstat bool // == (mode & ParserDbStat !=0)
indent uint8 // indentation used for tracing output
DBStats *DBStatistics
// moveLimit, 0 => no moveLimit
moveLimit int
limitReached bool
// Next token
pos ah.Position // token ah.Position
tok Token // one token look-ahead
lit []byte // token literal
// Parse tree
GameTree
}
// Add an error
func (p *Parser) Error(pos ah.Position, msg string) {
p.errors.Add(pos, msg)
}
// ReportException prints values of Properties that cannot be understood
func (p *Parser) ReportException(idx PropertyDefIdx, str []byte, err string) {
if idx != TM_idx {
fmt.Printf("BAD Property Value: %s:%d:%d: %s[%s] %s\n", p.pos.Filename, p.pos.Line, p.pos.Column, string(GetProperty(idx).ID), str, err)
}
}
// addProp maintains a variable sized array of properties.
// properties are maintained as a circular linked list.
func (p *Parser) addProp(n TreeNodeIdx, pv PropertyValue) {
err := p.AddAProp(n, pv)
if len(err) != 0 {
p.errors.Add(p.pos, err[0].Msg)
}
}
func (p *Parser) addNode(par TreeNodeIdx, ty TreeNodeType) TreeNodeIdx {
newIdx, err := p.AddChild(par, ty, p.Board.GetMovDepth())
if len(err) != 0 {
p.errors.Add(p.pos, "adding node "+err[0].Msg)
// TODO: need to exit, cannot continue without updating p.treeNodes etc.
}
return newIdx
}
// scannerMode returns the scanner mode bits given the Parser's mode bits.
func scannerMode(mode ParserMode) uint {
var m uint
if mode&ParseComments != 0 {
m |= ScanComments
}
return m
}
// Advance to the next token.
func (p *Parser) next0() {
// Because of one-token look-ahead, print the previous token
// when tracing as it provides a more readable output. The
// very first token (p.pos.Line == 0) is not initialized (it
// is token.ILLEGAL), so don't print it .
if p.trace && p.pos.Line > 0 {
s := p.tok.String()
switch {
case p.tok.IsLiteral():
p.printTrace(s, string(p.lit))
case p.tok.IsOperator():
p.printTrace("\"" + s + "\"")
default:
p.printTrace(s)
}
}
p.pos, p.tok, p.lit = p.scanner.Scan()
}
// Advance to the next token.
func (p *Parser) next() {
p.next0()
}
// initParser must be called before a Parser can be used
func (p *Parser) initParser(filename string, src []byte, mode ParserMode, fileLimit int) {
eh := func(pos ah.Position, msg string) { p.errors.Add(pos, msg) }
p.scanner.InitScanner(filename, src, eh, scannerMode(mode))
p.mode = mode
p.moveLimit = fileLimit
// for convenience (used frequently)
p.trace = (mode&TraceParser != 0) || ah.GetAHTrace()
p.play = (mode&ParserPlay != 0)
p.dbstat = (mode&ParserDbStat != 0)
if p.dbstat { // is this parser supposed to keep statistics?
if theDBStatistics == nil { // is this the first? allocate and initialize
theDBStatistics = new(DBStatistics)
theDBStatistics.initStats()
}
p.DBStats = theDBStatistics // all parsers share the globals
}
p.next()
p.GameTree.initGameTree()
}
func (p *Parser) errorExpected(pos ah.Position, msg string) {
if p.limitReached != true {
msg = "expected " + msg
if pos.Offset == p.pos.Offset {
// the error happened at the current ah.Position;
// make the error message more specific
msg += ", found '" + p.tok.String() + "'"
if p.tok.IsLiteral() {
msg += " " + string(p.lit)
}
}
p.errors.Add(pos, msg)
}
}
func (p *Parser) warningPropertyType(pos ah.Position, m error) {
msg := "PropertyType warning " + m.Error()
if pos.Offset == p.pos.Offset {
// the warning happened at the current ah.Position;
// make the warning message more specific
msg += ", at '" + p.tok.String() + "'"
if p.tok.IsLiteral() {
msg += " " + string(p.lit)
}
}
p.warnings.Add(pos, msg)
}
func (p *Parser) expect(tok Token) ah.Position {
pos := p.pos
if p.tok != tok {
p.errorExpected(pos, "'"+tok.String()+"'")
}
p.next() // make progress in any case
return pos
}
func (p *Parser) expect2(tok1, tok2 Token) ah.Position {
pos := p.pos
if p.tok != tok1 && p.tok != tok2 {
p.errorExpected(pos, "'"+tok1.String()+"' OR '"+tok2.String()+"'")
}
p.next() // make progress in any case
return pos
}
// ----------------------------------------------------------------------------
// Parsing support
func (p *Parser) printTrace(a ...interface{}) {
const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " +
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
const n = uint8(len(dots))
fmt.Printf("%5d:%3d: ", p.pos.Line, p.pos.Column)
i := 2 * p.indent
for ; i > n; i -= n {
fmt.Print(dots)
}
fmt.Print(dots[0:i])
fmt.Println(a...)
}
func trace(p *Parser, msg string) *Parser {
p.printTrace(msg, "(")
p.indent++
return p
}
// Usage pattern: defer un(trace(p, "..."));
func un(p *Parser) {
p.indent--
p.printTrace(")")
}
// ----------------------------------------------------------------------------
// Source files
func (p *Parser) parsePropValue(val PropValueType) (pv PropertyValue) {
if p.trace {
defer un(trace(p, "parsePropValue"))
}
pv.ValType = val
if len(p.lit) > 0 {
pv.StrValue = p.lit
}
switch val {
case Unknown, SimpText, Text, CompSimpText_simpText:
pv.ValType = val
if p.tok == STRING {
p.next()
p.expect(RBRACK)
} else if p.tok == RBRACK { // empty string
pv.ValType = None
p.next()
} else {
p.errorExpected(p.pos, ValueNames[val])
p.next()
p.expect(RBRACK)
}
case None_OR_compNum_simpText:
if p.tok == RBRACK {
pv.ValType = None
p.next()
} else if p.tok == STRING {
pv.ValType = CompNum_simpText
p.next()
p.expect(RBRACK)
}
case None:
if p.tok == RBRACK {
pv.ValType = None
p.next()
} else {
p.errorExpected(p.pos, ValueNames[val])
p.next()
p.expect(RBRACK)
}
case CompressedListOfPoint:
if (len(pv.StrValue) % 2) != 0 {
p.errors.Add(p.pos, "CompressedListOfPoint not even:"+string(pv.StrValue))
}
p.next()
p.expect(RBRACK)
case ListOfCompPoint_simpTest:
pv.ValType = ListOfCompPoint_simpTest
p.next()
p.expect(RBRACK)
for p.tok == LBRACK { // more than one, concatenate together, with separator brackets
var sep = make([]byte, 2)
sep[0] = ']'
sep[1] = '['
p.next()
pv.StrValue = append(pv.StrValue, sep...)
pv.StrValue = append(pv.StrValue, p.lit...)
p.next()
p.expect(RBRACK)
}
case ListOfCompPoint_Point:
pv.ValType = ListOfCompPoint_Point
p.next()
p.expect(RBRACK)
for p.tok == LBRACK { // more than one, concatenate together. User can process
p.next()
pv.StrValue = append(pv.StrValue, p.lit...)
p.next()
p.expect(RBRACK)
}
case EListOfPoint:
if p.tok == RBRACK {
pv.ValType = None
p.next()
} else {
pv.ValType = Point
_, err := SGFPoint(pv.StrValue)
if len(err) != 0 {
p.errors.Add(p.pos, "Bad "+ValueNames[val]+": "+err[0].Msg+": from "+string(pv.StrValue))
}
p.next()
p.expect(RBRACK)
for p.tok == LBRACK { // more than one, make it a CompressedListOfPoint
pv.ValType = ListOfPoint
p.next()
pv.StrValue = append(pv.StrValue, p.lit...)
if (len(pv.StrValue) % 2) != 0 {
p.errors.Add(p.pos, "EListOfPoint not even:"+string(pv.StrValue))
}
p.next()
p.expect(RBRACK)
}
}
case Point, Move, Stone:
_, err := SGFPoint(pv.StrValue)
if len(err) != 0 {
p.errors.Add(p.pos, "Bad "+ValueNames[val]+": "+err[0].Msg+": from "+string(pv.StrValue))
}
p.next()
p.expect(RBRACK)
case ListOfPoint, ListOfStone:
pv.ValType = Point
_, err := SGFPoint(pv.StrValue)
if len(err) != 0 {
p.errors.Add(p.pos, "Bad "+ValueNames[val]+": "+err[0].Msg+": from "+string(pv.StrValue))
}
p.next()
p.expect(RBRACK)
for p.tok == LBRACK { // more than one, make it a ListOfPoint
pv.ValType = ListOfPoint
p.next()
_, err := SGFPoint(p.lit)
if len(err) != 0 {
p.errors.Add(p.pos, "Bad "+ValueNames[val]+": "+err[0].Msg+": from "+string(pv.StrValue))
}
pv.StrValue = append(pv.StrValue, p.lit...)
if (len(pv.StrValue) % 2) != 0 {
p.errors.Add(p.pos, "CompressedListOfPoint not even:"+string(pv.StrValue))
}
p.next()
p.expect(RBRACK)
}
case Num_OR_compNum_num: // only used for SZ, don't have any rectangular board tests, for now
if p.tok == STRING {
var err error
idx_colon := strings.Index(string(p.lit), ":")
if idx_colon > 0 {
_, err = strconv.Atoi(string(p.lit[0:idx_colon]))
if err != nil {
p.errors.Add(p.pos, "Error in composite (column): "+err.Error()+string(p.lit[0:idx_colon]))
}
_, err = strconv.Atoi(string(p.lit[idx_colon+1:]))
if err != nil {
p.errors.Add(p.pos, "Error in composite (row): "+err.Error()+string(p.lit[idx_colon+1:]))
}
} else {
_, err = strconv.Atoi(string(p.lit))
if err != nil {
p.errors.Add(p.pos, "Error in number: "+err.Error()+string(p.lit))
}
}
} else {
p.errorExpected(p.pos, ValueNames[val])
}
p.next()
p.expect(RBRACK)
case Num_0_3, Num_1_4, Num_1_5_or_7_16, Num:
if p.tok == STRING {
i, _ := strconv.Atoi(string(p.lit))
if val != Num {
switch val {
case Num_0_3:
if i < 0 || i > 3 {
p.errors.Add(p.pos, "not in range 0-3: "+string(p.lit))
}
case Num_1_4:
if i < 1 || i > 4 {
p.errors.Add(p.pos, "not in range 1-4: "+string(p.lit))
}
case Num_1_5_or_7_16:
if i < 1 || i > 16 || i == 6 {
p.errors.Add(p.pos, "not in range 1-5 or 7-15: "+string(p.lit))
}
}
}
} else {
p.errorExpected(p.pos, ValueNames[val])
}
p.next()
p.expect(RBRACK)
case Real:
if p.tok == STRING { // pass as a string
p.next()
p.expect(RBRACK)
} else if p.tok == RBRACK { // empty string
pv.ValType = None
p.next()
} else {
p.errorExpected(p.pos, ValueNames[val])
p.next()
p.expect(RBRACK)
}
case Double:
p.errors.Add(p.pos, "Not Implemented: "+ValueNames[val])
p.next()
p.expect(RBRACK)
case Color:
p.errors.Add(p.pos, "Not Implemented: "+ValueNames[val])
p.next()
p.expect(RBRACK)
// not possible? default:
}
if p.trace {
fmt.Printf("ValType: %s String: %s\n", ValueNames[pv.ValType], string(pv.StrValue))
}
return pv
}
func TakeOutNum(RE_com []byte) (bas []byte, n int, sep byte, both bool) {
bas = make([]byte, len(RE_com))
for i, b := range RE_com {
bas[i] = b
}
ln := len(bas)
if ln > 0 {
if bas[0] == '(' {
sep = '('
if bas[ln-1] == ')' {
bas = bas[1 : ln-1]
both = true
} else {
bas = bas[1:ln]
}
} else if bas[0] == '{' {
sep = '{'
if bas[ln-1] == '}' {
bas = bas[1 : ln-1]
both = true
} else {
bas = bas[1:ln]
}
}
j := 0
for _, b := range bas {
if unicode.IsDigit(rune(b)) {
if n == 0 {
bas[j] = '%'
j += 1
}
n = 10*n + int(b-'0')
} else {
bas[j] = b
j += 1
}
}
bas = bas[0:j]
}
return bas, n, sep, both
}
func check_Rank(strVal []byte) (err string) {
s := string(strVal)
if s == "9" {
err = "check rank:" + s
}
if s == "9di" {
err = "check rank:" + s
}
if s == "9f" {
err = "check rank:" + s
}
if s == "9g" {
err = "check rank:" + s
}
if s == "5e" {
err = "check rank:" + s
}
if s == "98d" {
err = "check rank:" + s
}
if s == "2D" {
err = "check rank:" + s
}
if s == "7d ams" {
err = "check rank:" + s
}
if s == "0d" {
err = "check rank:" + s
}
if s == "2.5" {
err = "check rank:" + s
}
if s == "NR" {
err = "check rank:" + s
}
if s == "a5" {
err = "check rank:" + s
}
if s == "a6" {
err = "check rank:" + s
}
if s == "Holder" {
err = "check rank:" + s
}
if s == " 7d" {
err = "check rank:" + s
}
if s == "Wangwi" {
err = "check rank:" + s
}
if s == "7d Ama" {
err = "check rank:" + s
}
return err
}
func check_Name(strVal []byte) (err string) {
s := string(strVal)
if s == "artu" {
err = "check name:" + s
}
if s == "jy23" {
err = "check name:" + s
}
if s == "thug" {
err = "check name:" + s
}
if s == "Yoshida" {
err = "check name:" + s
}
if s == "Yi" {
err = "check name:" + s
}
if s == "World" {
err = "check name:" + s
}
if s == "Turtles" {
err = "check name:" + s
}
if s == "Two shodans" {
err = "check name:" + s
}
if s == "Storks" {
err = "check name:" + s
}
if s == "Seo" {
err = "check name:" + s
}
if s == "Old Lady of Black-horse Mountain" {
err = "check name:" + s
}
if s == "NHK viewers, by internet poll" {
err = "check name:" + s
}
if s == "NO1NO1" {
err = "check name:" + s
}
if s == "MoGo Titan" {
err = "check name:" + s
}
if s == "Miss Y." {
err = "check name:" + s
}
if s == "Maeda" {
err = "check name:" + s
}
if s == "Li Ang" {
err = "check name:" + s
}
if s == "Kuwata" {
err = "check name:" + s
}
if s == "Kuboniwa" {
err = "check name:" + s
}
if s == "KCC Igo program" {
err = "check name:" + s
}
if s == "Harada" {
err = "check name:" + s
}
if s == "Goemate" {
err = "check name:" + s
}
if s == "Go Professional III" {
err = "check name:" + s
}
if s == "GO4++ program" {
err = "check name:" + s
}
if s == "Fukuhara" {
err = "check name:" + s
}
if s == "Fuji Hiroshi" {
err = "check name:" + s
}
if s == "Fairy" {
err = "check name:" + s
}
if s == "Anon." {
err = "check name:" + s
}
if s == "An Immortal" {
err = "check name:" + s
}
if s == "A Go Review subscriber" {
err = "check name:" + s
}
if s == "99P" {
err = "check name:" + s
}
if s == "Another Immortal" {
err = "check name:" + s
}
return err
}
func check_RE(strVal []byte) (err string) {
s := string(strVal)
/* First round of fixes:
if s == "B+2,5" {
err = "check value: B+2,5 (must be B+2.5)"
}
if s == "W+0,5" {
err = "check value: W+0,5 (must be W+0.5)"
}
if s == "W+40 zi" {
err = "check value: W+40 zi (must be BW+40)"
}
if s == "W+34 zi" {
err = "check value: W+34 zi (must be W+34)"
}
if s == "B+05." {
err = "check value: B+05. (must be B+0.5)"
}
if s == "W4.5" {
err = "check value: W4.5 (must be W+4.5)"
}
if s == "w+R" {
err = "check value: w+R (must be W+R)"
}
if s == "B8.5" {
err = "check value: B8.5 (must be B+8.5)"
}
if s == "B++" {
err = "check value: B++ (must be B+)"
}
*/
if s == "UF" {
err = "check value: UF"
}
if s == "W+jigo" {
err = "check value: W+jigo"
}
if s == "B+).5" {
err = "check value: B+).5"
}
if s == "B+8.8" {
err = "check value: B+8.8"
}
// This is O.K. (in 1988/1988-08-23c.sgf:)
// There is a seki and rules are Ing Goe
//
// if s == "W+6.6" {
// err = "check value: W+6.6"
// }
if s == "W+3.35" {
err = "check value: W+3.35"
}
// This is O.K. (in 1988/1988-08-23b.sgf:)
// There is a seki and rules are Ing Goe
//
// if s == "B+1.83" {
// err = "check value: B+1.83"
// }
if s == "B+11.4" {
err = "check value: B+11.4"
}
if s == "B+2.4" {
err = "check value: B+2.4"
}
if s == "B+1.55" {
err = "check value: B+1.55"
}
if s == "B+35." {
err = "check value: B+35."
}
if s == "B+.75" {
err = "check value: B+.75"
}
if s == "B+.5" {
err = "check value: B+.5"
}
return err
}
func SplitRE(str []byte) (val []byte, com []byte) {
iLP := bytes.IndexByte(str, '(')
iLB := bytes.IndexByte(str, '{')
if iLP > 0 {
if iLB > 0 {
if iLB < iLP {
iLP = iLB
}
}
ix := iLP - 1
for str[ix] == ' ' {
if ix == 0 {
break
}
ix -= 1
}
val = str[0 : ix+1]
com = str[iLP:]
} else if iLB > 0 {
ix := iLB - 1
for str[ix] == ' ' {
if ix == 0 {
break
}
ix -= 1
}
val = str[0 : ix+1]
com = str[iLB:]
} else {
val = str
}
if string(val) == "Left unfinished" {
val = []byte("Void")
if len(com) == 0 {
com = []byte("(Left unfinished)")
} else {
var newCom []byte
if com[0] == '(' {
newCom = append([]byte("(Left unfinished. "), com[1:]...)
} else if com[0] == '{' {
newCom = append([]byte("{Left unfinished. "), com[1:]...)
} else {
newCom = append([]byte("(Left unfinished) "), com...)
}
com = newCom
}
}
return val, com
}
func (p *Parser) processProperty(pv PropertyValue, nodd TreeNodeIdx) (ret TreeNodeIdx) {
if p.trace {
defer un(trace(p, "processProperty"))
}
ret = nodd
// TODO: get rid of idx when all the "Not implemented:" messages are gone...
idx := pv.PropType
// fmt.Println("Node", nodd, "idx", idx, "Description", GetProperty(idx).Description);
// os.Exit(998)
if p.dbstat {
if idx >= 0 {
p.DBStats.ID_Counts[idx] += 1
} else {
p.DBStats.Unkn_Count += 1
}
}
switch idx {
case AB_idx:
if pv.ValType == Point {
// Add point to Board
mov, err := SGFPoint(pv.StrValue)
if len(err) != 0 {
p.errors.Add(p.pos, "Bad Point for AB: "+err.Error()+" B["+string(pv.StrValue)+"]")
} else {
err = p.DoAB(mov, p.play)
if len(err) != 0 {
p.errors.Add(p.pos, "Error from DoAB: "+err.Error()+" B["+string(pv.StrValue)+"]")
}
}
// Record the property:
p.addProp(ret, pv)
} else {
str := pv.StrValue
for len(str) > 0 { // compressed list of Points
// Make a new pv
npv := pv
npv.StrValue = str[0:2]
// Add point to Board
mov, err := SGFPoint(npv.StrValue)
if len(err) != 0 {
p.errors.Add(p.pos, "Bad Point List element for AB: "+err.Error()+" B["+string(pv.StrValue)+"]")
} else {
err = p.DoAB(mov, p.play)
if len(err) != 0 {
p.errors.Add(p.pos, "Error from DoAB:"+err.Error()+"in List element, B["+string(pv.StrValue)+"]")
}
}
str = str[2:]
}
// Record the property: (only once)
p.addProp(ret, pv)
}
case AE_idx:
if pv.ValType == Point {
// Add point to Board
mov, err := SGFPoint(pv.StrValue)
if len(err) != 0 {
p.errors.Add(p.pos, "Bad Point for AE: "+err.Error()+": from "+string(pv.StrValue))
} else {
err = p.DoAE(mov, p.play)
if len(err) != 0 {
p.errors.Add(p.pos, "Error from DoAE: "+err.Error()+": caused by "+string(pv.StrValue))
}
}
// Record the property:
p.addProp(ret, pv)
} else {
str := pv.StrValue
for len(str) > 0 { // compressed list of Points
// Make a new pv
npv := pv
npv.StrValue = str[0:2]
// Add point to Board
mov, err := SGFPoint(npv.StrValue)
if len(err) != 0 {
p.errors.Add(p.pos, "Bad Point List element for AE: "+err.Error()+": from "+string(npv.StrValue))
} else {
err = p.DoAE(mov, p.play)
if len(err) != 0 {
p.errors.Add(p.pos, "Error from DoAE:"+err.Error()+" in List element: "+string(npv.StrValue))
}
}
str = str[2:]
}
// Record the property: (only once)
p.addProp(ret, pv)
}
case AN_idx:
// set the board AN:
p.SetAN(pv.StrValue)
// Record the property:
p.addProp(ret, pv)
case AP_idx:
// set the board AP:
p.SetAP(pv.StrValue)
// record the property:
p.addProp(ret, pv)
case AR_idx:
// set the board AR:
p.DoAR(pv.StrValue)
// record the property:
p.addProp(ret, pv)
case AS_idx:
// record the property:
p.addProp(ret, pv)
case AW_idx:
if pv.ValType == Point {
// Add point to Board
mov, err := SGFPoint(pv.StrValue)
if len(err) != 0 {
p.errors.Add(p.pos, "Bad Point for AW: "+err.Error()+": from "+string(pv.StrValue))
} else {
err = p.DoAW(mov, p.play)
if len(err) != 0 {
p.errors.Add(p.pos, "Error from DoAW: "+err.Error()+": caused by "+string(pv.StrValue))
}
}
// Record the property:
p.addProp(ret, pv)
} else {
str := pv.StrValue
for len(str) > 0 { // compressed list of Points
// Make a new pv
npv := pv
npv.StrValue = str[0:2]
// Add point to Board
mov, err := SGFPoint(npv.StrValue)
if len(err) != 0 {
p.errors.Add(p.pos, "Bad Point List element for AW: "+err.Error()+": from "+string(npv.StrValue))
} else {
err = p.DoAW(mov, p.play)
if len(err) != 0 {
p.errors.Add(p.pos, "Error from DoAW: "+err.Error()+" in List element: "+string(npv.StrValue))
}
}
str = str[2:]
}
// Record the property: (once)
p.addProp(ret, pv)
}
case B_idx:
p.treeNodes[ret].TNodType = BlackMoveNode
mov, err := SGFPoint(pv.StrValue)
if len(err) != 0 {
p.errors.Add(p.pos, err.Error()+" in SGFPoint, B["+string(pv.StrValue)+"]")
} else {
p.treeNodes[ret].propListOrNodeLoc = PropIdx(mov)
movN, err := p.DoB(mov, p.play)
if movN == 1 && p.dbstat {
p.SetPlayerRank()
}
if len(err) != 0 {
p.warnings.Add(p.pos, err.Error()+" B["+string(pv.StrValue)+"]")
}
if (p.moveLimit > 0) && (movN >= p.moveLimit) {
p.limitReached = true
if p.trace {
p.printTrace("limitReached set to true in B")
}
}
}
case BL_idx:
// record the property:
p.addProp(ret, pv)
case BM_idx:
// record the property:
p.addProp(ret, pv)
case BR_idx:
// set the board BR:
p.SetBR(pv.StrValue)
if p.dbstat {
// count the BR values:
idx := string(pv.StrValue)
n, _ := p.DBStats.BWRank_map[idx]
p.DBStats.BWRank_map[idx] = n + 1
// check the rank
errStr := check_Rank(pv.StrValue)
if errStr != "" {
p.ReportException(BR_idx, pv.StrValue, errStr)
}
}
// record the property:
p.addProp(ret, pv)
case BT_idx:
// set the board BT:
p.SetBT(pv.StrValue)
// record the property:
p.addProp(ret, pv)
case C_idx:
// record the property:
// TODO: need to process escape characters?
if (p.mode & ParseComments) != 0 {
p.addProp(ret, pv)
}