diff --git a/src/sgfParser/collection.py b/src/sgfParser/collection.py --- a/src/sgfParser/collection.py +++ b/src/sgfParser/collection.py @@ -1,17 +1,19 @@ from sgfParser.node import Node -from . import skipWhitespace +from . import skipWhitespace, ParserError class Collection: def __init__(self,s): self.gameTrees=[] - i,x=GameTree.create(s,0) - if x is None: - print("error when parsing Collection") - return - while x is not None: + i=skipWhitespace(s,0) + if i>=len(s): return + elif not GameTree.fits(s,i): + raise ParserError("expected a GameTree starting with '('",s,i) + while GameTree.fits(s,i): + i,x=GameTree.create(s,i) self.gameTrees.append(x) - i,x=GameTree.create(s,i) + if i=len(s) or s[i]!="(": - # print("error when parsing GameTree") - return (start,None) - i,x=Node.create(s,i+1) - if x is None: - # print("error when parsing GameTree") - return (i,None) + + i=skipWhitespace(s,start+1) + if not Node.fits(s,i): + raise ParserError("expected a Node starting with ';'",s,i) + y=None - while x is not None: + while Node.fits(s,i): + i,x=Node.create(s,i) res.nodes.append(x) if y: y.addChild(x) x.setParent(y) y=x i=skipWhitespace(s,i) - i,x=Node.create(s, i) - i=skipWhitespace(s,i) - i,x=GameTree.create(s,i) - while x is not None: + + while GameTree.fits(s,i): + i,x=GameTree.create(s,i) res.branches.append(x) subroot=x.getNode(0) - if subroot: - subroot.setParent(y) + subroot.setParent(y) if y: y.addChild(subroot) i=skipWhitespace(s,i) - i,x=GameTree.create(s,i) - if s[i]!=")": - # print("error when parsing GameTree") - return (i,None) - return (i+1,res) + if i>=len(s) or s[i]!=")": + raise ParserError("expected end of the GameTree marked by ')'",s,i) + i=skipWhitespace(s,i+1) + return (i,res) ## Expand multiple games into distinct GameTrees and yield each. def listGames(self):