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.
Files changed (121) hide show
  1. data/bin/calabash-android-build.rb +6 -0
  2. data/bin/calabash-android-setup.rb +2 -3
  3. data/lib/calabash-android/canned_steps.md +1 -1
  4. data/lib/calabash-android/helpers.rb +35 -2
  5. data/lib/calabash-android/lib/TestServer.apk +0 -0
  6. data/lib/calabash-android/operations.rb +13 -6
  7. data/lib/calabash-android/version.rb +1 -1
  8. data/test-server/instrumentation-backend/.classpath +2 -1
  9. data/test-server/instrumentation-backend/antlr.sh +2 -0
  10. data/test-server/instrumentation-backend/antlr/UIQuery.g +70 -0
  11. data/test-server/instrumentation-backend/antlr/UIQuery.tokens +12 -0
  12. data/test-server/instrumentation-backend/build-libs/antlr-3.4-complete.jar +0 -0
  13. data/test-server/instrumentation-backend/build-libs/junit.jar +0 -0
  14. data/test-server/instrumentation-backend/build.xml +56 -0
  15. data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRFileStream.java +78 -0
  16. data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRInputStream.java +70 -0
  17. data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRReaderStream.java +95 -0
  18. data/test-server/instrumentation-backend/src/org/antlr/runtime/ANTLRStringStream.java +230 -0
  19. data/test-server/instrumentation-backend/src/org/antlr/runtime/BaseRecognizer.java +894 -0
  20. data/test-server/instrumentation-backend/src/org/antlr/runtime/BitSet.java +325 -0
  21. data/test-server/instrumentation-backend/src/org/antlr/runtime/BufferedTokenStream.java +272 -0
  22. data/test-server/instrumentation-backend/src/org/antlr/runtime/CharStream.java +57 -0
  23. data/test-server/instrumentation-backend/src/org/antlr/runtime/CharStreamState.java +45 -0
  24. data/test-server/instrumentation-backend/src/org/antlr/runtime/ClassicToken.java +141 -0
  25. data/test-server/instrumentation-backend/src/org/antlr/runtime/CommonToken.java +191 -0
  26. data/test-server/instrumentation-backend/src/org/antlr/runtime/CommonTokenStream.java +153 -0
  27. data/test-server/instrumentation-backend/src/org/antlr/runtime/DFA.java +250 -0
  28. data/test-server/instrumentation-backend/src/org/antlr/runtime/EarlyExitException.java +41 -0
  29. data/test-server/instrumentation-backend/src/org/antlr/runtime/FailedPredicateException.java +54 -0
  30. data/test-server/instrumentation-backend/src/org/antlr/runtime/IntStream.java +122 -0
  31. data/test-server/instrumentation-backend/src/org/antlr/runtime/LegacyCommonTokenStream.java +394 -0
  32. data/test-server/instrumentation-backend/src/org/antlr/runtime/Lexer.java +340 -0
  33. data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedNotSetException.java +41 -0
  34. data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedRangeException.java +45 -0
  35. data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedSetException.java +44 -0
  36. data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedTokenException.java +45 -0
  37. data/test-server/instrumentation-backend/src/org/antlr/runtime/MismatchedTreeNodeException.java +49 -0
  38. data/test-server/instrumentation-backend/src/org/antlr/runtime/MissingTokenException.java +56 -0
  39. data/test-server/instrumentation-backend/src/org/antlr/runtime/NoViableAltException.java +57 -0
  40. data/test-server/instrumentation-backend/src/org/antlr/runtime/Parser.java +98 -0
  41. data/test-server/instrumentation-backend/src/org/antlr/runtime/ParserRuleReturnScope.java +52 -0
  42. data/test-server/instrumentation-backend/src/org/antlr/runtime/RecognitionException.java +180 -0
  43. data/test-server/instrumentation-backend/src/org/antlr/runtime/RecognizerSharedState.java +144 -0
  44. data/test-server/instrumentation-backend/src/org/antlr/runtime/RuleReturnScope.java +42 -0
  45. data/test-server/instrumentation-backend/src/org/antlr/runtime/SerializedGrammar.java +204 -0
  46. data/test-server/instrumentation-backend/src/org/antlr/runtime/Token.java +92 -0
  47. data/test-server/instrumentation-backend/src/org/antlr/runtime/TokenRewriteStream.java +569 -0
  48. data/test-server/instrumentation-backend/src/org/antlr/runtime/TokenSource.java +54 -0
  49. data/test-server/instrumentation-backend/src/org/antlr/runtime/TokenStream.java +75 -0
  50. data/test-server/instrumentation-backend/src/org/antlr/runtime/UnbufferedTokenStream.java +82 -0
  51. data/test-server/instrumentation-backend/src/org/antlr/runtime/UnwantedTokenException.java +53 -0
  52. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/BlankDebugEventListener.java +77 -0
  53. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventHub.java +292 -0
  54. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventListener.java +323 -0
  55. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventRepeater.java +88 -0
  56. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugEventSocketProxy.java +358 -0
  57. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugParser.java +101 -0
  58. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTokenStream.java +156 -0
  59. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTreeAdaptor.java +250 -0
  60. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTreeNodeStream.java +155 -0
  61. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/DebugTreeParser.java +112 -0
  62. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/ParseTreeBuilder.java +109 -0
  63. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/Profiler.java +772 -0
  64. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/RemoteDebugEventSocketListener.java +541 -0
  65. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/TraceDebugEventListener.java +108 -0
  66. data/test-server/instrumentation-backend/src/org/antlr/runtime/debug/Tracer.java +69 -0
  67. data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/DoubleKeyMap.java +62 -0
  68. data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/FastQueue.java +100 -0
  69. data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/IntArray.java +87 -0
  70. data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/LookaheadStream.java +161 -0
  71. data/test-server/instrumentation-backend/src/org/antlr/runtime/misc/Stats.java +189 -0
  72. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/BaseTree.java +349 -0
  73. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/BaseTreeAdaptor.java +279 -0
  74. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/BufferedTreeNodeStream.java +489 -0
  75. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonErrorNode.java +108 -0
  76. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonTree.java +185 -0
  77. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonTreeAdaptor.java +168 -0
  78. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/CommonTreeNodeStream.java +171 -0
  79. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/ParseTree.java +119 -0
  80. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteCardinalityException.java +47 -0
  81. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteEarlyExitException.java +39 -0
  82. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteEmptyStreamException.java +35 -0
  83. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleElementStream.java +210 -0
  84. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleNodeStream.java +70 -0
  85. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleSubtreeStream.java +86 -0
  86. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/RewriteRuleTokenStream.java +76 -0
  87. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/Tree.java +127 -0
  88. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeAdaptor.java +263 -0
  89. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeFilter.java +135 -0
  90. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeIterator.java +132 -0
  91. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeNodeStream.java +106 -0
  92. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeParser.java +169 -0
  93. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreePatternLexer.java +135 -0
  94. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreePatternParser.java +154 -0
  95. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeRewriter.java +124 -0
  96. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeRuleReturnScope.java +41 -0
  97. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeVisitor.java +69 -0
  98. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeVisitorAction.java +47 -0
  99. data/test-server/instrumentation-backend/src/org/antlr/runtime/tree/TreeWizard.java +531 -0
  100. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/ClearAppData.java +22 -7
  101. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/InstrumentationBackend.java +4 -4
  102. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/Result.java +0 -1
  103. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/HttpServer.java +161 -129
  104. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/activity/SetActivityOrientation.java +5 -7
  105. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/view/GetViewProperty.java +1 -7
  106. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/Query.java +70 -61
  107. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/UIQuery.tokens +12 -0
  108. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ViewMapper.java +63 -0
  109. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/antlr/UIQuery.tokens +10 -0
  110. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/antlr/UIQueryLexer.java +945 -0
  111. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/antlr/UIQueryParser.java +463 -0
  112. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/InvalidUIQueryException.java +10 -0
  113. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryAST.java +8 -0
  114. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryASTClassName.java +115 -0
  115. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryASTWith.java +157 -0
  116. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryDirection.java +5 -0
  117. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryEvaluator.java +205 -0
  118. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/query/ast/UIQueryUtils.java +135 -0
  119. data/test-server/instrumentation-backend/tests/sh/calaba/instrumentationbackend/query/tests/UIQueryTest.java +134 -0
  120. metadata +106 -3
  121. data/test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/actions/activity/SetOrientation.java +0 -39
@@ -0,0 +1,279 @@
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 org.antlr.runtime.Token;
31
+ import org.antlr.runtime.TokenStream;
32
+ import org.antlr.runtime.RecognitionException;
33
+
34
+ import java.util.HashMap;
35
+ import java.util.Map;
36
+
37
+ /** A TreeAdaptor that works with any Tree implementation. */
38
+ public abstract class BaseTreeAdaptor implements TreeAdaptor {
39
+ /** System.identityHashCode() is not always unique; we have to
40
+ * track ourselves. That's ok, it's only for debugging, though it's
41
+ * expensive: we have to create a hashtable with all tree nodes in it.
42
+ */
43
+ protected Map treeToUniqueIDMap;
44
+ protected int uniqueNodeID = 1;
45
+
46
+ public Object nil() {
47
+ return create(null);
48
+ }
49
+
50
+ /** create tree node that holds the start and stop tokens associated
51
+ * with an error.
52
+ *
53
+ * If you specify your own kind of tree nodes, you will likely have to
54
+ * override this method. CommonTree returns Token.INVALID_TOKEN_TYPE
55
+ * if no token payload but you might have to set token type for diff
56
+ * node type.
57
+ *
58
+ * You don't have to subclass CommonErrorNode; you will likely need to
59
+ * subclass your own tree node class to avoid class cast exception.
60
+ */
61
+ public Object errorNode(TokenStream input, Token start, Token stop,
62
+ RecognitionException e)
63
+ {
64
+ CommonErrorNode t = new CommonErrorNode(input, start, stop, e);
65
+ //System.out.println("returning error node '"+t+"' @index="+input.index());
66
+ return t;
67
+ }
68
+
69
+ public boolean isNil(Object tree) {
70
+ return ((Tree)tree).isNil();
71
+ }
72
+
73
+ public Object dupTree(Object tree) {
74
+ return dupTree(tree, null);
75
+ }
76
+
77
+ /** This is generic in the sense that it will work with any kind of
78
+ * tree (not just Tree interface). It invokes the adaptor routines
79
+ * not the tree node routines to do the construction.
80
+ */
81
+ public Object dupTree(Object t, Object parent) {
82
+ if ( t==null ) {
83
+ return null;
84
+ }
85
+ Object newTree = dupNode(t);
86
+ // ensure new subtree root has parent/child index set
87
+ setChildIndex(newTree, getChildIndex(t)); // same index in new tree
88
+ setParent(newTree, parent);
89
+ int n = getChildCount(t);
90
+ for (int i = 0; i < n; i++) {
91
+ Object child = getChild(t, i);
92
+ Object newSubTree = dupTree(child, t);
93
+ addChild(newTree, newSubTree);
94
+ }
95
+ return newTree;
96
+ }
97
+
98
+ /** Add a child to the tree t. If child is a flat tree (a list), make all
99
+ * in list children of t. Warning: if t has no children, but child does
100
+ * and child isNil then you can decide it is ok to move children to t via
101
+ * t.children = child.children; i.e., without copying the array. Just
102
+ * make sure that this is consistent with have the user will build
103
+ * ASTs.
104
+ */
105
+ public void addChild(Object t, Object child) {
106
+ if ( t!=null && child!=null ) {
107
+ ((Tree)t).addChild((Tree)child);
108
+ }
109
+ }
110
+
111
+ /** If oldRoot is a nil root, just copy or move the children to newRoot.
112
+ * If not a nil root, make oldRoot a child of newRoot.
113
+ *
114
+ * old=^(nil a b c), new=r yields ^(r a b c)
115
+ * old=^(a b c), new=r yields ^(r ^(a b c))
116
+ *
117
+ * If newRoot is a nil-rooted single child tree, use the single
118
+ * child as the new root node.
119
+ *
120
+ * old=^(nil a b c), new=^(nil r) yields ^(r a b c)
121
+ * old=^(a b c), new=^(nil r) yields ^(r ^(a b c))
122
+ *
123
+ * If oldRoot was null, it's ok, just return newRoot (even if isNil).
124
+ *
125
+ * old=null, new=r yields r
126
+ * old=null, new=^(nil r) yields ^(nil r)
127
+ *
128
+ * Return newRoot. Throw an exception if newRoot is not a
129
+ * simple node or nil root with a single child node--it must be a root
130
+ * node. If newRoot is ^(nil x) return x as newRoot.
131
+ *
132
+ * Be advised that it's ok for newRoot to point at oldRoot's
133
+ * children; i.e., you don't have to copy the list. We are
134
+ * constructing these nodes so we should have this control for
135
+ * efficiency.
136
+ */
137
+ public Object becomeRoot(Object newRoot, Object oldRoot) {
138
+ //System.out.println("becomeroot new "+newRoot.toString()+" old "+oldRoot);
139
+ Tree newRootTree = (Tree)newRoot;
140
+ Tree oldRootTree = (Tree)oldRoot;
141
+ if ( oldRoot==null ) {
142
+ return newRoot;
143
+ }
144
+ // handle ^(nil real-node)
145
+ if ( newRootTree.isNil() ) {
146
+ int nc = newRootTree.getChildCount();
147
+ if ( nc==1 ) newRootTree = (Tree)newRootTree.getChild(0);
148
+ else if ( nc >1 ) {
149
+ // TODO: make tree run time exceptions hierarchy
150
+ throw new RuntimeException("more than one node as root (TODO: make exception hierarchy)");
151
+ }
152
+ }
153
+ // add oldRoot to newRoot; addChild takes care of case where oldRoot
154
+ // is a flat list (i.e., nil-rooted tree). All children of oldRoot
155
+ // are added to newRoot.
156
+ newRootTree.addChild(oldRootTree);
157
+ return newRootTree;
158
+ }
159
+
160
+ /** Transform ^(nil x) to x and nil to null */
161
+ public Object rulePostProcessing(Object root) {
162
+ //System.out.println("rulePostProcessing: "+((Tree)root).toStringTree());
163
+ Tree r = (Tree)root;
164
+ if ( r!=null && r.isNil() ) {
165
+ if ( r.getChildCount()==0 ) {
166
+ r = null;
167
+ }
168
+ else if ( r.getChildCount()==1 ) {
169
+ r = (Tree)r.getChild(0);
170
+ // whoever invokes rule will set parent and child index
171
+ r.setParent(null);
172
+ r.setChildIndex(-1);
173
+ }
174
+ }
175
+ return r;
176
+ }
177
+
178
+ public Object becomeRoot(Token newRoot, Object oldRoot) {
179
+ return becomeRoot(create(newRoot), oldRoot);
180
+ }
181
+
182
+ public Object create(int tokenType, Token fromToken) {
183
+ fromToken = createToken(fromToken);
184
+ //((ClassicToken)fromToken).setType(tokenType);
185
+ fromToken.setType(tokenType);
186
+ Tree t = (Tree)create(fromToken);
187
+ return t;
188
+ }
189
+
190
+ public Object create(int tokenType, Token fromToken, String text) {
191
+ if (fromToken == null) return create(tokenType, text);
192
+ fromToken = createToken(fromToken);
193
+ fromToken.setType(tokenType);
194
+ fromToken.setText(text);
195
+ Tree t = (Tree)create(fromToken);
196
+ return t;
197
+ }
198
+
199
+ public Object create(int tokenType, String text) {
200
+ Token fromToken = createToken(tokenType, text);
201
+ Tree t = (Tree)create(fromToken);
202
+ return t;
203
+ }
204
+
205
+ public int getType(Object t) {
206
+ return ((Tree)t).getType();
207
+ }
208
+
209
+ public void setType(Object t, int type) {
210
+ throw new NoSuchMethodError("don't know enough about Tree node");
211
+ }
212
+
213
+ public String getText(Object t) {
214
+ return ((Tree)t).getText();
215
+ }
216
+
217
+ public void setText(Object t, String text) {
218
+ throw new NoSuchMethodError("don't know enough about Tree node");
219
+ }
220
+
221
+ public Object getChild(Object t, int i) {
222
+ return ((Tree)t).getChild(i);
223
+ }
224
+
225
+ public void setChild(Object t, int i, Object child) {
226
+ ((Tree)t).setChild(i, (Tree)child);
227
+ }
228
+
229
+ public Object deleteChild(Object t, int i) {
230
+ return ((Tree)t).deleteChild(i);
231
+ }
232
+
233
+ public int getChildCount(Object t) {
234
+ return ((Tree)t).getChildCount();
235
+ }
236
+
237
+ public int getUniqueID(Object node) {
238
+ if ( treeToUniqueIDMap==null ) {
239
+ treeToUniqueIDMap = new HashMap();
240
+ }
241
+ Integer prevID = (Integer)treeToUniqueIDMap.get(node);
242
+ if ( prevID!=null ) {
243
+ return prevID.intValue();
244
+ }
245
+ int ID = uniqueNodeID;
246
+ treeToUniqueIDMap.put(node, new Integer(ID));
247
+ uniqueNodeID++;
248
+ return ID;
249
+ // GC makes these nonunique:
250
+ // return System.identityHashCode(node);
251
+ }
252
+
253
+ /** Tell me how to create a token for use with imaginary token nodes.
254
+ * For example, there is probably no input symbol associated with imaginary
255
+ * token DECL, but you need to create it as a payload or whatever for
256
+ * the DECL node as in ^(DECL type ID).
257
+ *
258
+ * If you care what the token payload objects' type is, you should
259
+ * override this method and any other createToken variant.
260
+ */
261
+ public abstract Token createToken(int tokenType, String text);
262
+
263
+ /** Tell me how to create a token for use with imaginary token nodes.
264
+ * For example, there is probably no input symbol associated with imaginary
265
+ * token DECL, but you need to create it as a payload or whatever for
266
+ * the DECL node as in ^(DECL type ID).
267
+ *
268
+ * This is a variant of createToken where the new token is derived from
269
+ * an actual real input token. Typically this is for converting '{'
270
+ * tokens to BLOCK etc... You'll see
271
+ *
272
+ * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ;
273
+ *
274
+ * If you care what the token payload objects' type is, you should
275
+ * override this method and any other createToken variant.
276
+ */
277
+ public abstract Token createToken(Token fromToken);
278
+ }
279
+
@@ -0,0 +1,489 @@
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 org.antlr.runtime.Token;
31
+ import org.antlr.runtime.TokenStream;
32
+ import org.antlr.runtime.misc.IntArray;
33
+ import java.util.*;
34
+
35
+ /** A buffered stream of tree nodes. Nodes can be from a tree of ANY kind.
36
+ *
37
+ * This node stream sucks all nodes out of the tree specified in
38
+ * the constructor during construction and makes pointers into
39
+ * the tree using an array of Object pointers. The stream necessarily
40
+ * includes pointers to DOWN and UP and EOF nodes.
41
+ *
42
+ * This stream knows how to mark/release for backtracking.
43
+ *
44
+ * This stream is most suitable for tree interpreters that need to
45
+ * jump around a lot or for tree parsers requiring speed (at cost of memory).
46
+ * There is some duplicated functionality here with UnBufferedTreeNodeStream
47
+ * but just in bookkeeping, not tree walking etc...
48
+ *
49
+ * TARGET DEVELOPERS:
50
+ *
51
+ * This is the old CommonTreeNodeStream that buffered up entire node stream.
52
+ * No need to implement really as new CommonTreeNodeStream is much better
53
+ * and covers what we need.
54
+ *
55
+ * @see CommonTreeNodeStream
56
+ */
57
+ public class BufferedTreeNodeStream implements TreeNodeStream {
58
+ public static final int DEFAULT_INITIAL_BUFFER_SIZE = 100;
59
+ public static final int INITIAL_CALL_STACK_SIZE = 10;
60
+
61
+ private static final String TAG = "ANTLR:TREE:BufferedTreeNodeStream";
62
+
63
+ protected class StreamIterator implements Iterator {
64
+ int i = 0;
65
+ public boolean hasNext() {
66
+ return i<nodes.size();
67
+ }
68
+
69
+ public Object next() {
70
+ int current = i;
71
+ i++;
72
+ if ( current < nodes.size() ) {
73
+ return nodes.get(current);
74
+ }
75
+ return eof;
76
+ }
77
+
78
+ public void remove() {
79
+ throw new RuntimeException("cannot remove nodes from stream");
80
+ }
81
+ }
82
+
83
+ // all these navigation nodes are shared and hence they
84
+ // cannot contain any line/column info
85
+
86
+ protected Object down;
87
+ protected Object up;
88
+ protected Object eof;
89
+
90
+ /** The complete mapping from stream index to tree node.
91
+ * This buffer includes pointers to DOWN, UP, and EOF nodes.
92
+ * It is built upon ctor invocation. The elements are type
93
+ * Object as we don't what the trees look like.
94
+ *
95
+ * Load upon first need of the buffer so we can set token types
96
+ * of interest for reverseIndexing. Slows us down a wee bit to
97
+ * do all of the if p==-1 testing everywhere though.
98
+ */
99
+ protected List nodes;
100
+
101
+ /** Pull nodes from which tree? */
102
+ protected Object root;
103
+
104
+ /** IF this tree (root) was created from a token stream, track it. */
105
+ protected TokenStream tokens;
106
+
107
+ /** What tree adaptor was used to build these trees */
108
+ TreeAdaptor adaptor;
109
+
110
+ /** Reuse same DOWN, UP navigation nodes unless this is true */
111
+ protected boolean uniqueNavigationNodes = false;
112
+
113
+ /** The index into the nodes list of the current node (next node
114
+ * to consume). If -1, nodes array not filled yet.
115
+ */
116
+ protected int p = -1;
117
+
118
+ /** Track the last mark() call result value for use in rewind(). */
119
+ protected int lastMarker;
120
+
121
+ /** Stack of indexes used for push/pop calls */
122
+ protected IntArray calls;
123
+
124
+ public BufferedTreeNodeStream(Object tree) {
125
+ this(new CommonTreeAdaptor(), tree);
126
+ }
127
+
128
+ public BufferedTreeNodeStream(TreeAdaptor adaptor, Object tree) {
129
+ this(adaptor, tree, DEFAULT_INITIAL_BUFFER_SIZE);
130
+ }
131
+
132
+ public BufferedTreeNodeStream(TreeAdaptor adaptor, Object tree, int initialBufferSize) {
133
+ this.root = tree;
134
+ this.adaptor = adaptor;
135
+ nodes = new ArrayList(initialBufferSize);
136
+ down = adaptor.create(Token.DOWN, "DOWN");
137
+ up = adaptor.create(Token.UP, "UP");
138
+ eof = adaptor.create(Token.EOF, "EOF");
139
+ }
140
+
141
+ /** Walk tree with depth-first-search and fill nodes buffer.
142
+ * Don't do DOWN, UP nodes if its a list (t is isNil).
143
+ */
144
+ protected void fillBuffer() {
145
+ fillBuffer(root);
146
+ //System.out.println("revIndex="+tokenTypeToStreamIndexesMap);
147
+ p = 0; // buffer of nodes intialized now
148
+ }
149
+
150
+ public void fillBuffer(Object t) {
151
+ boolean nil = adaptor.isNil(t);
152
+ if ( !nil ) {
153
+ nodes.add(t); // add this node
154
+ }
155
+ // add DOWN node if t has children
156
+ int n = adaptor.getChildCount(t);
157
+ if ( !nil && n>0 ) {
158
+ addNavigationNode(Token.DOWN);
159
+ }
160
+ // and now add all its children
161
+ for (int c=0; c<n; c++) {
162
+ Object child = adaptor.getChild(t,c);
163
+ fillBuffer(child);
164
+ }
165
+ // add UP node if t has children
166
+ if ( !nil && n>0 ) {
167
+ addNavigationNode(Token.UP);
168
+ }
169
+ }
170
+
171
+ /** What is the stream index for node? 0..n-1
172
+ * Return -1 if node not found.
173
+ */
174
+ protected int getNodeIndex(Object node) {
175
+ if ( p==-1 ) {
176
+ fillBuffer();
177
+ }
178
+ for (int i = 0; i < nodes.size(); i++) {
179
+ Object t = (Object) nodes.get(i);
180
+ if ( t==node ) {
181
+ return i;
182
+ }
183
+ }
184
+ return -1;
185
+ }
186
+
187
+ /** As we flatten the tree, we use UP, DOWN nodes to represent
188
+ * the tree structure. When debugging we need unique nodes
189
+ * so instantiate new ones when uniqueNavigationNodes is true.
190
+ */
191
+ protected void addNavigationNode(final int ttype) {
192
+ Object navNode = null;
193
+ if ( ttype==Token.DOWN ) {
194
+ if ( hasUniqueNavigationNodes() ) {
195
+ navNode = adaptor.create(Token.DOWN, "DOWN");
196
+ }
197
+ else {
198
+ navNode = down;
199
+ }
200
+ }
201
+ else {
202
+ if ( hasUniqueNavigationNodes() ) {
203
+ navNode = adaptor.create(Token.UP, "UP");
204
+ }
205
+ else {
206
+ navNode = up;
207
+ }
208
+ }
209
+ nodes.add(navNode);
210
+ }
211
+
212
+ public Object get(int i) {
213
+ if ( p==-1 ) {
214
+ fillBuffer();
215
+ }
216
+ return nodes.get(i);
217
+ }
218
+
219
+ public Object LT(int k) {
220
+ if ( p==-1 ) {
221
+ fillBuffer();
222
+ }
223
+ if ( k==0 ) {
224
+ return null;
225
+ }
226
+ if ( k<0 ) {
227
+ return LB(-k);
228
+ }
229
+ //System.out.print("LT(p="+p+","+k+")=");
230
+ if ( (p+k-1) >= nodes.size() ) {
231
+ return eof;
232
+ }
233
+ return nodes.get(p+k-1);
234
+ }
235
+
236
+ public Object getCurrentSymbol() { return LT(1); }
237
+
238
+ /*
239
+ public Object getLastTreeNode() {
240
+ int i = index();
241
+ if ( i>=size() ) {
242
+ i--; // if at EOF, have to start one back
243
+ }
244
+ System.out.println("start last node: "+i+" size=="+nodes.size());
245
+ while ( i>=0 &&
246
+ (adaptor.getType(get(i))==Token.EOF ||
247
+ adaptor.getType(get(i))==Token.UP ||
248
+ adaptor.getType(get(i))==Token.DOWN) )
249
+ {
250
+ i--;
251
+ }
252
+ System.out.println("stop at node: "+i+" "+nodes.get(i));
253
+ return nodes.get(i);
254
+ }
255
+ */
256
+
257
+ /** Look backwards k nodes */
258
+ protected Object LB(int k) {
259
+ if ( k==0 ) {
260
+ return null;
261
+ }
262
+ if ( (p-k)<0 ) {
263
+ return null;
264
+ }
265
+ return nodes.get(p-k);
266
+ }
267
+
268
+ public Object getTreeSource() {
269
+ return root;
270
+ }
271
+
272
+ public String getSourceName() {
273
+ return getTokenStream().getSourceName();
274
+ }
275
+
276
+ public TokenStream getTokenStream() {
277
+ return tokens;
278
+ }
279
+
280
+ public void setTokenStream(TokenStream tokens) {
281
+ this.tokens = tokens;
282
+ }
283
+
284
+ public TreeAdaptor getTreeAdaptor() {
285
+ return adaptor;
286
+ }
287
+
288
+ public void setTreeAdaptor(TreeAdaptor adaptor) {
289
+ this.adaptor = adaptor;
290
+ }
291
+
292
+ public boolean hasUniqueNavigationNodes() {
293
+ return uniqueNavigationNodes;
294
+ }
295
+
296
+ public void setUniqueNavigationNodes(boolean uniqueNavigationNodes) {
297
+ this.uniqueNavigationNodes = uniqueNavigationNodes;
298
+ }
299
+
300
+ public void consume() {
301
+ if ( p==-1 ) {
302
+ fillBuffer();
303
+ }
304
+ p++;
305
+ }
306
+
307
+ public int LA(int i) {
308
+ return adaptor.getType(LT(i));
309
+ }
310
+
311
+ public int mark() {
312
+ if ( p==-1 ) {
313
+ fillBuffer();
314
+ }
315
+ lastMarker = index();
316
+ return lastMarker;
317
+ }
318
+
319
+ public void release(int marker) {
320
+ // no resources to release
321
+ }
322
+
323
+ public int index() {
324
+ return p;
325
+ }
326
+
327
+ public void rewind(int marker) {
328
+ seek(marker);
329
+ }
330
+
331
+ public void rewind() {
332
+ seek(lastMarker);
333
+ }
334
+
335
+ public void seek(int index) {
336
+ if ( p==-1 ) {
337
+ fillBuffer();
338
+ }
339
+ p = index;
340
+ }
341
+
342
+ /** Make stream jump to a new location, saving old location.
343
+ * Switch back with pop().
344
+ */
345
+ public void push(int index) {
346
+ if ( calls==null ) {
347
+ calls = new IntArray();
348
+ }
349
+ calls.push(p); // save current index
350
+ seek(index);
351
+ }
352
+
353
+ /** Seek back to previous index saved during last push() call.
354
+ * Return top of stack (return index).
355
+ */
356
+ public int pop() {
357
+ int ret = calls.pop();
358
+ seek(ret);
359
+ return ret;
360
+ }
361
+
362
+ public void reset() {
363
+ p = 0;
364
+ lastMarker = 0;
365
+ if (calls != null) {
366
+ calls.clear();
367
+ }
368
+ }
369
+
370
+ public int size() {
371
+ if ( p==-1 ) {
372
+ fillBuffer();
373
+ }
374
+ return nodes.size();
375
+ }
376
+
377
+ public Iterator iterator() {
378
+ if ( p==-1 ) {
379
+ fillBuffer();
380
+ }
381
+ return new StreamIterator();
382
+ }
383
+
384
+ // TREE REWRITE INTERFACE
385
+
386
+ public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t) {
387
+ if ( parent!=null ) {
388
+ adaptor.replaceChildren(parent, startChildIndex, stopChildIndex, t);
389
+ }
390
+ }
391
+
392
+ /** Used for testing, just return the token type stream */
393
+ public String toTokenTypeString() {
394
+ if ( p==-1 ) {
395
+ fillBuffer();
396
+ }
397
+ StringBuffer buf = new StringBuffer();
398
+ for (int i = 0; i < nodes.size(); i++) {
399
+ Object t = (Object) nodes.get(i);
400
+ buf.append(" ");
401
+ buf.append(adaptor.getType(t));
402
+ }
403
+ return buf.toString();
404
+ }
405
+
406
+ /** Debugging */
407
+ public String toTokenString(int start, int stop) {
408
+ if ( p==-1 ) {
409
+ fillBuffer();
410
+ }
411
+ StringBuffer buf = new StringBuffer();
412
+ for (int i = start; i < nodes.size() && i <= stop; i++) {
413
+ Object t = (Object) nodes.get(i);
414
+ buf.append(" ");
415
+ buf.append(adaptor.getToken(t));
416
+ }
417
+ return buf.toString();
418
+ }
419
+
420
+ public String toString(Object start, Object stop) {
421
+ //System.out.println("toString");
422
+ android.util.Log.i(TAG, "toString");
423
+ if ( start==null || stop==null ) {
424
+ return null;
425
+ }
426
+ if ( p==-1 ) {
427
+ fillBuffer();
428
+ }
429
+ //System.out.println("stop: "+stop);
430
+ if ( start instanceof CommonTree ) {
431
+ //System.out.print("toString: "+((CommonTree)start).getToken()+", ");
432
+ android.util.Log.i(TAG, "toString: "+((CommonTree)start).getToken()+", ");
433
+ }
434
+ else {
435
+ //System.out.println(start);
436
+ android.util.Log.i(TAG, "" + start);
437
+ }
438
+ if ( stop instanceof CommonTree ) {
439
+ //System.out.println(((CommonTree)stop).getToken());
440
+ android.util.Log.i(TAG, "" + ((CommonTree)stop).getToken());
441
+ }
442
+ else {
443
+ //System.out.println(stop);
444
+ android.util.Log.i(TAG, "" + stop);
445
+ }
446
+ // if we have the token stream, use that to dump text in order
447
+ if ( tokens!=null ) {
448
+ int beginTokenIndex = adaptor.getTokenStartIndex(start);
449
+ int endTokenIndex = adaptor.getTokenStopIndex(stop);
450
+ // if it's a tree, use start/stop index from start node
451
+ // else use token range from start/stop nodes
452
+ if ( adaptor.getType(stop)==Token.UP ) {
453
+ endTokenIndex = adaptor.getTokenStopIndex(start);
454
+ }
455
+ else if ( adaptor.getType(stop)==Token.EOF ) {
456
+ endTokenIndex = size()-2; // don't use EOF
457
+ }
458
+ return tokens.toString(beginTokenIndex, endTokenIndex);
459
+ }
460
+ // walk nodes looking for start
461
+ Object t = null;
462
+ int i = 0;
463
+ for (; i < nodes.size(); i++) {
464
+ t = nodes.get(i);
465
+ if ( t==start ) {
466
+ break;
467
+ }
468
+ }
469
+ // now walk until we see stop, filling string buffer with text
470
+ StringBuffer buf = new StringBuffer();
471
+ t = nodes.get(i);
472
+ while ( t!=stop ) {
473
+ String text = adaptor.getText(t);
474
+ if ( text==null ) {
475
+ text = " "+String.valueOf(adaptor.getType(t));
476
+ }
477
+ buf.append(text);
478
+ i++;
479
+ t = nodes.get(i);
480
+ }
481
+ // include stop node too
482
+ String text = adaptor.getText(stop);
483
+ if ( text==null ) {
484
+ text = " "+String.valueOf(adaptor.getType(stop));
485
+ }
486
+ buf.append(text);
487
+ return buf.toString();
488
+ }
489
+ }