Make pretty error messages for LexerError

This commit is contained in:
Sascha Brawer 2015-08-01 10:24:44 +02:00
parent efbcba79a4
commit f4ed6b5a85
2 changed files with 18 additions and 0 deletions

View File

@ -9,6 +9,14 @@ class LexerError(Exception):
Exception.__init__(self, message)
self.location = location
def __str__(self):
message = Exception.__str__(self)
if self.location:
path, line, column = self.location
return "%s:%d:%d: %s" % (path, line, column, message)
else:
return message
class Lexer(object):
NUMBER = "NUMBER"

View File

@ -9,6 +9,16 @@ def lex(s):
return [(typ, tok) for (typ, tok, _) in Lexer(s, "test.fea")]
class LexerErrorTest(unittest.TestCase):
def test_str(self):
err = LexerError("Squeak!", ("foo.fea", 23, 42))
self.assertEqual(str(err), "foo.fea:23:42: Squeak!")
def test_str_nolocation(self):
err = LexerError("Squeak!", None)
self.assertEqual(str(err), "Squeak!")
class LexerTest(unittest.TestCase):
def test_empty(self):
self.assertEqual(lex(""), [])