summaryrefslogtreecommitdiff
path: root/qml/python/sgfparser.py
blob: 2ad91c9a8fa2c0122863dc8e236566847eb3ce8e (plain)
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
# File: sgfparser.py

##   This is part of uliGo 0.4, a program for practicing
##   go problems. For more information, see http://www.g0ertz.de/uligo/

##   Copyright (C) 2001-12 Ulrich Goertz (uligo@g0ertz.de)

##   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 2 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 (gpl.txt); if not, write to the Free Software
##   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##   The GNU GPL is also currently available at
##   http://www.gnu.org/copyleft/gpl.html

import re
import string

class SGFError(Exception): pass

reGameStart = re.compile(r'\(\s*;')
reRelevant = re.compile(r'[\[\]\(\);]')
reStartOfNode = re.compile(r'\s*;\s*')

def SGFescape(s):
    t = s.replace('\\', '\\\\')
    t = t.replace(']', '\\]')
    return t

class Node:
    def __init__(self, previous=None, SGFstring = '', level=0):
        self.previous = previous
        self.next = None
        self.up = None
        self.down = None
        self.level = level # self == self.previous.next[self.level]

        self.numChildren = 0

        self.SGFstring = SGFstring
        self.parsed = 0
        if self.SGFstring:
            self.parseNode()
        else:
            self.data = {}

        self.posyD = 0


    def getData(self):
        if not self.parsed: self.parseNode()
        return self.data

    def pathToNode(self):
        l = []
        n = self

        while n.previous:
            l.append(n.level)
            n = n.previous

        l.reverse()
        return l


    def parseNode(self):

        if self.parsed: return

        s = self.SGFstring
        i = 0

        match = reStartOfNode.search(s, i)
        if not match:
            raise SGFError('No node found')
        i = match.end()

        node = {}
        while i < len(s):

            while i < len(s) and s[i] in string.whitespace: i += 1
            if i >= len(s): break

            ID = []

            while not s[i] == '[':
                if s[i] in string.ascii_uppercase:
                    ID.append(s[i])
                elif not s[i] in string.ascii_lowercase + string.whitespace:
                    raise SGFError('Invalid Property ID')

                i += 1

                if i >= len(s):
                    raise SGFError('Property ID does not have any value')

            i += 1

            key = ''.join(ID)

            if key == '': raise SGFError('Property does not have a correct ID')


            if key in node:
                if not Node.sloppy:
                    raise SGFError('Multiple occurrence of SGF tag')
            else:
                node[key] = []

            propertyValueList = []
            while 1:
                propValue = []
                while s[i] != ']':
                    if s[i] == '\t':      # convert whitespace to ' '
                        propValue.append(' ')
                        i += 1
                        continue
                    if s[i] == '\\':
                        i += 1            # ignore escaped characters, throw away backslash
                        if s[i:i+2] in ['\n\r', '\r\n']:
                            i += 2
                            continue
                        elif s[i] in ['\n', '\r']:
                            i += 1
                            continue
                    propValue.append(s[i])
                    i += 1

                    if i >= len(s):
                        raise SGFError('Property value does not end')

                propertyValueList.append(''.join(propValue))

                i += 1

                while i < len(s) and s[i] in string.whitespace:
                    i += 1

                if i >= len(s) or s[i] != '[': break
                else: i += 1

            if key in ['B', 'W', 'AB', 'AW']:
                for N in range(len(propertyValueList)):
                    en = propertyValueList[N]
                    if Node.sloppy:
                        en = en.replace('\n', '')
                        en = en.replace('\r', '')
                    if not (len(en) == 2 or (len(en) == 0 and key in ['B', 'W'])):
                        raise SGFError('')
                    propertyValueList[N] = en

            node[key].extend(propertyValueList)

        self.data = node
        self.parsed = 1


Node.sloppy = 1

# ------------------------------------------------------------------------------------

class Cursor:

    """ Initialized with an SGF file. Then use game(n); next(n), previous to navigate.
    self.collection is list of Nodes, namely of the root nodes of the game trees.

    self.currentN is the current Node
    self.currentNode() returns self.currentN.data

    The sloppy option for __init__ determines if the following things, which are not allowed
    according to the SGF spec, are accepted nevertheless:
     - multiple occurrences of a tag in one node
     - line breaks in AB[]/AW[]/B[]/W[] tags (e.g. "B[a\nb]")
    """


    def __init__(self, sgf, sloppy = 1):
        Node.sloppy = sloppy

        self.height = 0
        self.width = 0
        self.posx = 0
        self.posy = 0

        self.root = Node(None, '', 0)

        self.parse(sgf)
        self.currentN = self.root.next
        self.setFlags()

    def setFlags(self):
        if self.currentN.next: self.atEnd = 0
        else: self.atEnd = 1
        if self.currentN.previous: self.atStart = 0
        else: self.atStart = 1

    def noChildren(self):
        return self.currentN.numChildren

    def currentNode(self):
        if not self.currentN.parsed:
            self.currentN.parseNode()
        return self.currentN.data

    def parse(self, sgf):

        curr = self.root

        p = -1           # start of the currently parsed node
        c = []           # list of nodes from which variations started
        last = ')'       # type of last aprsed bracked ('(' or ')')
        inbrackets = 0   # are the currently parsed characters in []'s?

        height_previous = 0
        width_currentVar = 0

        i = 0 # current parser position

        # skip everything before first (; :

        match = reGameStart.search(sgf, i)
        if not match:
            raise SGFError('No game found')

        i = match.start()

        while i < len(sgf):

            match = reRelevant.search(sgf, i)
            if not match:
                break
            i = match.end() - 1

            if inbrackets:
                if sgf[i]==']':
                    numberBackslashes = 0
                    j = i-1
                    while sgf[j] == '\\':
                        numberBackslashes += 1
                        j -= 1
                    if not (numberBackslashes % 2):
                        inbrackets = 0
                i = i + 1
                continue

            if sgf[i] == '[':
                inbrackets = 1

            if sgf[i] == '(':
                if last != ')':       # start of first variation of previous node
                    if p != -1: curr.SGFstring = sgf[p:i]

                nn = Node()
                nn.previous = curr

                width_currentVar += 1
                if width_currentVar > self.width: self.width = width_currentVar

                if curr.next:
                    last = curr.next
                    while last.down: last = last.down
                    nn.up = last
                    last.down = nn
                    nn.level = last.level + 1
                    self.height += 1
                    nn.posyD = self.height - height_previous
                else:
                    curr.next = nn
                    nn.posyD = 0
                    height_previous = self.height

                curr.numChildren += 1

                c.append((curr, width_currentVar-1, self.height))

                curr = nn

                p = -1
                last = '('

            if sgf[i] == ')':
                if last != ')' and p != -1:
                    curr.SGFstring = sgf[p:i]
                try:
                    curr, width_currentVar, height_previous = c.pop()
                except IndexError:
                    raise SGFError('Game tree parse error')
                last = ')'

            if sgf[i] == ';':
                if p != -1:
                    curr.SGFstring = sgf[p:i]
                    nn = Node()
                    nn.previous = curr
                    width_currentVar += 1
                    if width_currentVar > self.width: self.width = width_currentVar
                    nn.posyD = 0
                    curr.next = nn
                    curr.numChildren = 1
                    curr = nn
                p = i

            i = i + 1

        if inbrackets or c:
            raise SGFError('Game tree parse error')

        n = curr.next
        n.previous = None
        n.up = None

        while n.down:
            n = n.down
            n.previous = None


    def game(self, n):
        if n < self.root.numChildren:
            self.posx = 0
            self.posy = 0
            self.currentN = self.root.next
            for i in range(n): self.currentN = self.currentN.down
            self.setFlags()
        else:
            raise SGFError('Game not found')


    def delVariation(self, c):

        if c.previous:
            self.delVar(c)
        else:
            if c.next:
                node = c.next
                while node.down:
                    node = node.down
                    self.delVar(node.up)

                self.delVar(node)

            c.next = None

        self.setFlags()


    def delVar(self, node):
        if node.up: node.up.down = node.down
        else: node.previous.next = node.down

        if node.down:
            node.down.up = node.up
            node.down.posyD = node.posyD
            n = node.down
            while n:
                n.level -= 1
                n = n.down

        h = 0
        n = node
        while n.next:
            n = n.next
            while n.down:
                n = n.down
                h += n.posyD

        if node.up or node.down: h += 1

        p = node.previous
        p.numChildren -= 1

        while p:
            if p.down: p.down.posyD -= h
            p = p.previous


        self.height -= h




    def add(self, st):
        node = Node(self.currentN,st,0)

        node.down = None
        node.next = None
        node.numChildren = 0

        if not self.currentN.next:
            node.level = 0
            node.posyD = 0
            node.up = 0

            self.currentN.next = node
            self.currentN.numChildren = 1
        else:
            n = self.currentN.next
            while n.down:
                n = n.down
                self.posy += n.posyD


            n.down = node
            node.up = n
            node.level = n.level + 1
            node.next = None
            self.currentN.numChildren += 1

            node.posyD = 1
            while n.next:
                n = n.next
                while n.down:
                    n = n.down
                    node.posyD += n.posyD

            self.posy += node.posyD

            self.height += 1

            n = node
            while n.previous:
                n = n.previous
                if n.down: n.down.posyD += 1

        self.currentN = node

        self.posx += 1
        self.setFlags()

        if self.posx > self.width: self.width += 1







    def next(self, n=0):
        if n >= self.noChildren():
            raise SGFError('Variation not found')

        self.posx += 1

        self.currentN = self.currentN.next
        for i in range(n):
            self.currentN = self.currentN.down
            self.posy += self.currentN.posyD
        self.setFlags()
        return self.currentNode()

    def previous(self):
        if self.currentN.previous:
            while self.currentN.up:
                self.posy -= self.currentN.posyD
                self.currentN = self.currentN.up
            self.currentN = self.currentN.previous
            self.posx -= 1
        else: raise SGFError('No previous node')
        self.setFlags()
        return self.currentNode()

    def getRootNode(self, n):
        if not self.root: return
        if n >= self.root.numChildren: raise SGFError('Game not found')

        nn = self.root.next
        for i in range(n): nn = nn.down

        if not nn.parsed: nn.parseNode()

        return nn.data


    def updateCurrentNode(self):
        """ Put the data in self.currentNode into the corresponding string in self.collection.
        This will be called from an application which may have modified self.currentNode."""

        self.currentN.SGFstring = self.nodeToString(self.currentN.data)




    def updateRootNode(self, data, n=0):
        if n >= self.root.numChildren:
            raise SGFError('Game not found')

        nn = self.root.next
        for i in range(n): nn = nn.down

        nn.SGFstring = self.rootNodeToString(data)
        nn.parsed = 0
        nn.parseNode()


    def rootNodeToString(self, node):

        result = [';']
        keylist = ['GM', 'FF', 'SZ', 'PW', 'WR', 'PB', 'BR',
                   'EV', 'RO', 'DT', 'PC', 'KM', 'RE', 'US', 'GC']
        for key in keylist:
            if key in node:
                result.append(key)
                result.append('[' + SGFescape(node[key][0]) + ']\n')

        l = 0
        for key in node.keys():
            if not key in keylist:
                result.append(key)
                l += len(key)
                for item in node[key]:
                    result.append('[' + SGFescape(item) + ']\n')
                    l += len(item) + 2
                    if l > 72:
                        result.append('\n')
                        l = 0

        return ''.join(result)

    def nodeToString(self, node):
        l = 0
        result = [';']
        for k in node.keys():
            if l + len(k) > 72:
                result.append('\n')
                l = 0
            if not node[k]: continue
            result.append(k)
            l += len(k)
            for item in node[k]:
                if l + len(item) > 72:
                    result.append('\n')
                    l = 0
                l += len(item) + 2
                result.append('[' + SGFescape(item) + ']')

        return ''.join(result)


    def outputVar(self, node):

        result = []

        result.append(node.SGFstring)

        while node.next:
            node = node.next

            if node.down:
                while node.down:
                    result.append('(' + self.outputVar(node) + ')' )
                    node = node.down

                result.append('(' + self.outputVar(node) + ')' )
                return ''.join(result)

            else:
                result.append(node.SGFstring)

        return ''.join(result)



    def output(self):
        result = []

        n = self.root.next

        while n:
            result.append('(' + self.outputVar(n)+ ')\n')
            n = n.down

        return ''.join(result)