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)
def testBecomeRoot2(self): # 5 becomes root of ^(101 102 103) newRoot = CommonTree(CommonToken(5)) oldRoot = CommonTree(CommonToken(101)) oldRoot.addChild(CommonTree(CommonToken(102))) oldRoot.addChild(CommonTree(CommonToken(103))) self.adaptor.becomeRoot(newRoot, oldRoot) newRoot.sanityCheckParentAndChildIndexes()
def copy_node(node: CommonTree) -> CommonTree: token_str = node.text if not token_str: token_str = "" new_token = Token(text=token_str) new_token.TOKEN_NAMES_MAP = None new_tree_node = CommonTree(new_token) for child in node.children: new_tree_node.addChild(copy_node(child)) return new_tree_node
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()
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()
def testBecomeRoot6(self): # emulates construction of ^(5 6) root_0 = self.adaptor.nil() root_1 = self.adaptor.nil() root_1 = self.adaptor.becomeRoot(CommonTree(CommonToken(5)), root_1) self.adaptor.addChild(root_1, CommonTree(CommonToken(6))) self.adaptor.addChild(root_0, root_1) root_0.sanityCheckParentAndChildIndexes()
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)
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()
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)
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()
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()
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()
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)
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()
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()
def defer_push_message(self, defer_transition: Transition, defer_token: str) -> CommonTree: new_operation = CommonTree(CommonToken(text=defer_token)) new_operation.addChild(CommonTree(CommonToken(text="recv"))) new_operation.addChild( CommonTree(CommonToken(text=str(defer_transition.inMsg)))) return new_operation
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)
def update_messages(self, trans_operation: CommonTree, defer_msg_list_dependent_var: List[str], message_objects: List[PCCObject]) -> List[CommonTree]: local_update_operations = [] children = trans_operation.getChildren() defer_var_name = MurphiModular.vdeferpref + str( children[0]) + "_" + str(children[2].children[1]) in_msg_name = str(children[2].children[1]) # Update the variables in the deferred message retrieved from buffer if len(children[2].children) > 4: # Not a base message payload = children[2].children[4:] # Get the index of the dependent variable in message constructor for var_ind in range(0, len(payload)): local_var_name = str(payload[var_ind]) # Check if there exists a message dependency if local_var_name in defer_msg_list_dependent_var: # Find the message constructor for message_object in message_objects: # The right constructor was found if str(message_object) == str(children[2].children[0]): msg_object = message_object.structure.children[ var_ind + 1] msg_var = str(msg_object) local_update_operations.append( self.generate_update_assignement( defer_var_name, msg_var, local_var_name, in_msg_name)) return local_update_operations
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()
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.assertIsNone(r0.parent) self.assertEqual(-1, r0.childIndex)
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)
def testSingleNode(self): t = CommonTree(CommonToken(101)) stream = self.newStream(t) expecting = "101" found = self.toNodesOnlyString(stream) self.failUnlessEqual(expecting, found) expecting = "101" found = str(stream) self.failUnlessEqual(expecting, found)
def _CreateMsgObj(self, obj: CommonTree): self.msgNode.append(PCCObject(obj)) msgformat = obj.getChildren() msgtype = "" for ind in range(0, len(msgformat)): if ind == 0: msgtype = msgformat[ind].getText() else: if msgformat[ind].getText() == self.Data: self.dataMsgTypes.append(msgtype)
def testList(self): root = CommonTree(None) t = CommonTree(CommonToken(101)) t.addChild(CommonTree(CommonToken(102))) t.getChild(0).addChild(CommonTree(CommonToken(103))) t.addChild(CommonTree(CommonToken(104))) u = CommonTree(CommonToken(105)) root.addChild(t) root.addChild(u) stream = CommonTreeNodeStream(root) expecting = "101 102 103 104 105" found = self.toNodesOnlyString(stream) self.failUnlessEqual(expecting, found) expecting = "101 2 102 2 103 3 104 3 105" found = str(stream) self.failUnlessEqual(expecting, found)
def send_base_message(self, trans_operation: CommonTree, transition: Transition, var_names: Dict[str, str]) -> List[CommonTree]: ret_operation = trans_operation children = trans_operation.getChildren() if str(children[1]) in var_names: tmp_operation = copy.deepcopy(trans_operation) tmp_operation.token.text = MurphiModular.tPUSH_HL_DEFER tmp_operation.children[1].token.text = var_names[str(children[1])] transition.operation = copy.copy(transition.operation) transition.operation[transition.operation.index( trans_operation)] = tmp_operation return [ret_operation] return []
def defer_base_message(self, trans_operation: CommonTree, transition: Transition, var_names: Dict[str, str]) -> List[CommonTree]: local_defer_operations = [] children = trans_operation.getChildren() defer_var_name = MurphiModular.vdeferpref + str(children[0]) + "_" + \ str(children[2].children[1]) # Records the variable name of the child var_names[str(children[0])] = defer_var_name # Recover next deferred operation tmp_operation = copy.deepcopy(trans_operation) tmp_children = tmp_operation.getChildren() defer_var_name = MurphiModular.vdeferpref + str(tmp_children[0]) + "_" + \ str(tmp_children[2].children[1]) tmp_children[0].token.text = defer_var_name tmp_children[2].token.text = MurphiModular.tPOP_HL_DEFER local_defer_operations.append(tmp_operation) # Construct Original Message from deferred operation tmp_operation = copy.deepcopy(trans_operation) tmp_children = tmp_operation.getChildren() tmp_children[2].token.text = MurphiModular.tSEND_BASE_DEFER tmp_children[2].children[1].token.text = defer_var_name for ind in range(2, len(MurphiModular.BaseMsg)): tmp_children[2].children.pop(2) local_defer_operations.append(tmp_operation) tmp_operation = copy.deepcopy(trans_operation) tmp_children = tmp_operation.getChildren() # Generate BaseMessage that is deferred tmp_children[0].token.text = defer_var_name tmp_children[2].token.text = MurphiModular.tSEND_BASE_DEFER tmp_children[2].children[0].token.text = MurphiModular.rbasemessage for ind in range(len(MurphiModular.BaseMsg), len(tmp_children[2].getChildren())): tmp_children[2].children.pop(len(MurphiModular.BaseMsg)) # Update the transition transition.operation = copy.copy(transition.operation) transition.operation[transition.operation.index( trans_operation)] = tmp_operation return local_defer_operations
def test4Nodes(self): # ^(101 ^(102 103) 104) t = CommonTree(CommonToken(101)) t.addChild(CommonTree(CommonToken(102))) t.getChild(0).addChild(CommonTree(CommonToken(103))) t.addChild(CommonTree(CommonToken(104))) stream = self.newStream(t) expecting = "101 102 103 104" found = self.toNodesOnlyString(stream) self.failUnlessEqual(expecting, found) expecting = "101 2 102 2 103 3 104 3" found = str(stream) self.failUnlessEqual(expecting, found)
def testList(self): # ^(nil 101 102 103) r0 = CommonTree(None) c0 = CommonTree(CommonToken(101)) r0.addChild(c0) c1 = CommonTree(CommonToken(102)) r0.addChild(c1) c2 = CommonTree(CommonToken(103)) r0.addChild(c2) self.failUnless(r0.parent is None) self.failUnlessEqual(-1, r0.childIndex) self.failUnlessEqual(r0, c0.parent) self.failUnlessEqual(0, c0.childIndex) self.failUnlessEqual(r0, c1.parent) self.failUnlessEqual(1, c1.childIndex) self.failUnlessEqual(r0, c2.parent) self.failUnlessEqual(2, c2.childIndex)
def testLT(self): # ^(101 ^(102 103) 104) t = CommonTree(CommonToken(101)) t.addChild(CommonTree(CommonToken(102))) t.getChild(0).addChild(CommonTree(CommonToken(103))) t.addChild(CommonTree(CommonToken(104))) stream = self.newStream(t) self.failUnlessEqual(101, stream.LT(1).getType()) self.failUnlessEqual(DOWN, stream.LT(2).getType()) self.failUnlessEqual(102, stream.LT(3).getType()) self.failUnlessEqual(DOWN, stream.LT(4).getType()) self.failUnlessEqual(103, stream.LT(5).getType()) self.failUnlessEqual(UP, stream.LT(6).getType()) self.failUnlessEqual(104, stream.LT(7).getType()) self.failUnlessEqual(UP, stream.LT(8).getType()) self.failUnlessEqual(EOF, stream.LT(9).getType()) # check way ahead self.failUnlessEqual(EOF, stream.LT(100).getType())
def defer_left_dependent_operations( self, trans_operation: CommonTree, defer_msg_var_names: Dict[str, str], dependent_defer_msg_vars: Dict[str, str], defer_message_operation_dict: Dict[str, List[CommonTree]]): children = trans_operation.getChildren() cur_var_name = str(children[0]) # Left assignment # Remove [ASSIGN, var_name, = ] assignment_list = toStringList(trans_operation)[3:] # Variable reassignment e.g. new Msg object assigned to same var if cur_var_name in defer_msg_var_names and cur_var_name not in assignment_list: remove_var = defer_msg_var_names.pop(cur_var_name) del defer_message_operation_dict[cur_var_name] remove_key_list = [] # Clear dependent variables for dependent_var_name in dependent_defer_msg_vars: if dependent_defer_msg_vars[dependent_var_name] == remove_var: remove_key_list.append(dependent_var_name) for key in remove_key_list: del dependent_defer_msg_vars[key] else: for var_name in defer_msg_var_names: # Current value of variable is used in an assignment if var_name in assignment_list: dependent_defer_msg_vars[(children[0])] = var_name defer_message_operation_dict[var_name].append( trans_operation) continue for dependent_var_name in dependent_defer_msg_vars: if dependent_var_name in assignment_list: dependent_defer_msg_vars[(children[0])] = var_name defer_message_operation_dict[var_name].append( trans_operation)
def defer_msg_assignment( self, trans_operation: CommonTree, defer_msg_list_dependent_var: List[str], message_objects: List[PCCObject]) -> List[CommonTree]: local_defer_operations = [] children = trans_operation.getChildren() # Retrieve deferred operation tmp_operation = copy.deepcopy(trans_operation) tmp_children = tmp_operation.getChildren() defer_var_name = MurphiModular.vdeferpref + str(children[0]) + "_" + \ str(children[2].children[1]) tmp_children[0].token.text = defer_var_name tmp_children[2].token.text = MurphiModular.tPOP_HL_DEFER local_defer_operations.append(tmp_operation) local_defer_operations += self.update_messages( trans_operation, defer_msg_list_dependent_var, message_objects) return local_defer_operations
def testAddListToExistChildren(self): # Add child ^(nil 101 102 103) to root ^(5 6) # should add 101 102 103 to end of 5's child list root = CommonTree(CommonToken(5)) root.addChild(CommonTree(CommonToken(6))) # 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(1, c0.childIndex) self.failUnlessEqual(root, c0.parent) self.failUnlessEqual(2, c1.childIndex) self.failUnlessEqual(root, c0.parent) self.failUnlessEqual(3, c2.childIndex)
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.failUnlessEqual(expecting, t.toStringTree()) t.sanityCheckParentAndChildIndexes()
def _CreateConstant(self, obj: CommonTree): data = obj.getChildren() self.constNode[str(data[0])] = str(data[1])
def __init__(self, payload): if type(payload) == int: CommonTree.__init__(self, CommonToken(type=payload, text=self.spelling)) else: CommonTree.__init__(self, payload)
def testSeekFromStart(self): # ^(101 ^(102 103 ^(106 107) ) 104 105) # stream has 7 real + 6 nav nodes # Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF r0 = CommonTree(CommonToken(101)) r1 = CommonTree(CommonToken(102)) r0.addChild(r1) r1.addChild(CommonTree(CommonToken(103))) r2 = CommonTree(CommonToken(106)) r2.addChild(CommonTree(CommonToken(107))) r1.addChild(r2) r0.addChild(CommonTree(CommonToken(104))) r0.addChild(CommonTree(CommonToken(105))) stream = CommonTreeNodeStream(r0) stream.seek(7) # seek to 107 self.failUnlessEqual(107, stream.LT(1).getType()) stream.consume() # consume 107 stream.consume() # consume UP stream.consume() # consume UP self.failUnlessEqual(104, stream.LT(1).getType())
def defer_pop_message(self, defer_transition: Transition, defer_token: str) -> CommonTree: new_operation = CommonTree(CommonToken(text=MurphiModular.tASSIGN)) # Left side assignment new_operation.addChild( CommonTree(CommonToken(text=str(defer_transition.inMsg)))) # Assignment operator new_operation.addChild(CommonTree(CommonToken(text="="))) # Right side assignment new_operation.addChild(CommonTree(CommonToken(text=defer_token))) return new_operation
def generate_update_assignement(self, defer_var_name: str, msg_var: str, local_var_name: str, in_msg_name: str = ""): new_operation = CommonTree(CommonToken(text=MurphiModular.tASSIGN)) # Left side assignment left_tree = CommonTree(CommonToken(text=defer_var_name)) left_tree.addChild(CommonTree(CommonToken(text="."))) left_tree.addChild(CommonTree(CommonToken(text=msg_var))) new_operation.addChild(left_tree) new_operation.addChild(CommonTree(CommonToken(text="="))) # Right side assignment new_operation.addChild(CommonTree(CommonToken(text=local_var_name))) #left_tree = CommonTree(CommonToken(text=in_msg_name)) #left_tree.addChild(CommonTree(CommonToken(text="."))) #left_tree.addChild(CommonTree(CommonToken(text=msg_var))) return new_operation
def testMarkRewindNested(self): # ^(101 ^(102 103 ^(106 107) ) 104 105) # stream has 7 real + 6 nav nodes # Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF r0 = CommonTree(CommonToken(101)) r1 = CommonTree(CommonToken(102)) r0.addChild(r1) r1.addChild(CommonTree(CommonToken(103))) r2 = CommonTree(CommonToken(106)) r2.addChild(CommonTree(CommonToken(107))) r1.addChild(r2) r0.addChild(CommonTree(CommonToken(104))) r0.addChild(CommonTree(CommonToken(105))) stream = CommonTreeNodeStream(r0) m = stream.mark() # MARK at start stream.consume() # consume 101 stream.consume() # consume DN m2 = stream.mark() # MARK on 102 stream.consume() # consume 102 stream.consume() # consume DN stream.consume() # consume 103 stream.consume() # consume 106 stream.rewind(m2) # REWIND to 102 self.failUnlessEqual(102, stream.LT(1).getType()) stream.consume() self.failUnlessEqual(DOWN, stream.LT(1).getType()) stream.consume() # stop at 103 and rewind to start stream.rewind(m) # REWIND to 101 self.failUnlessEqual(101, stream.LT(1).getType()) stream.consume() self.failUnlessEqual(DOWN, stream.LT(1).getType()) stream.consume() self.failUnlessEqual(102, stream.LT(1).getType()) stream.consume() self.failUnlessEqual(DOWN, stream.LT(1).getType())
def __init__(self, payload): CommonTree.__init__(self, payload) self.label = None self.hasTextArg = None
def testDupTree(self): # ^(101 ^(102 103 ^(106 107) ) 104 105) r0 = CommonTree(CommonToken(101)) r1 = CommonTree(CommonToken(102)) r0.addChild(r1) r1.addChild(CommonTree(CommonToken(103))) r2 = CommonTree(CommonToken(106)) r2.addChild(CommonTree(CommonToken(107))) r1.addChild(r2) r0.addChild(CommonTree(CommonToken(104))) r0.addChild(CommonTree(CommonToken(105))) dup = self.adaptor.dupTree(r0) self.failUnless(dup.parent is None) self.failUnlessEqual(-1, dup.childIndex) dup.sanityCheckParentAndChildIndexes()
def testPushPop(self): # ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) # stream has 9 real + 8 nav nodes # Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP r0 = CommonTree(CommonToken(101)) r1 = CommonTree(CommonToken(102)) r1.addChild(CommonTree(CommonToken(103))) r0.addChild(r1) r2 = CommonTree(CommonToken(104)) r2.addChild(CommonTree(CommonToken(105))) r0.addChild(r2) r3 = CommonTree(CommonToken(106)) r3.addChild(CommonTree(CommonToken(107))) r0.addChild(r3) r0.addChild(CommonTree(CommonToken(108))) r0.addChild(CommonTree(CommonToken(109))) stream = CommonTreeNodeStream(r0) expecting = "101 2 102 2 103 3 104 2 105 3 106 2 107 3 108 109 3" found = str(stream) self.failUnlessEqual(expecting, found) # Assume we want to hit node 107 and then "call 102" then return indexOf102 = 2 indexOf107 = 12 for _ in range(indexOf107): # consume til 107 node stream.consume() # CALL 102 self.failUnlessEqual(107, stream.LT(1).getType()) stream.push(indexOf102) self.failUnlessEqual(102, stream.LT(1).getType()) stream.consume() # consume 102 self.failUnlessEqual(DOWN, stream.LT(1).getType()) stream.consume() # consume DN self.failUnlessEqual(103, stream.LT(1).getType()) stream.consume() # consume 103 self.failUnlessEqual(UP, stream.LT(1).getType()) # RETURN stream.pop() self.failUnlessEqual(107, stream.LT(1).getType())
def testNestedPushPop(self): # ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) # stream has 9 real + 8 nav nodes # Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP r0 = CommonTree(CommonToken(101)) r1 = CommonTree(CommonToken(102)) r1.addChild(CommonTree(CommonToken(103))) r0.addChild(r1) r2 = CommonTree(CommonToken(104)) r2.addChild(CommonTree(CommonToken(105))) r0.addChild(r2) r3 = CommonTree(CommonToken(106)) r3.addChild(CommonTree(CommonToken(107))) r0.addChild(r3) r0.addChild(CommonTree(CommonToken(108))) r0.addChild(CommonTree(CommonToken(109))) stream = CommonTreeNodeStream(r0) # Assume we want to hit node 107 and then "call 102", which # calls 104, then return indexOf102 = 2 indexOf107 = 12 for _ in range(indexOf107): # consume til 107 node stream.consume() self.failUnlessEqual(107, stream.LT(1).getType()) # CALL 102 stream.push(indexOf102) self.failUnlessEqual(102, stream.LT(1).getType()) stream.consume() # consume 102 self.failUnlessEqual(DOWN, stream.LT(1).getType()) stream.consume() # consume DN self.failUnlessEqual(103, stream.LT(1).getType()) stream.consume() # consume 103 # CALL 104 indexOf104 = 6 stream.push(indexOf104) self.failUnlessEqual(104, stream.LT(1).getType()) stream.consume() # consume 102 self.failUnlessEqual(DOWN, stream.LT(1).getType()) stream.consume() # consume DN self.failUnlessEqual(105, stream.LT(1).getType()) stream.consume() # consume 103 self.failUnlessEqual(UP, stream.LT(1).getType()) # RETURN (to UP node in 102 subtree) stream.pop() self.failUnlessEqual(UP, stream.LT(1).getType()) # RETURN (to empty stack) stream.pop() self.failUnlessEqual(107, stream.LT(1).getType())
def testPushPopFromEOF(self): # ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) # stream has 9 real + 8 nav nodes # Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP r0 = CommonTree(CommonToken(101)) r1 = CommonTree(CommonToken(102)) r1.addChild(CommonTree(CommonToken(103))) r0.addChild(r1) r2 = CommonTree(CommonToken(104)) r2.addChild(CommonTree(CommonToken(105))) r0.addChild(r2) r3 = CommonTree(CommonToken(106)) r3.addChild(CommonTree(CommonToken(107))) r0.addChild(r3) r0.addChild(CommonTree(CommonToken(108))) r0.addChild(CommonTree(CommonToken(109))) stream = CommonTreeNodeStream(r0) while stream.LA(1) != EOF: stream.consume() indexOf102 = 2 indexOf104 = 6 self.failUnlessEqual(EOF, stream.LT(1).getType()) # CALL 102 stream.push(indexOf102) self.failUnlessEqual(102, stream.LT(1).getType()) stream.consume() # consume 102 self.failUnlessEqual(DOWN, stream.LT(1).getType()) stream.consume() # consume DN self.failUnlessEqual(103, stream.LT(1).getType()) stream.consume() # consume 103 self.failUnlessEqual(UP, stream.LT(1).getType()) # RETURN (to empty stack) stream.pop() self.failUnlessEqual(EOF, stream.LT(1).getType()) # CALL 104 stream.push(indexOf104) self.failUnlessEqual(104, stream.LT(1).getType()) stream.consume() # consume 102 self.failUnlessEqual(DOWN, stream.LT(1).getType()) stream.consume() # consume DN self.failUnlessEqual(105, stream.LT(1).getType()) stream.consume() # consume 103 self.failUnlessEqual(UP, stream.LT(1).getType()) # RETURN (to empty stack) stream.pop() self.failUnlessEqual(EOF, stream.LT(1).getType())
def testIterator(self): r0 = CommonTree(CommonToken(101)) r1 = CommonTree(CommonToken(102)) r0.addChild(r1) r1.addChild(CommonTree(CommonToken(103))) r2 = CommonTree(CommonToken(106)) r2.addChild(CommonTree(CommonToken(107))) r1.addChild(r2) r0.addChild(CommonTree(CommonToken(104))) r0.addChild(CommonTree(CommonToken(105))) stream = CommonTreeNodeStream(r0) expecting = [ 101, DOWN, 102, DOWN, 103, 106, DOWN, 107, UP, UP, 104, 105, UP] found = [t.type for t in stream] self.assertEqual(expecting, found)
def testMarkRewindInMiddle(self): # ^(101 ^(102 103 ^(106 107) ) 104 105) # stream has 7 real + 6 nav nodes # Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF r0 = CommonTree(CommonToken(101)) r1 = CommonTree(CommonToken(102)) r0.addChild(r1) r1.addChild(CommonTree(CommonToken(103))) r2 = CommonTree(CommonToken(106)) r2.addChild(CommonTree(CommonToken(107))) r1.addChild(r2) r0.addChild(CommonTree(CommonToken(104))) r0.addChild(CommonTree(CommonToken(105))) stream = CommonTreeNodeStream(r0) for _ in range(7): # consume til middle # System.out.println(tream.LT(1).getType()) stream.consume() self.failUnlessEqual(107, stream.LT(1).getType()) m = stream.mark() # MARK stream.consume() # consume 107 stream.consume() # consume UP stream.consume() # consume UP stream.consume() # consume 104 stream.rewind(m) # REWIND self.failUnlessEqual(107, stream.LT(1).getType()) stream.consume() self.failUnlessEqual(UP, stream.LT(1).getType()) stream.consume() self.failUnlessEqual(UP, stream.LT(1).getType()) stream.consume() self.failUnlessEqual(104, stream.LT(1).getType()) stream.consume() # now we're past rewind position self.failUnlessEqual(105, stream.LT(1).getType()) stream.consume() self.failUnlessEqual(UP, stream.LT(1).getType()) stream.consume() self.failUnlessEqual(EOF, stream.LT(1).getType()) self.failUnlessEqual(UP, stream.LT(-1).getType())
def toString(self): if self.label is not None: return '%' + self.label + ':' + CommonTree.toString(self) else: return CommonTree.toString(self)
def testMarkRewindEntire(self): # ^(101 ^(102 103 ^(106 107) ) 104 105) # stream has 7 real + 6 nav nodes # Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF r0 = CommonTree(CommonToken(101)) r1 = CommonTree(CommonToken(102)) r0.addChild(r1) r1.addChild(CommonTree(CommonToken(103))) r2 = CommonTree(CommonToken(106)) r2.addChild(CommonTree(CommonToken(107))) r1.addChild(r2) r0.addChild(CommonTree(CommonToken(104))) r0.addChild(CommonTree(CommonToken(105))) stream = CommonTreeNodeStream(r0) m = stream.mark() # MARK for _ in range(13): # consume til end stream.LT(1) stream.consume() self.failUnlessEqual(EOF, stream.LT(1).getType()) self.failUnlessEqual(UP, stream.LT(-1).getType()) stream.rewind(m) # REWIND # consume til end again :) for _ in range(13): # consume til end stream.LT(1) stream.consume() self.failUnlessEqual(EOF, stream.LT(1).getType()) self.failUnlessEqual(UP, stream.LT(-1).getType())
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.failUnlessEqual(expecting, t.toStringTree()) t.sanityCheckParentAndChildIndexes()