Changeset - a81abb4e0968
[Not reviewed]
default
0 3 1
Laman - 9 years ago 2016-09-10 12:45:35

fixed sgfParser, added complex sgf test
2 files changed:
Changeset was too big and was cut off... Show full diff anyway
0 comments (0 inline, 0 general)
src/diana/diana.py
Show inline comments
 
import os
 
import re
 
import sys
 
 
import diana.sgf as sgf
 
import diana.go as go
 
import sgf
 
import go
 
 
if len(sys.argv)>1:
 
	files=sys.argv[1:]
 
else:
 
	sys.exit("no input file specified")
 
 
movesPerDiagram=75
 
minMovesPerDiagram=10
 
 
t=sys.stdout
 
c=28
 
padding=15
 
highNumbers=True
 
 
class Svg:
 
	content=""
 
	footer=""
 
	extension="svg"
 
 
	padding=15
 
	gridSize=28
 
	highNumbers=True
 
	
 
	def __init__(self):
src/diana/sgfParser.py
Show inline comments
 
import re
 
 
def skipWhitespace(str,start):
 
	i=start
 
	while i<len(str) and str[i].isspace(): i+=1
 
	return i
 
 
class Collection:
 
	gameTrees=[]
 
	
 
	def __init__(self,str):
 
		self.gameTrees=[]
 
		i,x=GameTree.create(str,0)
 
		if x is None:
 
			print("error when parsing Collection")
 
			return
 
		while x is not None:
 
			self.gameTrees.append(x)
 
			i,x=GameTree.create(str,i)
 
	
 
class GameTree:
 
	nodes=[]
 
	branches=[]
 
	def __init__(self):
 
		self.nodes=[]
 
		self.branches=[]
 
	
 
	# def __init__(self,str,start):
 
		# self.nodes=[]
 
		# self.branches=[]
 
		# if str[start]!="(":
 
			# print("error when parsing GameTree")
 
			# return (-1,None)
 
		# i,x=Node(str,start+1)
 
		# if x is None:
 
			# print("error when parsing GameTree")
 
			# return (-1,None)
 
		# while x is not None:
 
			# self.nodes.append(x)
 
			# i,x=Node(str,i)
 
		# if str[i]!=")":
 
			# print("error when parsing GameTree")
 
			# return (-1,None)
 
		# return (i+1,self)
 
		
 
	@staticmethod
 
	def create(str,start):
 
		res=GameTree()
 
		i=skipWhitespace(str,start)
 
		if i>=len(str) or str[i]!="(":
 
			# print("error when parsing GameTree")
 
			return (start,None)
 
		i,x=Node.create(str,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)
 
		while x is not None:
 
			res.branches.append(x)
 
			i=skipWhitespace(str,i)
 
			i,x=GameTree.create(str,i)
 
		if str[i]!=")":
 
			# print("error when parsing GameTree")
 
			return (i,None)
 
		return (i+1,res)
 
	
 
class Node:
 
	properties=dict()
 
	def __init__(self):
 
		self.properties=dict()
 
	
 
	@staticmethod
 
	def create(str,start):
 
		res=Node()
 
		if str[start]!=";":
 
			# print("error when parsing Node")
 
			return (start,None)
 
		i=skipWhitespace(str,start+1)
 
		i,x=Property.create(str,start+1)
 
		while x is not None:
 
			if x.name in res.properties:
 
				print('error: duplicate "{0}" property in node at position {1}. second value ignored'.format(x.name,start))
 
				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)
 
		return (i,res)
 
		
 
	def setProperty(self,name,value):
 
		self.properties[name]=value
 
		# zkontrolovat typ value
 
		
 
	def getProperty(self,name):
 
		if name in self.properties: return self.properties[name]
 
		else: return None
 
	
 
class Property:
 
	name=""
 
	value=""
 
	def __init__(self):
 
		self.name=""
 
		self.value=""
 
	
 
	@staticmethod
 
	def create(str,start):
 
		res=Property()
 
		i,x=Property.ident(str,start)
 
		if x is None:
 
			return (start,None)
 
		res.name=x
 
		i,x=PropValue.create(str,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)
 
		return (i,res)
 
		
 
	@staticmethod
 
	def ident(str,start):
 
		r=re.compile(r"[A-Z]+")
 
		m=r.match(str,start)
 
		if m is None: return (start,None)
 
		return (m.end(),m.group())
 
 
class PropValue:
 
	type=""
 
	value=None
 
	def __init__(self):
 
		self.type=""
 
		self.value=None
 
 
	patterns=dict()
 
	
 
	@staticmethod
 
	def create(str,start,name):
 
		if name in PropValue.patterns:
 
			return PropValue.patterns[name](str,start)
 
		else:
 
			print('warning, unknown property "{0}" at position {1}'.format(name,start))
 
			return PropValue.singleton(PropValue.anything)(str,start)
 
	
 
	# def singleton(str,start,vType):
 
		# if str[start]!="[":
 
			# return (start,None)
 
		# i,x=vType(str,start+1)
 
		# if x is None: return (start,None)
 
		# if str[i]!="]":
 
			# return (start,None)
 
		# return (i+1,x)
 
	
 
	def choose(*vTypes):
 
		def f(str,start):
 
			for vType in vTypes:
 
				i,x=vType(str,start)
 
				if x is not None: return (i,x)
 
			return (start,None)
 
		return f
 
	
 
	def singleton(vType):
 
		def f(str,start):
 
			if str[start]!="[":
 
				return (start,None)
 
			i,x=vType(str,start+1)
 
			if x is None: return (start,None)
 
			if str[i]!="]":
 
				return (start,None)
 
			return (i+1,x)
 
		return f
 
		
 
	# def listOf(str,start,vType,allowEmpty=False):
 
		# res=[]
 
		# i,x=singleton(str,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)
 
		# if len(res)==0 and not allowEmpty: return (start,None)
 
		# return (i,res)
 
		
 
	def listOf(vType,allowEmpty=False):
 
		def f(str,start):
 
			res=[]
 
			single=singleton(vType)
 
			single=PropValue.singleton(vType)
 
			i,x=single(str,start)
 
			while x!=None:
 
				res.append(x)
 
				i,x=single(str,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)
 
		# if b==None: return start,None
 
		# return (i,(a,b))
 
		
 
	def compose(vTypeA,vTypeB):
 
		def f(str,start):
 
			i,a=vTypeA(str,start)
 
			# print(">",i,a)
 
			if a==None or str[i]!=":": return (start,None)
 
			i,b=vTypeB(str,i+1)
 
			# print(">",i,b)
 
			if b==None: return start,None
 
			return (i,(a,b))
 
@@ -342,48 +351,56 @@ class PropValue:
 
		"PW":singleton(text()),
 
		"RE":singleton(text()),
 
		"RO":singleton(text()),
 
		"RU":singleton(text()),
 
		"SO":singleton(text()),
 
		"TM":singleton(real),
 
		"US":singleton(text()),
 
		"WR":singleton(text()),
 
		"WT":singleton(text()),
 
		"BL":singleton(real),
 
		"OB":singleton(number),
 
		"OW":singleton(number),
 
		"WL":singleton(real),
 
		"FG":choose(singleton(empty),singleton(compose(number,text()))), #
 
		"PM":singleton(number),
 
		"VW":listOf(point,allowEmpty=True), #
 
		
 
		# go specific
 
		"HA":singleton(number),
 
		"KM":singleton(real),
 
		"TB":listOf(point,allowEmpty=True),
 
		"TW":listOf(point,allowEmpty=True)
 
	}
 
 
class ParserError(Exception):
 
	def __init__(self,line,col,message):
 
		self.line=line
 
		self.col=col
 
		self.message=message
 
 
 
 
"""def property(str):
 
	# i=propIdent(str)
 
	# if i<0: return -1
 
	# j=i
 
	# i=propValue(str[i:])
 
	# while i>=0:
 
		# j+=i
 
		# i=propValue(str[i:])
 
	# return j
 
 
def propIdent(str):
 
	m=re.match(r"[A-Z]+",str)
 
	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
 
	else: return -1
 
 
class propValue:
 
	pass
 
 
def cValueType(str,start):

Changeset was too big and was cut off... Show full diff anyway

0 comments (0 inline, 0 general)