Files
@ 70d01e8dc839
Branch filter:
Location: Diana/src/sgfParser/propValues.py
70d01e8dc839
3.7 KiB
text/x-python
a little refactoring and optimization of parser's regular expressions
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 | import re
from . import ParserError, skipWhitespace
class Regexp:
number=re.compile(r"(\+|-|)\d+")
real=re.compile(r"(\+|-|)\d+(\.\d+)?")
point=re.compile(r"[a-zA-Z]{2}|")
class Composed:
def __init__(self,a=None,b=None):
self.a=a
self.b=b
def __str__(self):
return "{0}:{1}".format(self.a,self.b)
class Point:
def __init__(self,c,r):
self.r=r
self.c=c
def __iter__(self):
yield self.c
yield self.r
def __str__(self):
a=ord("a")
return chr(a+self.c)+chr(a+self.r)
## Metatype matching one of the provided types.
#
# Returns the first match, so the order is important.
def choose(*vTypes):
def f(s,start):
for vType in vTypes:
try:
i,x=vType(s,start)
return (i,x)
except ParserError: pass
raise ParserError("no variant of a 'choose' property value matched",s,start)
return f
def singletonFits(s,i):
return i<len(s) and s[i]=="["
def singletonEnds(s,i):
return i<len(s) and s[i]=="]"
def singleton(vType):
def f(s,start):
if not singletonFits(s,start):
raise ParserError("expected a property value starting with '['",s,start)
i,x=vType(s,start+1)
if not singletonEnds(s,i):
raise ParserError("expected a property value ending with ']'",s,i)
i=skipWhitespace(s,i+1)
return (i,x)
return f
def listOf(vType,allowEmpty=False):
def f(s,start):
i=start
if not singletonFits(s,i):
raise ParserError("expected a property value starting with '['",s,i)
if singletonEnds(s,i+1) and allowEmpty:
i=skipWhitespace(s,i+2)
return (i,[])
single=singleton(vType)
i,x=single(s,i)
res=[x]
while singletonFits(s,i):
i,x=single(s,i)
res.append(x)
return (i,res)
return f
def compose(vTypeA,vTypeB):
def f(s,start):
i,a=vTypeA(s,start)
if i>=len(s) or s[i]!=":":
raise ParserError("expected a composed property value separated by ':'",s,i)
i,b=vTypeB(s,i+1)
return (i,Composed(a,b))
return f
def number(s,start):
m=Regexp.number.match(s,start)
if m is None: raise ParserError("expected a number matching '{0}'".format(Regexp.number.pattern),s,start)
res=int(m.group(0))
return (m.end(),res)
def real(s,start):
m=Regexp.real.match(s,start)
if m is None: raise ParserError("expected a real number matching '{0}'".format(Regexp.real.pattern),s,start)
res=float(m.group(0))
return (m.end(),res)
def double(s,start):
c=s[start]
if c not in ("1", "2"):
raise ParserError("expected a double value, either '1' or '2'",s,start)
return (start+1,c)
def color(s,start):
c=s[start]
if c not in ("B", "W"):
raise ParserError("expected a color value, either 'B' or 'W'",s,start)
return (start+1,c)
def text(simple=True,composed=False):
def f(s,start):
res=""
esc=False
lastC=""
i=start
for i,c in enumerate(s[start:],start):
if esc:
if c!="\n" and c!="\r": res+=c
esc=False
elif (c=="\n" and lastC=="\r") or (c=="\r" and lastC=="\n"): pass
elif c=="\r" or c=="\n" and not simple:
res+="\n"
elif c.isspace():
res+=" "
elif c=="\\":
esc=True
elif c=="]" or (c==":" and composed):
break
else:
res+=c
lastC=c
return (i,res)
return f
def empty(s,start): return (start,"")
def anything(s,start):
esc=False
for i,c in enumerate(s[start:],start):
if esc: esc=False
elif c=="\\": esc=True
elif c=="]": break
return (i,s[start:i])
# go specific
def point(s,start):
m=Regexp.point.match(s,start) # !! limit to board size
if m is None: raise ParserError("expected a point value matching '{0}'".format(Regexp.point.pattern),s,start)
if m.group(0)=="": # pass, !! tt
return (m.end(),tuple())
col=m.group(0)[0]
row=m.group(0)[1]
col=ord(col)-(ord("a") if "a"<=col<="z" else ord("A")-26)
row=ord(row)-(ord("a") if "a"<=row<="z" else ord("A")-26)
return (m.end(),Point(col,row))
move=point
stone=point
|