Changeset - c6e20613189d
[Not reviewed]
default
0 4 0
Laman - 8 years ago 2017-01-29 18:39:34

added location of the ParserError in the parsed file
4 files changed with 33 insertions and 14 deletions:
0 comments (0 inline, 0 general)
src/sgfParser/__init__.py
Show inline comments
 
@@ -4,23 +4,25 @@ def skipWhitespace(s,start):
 
	return i
 

	
 

	
 
def lineNumber(s,i):
 
def strRowCol(s, i):
 
	k=0
 
	r=0
 
	r,c=0,0
 
	for (r,line) in enumerate(s.splitlines(True)):
 
		k+=len(line)
 
		if k>=i: break
 
	return r+1
 
		c=i-k
 
		if k+len(line)>i:
 
			break
 
		else:
 
			k+=len(line)
 
	return (r+1, c+1)
 

	
 

	
 
class ParserError(Exception):
 
	def __init__(self,message,s,i):
 
		# self.line=line
 
		# self.col=col
 
		# !! check for i<len(s)
 
		print(message)
 
		print(s[i:])
 
		self.message=message
 
	def __init__(self,msg,s,i):
 
		self.msg=msg
 
		self.row,self.col=strRowCol(s, i)
 

	
 
	def __str__(self):
 
		return "{0} at row {1}, col {2}".format(self.msg,self.row,self.col)
 

	
 

	
 
class ParserWarning(ParserError):
src/sgfParser/collection.py
Show inline comments
 
@@ -55,7 +55,7 @@ class GameTree:
 
			if y: y.addChild(subroot)
 
			i=skipWhitespace(s,i)
 
		if i>=len(s) or s[i]!=")":
 
			raise ParserError("expected end of the GameTree marked by ')'",s,i)
 
			raise ParserError("expected end of a GameTree marked by ')'",s,i)
 
		i=skipWhitespace(s,i+1)
 
		return (i,res)
 
src/sgfParser/property.py
Show inline comments
 
@@ -85,7 +85,7 @@ def compose(vTypeA,vTypeB):
 
	def f(s,start):
 
		i,a=vTypeA(s,start)
 
		if i>=len(s) or s[i]!=":":
 
			raise ParserError("a composed property value separated by ':' expected",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
src/tests/testSgfParser.py
Show inline comments
 
from itertools import chain
 
import unittest
 
from unittest import TestCase
 
import os
 

	
 
from sgfParser import strRowCol
 
from sgfParser.collection import Collection
 
from sgfParser.property import Property
 

	
 
@@ -9,6 +11,21 @@ from sgfParser.property import Property
 
dataDir=os.path.join(os.path.dirname(__file__), "data")
 

	
 

	
 
class TestUtils(TestCase):
 
	def testTextPos(self):
 
		s="abc\ndef\rgh\r\nij\n\rklmn"
 
		rc=[
 
			[1,2,3,4],
 
			[1,2,3,4],
 
			[1,2,3,4],
 
			[1,2,3], [1], # don't care about LFCR, we unicode now
 
			[1,2,3,4]
 
		]
 
		res=chain((r+1,c) for (r,row) in enumerate(rc) for c in row)
 
		for (i,(r,c)) in zip(range(len(s)+1), res):
 
			self.assertEqual(strRowCol(s, i), (r, c))
 

	
 

	
 
class TestProperty(TestCase):
 
	def testName(self):
 
		with self.assertRaises(AssertionError):
0 comments (0 inline, 0 general)