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,135 @@
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.RecognizerSharedState;
31
+ import org.antlr.runtime.RecognitionException;
32
+ import org.antlr.runtime.TokenStream;
33
+
34
+ /**
35
+ Cut-n-paste from material I'm not using in the book anymore (edit later
36
+ to make sense):
37
+
38
+ Now, how are we going to test these tree patterns against every
39
+ subtree in our original tree? In what order should we visit nodes?
40
+ For this application, it turns out we need a simple ``apply once''
41
+ rule application strategy and a ``down then up'' tree traversal
42
+ strategy. Let's look at rule application first.
43
+
44
+ As we visit each node, we need to see if any of our patterns match. If
45
+ a pattern matches, we execute the associated tree rewrite and move on
46
+ to the next node. In other words, we only look for a single rule
47
+ application opportunity (we'll see below that we sometimes need to
48
+ repeatedly apply rules). The following method applies a rule in a @cl
49
+ TreeParser (derived from a tree grammar) to a tree:
50
+
51
+ here is where weReferenced code/walking/patterns/TreePatternMatcher.java
52
+
53
+ It uses reflection to lookup the appropriate rule within the generated
54
+ tree parser class (@cl Simplify in this case). Most of the time, the
55
+ rule will not match the tree. To avoid issuing syntax errors and
56
+ attempting error recovery, it bumps up the backtracking level. Upon
57
+ failure, the invoked rule immediately returns. If you don't plan on
58
+ using this technique in your own ANTLR-based application, don't sweat
59
+ the details. This method boils down to ``call a rule to match a tree,
60
+ executing any embedded actions and rewrite rules.''
61
+
62
+ At this point, we know how to define tree grammar rules and how to
63
+ apply them to a particular subtree. The final piece of the tree
64
+ pattern matcher is the actual tree traversal. We have to get the
65
+ correct node visitation order. In particular, we need to perform the
66
+ scalar-vector multiply transformation on the way down (preorder) and
67
+ we need to reduce multiply-by-zero subtrees on the way up (postorder).
68
+
69
+ To implement a top-down visitor, we do a depth first walk of the tree,
70
+ executing an action in the preorder position. To get a bottom-up
71
+ visitor, we execute an action in the postorder position. ANTLR
72
+ provides a standard @cl TreeVisitor class with a depth first search @v
73
+ visit method. That method executes either a @m pre or @m post method
74
+ or both. In our case, we need to call @m applyOnce in both. On the way
75
+ down, we'll look for @r vmult patterns. On the way up,
76
+ we'll look for @r mult0 patterns.
77
+ */
78
+ public class TreeFilter extends TreeParser {
79
+ public interface fptr {
80
+ public void rule() throws RecognitionException;
81
+ }
82
+
83
+ protected TokenStream originalTokenStream;
84
+ protected TreeAdaptor originalAdaptor;
85
+
86
+ public TreeFilter(TreeNodeStream input) {
87
+ this(input, new RecognizerSharedState());
88
+ }
89
+ public TreeFilter(TreeNodeStream input, RecognizerSharedState state) {
90
+ super(input, state);
91
+ originalAdaptor = input.getTreeAdaptor();
92
+ originalTokenStream = input.getTokenStream();
93
+ }
94
+
95
+ public void applyOnce(Object t, fptr whichRule) {
96
+ if ( t==null ) return;
97
+ try {
98
+ // share TreeParser object but not parsing-related state
99
+ state = new RecognizerSharedState();
100
+ input = new CommonTreeNodeStream(originalAdaptor, t);
101
+ ((CommonTreeNodeStream)input).setTokenStream(originalTokenStream);
102
+ setBacktrackingLevel(1);
103
+ whichRule.rule();
104
+ setBacktrackingLevel(0);
105
+ }
106
+ catch (RecognitionException e) { ; }
107
+ }
108
+
109
+ public void downup(Object t) {
110
+ TreeVisitor v = new TreeVisitor(new CommonTreeAdaptor());
111
+ TreeVisitorAction actions = new TreeVisitorAction() {
112
+ public Object pre(Object t) { applyOnce(t, topdown_fptr); return t; }
113
+ public Object post(Object t) { applyOnce(t, bottomup_fptr); return t; }
114
+ };
115
+ v.visit(t, actions);
116
+ }
117
+
118
+ fptr topdown_fptr = new fptr() {
119
+ public void rule() throws RecognitionException {
120
+ topdown();
121
+ }
122
+ };
123
+
124
+ fptr bottomup_fptr = new fptr() {
125
+ public void rule() throws RecognitionException {
126
+ bottomup();
127
+ }
128
+ };
129
+
130
+ // methods the downup strategy uses to do the up and down rules.
131
+ // to override, just define tree grammar rule topdown and turn on
132
+ // filter=true.
133
+ public void topdown() throws RecognitionException {;}
134
+ public void bottomup() throws RecognitionException {;}
135
+ }
@@ -0,0 +1,132 @@
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.CommonToken;
32
+ import org.antlr.runtime.misc.FastQueue;
33
+
34
+ import java.util.Iterator;
35
+
36
+ /** Return a node stream from a doubly-linked tree whose nodes
37
+ * know what child index they are. No remove() is supported.
38
+ *
39
+ * Emit navigation nodes (DOWN, UP, and EOF) to let show tree structure.
40
+ */
41
+ public class TreeIterator implements Iterator {
42
+ protected TreeAdaptor adaptor;
43
+ protected Object root;
44
+ protected Object tree;
45
+ protected boolean firstTime = true;
46
+
47
+ // navigation nodes to return during walk and at end
48
+ public Object up;
49
+ public Object down;
50
+ public Object eof;
51
+
52
+ /** If we emit UP/DOWN nodes, we need to spit out multiple nodes per
53
+ * next() call.
54
+ */
55
+ protected FastQueue nodes;
56
+
57
+ public TreeIterator(Object tree) {
58
+ this(new CommonTreeAdaptor(),tree);
59
+ }
60
+
61
+ public TreeIterator(TreeAdaptor adaptor, Object tree) {
62
+ this.adaptor = adaptor;
63
+ this.tree = tree;
64
+ this.root = tree;
65
+ nodes = new FastQueue();
66
+ down = adaptor.create(Token.DOWN, "DOWN");
67
+ up = adaptor.create(Token.UP, "UP");
68
+ eof = adaptor.create(Token.EOF, "EOF");
69
+ }
70
+
71
+ public void reset() {
72
+ firstTime = true;
73
+ tree = root;
74
+ nodes.clear();
75
+ }
76
+
77
+ public boolean hasNext() {
78
+ if ( firstTime ) return root!=null;
79
+ if ( nodes!=null && nodes.size()>0 ) return true;
80
+ if ( tree==null ) return false;
81
+ if ( adaptor.getChildCount(tree)>0 ) return true;
82
+ return adaptor.getParent(tree)!=null; // back at root?
83
+ }
84
+
85
+ public Object next() {
86
+ if ( firstTime ) { // initial condition
87
+ firstTime = false;
88
+ if ( adaptor.getChildCount(tree)==0 ) { // single node tree (special)
89
+ nodes.add(eof);
90
+ return tree;
91
+ }
92
+ return tree;
93
+ }
94
+ // if any queued up, use those first
95
+ if ( nodes!=null && nodes.size()>0 ) return nodes.remove();
96
+
97
+ // no nodes left?
98
+ if ( tree==null ) return eof;
99
+
100
+ // next node will be child 0 if any children
101
+ if ( adaptor.getChildCount(tree)>0 ) {
102
+ tree = adaptor.getChild(tree, 0);
103
+ nodes.add(tree); // real node is next after DOWN
104
+ return down;
105
+ }
106
+ // if no children, look for next sibling of tree or ancestor
107
+ Object parent = adaptor.getParent(tree);
108
+ // while we're out of siblings, keep popping back up towards root
109
+ while ( parent!=null &&
110
+ adaptor.getChildIndex(tree)+1 >= adaptor.getChildCount(parent) )
111
+ {
112
+ nodes.add(up); // we're moving back up
113
+ tree = parent;
114
+ parent = adaptor.getParent(tree);
115
+ }
116
+ // no nodes left?
117
+ if ( parent==null ) {
118
+ tree = null; // back at root? nothing left then
119
+ nodes.add(eof); // add to queue, might have UP nodes in there
120
+ return nodes.remove();
121
+ }
122
+
123
+ // must have found a node with an unvisited sibling
124
+ // move to it and return it
125
+ int nextSiblingIndex = adaptor.getChildIndex(tree) + 1;
126
+ tree = adaptor.getChild(parent, nextSiblingIndex);
127
+ nodes.add(tree); // add to queue, might have UP nodes in there
128
+ return nodes.remove();
129
+ }
130
+
131
+ public void remove() { throw new UnsupportedOperationException(); }
132
+ }
@@ -0,0 +1,106 @@
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.IntStream;
31
+ import org.antlr.runtime.TokenStream;
32
+
33
+ /** A stream of tree nodes, accessing nodes from a tree of some kind */
34
+ public interface TreeNodeStream extends IntStream {
35
+ /** Get a tree node at an absolute index i; 0..n-1.
36
+ * If you don't want to buffer up nodes, then this method makes no
37
+ * sense for you.
38
+ */
39
+ public Object get(int i);
40
+
41
+ /** Get tree node at current input pointer + i ahead where i=1 is next node.
42
+ * i<0 indicates nodes in the past. So LT(-1) is previous node, but
43
+ * implementations are not required to provide results for k < -1.
44
+ * LT(0) is undefined. For i>=n, return null.
45
+ * Return null for LT(0) and any index that results in an absolute address
46
+ * that is negative.
47
+ *
48
+ * This is analogus to the LT() method of the TokenStream, but this
49
+ * returns a tree node instead of a token. Makes code gen identical
50
+ * for both parser and tree grammars. :)
51
+ */
52
+ public Object LT(int k);
53
+
54
+ /** Where is this stream pulling nodes from? This is not the name, but
55
+ * the object that provides node objects.
56
+ */
57
+ public Object getTreeSource();
58
+
59
+ /** If the tree associated with this stream was created from a TokenStream,
60
+ * you can specify it here. Used to do rule $text attribute in tree
61
+ * parser. Optional unless you use tree parser rule text attribute
62
+ * or output=template and rewrite=true options.
63
+ */
64
+ public TokenStream getTokenStream();
65
+
66
+ /** What adaptor can tell me how to interpret/navigate nodes and
67
+ * trees. E.g., get text of a node.
68
+ */
69
+ public TreeAdaptor getTreeAdaptor();
70
+
71
+ /** As we flatten the tree, we use UP, DOWN nodes to represent
72
+ * the tree structure. When debugging we need unique nodes
73
+ * so we have to instantiate new ones. When doing normal tree
74
+ * parsing, it's slow and a waste of memory to create unique
75
+ * navigation nodes. Default should be false;
76
+ */
77
+ public void setUniqueNavigationNodes(boolean uniqueNavigationNodes);
78
+
79
+ /** Reset the tree node stream in such a way that it acts like
80
+ * a freshly constructed stream.
81
+ */
82
+ public void reset();
83
+
84
+ /** Return the text of all nodes from start to stop, inclusive.
85
+ * If the stream does not buffer all the nodes then it can still
86
+ * walk recursively from start until stop. You can always return
87
+ * null or "" too, but users should not access $ruleLabel.text in
88
+ * an action of course in that case.
89
+ */
90
+ public String toString(Object start, Object stop);
91
+
92
+
93
+ // REWRITING TREES (used by tree parser)
94
+
95
+ /** Replace from start to stop child index of parent with t, which might
96
+ * be a list. Number of children may be different
97
+ * after this call. The stream is notified because it is walking the
98
+ * tree and might need to know you are monkeying with the underlying
99
+ * tree. Also, it might be able to modify the node stream to avoid
100
+ * restreaming for future phases.
101
+ *
102
+ * If parent is null, don't do anything; must be at root of overall tree.
103
+ * Can't replace whatever points to the parent externally. Do nothing.
104
+ */
105
+ public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t);
106
+ }
@@ -0,0 +1,169 @@
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
+ import java.util.regex.Pattern;
33
+ import java.util.regex.Matcher;
34
+
35
+ /** A parser for a stream of tree nodes. "tree grammars" result in a subclass
36
+ * of this. All the error reporting and recovery is shared with Parser via
37
+ * the BaseRecognizer superclass.
38
+ */
39
+ public class TreeParser extends BaseRecognizer {
40
+ public static final int DOWN = Token.DOWN;
41
+ public static final int UP = Token.UP;
42
+
43
+ // precompiled regex used by inContext
44
+ static String dotdot = ".*[^.]\\.\\.[^.].*";
45
+ static String doubleEtc = ".*\\.\\.\\.\\s+\\.\\.\\..*";
46
+ static Pattern dotdotPattern = Pattern.compile(dotdot);
47
+ static Pattern doubleEtcPattern = Pattern.compile(doubleEtc);
48
+
49
+ protected TreeNodeStream input;
50
+
51
+ public TreeParser(TreeNodeStream input) {
52
+ super(); // highlight that we go to super to set state object
53
+ setTreeNodeStream(input);
54
+ }
55
+
56
+ public TreeParser(TreeNodeStream input, RecognizerSharedState state) {
57
+ super(state); // share the state object with another parser
58
+ setTreeNodeStream(input);
59
+ }
60
+
61
+ public void reset() {
62
+ super.reset(); // reset all recognizer state variables
63
+ if ( input!=null ) {
64
+ input.seek(0); // rewind the input
65
+ }
66
+ }
67
+
68
+ /** Set the input stream */
69
+ public void setTreeNodeStream(TreeNodeStream input) {
70
+ this.input = input;
71
+ }
72
+
73
+ public TreeNodeStream getTreeNodeStream() {
74
+ return input;
75
+ }
76
+
77
+ public String getSourceName() {
78
+ return input.getSourceName();
79
+ }
80
+
81
+ protected Object getCurrentInputSymbol(IntStream input) {
82
+ return ((TreeNodeStream)input).LT(1);
83
+ }
84
+
85
+ protected Object getMissingSymbol(IntStream input,
86
+ RecognitionException e,
87
+ int expectedTokenType,
88
+ BitSet follow)
89
+ {
90
+ String tokenText =
91
+ "<missing "+getTokenNames()[expectedTokenType]+">";
92
+ TreeAdaptor adaptor = ((TreeNodeStream)e.input).getTreeAdaptor();
93
+ return adaptor.create(new CommonToken(expectedTokenType, tokenText));
94
+ }
95
+
96
+ /** Match '.' in tree parser has special meaning. Skip node or
97
+ * entire tree if node has children. If children, scan until
98
+ * corresponding UP node.
99
+ */
100
+ public void matchAny(IntStream ignore) { // ignore stream, copy of input
101
+ state.errorRecovery = false;
102
+ state.failed = false;
103
+ Object look = input.LT(1);
104
+ if ( input.getTreeAdaptor().getChildCount(look)==0 ) {
105
+ input.consume(); // not subtree, consume 1 node and return
106
+ return;
107
+ }
108
+ // current node is a subtree, skip to corresponding UP.
109
+ // must count nesting level to get right UP
110
+ int level=0;
111
+ int tokenType = input.getTreeAdaptor().getType(look);
112
+ while ( tokenType!=Token.EOF && !(tokenType==UP && level==0) ) {
113
+ input.consume();
114
+ look = input.LT(1);
115
+ tokenType = input.getTreeAdaptor().getType(look);
116
+ if ( tokenType == DOWN ) {
117
+ level++;
118
+ }
119
+ else if ( tokenType == UP ) {
120
+ level--;
121
+ }
122
+ }
123
+ input.consume(); // consume UP
124
+ }
125
+
126
+ /** We have DOWN/UP nodes in the stream that have no line info; override.
127
+ * plus we want to alter the exception type. Don't try to recover
128
+ * from tree parser errors inline...
129
+ */
130
+ protected Object recoverFromMismatchedToken(IntStream input,
131
+ int ttype,
132
+ BitSet follow)
133
+ throws RecognitionException
134
+ {
135
+ throw new MismatchedTreeNodeException(ttype, (TreeNodeStream)input);
136
+ }
137
+
138
+ /** Prefix error message with the grammar name because message is
139
+ * always intended for the programmer because the parser built
140
+ * the input tree not the user.
141
+ */
142
+ public String getErrorHeader(RecognitionException e) {
143
+ return getGrammarFileName()+": node from "+
144
+ (e.approximateLineInfo?"after ":"")+"line "+e.line+":"+e.charPositionInLine;
145
+ }
146
+
147
+ /** Tree parsers parse nodes they usually have a token object as
148
+ * payload. Set the exception token and do the default behavior.
149
+ */
150
+ public String getErrorMessage(RecognitionException e, String[] tokenNames) {
151
+ if ( this instanceof TreeParser ) {
152
+ TreeAdaptor adaptor = ((TreeNodeStream)e.input).getTreeAdaptor();
153
+ e.token = adaptor.getToken(e.node);
154
+ if ( e.token==null ) { // could be an UP/DOWN node
155
+ e.token = new CommonToken(adaptor.getType(e.node),
156
+ adaptor.getText(e.node));
157
+ }
158
+ }
159
+ return super.getErrorMessage(e, tokenNames);
160
+ }
161
+
162
+ public void traceIn(String ruleName, int ruleIndex) {
163
+ super.traceIn(ruleName, ruleIndex, input.LT(1));
164
+ }
165
+
166
+ public void traceOut(String ruleName, int ruleIndex) {
167
+ super.traceOut(ruleName, ruleIndex, input.LT(1));
168
+ }
169
+ }