1 | using System; |
---|
2 | using ASTFactory = antlr.ASTFactory; |
---|
3 | using CommonAST = antlr.CommonAST; |
---|
4 | using Token = antlr.Token; |
---|
5 | using BaseAST = antlr.BaseAST; |
---|
6 | using AST = antlr.collections.AST; |
---|
7 | |
---|
8 | // wh: bug(?) in DotGNU 0.6 - "using antlr" will workaround the problem. |
---|
9 | #if __CSCC__ |
---|
10 | using antlr; |
---|
11 | #endif |
---|
12 | |
---|
13 | /// <summary>Test the new heterogeneous token type to tree node mapping stuff.</summary> |
---|
14 | public class TestASTFactory { |
---|
15 | private ASTFactory factory; |
---|
16 | |
---|
17 | public TestASTFactory() |
---|
18 | { |
---|
19 | factory = new ASTFactory(); |
---|
20 | factory.setMaxNodeType(56); |
---|
21 | } |
---|
22 | |
---|
23 | public static void Main(string[] args) { |
---|
24 | TestASTFactory testHarness = new TestASTFactory(); |
---|
25 | if ( !testHarness.testDefaultCreate() ) error("testDefaultCreate"); |
---|
26 | else success("testDefaultCreate"); |
---|
27 | if ( !testHarness.testSpecificHomoCreate() ) error("testSpecificHomoCreate"); |
---|
28 | else success("testSpecificHomoCreate"); |
---|
29 | if ( !testHarness.testDynamicHeteroCreate() ) error("testDynamicHeteroCreate"); |
---|
30 | else success("testDynamicHeteroCreate"); |
---|
31 | if ( !testHarness.testNodeDup() ) error("testNodeDup"); |
---|
32 | else success("testNodeDup"); |
---|
33 | if ( !testHarness.testHeteroTreeDup() ) error("testHeteroTreeDup"); |
---|
34 | else success("testHeteroTreeDup"); |
---|
35 | } |
---|
36 | |
---|
37 | public bool testDefaultCreate() { |
---|
38 | factory = new ASTFactory(); |
---|
39 | AST t = factory.create(); |
---|
40 | return checkNode(t, typeof(CommonAST), Token.INVALID_TYPE); |
---|
41 | } |
---|
42 | |
---|
43 | public bool testSpecificHomoCreate() { |
---|
44 | factory = new ASTFactory(); |
---|
45 | factory.setASTNodeType("MyAST"); |
---|
46 | AST t = factory.create(); |
---|
47 | factory.setASTNodeType("antlr.CommonAST"); // put it back |
---|
48 | return checkNode(t, typeof(MyAST), Token.INVALID_TYPE); |
---|
49 | } |
---|
50 | |
---|
51 | public bool testDynamicHeteroCreate() { |
---|
52 | factory = new ASTFactory(); |
---|
53 | factory.setMaxNodeType(55); |
---|
54 | factory.setTokenTypeASTNodeType(49,"ASTType49"); |
---|
55 | AST t = factory.create(49); |
---|
56 | bool a = checkNode(t, typeof(ASTType49), 49); |
---|
57 | AST u = factory.create(55); |
---|
58 | bool b = checkNode(u, typeof(CommonAST), 55); |
---|
59 | AST v = factory.create(49,"","MyAST"); |
---|
60 | bool c = checkNode(v, typeof(MyAST), 49); |
---|
61 | return a&&b&&c; |
---|
62 | } |
---|
63 | |
---|
64 | public bool testNodeDup() { |
---|
65 | factory = new ASTFactory(); |
---|
66 | factory.setMaxNodeType(49); |
---|
67 | AST t = factory.create(); |
---|
68 | bool a = t.Equals(factory.dup(t)); |
---|
69 | bool b = !t.Equals(null); |
---|
70 | AST u = factory.create(49,"","ASTType49"); |
---|
71 | bool c = checkNode(factory.dup(u),typeof(ASTType49), 49); |
---|
72 | bool d = u.Equals(factory.dup(u)); |
---|
73 | return a&&b&&c&&d; |
---|
74 | } |
---|
75 | |
---|
76 | public bool testHeteroTreeDup() { |
---|
77 | factory = new ASTFactory(); |
---|
78 | factory.setMaxNodeType(49); |
---|
79 | // create a tree and try to dup: |
---|
80 | // ( [type 1] [type 2] ( [type 49] [type 3 #2] ) [type 3] ) |
---|
81 | AST x = factory.create(1,"[type 1]","MyAST"); // will be root |
---|
82 | AST y = factory.create(2,"[type 2]","MyAST"); |
---|
83 | AST z = factory.create(3,"[type 3]","MyAST"); |
---|
84 | AST sub = factory.create(49,"[type 49]","ASTType49"); |
---|
85 | sub.addChild(factory.create(3,"[type 3 #2]","MyAST")); |
---|
86 | AST t = factory.make(new AST[] {x,y,sub,z}); |
---|
87 | AST dup_t = factory.dupList(t); |
---|
88 | // check structure |
---|
89 | bool a = dup_t.EqualsList(t); |
---|
90 | // check types |
---|
91 | bool b = equalsNodeTypesList(t,dup_t); |
---|
92 | |
---|
93 | return a&&b; |
---|
94 | } |
---|
95 | |
---|
96 | protected bool checkNode(AST t, Type typ, int tokenType) { |
---|
97 | if ( t==null ) { |
---|
98 | return false; |
---|
99 | } |
---|
100 | if ( t.GetType()!=typ ) { |
---|
101 | return false; |
---|
102 | } |
---|
103 | if ( t.Type!=tokenType ) { |
---|
104 | return false; |
---|
105 | } |
---|
106 | return true; |
---|
107 | } |
---|
108 | |
---|
109 | /** Is t an exact structural match of this tree with the same node |
---|
110 | * types? 'self' is considered the start of a sibling list. |
---|
111 | */ |
---|
112 | protected bool equalsNodeTypesList(AST self, AST t) { |
---|
113 | // Console.Out.WriteLine("self="+self+", t="+t); |
---|
114 | // Console.Out.WriteLine("self.class="+self.getClass()+", t.class="+t.getClass()); |
---|
115 | AST sibling; |
---|
116 | |
---|
117 | // the empty tree is not a match of any non-null tree. |
---|
118 | if (t == null) { |
---|
119 | return false; |
---|
120 | } |
---|
121 | |
---|
122 | // Otherwise, start walking sibling lists. First mismatch, return false. |
---|
123 | for (sibling = self; |
---|
124 | sibling != null && t != null; |
---|
125 | sibling = sibling.getNextSibling(), t = t.getNextSibling()) |
---|
126 | { |
---|
127 | // Console.Out.WriteLine("sibling="+sibling+", t="+t); |
---|
128 | // as a quick optimization, check root types first. |
---|
129 | if ( sibling.GetType()!=t.GetType() ) { |
---|
130 | return false; |
---|
131 | } |
---|
132 | // if roots match, do full list match test on children. |
---|
133 | if (sibling.getFirstChild() != null) { |
---|
134 | if (!equalsNodeTypesList(sibling.getFirstChild(), |
---|
135 | t.getFirstChild())) |
---|
136 | { |
---|
137 | return false; |
---|
138 | } |
---|
139 | } |
---|
140 | // sibling has no kids, make sure t doesn't either |
---|
141 | else if (t.getFirstChild() != null) { |
---|
142 | return false; |
---|
143 | } |
---|
144 | } |
---|
145 | if (sibling == null && t == null) { |
---|
146 | return true; |
---|
147 | } |
---|
148 | // one sibling list has more than the other |
---|
149 | return false; |
---|
150 | } |
---|
151 | |
---|
152 | public static void error(string test) { |
---|
153 | Console.Out.WriteLine("Test "+test+" FAILED"); |
---|
154 | } |
---|
155 | |
---|
156 | public static void success(string test) { |
---|
157 | Console.Out.WriteLine("Test "+test+" succeeded"); |
---|
158 | } |
---|
159 | } |
---|