# HG changeset patch # User Laman # Date 2016-09-18 15:57:37 # Node ID fd76ef287e30281b99efd89e7f5614c94e08cc1a # Parent a81abb4e09681606f04627d18db97a22ae95b810 sgfParser s/str/s diff --git a/src/diana/__init__.py b/src/diana/__init__.py old mode 100644 new mode 100755 diff --git a/src/diana/sgfParser.py b/src/diana/sgfParser.py --- a/src/diana/sgfParser.py +++ b/src/diana/sgfParser.py @@ -1,66 +1,66 @@ import re -def skipWhitespace(str,start): +def skipWhitespace(s,start): i=start - while i=len(str) or str[i]!="(": + i=skipWhitespace(s,start) + if i>=len(s) or s[i]!="(": # print("error when parsing GameTree") return (start,None) - i,x=Node.create(str,start+1) + i,x=Node.create(s,start+1) if x is None: # print("error when parsing GameTree") return (i,None) while x is not None: res.nodes.append(x) - i=skipWhitespace(str,i) - i,x=Node.create(str,i) - i=skipWhitespace(str,i) - i,x=GameTree.create(str,i) + 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: res.branches.append(x) - i=skipWhitespace(str,i) - i,x=GameTree.create(str,i) - if str[i]!=")": + 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) @@ -70,21 +70,21 @@ class Node: self.properties=dict() @staticmethod - def create(str,start): + def create(s,start): res=Node() - if str[start]!=";": + if s[start]!=";": # print("error when parsing Node") return (start,None) - i=skipWhitespace(str,start+1) - i,x=Property.create(str,start+1) + i=skipWhitespace(s,start+1) + i,x=Property.create(s,start+1) while x is not None: if x.name in res.properties: print(res.properties) raise ParserError(0,0,'duplicate "{0}" property in node at position {1}. second value ignored'.format(x.name,start)) else: res.properties[x.name]=x - i=skipWhitespace(str,i) - i,x=Property.create(str,i) + i=skipWhitespace(s,i) + i,x=Property.create(s,i) return (i,res) def setProperty(self,name,value): @@ -101,26 +101,26 @@ class Property: self.value="" @staticmethod - def create(str,start): + def create(s,start): res=Property() - i,x=Property.ident(str,start) + i,x=Property.ident(s,start) if x is None: return (start,None) res.name=x - i,x=PropValue.create(str,i,res.name) + 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) # while x is not None: # přesunuto do PropValue.listOf # res.values.append(x) - # i=skipWhitespace(str,i) - # i,x=PropValue.create(str,i,res.name) + # i=skipWhitespace(s,i) + # i,x=PropValue.create(s,i,res.name) return (i,res) @staticmethod - def ident(str,start): + def ident(s,start): r=re.compile(r"[A-Z]+") - m=r.match(str,start) + m=r.match(s,start) if m is None: return (start,None) return (m.end(),m.group()) @@ -132,113 +132,113 @@ class PropValue: patterns=dict() @staticmethod - def create(str,start,name): + def create(s,start,name): if name in PropValue.patterns: - return PropValue.patterns[name](str,start) + return PropValue.patterns[name](s,start) else: print('warning, unknown property "{0}" at position {1}'.format(name,start)) - return PropValue.singleton(PropValue.anything)(str,start) + return PropValue.singleton(PropValue.anything)(s,start) - # def singleton(str,start,vType): - # if str[start]!="[": + # def singleton(s,start,vType): + # if s[start]!="[": # return (start,None) - # i,x=vType(str,start+1) + # i,x=vType(s,start+1) # if x is None: return (start,None) - # if str[i]!="]": + # if s[i]!="]": # return (start,None) # return (i+1,x) def choose(*vTypes): - def f(str,start): + def f(s,start): for vType in vTypes: - i,x=vType(str,start) + i,x=vType(s,start) if x is not None: return (i,x) return (start,None) return f def singleton(vType): - def f(str,start): - if str[start]!="[": + def f(s,start): + if s[start]!="[": return (start,None) - i,x=vType(str,start+1) + i,x=vType(s,start+1) if x is None: return (start,None) - if str[i]!="]": + if s[i]!="]": return (start,None) return (i+1,x) return f - # def listOf(str,start,vType,allowEmpty=False): + # def listOf(s,start,vType,allowEmpty=False): # res=[] - # i,x=singleton(str,start,vType) + # i,x=singleton(s,start,vType) # # singleton(vType) if vType not tuple else compose(vType[0],vType[1]) # while x!=None: # res.append(x) - # i,x=singleton(str,i,vType) + # i,x=singleton(s,i,vType) # if len(res)==0 and not allowEmpty: return (start,None) # return (i,res) def listOf(vType,allowEmpty=False): - def f(str,start): + def f(s,start): res=[] single=PropValue.singleton(vType) - i,x=single(str,start) + i,x=single(s,start) while x!=None: res.append(x) - i,x=single(str,i) + i,x=single(s,i) if len(res)==0 and not allowEmpty: return (start,None) return (i,res) return f - # def compose(str,start,vTypeA,vTypeB): - # i,a=vTypeA(str,start) - # if a==None or str[i]!=":": return (start,None) - # i,b=vTypeB(str,i+1) + # def compose(s,start,vTypeA,vTypeB): + # i,a=vTypeA(s,start) + # if a==None or s[i]!=":": return (start,None) + # i,b=vTypeB(s,i+1) # if b==None: return start,None # return (i,(a,b)) def compose(vTypeA,vTypeB): - def f(str,start): - i,a=vTypeA(str,start) + def f(s,start): + i,a=vTypeA(s,start) # print(">",i,a) - if a==None or str[i]!=":": return (start,None) - i,b=vTypeB(str,i+1) + if a==None or s[i]!=":": return (start,None) + i,b=vTypeB(s,i+1) # print(">",i,b) if b==None: return start,None return (i,(a,b)) return f - def number(str,start): + def number(s,start): r=re.compile(r"(\+|-|)\d+") - m=r.match(str,start) + m=r.match(s,start) if m is None: return (start,None) res=int(m.group(0)) return (m.end(),res) - def real(str,start): + def real(s,start): r=re.compile(r"(\+|-|)\d+(\.\d+)?") - m=r.match(str,start) + m=r.match(s,start) if m is None: return (start,None) res=float(m.group(0)) return (m.end(),res) - def double(str,start): + def double(s,start): r=re.compile(r"1|2") - m=r.match(str,start) + m=r.match(s,start) if m is None: return (start,None) res=int(m.group(0)) return (m.end(),res) - def color(str,start): + def color(s,start): r=re.compile(r"B|W") - m=r.match(str,start) + m=r.match(s,start) if m is None: return (start,None) return (m.end(),m.group(0)) - # def simpleText(str,start): + # def simpleText(s,start): # res="" # esc=False # lineBreak=False - # for c in str: + # for c in s: # if esc: # res+=c # esc=False @@ -251,11 +251,11 @@ class PropValue: # return res def text(simple=True,composed=False): - def f(str,start): + def f(s,start): res="" esc=False lastC="" - for i,c in enumerate(str[start:],start): + for i,c in enumerate(s[start:],start): if esc: if c!="\n" and c!="\r": res+=c esc=False @@ -274,20 +274,20 @@ class PropValue: return (i,res) return f - def empty(str,start): return (start,"") + def empty(s,start): return (start,"") - def anything(str,start): + def anything(s,start): esc=False - for i,c in enumerate(str[start:],start): + for i,c in enumerate(s[start:],start): if esc: esc=False elif c=="\\": esc=True elif c=="]": break - return (i,str[start:i]) + return (i,s[start:i]) # go specific - def point(str,start): + def point(s,start): r=re.compile(r"[a-zA-Z]{2}|") # !! limit to board size - m=r.match(str,start) + m=r.match(s,start) if m is None: return (start,None) if m.group(0)=="": # pass, !! tt return (m.end(),tuple()) @@ -380,62 +380,62 @@ class ParserError(Exception): -"""def property(str): - # i=propIdent(str) +"""def property(s): + # i=propIdent(s) # if i<0: return -1 # j=i - # i=propValue(str[i:]) + # i=propValue(s[i:]) # while i>=0: # j+=i - # i=propValue(str[i:]) + # i=propValue(s[i:]) # return j -def propIdent(str): - m=re.match(r"[A-Z]+",str) +def propIdent(s): + m=re.match(r"[A-Z]+",s) if m is None: return -1 return m.end() -def propValue(str): - i=cValueType(str[1:]) - if str[0]=="[" and i>=0 and str[i]=="]": return i+1 +def propValue(s): + i=cValueType(s[1:]) + if s[0]=="[" and i>=0 and s[i]=="]": return i+1 else: return -1 class propValue: pass -def cValueType(str,start): +def cValueType(s,start): matches=[real,number,double,color,simpleText] for f in matches: - i,x=f(str,start) + i,x=f(s,start) if x is not None: return i,x return 1/0 -def number(str,start): +def number(s,start): r=re.compile(r"(\+|-|)\d+") - m=r.match(str,start) + m=r.match(s,start) if m is None: return (-1,None) x=int(m.group(0)) return (m.end(),x) -def real(str): - m=re.match(r"(\+|-|)\d+(\.\d+)?",str) +def real(s): + m=re.match(r"(\+|-|)\d+(\.\d+)?",s) if m is None: return -1 return m.end() -def double(str): - m=re.match(r"1|2",str) +def double(s): + m=re.match(r"1|2",s) if m is None: return -1 return m.end() -def color(str): - m=re.match(r"B|W",str) +def color(s): + m=re.match(r"B|W",s) if m is None: return -1 return m.end() -def simpleText(str): +def simpleText(s): res=r"" esc=False - for c in str: + for c in s: if esc: res+=c esc=False diff --git a/src/diana/tests/__init__.py b/src/diana/tests/__init__.py old mode 100644 new mode 100755 diff --git a/src/diana/tests/data/kogos.sgf b/src/diana/tests/data/kogos.sgf old mode 100644 new mode 100755 diff --git a/src/diana/tests/sgfParser.py b/src/diana/tests/sgfParser.py old mode 100644 new mode 100755