Make pretty error messages for LexerError
This commit is contained in:
parent
efbcba79a4
commit
f4ed6b5a85
@ -9,6 +9,14 @@ class LexerError(Exception):
|
|||||||
Exception.__init__(self, message)
|
Exception.__init__(self, message)
|
||||||
self.location = location
|
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):
|
class Lexer(object):
|
||||||
NUMBER = "NUMBER"
|
NUMBER = "NUMBER"
|
||||||
|
@ -9,6 +9,16 @@ def lex(s):
|
|||||||
return [(typ, tok) for (typ, tok, _) in Lexer(s, "test.fea")]
|
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):
|
class LexerTest(unittest.TestCase):
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
self.assertEqual(lex(""), [])
|
self.assertEqual(lex(""), [])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user