calabash-android 0.4.0.pre1 → 0.4.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/calabash-android-build.rb +6 -0
- data/bin/calabash-android-setup.rb +2 -3
- data/lib/calabash-android/canned_steps.md +1 -1
- data/lib/calabash-android/helpers.rb +35 -2
- data/lib/calabash-android/lib/TestServer.apk +0 -0
- data/lib/calabash-android/operations.rb +13 -6
- data/lib/calabash-android/version.rb +1 -1
- data/test-server/instrumentation-backend/.classpath +2 -1
- data/test-server/instrumentation-backend/antlr.sh +2 -0
- data/test-server/instrumentation-backend/antlr/UIQuery.g +70 -0
- data/test-server/instrumentation-backend/antlr/UIQuery.tokens +12 -0
- data/test-server/instrumentation-backend/build-libs/antlr-3.4-complete.jar +0 -0
- data/test-server/instrumentation-backend/build-libs/junit.jar +0 -0
- data/test-server/instrumentation-backend/build.xml +56 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRFileStream.java +78 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRInputStream.java +70 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRReaderStream.java +95 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRStringStream.java +230 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/BaseRecognizer.java +894 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/BitSet.java +325 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/BufferedTokenStream.java +272 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/CharStream.java +57 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/CharStreamState.java +45 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/ClassicToken.java +141 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/CommonToken.java +191 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/CommonTokenStream.java +153 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/DFA.java +250 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/EarlyExitException.java +41 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/FailedPredicateException.java +54 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/IntStream.java +122 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/LegacyCommonTokenStream.java +394 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/Lexer.java +340 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedNotSetException.java +41 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedRangeException.java +45 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedSetException.java +44 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedTokenException.java +45 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedTreeNodeException.java +49 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/MissingTokenException.java +56 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/NoViableAltException.java +57 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/Parser.java +98 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/ParserRuleReturnScope.java +52 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/RecognitionException.java +180 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/RecognizerSharedState.java +144 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/RuleReturnScope.java +42 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/SerializedGrammar.java +204 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/Token.java +92 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/TokenRewriteStream.java +569 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/TokenSource.java +54 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/TokenStream.java +75 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/UnbufferedTokenStream.java +82 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/UnwantedTokenException.java +53 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/BlankDebugEventListener.java +77 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventHub.java +292 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventListener.java +323 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventRepeater.java +88 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventSocketProxy.java +358 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugParser.java +101 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTokenStream.java +156 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTreeAdaptor.java +250 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTreeNodeStream.java +155 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTreeParser.java +112 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/ParseTreeBuilder.java +109 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/Profiler.java +772 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/RemoteDebugEventSocketListener.java +541 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/TraceDebugEventListener.java +108 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/Tracer.java +69 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/DoubleKeyMap.java +62 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/FastQueue.java +100 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/IntArray.java +87 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/LookaheadStream.java +161 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/Stats.java +189 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/BaseTree.java +349 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/BaseTreeAdaptor.java +279 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/BufferedTreeNodeStream.java +489 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonErrorNode.java +108 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonTree.java +185 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonTreeAdaptor.java +168 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonTreeNodeStream.java +171 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/ParseTree.java +119 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteCardinalityException.java +47 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteEarlyExitException.java +39 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteEmptyStreamException.java +35 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleElementStream.java +210 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleNodeStream.java +70 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleSubtreeStream.java +86 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleTokenStream.java +76 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/Tree.java +127 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeAdaptor.java +263 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeFilter.java +135 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeIterator.java +132 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeNodeStream.java +106 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeParser.java +169 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreePatternLexer.java +135 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreePatternParser.java +154 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeRewriter.java +124 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeRuleReturnScope.java +41 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeVisitor.java +69 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeVisitorAction.java +47 -0
- data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeWizard.java +531 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/ClearAppData.java +22 -7
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/InstrumentationBackend.java +4 -4
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/Result.java +0 -1
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/HttpServer.java +161 -129
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/activity/SetActivityOrientation.java +5 -7
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/view/GetViewProperty.java +1 -7
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/Query.java +70 -61
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/UIQuery.tokens +12 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ViewMapper.java +63 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/antlr/UIQuery.tokens +10 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/antlr/UIQueryLexer.java +945 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/antlr/UIQueryParser.java +463 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/InvalidUIQueryException.java +10 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryAST.java +8 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryASTClassName.java +115 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryASTWith.java +157 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryDirection.java +5 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryEvaluator.java +205 -0
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryUtils.java +135 -0
- data/test-server/instrumentation-backend/tests/sh/calaba/instrumentationbackend/query/tests/UIQueryTest.java +134 -0
- metadata +106 -3
- data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/activity/SetOrientation.java +0 -39
@@ -0,0 +1,189 @@
|
|
1
|
+
/*
|
2
|
+
[The "BSD license"]
|
3
|
+
Copyright (c) 2005-2009 Terence Parr
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions
|
8
|
+
are met:
|
9
|
+
1. Redistributions of source code must retain the above copyright
|
10
|
+
notice, this list of conditions and the following disclaimer.
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer in the
|
13
|
+
documentation and/or other materials provided with the distribution.
|
14
|
+
3. The name of the author may not be used to endorse or promote products
|
15
|
+
derived from this software without specific prior written permission.
|
16
|
+
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
18
|
+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
19
|
+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
20
|
+
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
22
|
+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
23
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
24
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
26
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
*/
|
28
|
+
package org.antlr.runtime.misc;
|
29
|
+
|
30
|
+
import java.io.*;
|
31
|
+
import java.util.List;
|
32
|
+
|
33
|
+
/** Stats routines needed by profiler etc...
|
34
|
+
|
35
|
+
// note that these routines return 0.0 if no values exist in the X[]
|
36
|
+
// which is not "correct", but it is useful so I don't generate NaN
|
37
|
+
// in my output
|
38
|
+
|
39
|
+
*/
|
40
|
+
public class Stats {
|
41
|
+
public static final String ANTLRWORKS_DIR = "antlrworks";
|
42
|
+
|
43
|
+
/** Compute the sample (unbiased estimator) standard deviation following:
|
44
|
+
*
|
45
|
+
* Computing Deviations: Standard Accuracy
|
46
|
+
* Tony F. Chan and John Gregg Lewis
|
47
|
+
* Stanford University
|
48
|
+
* Communications of ACM September 1979 of Volume 22 the ACM Number 9
|
49
|
+
*
|
50
|
+
* The "two-pass" method from the paper; supposed to have better
|
51
|
+
* numerical properties than the textbook summation/sqrt. To me
|
52
|
+
* this looks like the textbook method, but I ain't no numerical
|
53
|
+
* methods guy.
|
54
|
+
*/
|
55
|
+
public static double stddev(int[] X) {
|
56
|
+
int m = X.length;
|
57
|
+
if ( m<=1 ) {
|
58
|
+
return 0;
|
59
|
+
}
|
60
|
+
double xbar = avg(X);
|
61
|
+
double s2 = 0.0;
|
62
|
+
for (int i=0; i<m; i++){
|
63
|
+
s2 += (X[i] - xbar)*(X[i] - xbar);
|
64
|
+
}
|
65
|
+
s2 = s2/(m-1);
|
66
|
+
return Math.sqrt(s2);
|
67
|
+
}
|
68
|
+
|
69
|
+
/** Compute the sample mean */
|
70
|
+
public static double avg(int[] X) {
|
71
|
+
double xbar = 0.0;
|
72
|
+
int m = X.length;
|
73
|
+
if ( m==0 ) {
|
74
|
+
return 0;
|
75
|
+
}
|
76
|
+
for (int i=0; i<m; i++){
|
77
|
+
xbar += X[i];
|
78
|
+
}
|
79
|
+
if ( xbar>=0.0 ) {
|
80
|
+
return xbar / m;
|
81
|
+
}
|
82
|
+
return 0.0;
|
83
|
+
}
|
84
|
+
|
85
|
+
public static int min(int[] X) {
|
86
|
+
int min = Integer.MAX_VALUE;
|
87
|
+
int m = X.length;
|
88
|
+
if ( m==0 ) {
|
89
|
+
return 0;
|
90
|
+
}
|
91
|
+
for (int i=0; i<m; i++){
|
92
|
+
if ( X[i] < min ) {
|
93
|
+
min = X[i];
|
94
|
+
}
|
95
|
+
}
|
96
|
+
return min;
|
97
|
+
}
|
98
|
+
|
99
|
+
public static int max(int[] X) {
|
100
|
+
int max = Integer.MIN_VALUE;
|
101
|
+
int m = X.length;
|
102
|
+
if ( m==0 ) {
|
103
|
+
return 0;
|
104
|
+
}
|
105
|
+
for (int i=0; i<m; i++){
|
106
|
+
if ( X[i] > max ) {
|
107
|
+
max = X[i];
|
108
|
+
}
|
109
|
+
}
|
110
|
+
return max;
|
111
|
+
}
|
112
|
+
|
113
|
+
/** Compute the sample mean */
|
114
|
+
public static double avg(List<Integer> X) {
|
115
|
+
double xbar = 0.0;
|
116
|
+
int m = X.size();
|
117
|
+
if ( m==0 ) {
|
118
|
+
return 0;
|
119
|
+
}
|
120
|
+
for (int i=0; i<m; i++){
|
121
|
+
xbar += X.get(i);
|
122
|
+
}
|
123
|
+
if ( xbar>=0.0 ) {
|
124
|
+
return xbar / m;
|
125
|
+
}
|
126
|
+
return 0.0;
|
127
|
+
}
|
128
|
+
|
129
|
+
public static int min(List<Integer> X) {
|
130
|
+
int min = Integer.MAX_VALUE;
|
131
|
+
int m = X.size();
|
132
|
+
if ( m==0 ) {
|
133
|
+
return 0;
|
134
|
+
}
|
135
|
+
for (int i=0; i<m; i++){
|
136
|
+
if ( X.get(i) < min ) {
|
137
|
+
min = X.get(i);
|
138
|
+
}
|
139
|
+
}
|
140
|
+
return min;
|
141
|
+
}
|
142
|
+
|
143
|
+
public static int max(List<Integer> X) {
|
144
|
+
int max = Integer.MIN_VALUE;
|
145
|
+
int m = X.size();
|
146
|
+
if ( m==0 ) {
|
147
|
+
return 0;
|
148
|
+
}
|
149
|
+
for (int i=0; i<m; i++){
|
150
|
+
if ( X.get(i) > max ) {
|
151
|
+
max = X.get(i);
|
152
|
+
}
|
153
|
+
}
|
154
|
+
return max;
|
155
|
+
}
|
156
|
+
|
157
|
+
public static int sum(int[] X) {
|
158
|
+
int s = 0;
|
159
|
+
int m = X.length;
|
160
|
+
if ( m==0 ) {
|
161
|
+
return 0;
|
162
|
+
}
|
163
|
+
for (int i=0; i<m; i++){
|
164
|
+
s += X[i];
|
165
|
+
}
|
166
|
+
return s;
|
167
|
+
}
|
168
|
+
|
169
|
+
public static void writeReport(String filename, String data) throws IOException {
|
170
|
+
String absoluteFilename = getAbsoluteFileName(filename);
|
171
|
+
File f = new File(absoluteFilename);
|
172
|
+
File parent = f.getParentFile();
|
173
|
+
parent.mkdirs(); // ensure parent dir exists
|
174
|
+
// write file
|
175
|
+
FileOutputStream fos = new FileOutputStream(f, true); // append
|
176
|
+
BufferedOutputStream bos = new BufferedOutputStream(fos);
|
177
|
+
PrintStream ps = new PrintStream(bos);
|
178
|
+
ps.println(data);
|
179
|
+
ps.close();
|
180
|
+
bos.close();
|
181
|
+
fos.close();
|
182
|
+
}
|
183
|
+
|
184
|
+
public static String getAbsoluteFileName(String filename) {
|
185
|
+
return System.getProperty("user.home")+File.separator+
|
186
|
+
ANTLRWORKS_DIR +File.separator+
|
187
|
+
filename;
|
188
|
+
}
|
189
|
+
}
|
@@ -0,0 +1,349 @@
|
|
1
|
+
/*
|
2
|
+
[The "BSD license"]
|
3
|
+
Copyright (c) 2005-2009 Terence Parr
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions
|
8
|
+
are met:
|
9
|
+
1. Redistributions of source code must retain the above copyright
|
10
|
+
notice, this list of conditions and the following disclaimer.
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer in the
|
13
|
+
documentation and/or other materials provided with the distribution.
|
14
|
+
3. The name of the author may not be used to endorse or promote products
|
15
|
+
derived from this software without specific prior written permission.
|
16
|
+
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
18
|
+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
19
|
+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
20
|
+
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
22
|
+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
23
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
24
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
26
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
*/
|
28
|
+
package org.antlr.runtime.tree;
|
29
|
+
|
30
|
+
import java.util.ArrayList;
|
31
|
+
import java.util.List;
|
32
|
+
|
33
|
+
/** A generic tree implementation with no payload. You must subclass to
|
34
|
+
* actually have any user data. ANTLR v3 uses a list of children approach
|
35
|
+
* instead of the child-sibling approach in v2. A flat tree (a list) is
|
36
|
+
* an empty node whose children represent the list. An empty, but
|
37
|
+
* non-null node is called "nil".
|
38
|
+
*/
|
39
|
+
public abstract class BaseTree implements Tree {
|
40
|
+
protected List children;
|
41
|
+
|
42
|
+
public BaseTree() {
|
43
|
+
}
|
44
|
+
|
45
|
+
/** Create a new node from an existing node does nothing for BaseTree
|
46
|
+
* as there are no fields other than the children list, which cannot
|
47
|
+
* be copied as the children are not considered part of this node.
|
48
|
+
*/
|
49
|
+
public BaseTree(Tree node) {
|
50
|
+
}
|
51
|
+
|
52
|
+
public Tree getChild(int i) {
|
53
|
+
if ( children==null || i>=children.size() ) {
|
54
|
+
return null;
|
55
|
+
}
|
56
|
+
return (Tree)children.get(i);
|
57
|
+
}
|
58
|
+
|
59
|
+
/** Get the children internal List; note that if you directly mess with
|
60
|
+
* the list, do so at your own risk.
|
61
|
+
*/
|
62
|
+
public List getChildren() {
|
63
|
+
return children;
|
64
|
+
}
|
65
|
+
|
66
|
+
public Tree getFirstChildWithType(int type) {
|
67
|
+
for (int i = 0; children!=null && i < children.size(); i++) {
|
68
|
+
Tree t = (Tree) children.get(i);
|
69
|
+
if ( t.getType()==type ) {
|
70
|
+
return t;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
return null;
|
74
|
+
}
|
75
|
+
|
76
|
+
public int getChildCount() {
|
77
|
+
if ( children==null ) {
|
78
|
+
return 0;
|
79
|
+
}
|
80
|
+
return children.size();
|
81
|
+
}
|
82
|
+
|
83
|
+
/** Add t as child of this node.
|
84
|
+
*
|
85
|
+
* Warning: if t has no children, but child does
|
86
|
+
* and child isNil then this routine moves children to t via
|
87
|
+
* t.children = child.children; i.e., without copying the array.
|
88
|
+
*/
|
89
|
+
public void addChild(Tree t) {
|
90
|
+
//System.out.println("add child "+t.toStringTree()+" "+this.toStringTree());
|
91
|
+
//System.out.println("existing children: "+children);
|
92
|
+
if ( t==null ) {
|
93
|
+
return; // do nothing upon addChild(null)
|
94
|
+
}
|
95
|
+
BaseTree childTree = (BaseTree)t;
|
96
|
+
if ( childTree.isNil() ) { // t is an empty node possibly with children
|
97
|
+
if ( this.children!=null && this.children == childTree.children ) {
|
98
|
+
throw new RuntimeException("attempt to add child list to itself");
|
99
|
+
}
|
100
|
+
// just add all of childTree's children to this
|
101
|
+
if ( childTree.children!=null ) {
|
102
|
+
if ( this.children!=null ) { // must copy, this has children already
|
103
|
+
int n = childTree.children.size();
|
104
|
+
for (int i = 0; i < n; i++) {
|
105
|
+
Tree c = (Tree)childTree.children.get(i);
|
106
|
+
this.children.add(c);
|
107
|
+
// handle double-link stuff for each child of nil root
|
108
|
+
c.setParent(this);
|
109
|
+
c.setChildIndex(children.size()-1);
|
110
|
+
}
|
111
|
+
}
|
112
|
+
else {
|
113
|
+
// no children for this but t has children; just set pointer
|
114
|
+
// call general freshener routine
|
115
|
+
this.children = childTree.children;
|
116
|
+
this.freshenParentAndChildIndexes();
|
117
|
+
}
|
118
|
+
}
|
119
|
+
}
|
120
|
+
else { // child is not nil (don't care about children)
|
121
|
+
if ( children==null ) {
|
122
|
+
children = createChildrenList(); // create children list on demand
|
123
|
+
}
|
124
|
+
children.add(t);
|
125
|
+
childTree.setParent(this);
|
126
|
+
childTree.setChildIndex(children.size()-1);
|
127
|
+
}
|
128
|
+
// System.out.println("now children are: "+children);
|
129
|
+
}
|
130
|
+
|
131
|
+
/** Add all elements of kids list as children of this node */
|
132
|
+
public void addChildren(List kids) {
|
133
|
+
for (int i = 0; i < kids.size(); i++) {
|
134
|
+
Tree t = (Tree) kids.get(i);
|
135
|
+
addChild(t);
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
public void setChild(int i, Tree t) {
|
140
|
+
if ( t==null ) {
|
141
|
+
return;
|
142
|
+
}
|
143
|
+
if ( t.isNil() ) {
|
144
|
+
throw new IllegalArgumentException("Can't set single child to a list");
|
145
|
+
}
|
146
|
+
if ( children==null ) {
|
147
|
+
children = createChildrenList();
|
148
|
+
}
|
149
|
+
children.set(i, t);
|
150
|
+
t.setParent(this);
|
151
|
+
t.setChildIndex(i);
|
152
|
+
}
|
153
|
+
|
154
|
+
public Object deleteChild(int i) {
|
155
|
+
if ( children==null ) {
|
156
|
+
return null;
|
157
|
+
}
|
158
|
+
Tree killed = (Tree)children.remove(i);
|
159
|
+
// walk rest and decrement their child indexes
|
160
|
+
this.freshenParentAndChildIndexes(i);
|
161
|
+
return killed;
|
162
|
+
}
|
163
|
+
|
164
|
+
/** Delete children from start to stop and replace with t even if t is
|
165
|
+
* a list (nil-root tree). num of children can increase or decrease.
|
166
|
+
* For huge child lists, inserting children can force walking rest of
|
167
|
+
* children to set their childindex; could be slow.
|
168
|
+
*/
|
169
|
+
public void replaceChildren(int startChildIndex, int stopChildIndex, Object t) {
|
170
|
+
/*
|
171
|
+
System.out.println("replaceChildren "+startChildIndex+", "+stopChildIndex+
|
172
|
+
" with "+((BaseTree)t).toStringTree());
|
173
|
+
System.out.println("in="+toStringTree());
|
174
|
+
*/
|
175
|
+
if ( children==null ) {
|
176
|
+
throw new IllegalArgumentException("indexes invalid; no children in list");
|
177
|
+
}
|
178
|
+
int replacingHowMany = stopChildIndex - startChildIndex + 1;
|
179
|
+
int replacingWithHowMany;
|
180
|
+
BaseTree newTree = (BaseTree)t;
|
181
|
+
List newChildren = null;
|
182
|
+
// normalize to a list of children to add: newChildren
|
183
|
+
if ( newTree.isNil() ) {
|
184
|
+
newChildren = newTree.children;
|
185
|
+
}
|
186
|
+
else {
|
187
|
+
newChildren = new ArrayList(1);
|
188
|
+
newChildren.add(newTree);
|
189
|
+
}
|
190
|
+
replacingWithHowMany = newChildren.size();
|
191
|
+
int numNewChildren = newChildren.size();
|
192
|
+
int delta = replacingHowMany - replacingWithHowMany;
|
193
|
+
// if same number of nodes, do direct replace
|
194
|
+
if ( delta == 0 ) {
|
195
|
+
int j = 0; // index into new children
|
196
|
+
for (int i=startChildIndex; i<=stopChildIndex; i++) {
|
197
|
+
BaseTree child = (BaseTree)newChildren.get(j);
|
198
|
+
children.set(i, child);
|
199
|
+
child.setParent(this);
|
200
|
+
child.setChildIndex(i);
|
201
|
+
j++;
|
202
|
+
}
|
203
|
+
}
|
204
|
+
else if ( delta > 0 ) { // fewer new nodes than there were
|
205
|
+
// set children and then delete extra
|
206
|
+
for (int j=0; j<numNewChildren; j++) {
|
207
|
+
children.set(startChildIndex+j, newChildren.get(j));
|
208
|
+
}
|
209
|
+
int indexToDelete = startChildIndex+numNewChildren;
|
210
|
+
for (int c=indexToDelete; c<=stopChildIndex; c++) {
|
211
|
+
// delete same index, shifting everybody down each time
|
212
|
+
children.remove(indexToDelete);
|
213
|
+
}
|
214
|
+
freshenParentAndChildIndexes(startChildIndex);
|
215
|
+
}
|
216
|
+
else { // more new nodes than were there before
|
217
|
+
// fill in as many children as we can (replacingHowMany) w/o moving data
|
218
|
+
for (int j=0; j<replacingHowMany; j++) {
|
219
|
+
children.set(startChildIndex+j, newChildren.get(j));
|
220
|
+
}
|
221
|
+
int numToInsert = replacingWithHowMany-replacingHowMany;
|
222
|
+
for (int j=replacingHowMany; j<replacingWithHowMany; j++) {
|
223
|
+
children.add(startChildIndex+j, newChildren.get(j));
|
224
|
+
}
|
225
|
+
freshenParentAndChildIndexes(startChildIndex);
|
226
|
+
}
|
227
|
+
//System.out.println("out="+toStringTree());
|
228
|
+
}
|
229
|
+
|
230
|
+
/** Override in a subclass to change the impl of children list */
|
231
|
+
protected List createChildrenList() {
|
232
|
+
return new ArrayList();
|
233
|
+
}
|
234
|
+
|
235
|
+
public boolean isNil() {
|
236
|
+
return false;
|
237
|
+
}
|
238
|
+
|
239
|
+
/** Set the parent and child index values for all child of t */
|
240
|
+
public void freshenParentAndChildIndexes() {
|
241
|
+
freshenParentAndChildIndexes(0);
|
242
|
+
}
|
243
|
+
|
244
|
+
public void freshenParentAndChildIndexes(int offset) {
|
245
|
+
int n = getChildCount();
|
246
|
+
for (int c = offset; c < n; c++) {
|
247
|
+
Tree child = (Tree)getChild(c);
|
248
|
+
child.setChildIndex(c);
|
249
|
+
child.setParent(this);
|
250
|
+
}
|
251
|
+
}
|
252
|
+
|
253
|
+
public void sanityCheckParentAndChildIndexes() {
|
254
|
+
sanityCheckParentAndChildIndexes(null, -1);
|
255
|
+
}
|
256
|
+
|
257
|
+
public void sanityCheckParentAndChildIndexes(Tree parent, int i) {
|
258
|
+
if ( parent!=this.getParent() ) {
|
259
|
+
throw new IllegalStateException("parents don't match; expected "+parent+" found "+this.getParent());
|
260
|
+
}
|
261
|
+
if ( i!=this.getChildIndex() ) {
|
262
|
+
throw new IllegalStateException("child indexes don't match; expected "+i+" found "+this.getChildIndex());
|
263
|
+
}
|
264
|
+
int n = this.getChildCount();
|
265
|
+
for (int c = 0; c < n; c++) {
|
266
|
+
CommonTree child = (CommonTree)this.getChild(c);
|
267
|
+
child.sanityCheckParentAndChildIndexes(this, c);
|
268
|
+
}
|
269
|
+
}
|
270
|
+
|
271
|
+
/** BaseTree doesn't track child indexes. */
|
272
|
+
public int getChildIndex() {
|
273
|
+
return 0;
|
274
|
+
}
|
275
|
+
public void setChildIndex(int index) {
|
276
|
+
}
|
277
|
+
|
278
|
+
/** BaseTree doesn't track parent pointers. */
|
279
|
+
public Tree getParent() {
|
280
|
+
return null;
|
281
|
+
}
|
282
|
+
|
283
|
+
public void setParent(Tree t) {
|
284
|
+
}
|
285
|
+
|
286
|
+
/** Walk upwards looking for ancestor with this token type. */
|
287
|
+
public boolean hasAncestor(int ttype) { return getAncestor(ttype)!=null; }
|
288
|
+
|
289
|
+
/** Walk upwards and get first ancestor with this token type. */
|
290
|
+
public Tree getAncestor(int ttype) {
|
291
|
+
Tree t = this;
|
292
|
+
t = t.getParent();
|
293
|
+
while ( t!=null ) {
|
294
|
+
if ( t.getType()==ttype ) return t;
|
295
|
+
t = t.getParent();
|
296
|
+
}
|
297
|
+
return null;
|
298
|
+
}
|
299
|
+
|
300
|
+
/** Return a list of all ancestors of this node. The first node of
|
301
|
+
* list is the root and the last is the parent of this node.
|
302
|
+
*/
|
303
|
+
public List getAncestors() {
|
304
|
+
if ( getParent()==null ) return null;
|
305
|
+
List ancestors = new ArrayList();
|
306
|
+
Tree t = this;
|
307
|
+
t = t.getParent();
|
308
|
+
while ( t!=null ) {
|
309
|
+
ancestors.add(0, t); // insert at start
|
310
|
+
t = t.getParent();
|
311
|
+
}
|
312
|
+
return ancestors;
|
313
|
+
}
|
314
|
+
|
315
|
+
/** Print out a whole tree not just a node */
|
316
|
+
public String toStringTree() {
|
317
|
+
if ( children==null || children.size()==0 ) {
|
318
|
+
return this.toString();
|
319
|
+
}
|
320
|
+
StringBuffer buf = new StringBuffer();
|
321
|
+
if ( !isNil() ) {
|
322
|
+
buf.append("(");
|
323
|
+
buf.append(this.toString());
|
324
|
+
buf.append(' ');
|
325
|
+
}
|
326
|
+
for (int i = 0; children!=null && i < children.size(); i++) {
|
327
|
+
Tree t = (Tree)children.get(i);
|
328
|
+
if ( i>0 ) {
|
329
|
+
buf.append(' ');
|
330
|
+
}
|
331
|
+
buf.append(t.toStringTree());
|
332
|
+
}
|
333
|
+
if ( !isNil() ) {
|
334
|
+
buf.append(")");
|
335
|
+
}
|
336
|
+
return buf.toString();
|
337
|
+
}
|
338
|
+
|
339
|
+
public int getLine() {
|
340
|
+
return 0;
|
341
|
+
}
|
342
|
+
|
343
|
+
public int getCharPositionInLine() {
|
344
|
+
return 0;
|
345
|
+
}
|
346
|
+
|
347
|
+
/** Override to say how a node (not a tree) should look as text */
|
348
|
+
public abstract String toString();
|
349
|
+
}
|