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,108 @@
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.*;
31
+
32
+ /** A node representing erroneous token range in token stream */
33
+ public class CommonErrorNode extends CommonTree {
34
+ public IntStream input;
35
+ public Token start;
36
+ public Token stop;
37
+ public RecognitionException trappedException;
38
+
39
+ public CommonErrorNode(TokenStream input, Token start, Token stop,
40
+ RecognitionException e)
41
+ {
42
+ //System.out.println("start: "+start+", stop: "+stop);
43
+ if ( stop==null ||
44
+ (stop.getTokenIndex() < start.getTokenIndex() &&
45
+ stop.getType()!=Token.EOF) )
46
+ {
47
+ // sometimes resync does not consume a token (when LT(1) is
48
+ // in follow set. So, stop will be 1 to left to start. adjust.
49
+ // Also handle case where start is the first token and no token
50
+ // is consumed during recovery; LT(-1) will return null.
51
+ stop = start;
52
+ }
53
+ this.input = input;
54
+ this.start = start;
55
+ this.stop = stop;
56
+ this.trappedException = e;
57
+ }
58
+
59
+ public boolean isNil() {
60
+ return false;
61
+ }
62
+
63
+ public int getType() {
64
+ return Token.INVALID_TOKEN_TYPE;
65
+ }
66
+
67
+ public String getText() {
68
+ String badText = null;
69
+ if ( start instanceof Token ) {
70
+ int i = ((Token)start).getTokenIndex();
71
+ int j = ((Token)stop).getTokenIndex();
72
+ if ( ((Token)stop).getType() == Token.EOF ) {
73
+ j = ((TokenStream)input).size();
74
+ }
75
+ badText = ((TokenStream)input).toString(i, j);
76
+ }
77
+ else if ( start instanceof Tree ) {
78
+ badText = ((TreeNodeStream)input).toString(start, stop);
79
+ }
80
+ else {
81
+ // people should subclass if they alter the tree type so this
82
+ // next one is for sure correct.
83
+ badText = "<unknown>";
84
+ }
85
+ return badText;
86
+ }
87
+
88
+ public String toString() {
89
+ if ( trappedException instanceof MissingTokenException ) {
90
+ return "<missing type: "+
91
+ ((MissingTokenException)trappedException).getMissingType()+
92
+ ">";
93
+ }
94
+ else if ( trappedException instanceof UnwantedTokenException ) {
95
+ return "<extraneous: "+
96
+ ((UnwantedTokenException)trappedException).getUnexpectedToken()+
97
+ ", resync="+getText()+">";
98
+ }
99
+ else if ( trappedException instanceof MismatchedTokenException ) {
100
+ return "<mismatched token: "+trappedException.token+", resync="+getText()+">";
101
+ }
102
+ else if ( trappedException instanceof NoViableAltException ) {
103
+ return "<unexpected: "+trappedException.token+
104
+ ", resync="+getText()+">";
105
+ }
106
+ return "<error: "+getText()+">";
107
+ }
108
+ }
@@ -0,0 +1,185 @@
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
+
32
+ /** A tree node that is wrapper for a Token object. After 3.0 release
33
+ * while building tree rewrite stuff, it became clear that computing
34
+ * parent and child index is very difficult and cumbersome. Better to
35
+ * spend the space in every tree node. If you don't want these extra
36
+ * fields, it's easy to cut them out in your own BaseTree subclass.
37
+ */
38
+ public class CommonTree extends BaseTree {
39
+ /** A single token is the payload */
40
+ public Token token;
41
+
42
+ /** What token indexes bracket all tokens associated with this node
43
+ * and below?
44
+ */
45
+ protected int startIndex=-1, stopIndex=-1;
46
+
47
+ /** Who is the parent node of this node; if null, implies node is root */
48
+ public CommonTree parent;
49
+
50
+ /** What index is this node in the child list? Range: 0..n-1 */
51
+ public int childIndex = -1;
52
+
53
+ public CommonTree() { }
54
+
55
+ public CommonTree(CommonTree node) {
56
+ super(node);
57
+ this.token = node.token;
58
+ this.startIndex = node.startIndex;
59
+ this.stopIndex = node.stopIndex;
60
+ }
61
+
62
+ public CommonTree(Token t) {
63
+ this.token = t;
64
+ }
65
+
66
+ public Token getToken() {
67
+ return token;
68
+ }
69
+
70
+ public Tree dupNode() {
71
+ return new CommonTree(this);
72
+ }
73
+
74
+ public boolean isNil() {
75
+ return token==null;
76
+ }
77
+
78
+ public int getType() {
79
+ if ( token==null ) {
80
+ return Token.INVALID_TOKEN_TYPE;
81
+ }
82
+ return token.getType();
83
+ }
84
+
85
+ public String getText() {
86
+ if ( token==null ) {
87
+ return null;
88
+ }
89
+ return token.getText();
90
+ }
91
+
92
+ public int getLine() {
93
+ if ( token==null || token.getLine()==0 ) {
94
+ if ( getChildCount()>0 ) {
95
+ return getChild(0).getLine();
96
+ }
97
+ return 0;
98
+ }
99
+ return token.getLine();
100
+ }
101
+
102
+ public int getCharPositionInLine() {
103
+ if ( token==null || token.getCharPositionInLine()==-1 ) {
104
+ if ( getChildCount()>0 ) {
105
+ return getChild(0).getCharPositionInLine();
106
+ }
107
+ return 0;
108
+ }
109
+ return token.getCharPositionInLine();
110
+ }
111
+
112
+ public int getTokenStartIndex() {
113
+ if ( startIndex==-1 && token!=null ) {
114
+ return token.getTokenIndex();
115
+ }
116
+ return startIndex;
117
+ }
118
+
119
+ public void setTokenStartIndex(int index) {
120
+ startIndex = index;
121
+ }
122
+
123
+ public int getTokenStopIndex() {
124
+ if ( stopIndex==-1 && token!=null ) {
125
+ return token.getTokenIndex();
126
+ }
127
+ return stopIndex;
128
+ }
129
+
130
+ public void setTokenStopIndex(int index) {
131
+ stopIndex = index;
132
+ }
133
+
134
+ /** For every node in this subtree, make sure it's start/stop token's
135
+ * are set. Walk depth first, visit bottom up. Only updates nodes
136
+ * with at least one token index < 0.
137
+ */
138
+ public void setUnknownTokenBoundaries() {
139
+ if ( children==null ) {
140
+ if ( startIndex<0 || stopIndex<0 ) {
141
+ startIndex = stopIndex = token.getTokenIndex();
142
+ }
143
+ return;
144
+ }
145
+ for (int i=0; i<children.size(); i++) {
146
+ ((CommonTree)children.get(i)).setUnknownTokenBoundaries();
147
+ }
148
+ if ( startIndex>=0 && stopIndex>=0 ) return; // already set
149
+ if ( children.size() > 0 ) {
150
+ CommonTree firstChild = (CommonTree)children.get(0);
151
+ CommonTree lastChild = (CommonTree)children.get(children.size()-1);
152
+ startIndex = firstChild.getTokenStartIndex();
153
+ stopIndex = lastChild.getTokenStopIndex();
154
+ }
155
+ }
156
+
157
+ public int getChildIndex() {
158
+ return childIndex;
159
+ }
160
+
161
+ public Tree getParent() {
162
+ return parent;
163
+ }
164
+
165
+ public void setParent(Tree t) {
166
+ this.parent = (CommonTree)t;
167
+ }
168
+
169
+ public void setChildIndex(int index) {
170
+ this.childIndex = index;
171
+ }
172
+
173
+ public String toString() {
174
+ if ( isNil() ) {
175
+ return "nil";
176
+ }
177
+ if ( getType()==Token.INVALID_TOKEN_TYPE ) {
178
+ return "<errornode>";
179
+ }
180
+ if ( token==null ) {
181
+ return null;
182
+ }
183
+ return token.getText();
184
+ }
185
+ }
@@ -0,0 +1,168 @@
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.CommonToken;
31
+ import org.antlr.runtime.Token;
32
+
33
+ /** A TreeAdaptor that works with any Tree implementation. It provides
34
+ * really just factory methods; all the work is done by BaseTreeAdaptor.
35
+ * If you would like to have different tokens created than ClassicToken
36
+ * objects, you need to override this and then set the parser tree adaptor to
37
+ * use your subclass.
38
+ *
39
+ * To get your parser to build nodes of a different type, override
40
+ * create(Token), errorNode(), and to be safe, YourTreeClass.dupNode().
41
+ * dupNode is called to duplicate nodes during rewrite operations.
42
+ */
43
+ public class CommonTreeAdaptor extends BaseTreeAdaptor {
44
+ /** Duplicate a node. This is part of the factory;
45
+ * override if you want another kind of node to be built.
46
+ *
47
+ * I could use reflection to prevent having to override this
48
+ * but reflection is slow.
49
+ */
50
+ public Object dupNode(Object t) {
51
+ if ( t==null ) return null;
52
+ return ((Tree)t).dupNode();
53
+ }
54
+
55
+ public Object create(Token payload) {
56
+ return new CommonTree(payload);
57
+ }
58
+
59
+ /** Tell me how to create a token for use with imaginary token nodes.
60
+ * For example, there is probably no input symbol associated with imaginary
61
+ * token DECL, but you need to create it as a payload or whatever for
62
+ * the DECL node as in ^(DECL type ID).
63
+ *
64
+ * If you care what the token payload objects' type is, you should
65
+ * override this method and any other createToken variant.
66
+ */
67
+ public Token createToken(int tokenType, String text) {
68
+ return new CommonToken(tokenType, text);
69
+ }
70
+
71
+ /** Tell me how to create a token for use with imaginary token nodes.
72
+ * For example, there is probably no input symbol associated with imaginary
73
+ * token DECL, but you need to create it as a payload or whatever for
74
+ * the DECL node as in ^(DECL type ID).
75
+ *
76
+ * This is a variant of createToken where the new token is derived from
77
+ * an actual real input token. Typically this is for converting '{'
78
+ * tokens to BLOCK etc... You'll see
79
+ *
80
+ * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ;
81
+ *
82
+ * If you care what the token payload objects' type is, you should
83
+ * override this method and any other createToken variant.
84
+ */
85
+ public Token createToken(Token fromToken) {
86
+ return new CommonToken(fromToken);
87
+ }
88
+
89
+ /** Track start/stop token for subtree root created for a rule.
90
+ * Only works with Tree nodes. For rules that match nothing,
91
+ * seems like this will yield start=i and stop=i-1 in a nil node.
92
+ * Might be useful info so I'll not force to be i..i.
93
+ */
94
+ public void setTokenBoundaries(Object t, Token startToken, Token stopToken) {
95
+ if ( t==null ) return;
96
+ int start = 0;
97
+ int stop = 0;
98
+ if ( startToken!=null ) start = startToken.getTokenIndex();
99
+ if ( stopToken!=null ) stop = stopToken.getTokenIndex();
100
+ ((Tree)t).setTokenStartIndex(start);
101
+ ((Tree)t).setTokenStopIndex(stop);
102
+ }
103
+
104
+ public int getTokenStartIndex(Object t) {
105
+ if ( t==null ) return -1;
106
+ return ((Tree)t).getTokenStartIndex();
107
+ }
108
+
109
+ public int getTokenStopIndex(Object t) {
110
+ if ( t==null ) return -1;
111
+ return ((Tree)t).getTokenStopIndex();
112
+ }
113
+
114
+ public String getText(Object t) {
115
+ if ( t==null ) return null;
116
+ return ((Tree)t).getText();
117
+ }
118
+
119
+ public int getType(Object t) {
120
+ if ( t==null ) return Token.INVALID_TOKEN_TYPE;
121
+ return ((Tree)t).getType();
122
+ }
123
+
124
+ /** What is the Token associated with this node? If
125
+ * you are not using CommonTree, then you must
126
+ * override this in your own adaptor.
127
+ */
128
+ public Token getToken(Object t) {
129
+ if ( t instanceof CommonTree ) {
130
+ return ((CommonTree)t).getToken();
131
+ }
132
+ return null; // no idea what to do
133
+ }
134
+
135
+ public Object getChild(Object t, int i) {
136
+ if ( t==null ) return null;
137
+ return ((Tree)t).getChild(i);
138
+ }
139
+
140
+ public int getChildCount(Object t) {
141
+ if ( t==null ) return 0;
142
+ return ((Tree)t).getChildCount();
143
+ }
144
+
145
+ public Object getParent(Object t) {
146
+ if ( t==null ) return null;
147
+ return ((Tree)t).getParent();
148
+ }
149
+
150
+ public void setParent(Object t, Object parent) {
151
+ if ( t!=null ) ((Tree)t).setParent((Tree)parent);
152
+ }
153
+
154
+ public int getChildIndex(Object t) {
155
+ if ( t==null ) return 0;
156
+ return ((Tree)t).getChildIndex();
157
+ }
158
+
159
+ public void setChildIndex(Object t, int index) {
160
+ if ( t!=null ) ((Tree)t).setChildIndex(index);
161
+ }
162
+
163
+ public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t) {
164
+ if ( parent!=null ) {
165
+ ((Tree)parent).replaceChildren(startChildIndex, stopChildIndex, t);
166
+ }
167
+ }
168
+ }