diff --git a/src/sgfParser/property.py b/src/sgfParser/property.py --- a/src/sgfParser/property.py +++ b/src/sgfParser/property.py @@ -5,39 +5,6 @@ GAME_INFO=1 UNKNOWN=99 -class Property: - def __init__(self): - self.name="" - self.value="" - - @staticmethod - def create(s,start): - res=Property() - i,x=Property.ident(s,start) - if x is None: - return (start,None) - res.name=x - i,x=PropValue.create(s,i,res.name) - if x is None: - print('error when parsing property "{0}" at position {1}'.format(res.name,i)) - return (start,None) - res.value=x - return (i,res) - - @staticmethod - def ident(s,start): - r=re.compile(r"[A-Z]+") - m=r.match(s,start) - if m is None: return (start,None) - return (m.end(),m.group()) - - @property - def type(self): - gameInfo={"AN","BR","BT","CP","DT","EV","GN","GC","ON","OT","PB","PC","PW","RE","RO","RU","SO","TM","US","WR","WT"} - if self.name in gameInfo: return GAME_INFO - else: return UNKNOWN - - def choose(*vTypes): def f(s,start): for vType in vTypes: @@ -149,6 +116,7 @@ def anything(s,start): elif c=="]": break return (i,s[start:i]) + # go specific def point(s,start): r=re.compile(r"[a-zA-Z]{2}|") # !! limit to board size @@ -166,19 +134,53 @@ move=point stone=point -class PropValue: +class Property: def __init__(self): - self.type="" - self.value=None + self.name="" + self.value="" @staticmethod - def create(s,start,name): - if name in PropValue.patterns: - return PropValue.patterns[name](s,start) + def create(s,start): + res=Property() + i,x=Property.ident(s,start) + if x is None: + return (start,None) + res.name=x + i,x=Property.createValue(s,i,res.name) + if x is None: + print('error when parsing property "{0}" at position {1}'.format(res.name,i)) + return (start,None) + res.value=x + return (i,res) + + @staticmethod + def ident(s,start): + r=re.compile(r"[A-Z]+") + m=r.match(s,start) + if m is None: return (start,None) + return (m.end(),m.group()) + + @staticmethod + def createValue(s,start,name): + if name in Property.patterns: + return Property.patterns[name](s,start) else: print('warning, unknown property "{0}" at position {1}'.format(name,start)) return singleton(anything)(s,start) + @property + def type(self): + gameInfo={"AN","BR","BT","CP","DT","EV","GN","GC","ON","OT","PB","PC","PW","RE","RO","RU","SO","TM","US","WR","WT"} + if self.name in gameInfo: return GAME_INFO + else: return UNKNOWN + + def copy(self): + res=Property() + res.name=self.name + # !! wouldn't work for listOf(listOf()) + res.value=self.value if not isinstance(self.value,list) else self.value[:] + return res + patterns={ "B":singleton(move), "KO":singleton(empty), @@ -252,5 +254,4 @@ class PropValue: } -# TODO: -# date +# !! TODO: date