def testDotDot(self): tree = "(PRINT (MULT ID[x] (VEC (MULT INT[9] INT[1]) INT[2] INT[3])))" adaptor = CommonTreeAdaptor() wiz = TreeWizard(adaptor, self.tokenNames) t = wiz.create(tree) labels = {} valid = wiz.parse(t, "(PRINT (MULT ID (VEC (MULT INT %x:INT) INT INT)))", labels) self.assertTrue(valid) node = labels.get("x") try: TreeParser._inContext(adaptor, self.tokenNames, node, "PRINT .. VEC") self.fail() except ValueError, exc: expecting = "invalid syntax: .." found = str(exc) self.assertEquals(expecting, found)
def testNotParent(self): tree = "(PRINT (MULT ID[x] (VEC (MULT INT[9] INT[1]) INT[2] INT[3])))" adaptor = CommonTreeAdaptor() wiz = TreeWizard(adaptor, self.tokenNames) t = wiz.create(tree) labels = {} valid = wiz.parse(t, "(PRINT (MULT ID (VEC (MULT INT %x:INT) INT INT)))", labels) self.assertTrue(valid) node = labels.get("x") expecting = False found = TreeParser._inContext(adaptor, self.tokenNames, node, "VEC") self.assertEquals(expecting, found)
def testLotsOfWildcards(self): tree = "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID[x] (VEC INT[1] INT[2] INT[3]))))" adaptor = CommonTreeAdaptor() wiz = TreeWizard(adaptor, self.tokenNames) t = wiz.create(tree) labels = {} valid = wiz.parse(t, "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID (VEC INT %x:INT INT))))", labels) self.assertTrue(valid) node = labels.get("x") expecting = True found = TreeParser._inContext(adaptor, self.tokenNames, node, "... PRINT ... VEC ...") self.assertEquals(expecting, found)
def testNoParent(self): tree = "(PRINT (MULT ID[x] (VEC INT[1] INT[2] INT[3])))" adaptor = CommonTreeAdaptor() wiz = TreeWizard(adaptor, self.tokenNames) t = wiz.create(tree) labels = {} valid = wiz.parse(t, "(%x:PRINT (MULT ID (VEC INT INT INT)))", labels) self.assertTrue(valid) node = labels.get("x") expecting = False found = TreeParser._inContext(adaptor, self.tokenNames, node, "VEC") self.assertEqual(expecting, found)
def testDeepAndFindRoot(self): tree = "(PRINT (MULT ID[x] (VEC (MULT INT[9] INT[1]) INT[2] INT[3])))" adaptor = CommonTreeAdaptor() wiz = TreeWizard(adaptor, self.tokenNames) t = wiz.create(tree) labels = {} valid = wiz.parse(t, "(PRINT (MULT ID (VEC (MULT INT %x:INT) INT INT)))", labels) self.assertTrue(valid) node = labels.get("x") expecting = True found = TreeParser._inContext(adaptor, self.tokenNames, node, "PRINT ...") self.assertEqual(expecting, found)
def testWildcardAtStartIgnored(self): tree = "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID[x] (VEC INT[1] INT[2] INT[3]))))" adaptor = CommonTreeAdaptor() wiz = TreeWizard(adaptor, self.tokenNames) t = wiz.create(tree) labels = {} valid = wiz.parse( t, "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID (VEC INT %x:INT INT))))", labels) self.assertTrue(valid) node = labels.get("x") expecting = True found = TreeParser._inContext(adaptor, self.tokenNames, node, "...VEC") self.assertEqual(expecting, found)
def testMismatch(self): tree = "(PRINT (MULT ID[x] (VEC (MULT INT[9] INT[1]) INT[2] INT[3])))" adaptor = CommonTreeAdaptor() wiz = TreeWizard(adaptor, self.tokenNames) t = wiz.create(tree) labels = {} valid = wiz.parse(t, "(PRINT (MULT ID (VEC (MULT INT %x:INT) INT INT)))", labels) self.assertTrue(valid) node = labels.get("x") expecting = False ## missing MULT found = TreeParser._inContext(adaptor, self.tokenNames, node, "PRINT VEC MULT") self.assertEquals(expecting, found)