本文整理汇总了C#中Antlr.Runtime.Tree.CommonTree.AddChild方法的典型用法代码示例。如果您正苦于以下问题:C# CommonTree.AddChild方法的具体用法?C# CommonTree.AddChild怎么用?C# CommonTree.AddChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Antlr.Runtime.Tree.CommonTree
的用法示例。
在下文中一共展示了CommonTree.AddChild方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestAddListToExistChildren
public void TestAddListToExistChildren()
{
// Add child ^(nil 101 102 103) to root ^(5 6)
// should add 101 102 103 to end of 5's child list
CommonTree root = new CommonTree( new CommonToken( 5 ) );
root.AddChild( new CommonTree( new CommonToken( 6 ) ) );
// child tree
CommonTree r0 = new CommonTree( (IToken)null );
CommonTree c0, c1, c2;
r0.AddChild( c0 = new CommonTree( new CommonToken( 101 ) ) );
r0.AddChild( c1 = new CommonTree( new CommonToken( 102 ) ) );
r0.AddChild( c2 = new CommonTree( new CommonToken( 103 ) ) );
root.AddChild( r0 );
assertNull( root.Parent );
assertEquals( -1, root.ChildIndex );
// check children of root all point at root
assertEquals( root, c0.Parent );
assertEquals( 1, c0.ChildIndex );
assertEquals( root, c0.Parent );
assertEquals( 2, c1.ChildIndex );
assertEquals( root, c0.Parent );
assertEquals( 3, c2.ChildIndex );
}
示例2: TestSeek
public void TestSeek()
{
// ^(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
ITree r0 = new CommonTree( new CommonToken( 101 ) );
ITree r1 = new CommonTree( new CommonToken( 102 ) );
r0.AddChild( r1 );
r1.AddChild( new CommonTree( new CommonToken( 103 ) ) );
ITree r2 = new CommonTree( new CommonToken( 106 ) );
r2.AddChild( new CommonTree( new CommonToken( 107 ) ) );
r1.AddChild( r2 );
r0.AddChild( new CommonTree( new CommonToken( 104 ) ) );
r0.AddChild( new CommonTree( new CommonToken( 105 ) ) );
ITreeNodeStream stream = newStream( r0 );
stream.Consume(); // consume 101
stream.Consume(); // consume DN
stream.Consume(); // consume 102
stream.Seek( 7 ); // seek to 107
Assert.AreEqual( 107, ( (ITree)stream.LT( 1 ) ).Type );
stream.Consume(); // consume 107
stream.Consume(); // consume UP
stream.Consume(); // consume UP
Assert.AreEqual( 104, ( (ITree)stream.LT( 1 ) ).Type );
}
示例3: Test4Nodes
public void Test4Nodes()
{
// ^(101 ^(102 103) 104)
CommonTree r0 = new CommonTree( new CommonToken( 101 ) );
r0.AddChild( new CommonTree( new CommonToken( 102 ) ) );
r0.GetChild( 0 ).AddChild( new CommonTree( new CommonToken( 103 ) ) );
r0.AddChild( new CommonTree( new CommonToken( 104 ) ) );
assertNull( r0.Parent );
assertEquals( -1, r0.ChildIndex );
}
示例4: Test4Nodes
public void Test4Nodes()
{
// ^(101 ^(102 103) 104)
ITree t = new CommonTree( new CommonToken( 101 ) );
t.AddChild( new CommonTree( new CommonToken( 102 ) ) );
t.GetChild( 0 ).AddChild( new CommonTree( new CommonToken( 103 ) ) );
t.AddChild( new CommonTree( new CommonToken( 104 ) ) );
ITreeNodeStream stream = newStream( t );
string expecting = " 101 102 103 104";
string found = ToNodesOnlyString( stream );
Assert.AreEqual( expecting, found );
expecting = " 101 2 102 2 103 3 104 3";
found = ToTokenTypeString( stream );
Assert.AreEqual( expecting, found );
}
示例5: TestAoverB
public void TestAoverB()
{
ITree t = new CommonTree( new CommonToken( 101 ) );
t.AddChild( new CommonTree( new CommonToken( 102 ) ) );
ITreeNodeStream stream = newStream( t );
string expecting = " 101 102";
string found = ToNodesOnlyString( stream );
Assert.AreEqual( expecting, found );
expecting = " 101 2 102 3";
found = ToTokenTypeString( stream );
Assert.AreEqual( expecting, found );
}
示例6: TestLT
public void TestLT()
{
// ^(101 ^(102 103) 104)
ITree t = new CommonTree( new CommonToken( 101 ) );
t.AddChild( new CommonTree( new CommonToken( 102 ) ) );
t.GetChild( 0 ).AddChild( new CommonTree( new CommonToken( 103 ) ) );
t.AddChild( new CommonTree( new CommonToken( 104 ) ) );
ITreeNodeStream stream = newStream( t );
Assert.AreEqual( 101, ( (ITree)stream.LT( 1 ) ).Type );
Assert.AreEqual( TokenTypes.Down, ( (ITree)stream.LT( 2 ) ).Type );
Assert.AreEqual( 102, ( (ITree)stream.LT( 3 ) ).Type );
Assert.AreEqual( TokenTypes.Down, ( (ITree)stream.LT( 4 ) ).Type );
Assert.AreEqual( 103, ( (ITree)stream.LT( 5 ) ).Type );
Assert.AreEqual( TokenTypes.Up, ( (ITree)stream.LT( 6 ) ).Type );
Assert.AreEqual( 104, ( (ITree)stream.LT( 7 ) ).Type );
Assert.AreEqual( TokenTypes.Up, ( (ITree)stream.LT( 8 ) ).Type );
Assert.AreEqual( TokenTypes.EndOfFile, ( (ITree)stream.LT( 9 ) ).Type );
// check way ahead
Assert.AreEqual( TokenTypes.EndOfFile, ( (ITree)stream.LT( 100 ) ).Type );
}
示例7: TestListWithOneNode
public void TestListWithOneNode()
{
ITree root = new CommonTree( (IToken)null );
root.AddChild( new CommonTree( new CommonToken( 101 ) ) );
ITreeNodeStream stream = newStream( root );
string expecting = " 101";
string found = ToNodesOnlyString( stream );
Assert.AreEqual( expecting, found );
expecting = " 101";
found = ToTokenTypeString( stream );
Assert.AreEqual( expecting, found );
}
示例8: TestList
public void TestList()
{
ITree root = new CommonTree( (IToken)null );
ITree t = new CommonTree( new CommonToken( 101 ) );
t.AddChild( new CommonTree( new CommonToken( 102 ) ) );
t.GetChild( 0 ).AddChild( new CommonTree( new CommonToken( 103 ) ) );
t.AddChild( new CommonTree( new CommonToken( 104 ) ) );
ITree u = new CommonTree( new CommonToken( 105 ) );
root.AddChild( t );
root.AddChild( u );
ITreeNodeStream stream = newStream( root );
string expecting = " 101 102 103 104 105";
string found = ToNodesOnlyString( stream );
Assert.AreEqual( expecting, found );
expecting = " 101 2 102 2 103 3 104 3 105";
found = ToTokenTypeString( stream );
Assert.AreEqual( expecting, found );
}
示例9: TestFlatList
public void TestFlatList()
{
ITree root = new CommonTree( (IToken)null );
root.AddChild( new CommonTree( new CommonToken( 101 ) ) );
root.AddChild( new CommonTree( new CommonToken( 102 ) ) );
root.AddChild( new CommonTree( new CommonToken( 103 ) ) );
ITreeNodeStream stream = newStream( root );
string expecting = " 101 102 103";
string found = ToNodesOnlyString( stream );
assertEquals( expecting, found );
expecting = " 101 102 103";
found = ToTokenTypeString( stream );
assertEquals( expecting, found );
}
示例10: TestReplaceAtRight
public void TestReplaceAtRight()
{
CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) ); // index 2
CommonTree newChild = new CommonTree( new CommonToken( 99, "x" ) );
t.ReplaceChildren( 2, 2, newChild );
string expecting = "(a b c x)";
Assert.AreEqual( expecting, t.ToStringTree() );
t.SanityCheckParentAndChildIndexes();
}
示例11: Check
public static DataType Check(AstNode node, Context context)
{
switch (node.Type)
{
case CCompilerLexer.PROGRAM:
{
if (context == null)
context = new Context(context);
CheckBlock(node, context);
return DataType.Void;
}
case CCompilerLexer.BLOCK:
{
context = new Context(context);
CheckBlock(node, context);
return DataType.Void;
}
case CCompilerLexer.VAR:
{
List<AstNode> nodes = new List<AstNode>();
DataType dataType = StrToDataType(node.GetChild(0).Text);
for (int i = 0; i < node.GetChild(0).ChildCount; i++)
{
AstNode temp = (AstNode)node.GetChild(0).GetChild(i);
if (temp.Token.Type == CCompilerLexer.ASSIGN)
{
Ident ident = context.InThisContext(temp.GetChild(0).Text);
if (ident != null)
throw new ApplicationException(string.Format("Identifier {0} already exists", temp.GetChild(0).Text));
AstNode var = new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.VAR, "VAR"));
var.AddChild(new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.IDENT, DataTypeToStr(dataType))));
var.GetChild(0).AddChild(new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.IDENT, temp.GetChild(0).Text)));
nodes.Add(var);
nodes.Add(temp);
}
else
{
Ident ident = context.InThisContext(temp.Text);
if (ident != null)
throw new IntepreterException(string.Format("Identifier {0} already exists", temp.Text));
AstNode var = new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.VAR, "VAR"));
var.AddChild(new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.IDENT, DataTypeToStr(dataType))));
var.GetChild(0).AddChild(temp);
nodes.Add(var);
}
string name = nodes[0].GetChild(0).GetChild(0).Text;
context[name] = new Ident(name, context.ParentContext == null ? IdentType.GlobalVar : IdentType.LocalVar, dataType, nodes[0]);
Antlr.Runtime.Tree.CommonTree tree = new Antlr.Runtime.Tree.CommonTree();
foreach (AstNode n in nodes)
tree.AddChild(n);
node.Parent.ReplaceChildren(node.ChildIndex, node.ChildIndex, tree);
}
return DataType.Void;
}
case CCompilerLexer.FUNC_DECL:
{
DataType dataType = StrToDataType(node.GetChild(0).Text);
string name = node.GetChild(1).Text;
Ident ident = context[name];
if (ident != null)
throw new IntepreterException(string.Format("Identifier {0} already exists", name));
Ident func = new Ident(name, IdentType.Function, dataType, node);
context[name] = func;
context = new Context(context);
AstNode _params = (AstNode)node.GetChild(2);
for (int i = 0; i < _params.ChildCount; i++)
{
DataType paramDataType = StrToDataType(_params.GetChild(i).Text);
string paramName = _params.GetChild(i).GetChild(0).Text;
if (paramDataType == DataType.Void)
throw new IntepreterException(string.Format("In function {0} void param {1}", name, paramName));
context[paramName] = new Ident(paramName, IdentType.Param, paramDataType, (AstNode)_params.GetChild(i));
}
context.Function = func;
Check((AstNode)node.GetChild(3), context);
return DataType.Void;
}
case CCompilerLexer.CALL:
{
Ident ident = context[node.GetChild(0).Text];
if (ident == null)
throw new IntepreterException(string.Format("Unknown identifier {0}", node.GetChild(0).Text));
if (ident.IdentType != IdentType.Function)
throw new IntepreterException(string.Format("Identifier {0} is not function", node.GetChild(0).Text));
if (node.GetChild(1).ChildCount != ident.Node.GetChild(2).ChildCount)
throw new IntepreterException(string.Format("Not equals params count in function {0}", node.GetChild(0).Text));
for (int i = 0; i < ident.Node.GetChild(2).ChildCount; i++)
{
DataType formalDataType = StrToDataType(ident.Node.GetChild(2).GetChild(i).Text);
DataType factDataType = Check((AstNode)node.GetChild(1).GetChild(i), context);
if (formalDataType != factDataType)
{
if (formalDataType == DataType.Double && factDataType == DataType.Int)
convert((AstNode)node.GetChild(1).GetChild(i), DataType.Double);
//.........这里部分代码省略.........
示例12: TestMarkRewindNested
public void TestMarkRewindNested()
{
// ^(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
ITree r0 = new CommonTree( new CommonToken( 101 ) );
ITree r1 = new CommonTree( new CommonToken( 102 ) );
r0.AddChild( r1 );
r1.AddChild( new CommonTree( new CommonToken( 103 ) ) );
ITree r2 = new CommonTree( new CommonToken( 106 ) );
r2.AddChild( new CommonTree( new CommonToken( 107 ) ) );
r1.AddChild( r2 );
r0.AddChild( new CommonTree( new CommonToken( 104 ) ) );
r0.AddChild( new CommonTree( new CommonToken( 105 ) ) );
ITreeNodeStream stream = newStream( r0 );
int m = stream.Mark(); // MARK at start
stream.Consume(); // consume 101
stream.Consume(); // consume DN
int 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
Assert.AreEqual( 102, ( (ITree)stream.LT( 1 ) ).Type );
stream.Consume();
Assert.AreEqual( TokenTypes.Down, ( (ITree)stream.LT( 1 ) ).Type );
stream.Consume();
// stop at 103 and rewind to start
stream.Rewind( m ); // REWIND to 101
Assert.AreEqual( 101, ( (ITree)stream.LT( 1 ) ).Type );
stream.Consume();
Assert.AreEqual( TokenTypes.Down, ( (ITree)stream.LT( 1 ) ).Type );
stream.Consume();
Assert.AreEqual( 102, ( (ITree)stream.LT( 1 ) ).Type );
stream.Consume();
Assert.AreEqual( TokenTypes.Down, ( (ITree)stream.LT( 1 ) ).Type );
}
示例13: TestReplaceOneWithTwoInMiddle
public void TestReplaceOneWithTwoInMiddle()
{
CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );
CommonTree newChildren = (CommonTree)adaptor.Nil();
newChildren.AddChild( new CommonTree( new CommonToken( 99, "x" ) ) );
newChildren.AddChild( new CommonTree( new CommonToken( 99, "y" ) ) );
t.ReplaceChildren( 1, 1, newChildren );
string expecting = "(a b x y d)";
assertEquals( expecting, t.ToStringTree() );
t.SanityCheckParentAndChildIndexes();
}
示例14: TestList2
public void TestList2()
{
// Add child ^(nil 101 102 103) to root 5
// should pull 101 102 103 directly to become 5's child list
CommonTree root = new CommonTree( new CommonToken( 5 ) );
// child tree
CommonTree r0 = new CommonTree( (IToken)null );
CommonTree c0, c1, c2;
r0.AddChild( c0 = new CommonTree( new CommonToken( 101 ) ) );
r0.AddChild( c1 = new CommonTree( new CommonToken( 102 ) ) );
r0.AddChild( c2 = new CommonTree( new CommonToken( 103 ) ) );
root.AddChild( r0 );
assertNull( root.Parent );
assertEquals( -1, root.ChildIndex );
// check children of root all point at root
assertEquals( root, c0.Parent );
assertEquals( 0, c0.ChildIndex );
assertEquals( root, c0.Parent );
assertEquals( 1, c1.ChildIndex );
assertEquals( root, c0.Parent );
assertEquals( 2, c2.ChildIndex );
}
示例15: TestReplaceInMiddle
public void TestReplaceInMiddle()
{
CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) ); // index 1
t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );
CommonTree newChild = new CommonTree( new CommonToken( 99, "x" ) );
t.ReplaceChildren( 1, 1, newChild );
string expecting = "(a b x d)";
assertEquals( expecting, t.ToStringTree() );
t.SanityCheckParentAndChildIndexes();
}