[feaLib.ast] add __str__ to all AST elements

which calls the asFea() method,
so one can e.g. print(doc.statements[0])
This commit is contained in:
Cosimo Lupo 2017-11-16 15:40:52 +00:00
parent 0ff6be5ff8
commit fdab63f0b6
2 changed files with 15 additions and 16 deletions

View File

@ -47,7 +47,8 @@ def asFea(g):
return g return g
class Statement(object): class Element(object):
def __init__(self, location): def __init__(self, location):
self.location = location self.location = location
@ -55,28 +56,25 @@ class Statement(object):
pass pass
def asFea(self, indent=""): def asFea(self, indent=""):
pass raise NotImplementedError
def __str__(self):
return self.asFea()
class Expression(object): class Statement(Element):
def __init__(self, location): pass
self.location = location
def build(self, builder):
pass
def asFea(self, indent=""):
pass
class Comment(object): class Expression(Element):
pass
class Comment(Element):
def __init__(self, location, text): def __init__(self, location, text):
self.location = location super(Comment, self).__init__(location)
self.text = text self.text = text
def build(self, builder):
pass
def asFea(self, indent=""): def asFea(self, indent=""):
return self.text return self.text

View File

@ -81,6 +81,7 @@ class ParserTest(unittest.TestCase):
c2 = doc.statements[1].statements[1] c2 = doc.statements[1].statements[1]
self.assertEqual(type(c1), ast.Comment) self.assertEqual(type(c1), ast.Comment)
self.assertEqual(c1.text, "# Initial") self.assertEqual(c1.text, "# Initial")
self.assertEqual(str(c1), "# Initial")
self.assertEqual(type(c2), ast.Comment) self.assertEqual(type(c2), ast.Comment)
self.assertEqual(c2.text, "# simple") self.assertEqual(c2.text, "# simple")
self.assertEqual(doc.statements[1].name, "test") self.assertEqual(doc.statements[1].name, "test")