本文整理汇总了Python中antlr3.tree.CommonTree类的典型用法代码示例。如果您正苦于以下问题:Python CommonTree类的具体用法?Python CommonTree怎么用?Python CommonTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CommonTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testList2
def testList2(self):
# Add child ^(nil 101 102 103) to root 5
# should pull 101 102 103 directly to become 5's child list
root = CommonTree(CommonToken(5))
# child tree
r0 = CommonTree(None)
c0 = CommonTree(CommonToken(101))
r0.addChild(c0)
c1 = CommonTree(CommonToken(102))
r0.addChild(c1)
c2 = CommonTree(CommonToken(103))
r0.addChild(c2)
root.addChild(r0)
self.failUnless(root.parent is None)
self.failUnlessEqual(-1, root.childIndex)
# check children of root all point at root
self.failUnlessEqual(root, c0.parent)
self.failUnlessEqual(0, c0.childIndex)
self.failUnlessEqual(root, c0.parent)
self.failUnlessEqual(1, c1.childIndex)
self.failUnlessEqual(root, c0.parent)
self.failUnlessEqual(2, c2.childIndex)
示例2: testReplaceWithNoChildren
def testReplaceWithNoChildren(self):
t = CommonTree(CommonToken(101))
newChild = CommonTree(CommonToken(5))
error = False
try:
t.replaceChildren(0, 0, newChild)
except IndexError:
error = True
self.failUnless(error)
示例3: testReplaceInMiddle
def testReplaceInMiddle(self):
t = CommonTree(CommonToken(99, text="a"))
t.addChild(CommonTree(CommonToken(99, text="b")))
t.addChild(CommonTree(CommonToken(99, text="c"))) # index 1
t.addChild(CommonTree(CommonToken(99, text="d")))
newChild = CommonTree(CommonToken(99, text="x"))
t.replaceChildren(1, 1, newChild)
expecting = "(a b x d)"
self.failUnlessEqual(expecting, t.toStringTree())
t.sanityCheckParentAndChildIndexes()
示例4: testReplaceAtRight
def testReplaceAtRight(self):
t = CommonTree(CommonToken(99, text="a"))
t.addChild(CommonTree(CommonToken(99, text="b")))
t.addChild(CommonTree(CommonToken(99, text="c")))
t.addChild(CommonTree(CommonToken(99, text="d"))) # index 2
newChild = CommonTree(CommonToken(99, text="x"))
t.replaceChildren(2, 2, newChild)
expecting = "(a b c x)"
self.failUnlessEqual(expecting, t.toStringTree())
t.sanityCheckParentAndChildIndexes()
示例5: testReplaceAtLeft
def testReplaceAtLeft(self):
t = CommonTree(CommonToken(99, text="a"))
t.addChild(CommonTree(CommonToken(99, text="b"))) # index 0
t.addChild(CommonTree(CommonToken(99, text="c")))
t.addChild(CommonTree(CommonToken(99, text="d")))
newChild = CommonTree(CommonToken(99, text="x"))
t.replaceChildren(0, 0, newChild)
expecting = "(a x c d)"
self.assertEqual(expecting, t.toStringTree())
t.sanityCheckParentAndChildIndexes()
示例6: testBecomeRoot3
def testBecomeRoot3(self):
# ^(nil 5) becomes root of ^(nil 101 102 103)
newRoot = CommonTree(None)
newRoot.addChild(CommonTree(CommonToken(5)))
oldRoot = CommonTree(None)
oldRoot.addChild(CommonTree(CommonToken(101)))
oldRoot.addChild(CommonTree(CommonToken(102)))
oldRoot.addChild(CommonTree(CommonToken(103)))
self.adaptor.becomeRoot(newRoot, oldRoot)
newRoot.sanityCheckParentAndChildIndexes()
示例7: testAoverB
def testAoverB(self):
t = CommonTree(CommonToken(101))
t.addChild(CommonTree(CommonToken(102)))
stream = self.newStream(t)
expecting = "101 102"
found = self.toNodesOnlyString(stream)
self.failUnlessEqual(expecting, found)
expecting = "101 2 102 3"
found = str(stream)
self.failUnlessEqual(expecting, found)
示例8: testReplaceTwoWithOneAtLeft
def testReplaceTwoWithOneAtLeft(self):
t = CommonTree(CommonToken(99, text="a"))
t.addChild(CommonTree(CommonToken(99, text="b")))
t.addChild(CommonTree(CommonToken(99, text="c")))
t.addChild(CommonTree(CommonToken(99, text="d")))
newChild = CommonTree(CommonToken(99, text="x"))
t.replaceChildren(0, 1, newChild)
expecting = "(a x d)"
self.failUnlessEqual(expecting, t.toStringTree())
t.sanityCheckParentAndChildIndexes()
示例9: testReplaceTwoWithOneAtRight
def testReplaceTwoWithOneAtRight(self):
t = CommonTree(CommonToken(99, text="a"))
t.addChild(CommonTree(CommonToken(99, text="b")))
t.addChild(CommonTree(CommonToken(99, text="c")))
t.addChild(CommonTree(CommonToken(99, text="d")))
newChild = CommonTree(CommonToken(99, text="x"))
t.replaceChildren(1, 2, newChild)
expecting = "(a b x)"
self.assertEqual(expecting, t.toStringTree())
t.sanityCheckParentAndChildIndexes()
示例10: testListWithOneNode
def testListWithOneNode(self):
root = CommonTree(None)
root.addChild(CommonTree(CommonToken(101)))
stream = CommonTreeNodeStream(root)
expecting = "101"
found = self.toNodesOnlyString(stream)
self.failUnlessEqual(expecting, found)
expecting = "101"
found = str(stream)
self.failUnlessEqual(expecting, found)
示例11: testReplaceOneWithTwoInMiddle
def testReplaceOneWithTwoInMiddle(self):
t = CommonTree(CommonToken(99, text="a"))
t.addChild(CommonTree(CommonToken(99, text="b")))
t.addChild(CommonTree(CommonToken(99, text="c")))
t.addChild(CommonTree(CommonToken(99, text="d")))
newChildren = self.adaptor.nil()
newChildren.addChild(CommonTree(CommonToken(99, text="x")))
newChildren.addChild(CommonTree(CommonToken(99, text="y")))
t.replaceChildren(1, 1, newChildren)
expecting = "(a b x y d)"
self.assertEqual(expecting, t.toStringTree())
t.sanityCheckParentAndChildIndexes()
示例12: testReplaceAllWithTwo
def testReplaceAllWithTwo(self):
t = CommonTree(CommonToken(99, text="a"))
t.addChild(CommonTree(CommonToken(99, text="b")))
t.addChild(CommonTree(CommonToken(99, text="c")))
t.addChild(CommonTree(CommonToken(99, text="d")))
newChildren = self.adaptor.nil()
newChildren.addChild(CommonTree(CommonToken(99, text="x")))
newChildren.addChild(CommonTree(CommonToken(99, text="y")))
t.replaceChildren(0, 2, newChildren)
expecting = "(a x y)"
self.failUnlessEqual(expecting, t.toStringTree())
t.sanityCheckParentAndChildIndexes()
示例13: testFlatList
def testFlatList(self):
root = CommonTree(None)
root.addChild(CommonTree(CommonToken(101)))
root.addChild(CommonTree(CommonToken(102)))
root.addChild(CommonTree(CommonToken(103)))
stream = CommonTreeNodeStream(root)
expecting = "101 102 103"
found = self.toNodesOnlyString(stream)
self.assertEqual(expecting, found)
expecting = "101 102 103"
found = str(stream)
self.assertEqual(expecting, found)
示例14: testReplaceWithOneChildren
def testReplaceWithOneChildren(self):
# assume token type 99 and use text
t = CommonTree(CommonToken(99, text="a"))
c0 = CommonTree(CommonToken(99, text="b"))
t.addChild(c0)
newChild = CommonTree(CommonToken(99, text="c"))
t.replaceChildren(0, 0, newChild)
expecting = "(a c)"
self.failUnlessEqual(expecting, t.toStringTree())
t.sanityCheckParentAndChildIndexes()
示例15: test4Nodes
def test4Nodes(self):
# ^(101 ^(102 103) 104)
r0 = CommonTree(CommonToken(101))
r0.addChild(CommonTree(CommonToken(102)))
r0.getChild(0).addChild(CommonTree(CommonToken(103)))
r0.addChild(CommonTree(CommonToken(104)))
self.failUnless(r0.parent is None)
self.failUnlessEqual(-1, r0.childIndex)