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,171 @@
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.LookaheadStream;
33
+ import org.antlr.runtime.misc.IntArray;
34
+
35
+ import java.util.*;
36
+
37
+ public class CommonTreeNodeStream extends LookaheadStream<Object> implements TreeNodeStream {
38
+ public static final int DEFAULT_INITIAL_BUFFER_SIZE = 100;
39
+ public static final int INITIAL_CALL_STACK_SIZE = 10;
40
+
41
+ /** Pull nodes from which tree? */
42
+ protected Object root;
43
+
44
+ /** If this tree (root) was created from a token stream, track it. */
45
+ protected TokenStream tokens;
46
+
47
+ /** What tree adaptor was used to build these trees */
48
+ TreeAdaptor adaptor;
49
+
50
+ /** The tree iterator we using */
51
+ protected TreeIterator it;
52
+
53
+ /** Stack of indexes used for push/pop calls */
54
+ protected IntArray calls;
55
+
56
+ /** Tree (nil A B C) trees like flat A B C streams */
57
+ protected boolean hasNilRoot = false;
58
+
59
+ /** Tracks tree depth. Level=0 means we're at root node level. */
60
+ protected int level = 0;
61
+
62
+ public CommonTreeNodeStream(Object tree) {
63
+ this(new CommonTreeAdaptor(), tree);
64
+ }
65
+
66
+ public CommonTreeNodeStream(TreeAdaptor adaptor, Object tree) {
67
+ this.root = tree;
68
+ this.adaptor = adaptor;
69
+ it = new TreeIterator(adaptor,root);
70
+ }
71
+
72
+ public void reset() {
73
+ super.reset();
74
+ it.reset();
75
+ hasNilRoot = false;
76
+ level = 0;
77
+ if ( calls != null ) calls.clear();
78
+ }
79
+
80
+ /** Pull elements from tree iterator. Track tree level 0..max_level.
81
+ * If nil rooted tree, don't give initial nil and DOWN nor final UP.
82
+ */
83
+ public Object nextElement() {
84
+ Object t = it.next();
85
+ //System.out.println("pulled "+adaptor.getType(t));
86
+ if ( t == it.up ) {
87
+ level--;
88
+ if ( level==0 && hasNilRoot ) return it.next(); // don't give last UP; get EOF
89
+ }
90
+ else if ( t == it.down ) level++;
91
+ if ( level==0 && adaptor.isNil(t) ) { // if nil root, scarf nil, DOWN
92
+ hasNilRoot = true;
93
+ t = it.next(); // t is now DOWN, so get first real node next
94
+ level++;
95
+ t = it.next();
96
+ }
97
+ return t;
98
+ }
99
+
100
+ public boolean isEOF(Object o) { return adaptor.getType(o) == Token.EOF; }
101
+
102
+ public void setUniqueNavigationNodes(boolean uniqueNavigationNodes) { }
103
+
104
+ public Object getTreeSource() { return root; }
105
+
106
+ public String getSourceName() { return getTokenStream().getSourceName(); }
107
+
108
+ public TokenStream getTokenStream() { return tokens; }
109
+
110
+ public void setTokenStream(TokenStream tokens) { this.tokens = tokens; }
111
+
112
+ public TreeAdaptor getTreeAdaptor() { return adaptor; }
113
+
114
+ public void setTreeAdaptor(TreeAdaptor adaptor) { this.adaptor = adaptor; }
115
+
116
+ public Object get(int i) {
117
+ throw new UnsupportedOperationException("Absolute node indexes are meaningless in an unbuffered stream");
118
+ }
119
+
120
+ public int LA(int i) { return adaptor.getType(LT(i)); }
121
+
122
+ /** Make stream jump to a new location, saving old location.
123
+ * Switch back with pop().
124
+ */
125
+ public void push(int index) {
126
+ if ( calls==null ) {
127
+ calls = new IntArray();
128
+ }
129
+ calls.push(p); // save current index
130
+ seek(index);
131
+ }
132
+
133
+ /** Seek back to previous index saved during last push() call.
134
+ * Return top of stack (return index).
135
+ */
136
+ public int pop() {
137
+ int ret = calls.pop();
138
+ seek(ret);
139
+ return ret;
140
+ }
141
+
142
+ // TREE REWRITE INTERFACE
143
+
144
+ public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t) {
145
+ if ( parent!=null ) {
146
+ adaptor.replaceChildren(parent, startChildIndex, stopChildIndex, t);
147
+ }
148
+ }
149
+
150
+ public String toString(Object start, Object stop) {
151
+ // we'll have to walk from start to stop in tree; we're not keeping
152
+ // a complete node stream buffer
153
+ return "n/a";
154
+ }
155
+
156
+ /** For debugging; destructive: moves tree iterator to end. */
157
+ public String toTokenTypeString() {
158
+ reset();
159
+ StringBuffer buf = new StringBuffer();
160
+ Object o = LT(1);
161
+ int type = adaptor.getType(o);
162
+ while ( type!=Token.EOF ) {
163
+ buf.append(" ");
164
+ buf.append(type);
165
+ consume();
166
+ o = LT(1);
167
+ type = adaptor.getType(o);
168
+ }
169
+ return buf.toString();
170
+ }
171
+ }
@@ -0,0 +1,119 @@
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
+ import java.util.List;
33
+
34
+ /** A record of the rules used to match a token sequence. The tokens
35
+ * end up as the leaves of this tree and rule nodes are the interior nodes.
36
+ * This really adds no functionality, it is just an alias for CommonTree
37
+ * that is more meaningful (specific) and holds a String to display for a node.
38
+ */
39
+ public class ParseTree extends BaseTree {
40
+ public Object payload;
41
+ public List hiddenTokens;
42
+
43
+ public ParseTree(Object label) {
44
+ this.payload = label;
45
+ }
46
+
47
+ public Tree dupNode() {
48
+ return null;
49
+ }
50
+
51
+ public int getType() {
52
+ return 0;
53
+ }
54
+
55
+ public String getText() {
56
+ return toString();
57
+ }
58
+
59
+ public int getTokenStartIndex() {
60
+ return 0;
61
+ }
62
+
63
+ public void setTokenStartIndex(int index) {
64
+ }
65
+
66
+ public int getTokenStopIndex() {
67
+ return 0;
68
+ }
69
+
70
+ public void setTokenStopIndex(int index) {
71
+ }
72
+
73
+ public String toString() {
74
+ if ( payload instanceof Token ) {
75
+ Token t = (Token)payload;
76
+ if ( t.getType() == Token.EOF ) {
77
+ return "<EOF>";
78
+ }
79
+ return t.getText();
80
+ }
81
+ return payload.toString();
82
+ }
83
+
84
+ /** Emit a token and all hidden nodes before. EOF node holds all
85
+ * hidden tokens after last real token.
86
+ */
87
+ public String toStringWithHiddenTokens() {
88
+ StringBuffer buf = new StringBuffer();
89
+ if ( hiddenTokens!=null ) {
90
+ for (int i = 0; i < hiddenTokens.size(); i++) {
91
+ Token hidden = (Token) hiddenTokens.get(i);
92
+ buf.append(hidden.getText());
93
+ }
94
+ }
95
+ String nodeText = this.toString();
96
+ if ( !nodeText.equals("<EOF>") ) buf.append(nodeText);
97
+ return buf.toString();
98
+ }
99
+
100
+ /** Print out the leaves of this tree, which means printing original
101
+ * input back out.
102
+ */
103
+ public String toInputString() {
104
+ StringBuffer buf = new StringBuffer();
105
+ _toStringLeaves(buf);
106
+ return buf.toString();
107
+ }
108
+
109
+ public void _toStringLeaves(StringBuffer buf) {
110
+ if ( payload instanceof Token ) { // leaf node token?
111
+ buf.append(this.toStringWithHiddenTokens());
112
+ return;
113
+ }
114
+ for (int i = 0; children!=null && i < children.size(); i++) {
115
+ ParseTree t = (ParseTree)children.get(i);
116
+ t._toStringLeaves(buf);
117
+ }
118
+ }
119
+ }
@@ -0,0 +1,47 @@
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
+ /** Base class for all exceptions thrown during AST rewrite construction.
31
+ * This signifies a case where the cardinality of two or more elements
32
+ * in a subrule are different: (ID INT)+ where |ID|!=|INT|
33
+ */
34
+ public class RewriteCardinalityException extends RuntimeException {
35
+ public String elementDescription;
36
+
37
+ public RewriteCardinalityException(String elementDescription) {
38
+ this.elementDescription = elementDescription;
39
+ }
40
+
41
+ public String getMessage() {
42
+ if ( elementDescription!=null ) {
43
+ return elementDescription;
44
+ }
45
+ return null;
46
+ }
47
+ }
@@ -0,0 +1,39 @@
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
+ /** No elements within a (...)+ in a rewrite rule */
31
+ public class RewriteEarlyExitException extends RewriteCardinalityException {
32
+ public RewriteEarlyExitException() {
33
+ super(null);
34
+ }
35
+ public RewriteEarlyExitException(String elementDescription) {
36
+ super(elementDescription);
37
+ }
38
+
39
+ }
@@ -0,0 +1,35 @@
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
+ /** Ref to ID or expr but no tokens in ID stream or subtrees in expr stream */
31
+ public class RewriteEmptyStreamException extends RewriteCardinalityException {
32
+ public RewriteEmptyStreamException(String elementDescription) {
33
+ super(elementDescription);
34
+ }
35
+ }
@@ -0,0 +1,210 @@
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 list of elements tracked in an alternative to be used in
34
+ * a -> rewrite rule. We need to subclass to fill in the next() method,
35
+ * which returns either an AST node wrapped around a token payload or
36
+ * an existing subtree.
37
+ *
38
+ * Once you start next()ing, do not try to add more elements. It will
39
+ * break the cursor tracking I believe.
40
+ *
41
+ * @see org.antlr.runtime.tree.RewriteRuleSubtreeStream
42
+ * @see org.antlr.runtime.tree.RewriteRuleTokenStream
43
+ *
44
+ * TODO: add mechanism to detect/puke on modification after reading from stream
45
+ */
46
+ public abstract class RewriteRuleElementStream {
47
+ /** Cursor 0..n-1. If singleElement!=null, cursor is 0 until you next(),
48
+ * which bumps it to 1 meaning no more elements.
49
+ */
50
+ protected int cursor = 0;
51
+
52
+ /** Track single elements w/o creating a list. Upon 2nd add, alloc list */
53
+ protected Object singleElement;
54
+
55
+ /** The list of tokens or subtrees we are tracking */
56
+ protected List elements;
57
+
58
+ /** Once a node / subtree has been used in a stream, it must be dup'd
59
+ * from then on. Streams are reset after subrules so that the streams
60
+ * can be reused in future subrules. So, reset must set a dirty bit.
61
+ * If dirty, then next() always returns a dup.
62
+ *
63
+ * I wanted to use "naughty bit" here, but couldn't think of a way
64
+ * to use "naughty".
65
+ *
66
+ * TODO: unused?
67
+ */
68
+ protected boolean dirty = false;
69
+
70
+ /** The element or stream description; usually has name of the token or
71
+ * rule reference that this list tracks. Can include rulename too, but
72
+ * the exception would track that info.
73
+ */
74
+ protected String elementDescription;
75
+ protected TreeAdaptor adaptor;
76
+
77
+ public RewriteRuleElementStream(TreeAdaptor adaptor, String elementDescription) {
78
+ this.elementDescription = elementDescription;
79
+ this.adaptor = adaptor;
80
+ }
81
+
82
+ /** Create a stream with one element */
83
+ public RewriteRuleElementStream(TreeAdaptor adaptor,
84
+ String elementDescription,
85
+ Object oneElement)
86
+ {
87
+ this(adaptor, elementDescription);
88
+ add(oneElement);
89
+ }
90
+
91
+ /** Create a stream, but feed off an existing list */
92
+ public RewriteRuleElementStream(TreeAdaptor adaptor,
93
+ String elementDescription,
94
+ List elements)
95
+ {
96
+ this(adaptor, elementDescription);
97
+ this.singleElement = null;
98
+ this.elements = elements;
99
+ }
100
+
101
+ /** Reset the condition of this stream so that it appears we have
102
+ * not consumed any of its elements. Elements themselves are untouched.
103
+ * Once we reset the stream, any future use will need duplicates. Set
104
+ * the dirty bit.
105
+ */
106
+ public void reset() {
107
+ cursor = 0;
108
+ dirty = true;
109
+ }
110
+
111
+ public void add(Object el) {
112
+ //System.out.println("add '"+elementDescription+"' is "+el);
113
+ if ( el==null ) {
114
+ return;
115
+ }
116
+ if ( elements!=null ) { // if in list, just add
117
+ elements.add(el);
118
+ return;
119
+ }
120
+ if ( singleElement == null ) { // no elements yet, track w/o list
121
+ singleElement = el;
122
+ return;
123
+ }
124
+ // adding 2nd element, move to list
125
+ elements = new ArrayList(5);
126
+ elements.add(singleElement);
127
+ singleElement = null;
128
+ elements.add(el);
129
+ }
130
+
131
+ /** Return the next element in the stream. If out of elements, throw
132
+ * an exception unless size()==1. If size is 1, then return elements[0].
133
+ * Return a duplicate node/subtree if stream is out of elements and
134
+ * size==1. If we've already used the element, dup (dirty bit set).
135
+ */
136
+ public Object nextTree() {
137
+ int n = size();
138
+ if ( dirty || (cursor>=n && n==1) ) {
139
+ // if out of elements and size is 1, dup
140
+ Object el = _next();
141
+ return dup(el);
142
+ }
143
+ // test size above then fetch
144
+ Object el = _next();
145
+ return el;
146
+ }
147
+
148
+ /** do the work of getting the next element, making sure that it's
149
+ * a tree node or subtree. Deal with the optimization of single-
150
+ * element list versus list of size > 1. Throw an exception
151
+ * if the stream is empty or we're out of elements and size>1.
152
+ * protected so you can override in a subclass if necessary.
153
+ */
154
+ protected Object _next() {
155
+ int n = size();
156
+ if ( n ==0 ) {
157
+ throw new RewriteEmptyStreamException(elementDescription);
158
+ }
159
+ if ( cursor>= n) { // out of elements?
160
+ if ( n ==1 ) { // if size is 1, it's ok; return and we'll dup
161
+ return toTree(singleElement);
162
+ }
163
+ // out of elements and size was not 1, so we can't dup
164
+ throw new RewriteCardinalityException(elementDescription);
165
+ }
166
+ // we have elements
167
+ if ( singleElement!=null ) {
168
+ cursor++; // move cursor even for single element list
169
+ return toTree(singleElement);
170
+ }
171
+ // must have more than one in list, pull from elements
172
+ Object o = toTree(elements.get(cursor));
173
+ cursor++;
174
+ return o;
175
+ }
176
+
177
+ /** When constructing trees, sometimes we need to dup a token or AST
178
+ * subtree. Dup'ing a token means just creating another AST node
179
+ * around it. For trees, you must call the adaptor.dupTree() unless
180
+ * the element is for a tree root; then it must be a node dup.
181
+ */
182
+ protected abstract Object dup(Object el);
183
+
184
+ /** Ensure stream emits trees; tokens must be converted to AST nodes.
185
+ * AST nodes can be passed through unmolested.
186
+ */
187
+ protected Object toTree(Object el) {
188
+ return el;
189
+ }
190
+
191
+ public boolean hasNext() {
192
+ return (singleElement != null && cursor < 1) ||
193
+ (elements!=null && cursor < elements.size());
194
+ }
195
+
196
+ public int size() {
197
+ int n = 0;
198
+ if ( singleElement != null ) {
199
+ n = 1;
200
+ }
201
+ if ( elements!=null ) {
202
+ return elements.size();
203
+ }
204
+ return n;
205
+ }
206
+
207
+ public String getDescription() {
208
+ return elementDescription;
209
+ }
210
+ }