diff --git a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java index 05efcdb03..fc64b3afd 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java @@ -29,8 +29,7 @@ package org.antlr.v4.runtime; import org.antlr.v4.runtime.misc.IntervalSet; -import org.antlr.v4.runtime.tree.ASTAdaptor; -import org.antlr.v4.runtime.tree.TreeNodeStream; +import org.antlr.v4.runtime.tree.*; /** The root of the ANTLR exception hierarchy. * @@ -127,7 +126,7 @@ public class RecognitionException extends RuntimeException { this.line = token.getLine(); this.charPositionInLine = token.getCharPositionInLine(); } - if ( input instanceof TreeNodeStream) { + if ( input instanceof ASTNodeStream) { //extractInformationFromTreeNodeStream(input); } else { @@ -186,8 +185,8 @@ public class RecognitionException extends RuntimeException { if ( recognizer.getInputStream() instanceof TokenStream) { return token.getType(); } - else if ( recognizer.getInputStream() instanceof TreeNodeStream) { - TreeNodeStream nodes = (TreeNodeStream)recognizer.getInputStream(); + else if ( recognizer.getInputStream() instanceof ASTNodeStream) { + ASTNodeStream nodes = (ASTNodeStream)recognizer.getInputStream(); ASTAdaptor adaptor = nodes.getTreeAdaptor(); return adaptor.getType(node); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java index 152b0580d..7620028bf 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java @@ -122,8 +122,8 @@ public class Recognizer { msg = "mismatched input "+getTokenErrorDisplay(e.token)+ " expecting "+tokenName; } - else if ( e instanceof MismatchedTreeNodeException ) { - MismatchedTreeNodeException mtne = (MismatchedTreeNodeException)e; + else if ( e instanceof MismatchedASTNodeException) { + MismatchedASTNodeException mtne = (MismatchedASTNodeException)e; String tokenName=""; if ( mtne.expecting.member(Token.EOF) ) { tokenName = "EOF"; diff --git a/runtime/Java/src/org/antlr/v4/runtime/debug/DebugTreeNodeStream.java b/runtime/Java/src/org/antlr/v4/runtime/debug/DebugTreeNodeStream.java index 5f187ffb2..a89ec94d1 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/debug/DebugTreeNodeStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/debug/DebugTreeNodeStream.java @@ -34,16 +34,16 @@ import org.antlr.v4.runtime.tree.*; * and a debug listener. As node stream calls come in, debug events * are triggered. */ -public class DebugTreeNodeStream implements TreeNodeStream { +public class DebugTreeNodeStream implements ASTNodeStream { protected DebugEventListener dbg; protected ASTAdaptor adaptor; - protected TreeNodeStream input; + protected ASTNodeStream input; protected boolean initialStreamState = true; /** Track the last mark() call result value for use in rewind(). */ protected int lastMarker; - public DebugTreeNodeStream(TreeNodeStream input, + public DebugTreeNodeStream(ASTNodeStream input, DebugEventListener dbg) { this.input = input; diff --git a/runtime/Java/src/org/antlr/v4/runtime/debug/DebugTreeParser.java b/runtime/Java/src/org/antlr/v4/runtime/debug/DebugTreeParser.java index 6dcec2432..f2ba46436 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/debug/DebugTreeParser.java +++ b/runtime/Java/src/org/antlr/v4/runtime/debug/DebugTreeParser.java @@ -28,8 +28,7 @@ package org.antlr.v4.runtime.debug; import org.antlr.v4.runtime.RecognitionException; -import org.antlr.v4.runtime.tree.TreeNodeStream; -import org.antlr.v4.runtime.tree.TreeParser; +import org.antlr.v4.runtime.tree.*; import java.io.IOException; @@ -45,12 +44,12 @@ public class DebugTreeParser extends TreeParser { /** Create a normal parser except wrap the token stream in a debug * proxy that fires consume events. */ - public DebugTreeParser(TreeNodeStream input, DebugEventListener dbg) { + public DebugTreeParser(ASTNodeStream input, DebugEventListener dbg) { super(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,dbg)); setDebugListener(dbg); } - public DebugTreeParser(TreeNodeStream input) { + public DebugTreeParser(ASTNodeStream input) { super(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,null)); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeNodeStream.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ASTNodeStream.java similarity index 98% rename from runtime/Java/src/org/antlr/v4/runtime/tree/TreeNodeStream.java rename to runtime/Java/src/org/antlr/v4/runtime/tree/ASTNodeStream.java index b269c86b6..72eabd382 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeNodeStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/ASTNodeStream.java @@ -32,8 +32,7 @@ package org.antlr.v4.runtime.tree; import org.antlr.v4.runtime.*; /** A stream of tree nodes, accessing nodes from a tree of some kind */ -// TODO: rename to ASTNodeStream? -public interface TreeNodeStream extends IntStream { +public interface ASTNodeStream extends IntStream { /** Get a tree node at an absolute index i; 0..n-1. * If you don't want to buffer up nodes, then this method makes no * sense for you. diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTNodeStream.java b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTNodeStream.java index 37c577f58..791b023bc 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTNodeStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTNodeStream.java @@ -34,7 +34,7 @@ import org.antlr.v4.runtime.misc.LookaheadStream; import java.util.Stack; -public class CommonASTNodeStream extends LookaheadStream implements TreeNodeStream { +public class CommonASTNodeStream extends LookaheadStream implements ASTNodeStream { public static final int DEFAULT_INITIAL_BUFFER_SIZE = 100; public static final int INITIAL_CALL_STACK_SIZE = 10; diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonErrorNode.java b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonErrorNode.java index d59479772..abe2f91af 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonErrorNode.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonErrorNode.java @@ -77,7 +77,7 @@ public class CommonErrorNode extends CommonAST { badText = ((TokenStream)input).toString(i, j); } else if ( start instanceof Tree ) { - badText = ((TreeNodeStream)input).toString(start, stop); + badText = ((ASTNodeStream)input).toString(start, stop); } else { // people should subclass if they alter the tree type so this diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java index 16d523f99..c2545bd70 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java @@ -29,8 +29,7 @@ package org.antlr.v4.runtime.tree; -import org.antlr.v4.runtime.RecognitionException; -import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; /** Cut-n-paste from material I'm not using in the book anymore (edit later @@ -84,7 +83,7 @@ public class TreeFilter extends TreeParser { protected TokenStream originalTokenStream; protected ASTAdaptor originalAdaptor; - public TreeFilter(TreeNodeStream input) { + public TreeFilter(ASTNodeStream input) { super(input); originalAdaptor = (ASTAdaptor) input.getTreeAdaptor(); originalTokenStream = input.getTokenStream(); diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeParser.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreeParser.java index 4c145d83c..71aedba3c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeParser.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/TreeParser.java @@ -31,8 +31,7 @@ package org.antlr.v4.runtime.tree; import org.antlr.v4.runtime.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.regex.*; /** A parser for a stream of tree nodes. "tree grammars" result in a subclass * of this. All the error reporting and recovery is shared with Parser via @@ -48,9 +47,9 @@ public class TreeParser extends BaseRecognizer { static Pattern dotdotPattern = Pattern.compile(dotdot); static Pattern doubleEtcPattern = Pattern.compile(doubleEtc); - protected TreeNodeStream _input; + protected ASTNodeStream _input; - public TreeParser(TreeNodeStream input) { + public TreeParser(ASTNodeStream input) { super(input); } @@ -64,10 +63,10 @@ public class TreeParser extends BaseRecognizer { protected Object getCurrentInputSymbol() { return _input.LT(1); } @Override - public TreeNodeStream getInputStream() { return _input; } + public ASTNodeStream getInputStream() { return _input; } @Override - public void setInputStream(IntStream input) { _input = (TreeNodeStream)input; } + public void setInputStream(IntStream input) { _input = (ASTNodeStream)input; } /** Always called by generated parsers upon entry to a rule. * This occurs after the new context has been pushed. Access field @@ -87,7 +86,7 @@ public class TreeParser extends BaseRecognizer { } protected Object getCurrentInputSymbol(IntStream input) { - return ((TreeNodeStream)input).LT(1); + return ((ASTNodeStream)input).LT(1); } protected Object getMissingSymbol(IntStream input, @@ -96,7 +95,7 @@ public class TreeParser extends BaseRecognizer { { String tokenText = ""; - ASTAdaptor adaptor = ((TreeNodeStream)e.input).getTreeAdaptor(); + ASTAdaptor adaptor = ((ASTNodeStream)e.input).getTreeAdaptor(); return adaptor.create(new CommonToken(expectedTokenType, tokenText)); } @@ -156,7 +155,7 @@ public class TreeParser extends BaseRecognizer { */ public String getErrorMessage(RecognitionException e, String[] tokenNames) { if ( this instanceof TreeParser ) { - ASTAdaptor adaptor = ((TreeNodeStream)e.input).getTreeAdaptor(); + ASTAdaptor adaptor = ((ASTNodeStream)e.input).getTreeAdaptor(); e.token = adaptor.getToken(e.node); if ( e.token==null ) { // could be an UP/DOWN node e.token = new CommonToken(adaptor.getType(e.node), diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternLexer.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternLexer.java new file mode 100644 index 000000000..2e0ee4a81 --- /dev/null +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternLexer.java @@ -0,0 +1,136 @@ +/* + [The "BSD license"] + Copyright (c) 2011 Terence Parr + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.antlr.v4.runtime.tree; + +public class TreePatternLexer { + public static final int EOF = -1; + public static final int BEGIN = 1; + public static final int END = 2; + public static final int ID = 3; + public static final int ARG = 4; + public static final int PERCENT = 5; + public static final int COLON = 6; + public static final int DOT = 7; + + /** The tree pattern to lex like "(A B C)" */ + protected String pattern; + + /** Index into input string */ + protected int p = -1; + + /** Current char */ + protected int c; + + /** How long is the pattern in char? */ + protected int n; + + /** Set when token type is ID or ARG (name mimics Java's StreamTokenizer) */ + public StringBuffer sval = new StringBuffer(); + + public boolean error = false; + + public TreePatternLexer(String pattern) { + this.pattern = pattern; + this.n = pattern.length(); + consume(); + } + + public int nextToken() { + sval.setLength(0); // reset, but reuse buffer + while ( c != EOF ) { + if ( c==' ' || c=='\n' || c=='\r' || c=='\t' ) { + consume(); + continue; + } + if ( (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_' ) { + sval.append((char)c); + consume(); + while ( (c>='a' && c<='z') || (c>='A' && c<='Z') || + (c>='0' && c<='9') || c=='_' ) + { + sval.append((char)c); + consume(); + } + return ID; + } + if ( c=='(' ) { + consume(); + return BEGIN; + } + if ( c==')' ) { + consume(); + return END; + } + if ( c=='%' ) { + consume(); + return PERCENT; + } + if ( c==':' ) { + consume(); + return COLON; + } + if ( c=='.' ) { + consume(); + return DOT; + } + if ( c=='[' ) { // grab [x] as a string, returning x + consume(); + while ( c!=']' ) { + if ( c=='\\' ) { + consume(); + if ( c!=']' ) { + sval.append('\\'); + } + sval.append((char)c); + } + else { + sval.append((char)c); + } + consume(); + } + consume(); + return ARG; + } + consume(); + error = true; + return EOF; + } + return EOF; + } + + protected void consume() { + p++; + if ( p>=n ) { + c = EOF; + } + else { + c = pattern.charAt(p); + } + } +} diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternParser.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternParser.java new file mode 100644 index 000000000..4573d9134 --- /dev/null +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/TreePatternParser.java @@ -0,0 +1,155 @@ +/* + [The "BSD license"] + Copyright (c) 2011 Terence Parr + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.antlr.v4.runtime.tree; + + +import org.antlr.v4.runtime.*; + +public class TreePatternParser { + protected TreePatternLexer tokenizer; + protected int ttype; + protected TreeWizard wizard; + protected ASTAdaptor adaptor; + + public TreePatternParser(TreePatternLexer tokenizer, TreeWizard wizard, ASTAdaptor adaptor) { + this.tokenizer = tokenizer; + this.wizard = wizard; + this.adaptor = adaptor; + ttype = tokenizer.nextToken(); // kickstart + } + + public Object pattern() { + if ( ttype==TreePatternLexer.BEGIN ) { + return parseTree(); + } + else if ( ttype==TreePatternLexer.ID ) { + Object node = parseNode(); + if ( ttype==TreePatternLexer.EOF ) { + return node; + } + return null; // extra junk on end + } + return null; + } + + public Object parseTree() { + if ( ttype != TreePatternLexer.BEGIN ) { + throw new RuntimeException("no BEGIN"); + } + ttype = tokenizer.nextToken(); + Object root = parseNode(); + if ( root==null ) { + return null; + } + while ( ttype==TreePatternLexer.BEGIN || + ttype==TreePatternLexer.ID || + ttype==TreePatternLexer.PERCENT || + ttype==TreePatternLexer.DOT ) + { + if ( ttype==TreePatternLexer.BEGIN ) { + Object subtree = parseTree(); + adaptor.addChild(root, subtree); + } + else { + Object child = parseNode(); + if ( child==null ) { + return null; + } + adaptor.addChild(root, child); + } + } + if ( ttype != TreePatternLexer.END ) { + throw new RuntimeException("no END"); + } + ttype = tokenizer.nextToken(); + return root; + } + + public Object parseNode() { + // "%label:" prefix + String label = null; + if ( ttype == TreePatternLexer.PERCENT ) { + ttype = tokenizer.nextToken(); + if ( ttype != TreePatternLexer.ID ) { + return null; + } + label = tokenizer.sval.toString(); + ttype = tokenizer.nextToken(); + if ( ttype != TreePatternLexer.COLON ) { + return null; + } + ttype = tokenizer.nextToken(); // move to ID following colon + } + + // Wildcard? + if ( ttype == TreePatternLexer.DOT ) { + ttype = tokenizer.nextToken(); + Token wildcardPayload = new CommonToken(0, "."); + TreeWizard.TreePattern node = + new TreeWizard.WildcardTreePattern(wildcardPayload); + if ( label!=null ) { + node.label = label; + } + return node; + } + + // "ID" or "ID[arg]" + if ( ttype != TreePatternLexer.ID ) { + return null; + } + String tokenName = tokenizer.sval.toString(); + ttype = tokenizer.nextToken(); + if ( tokenName.equals("nil") ) { + return adaptor.nil(); + } + String text = tokenName; + // check for arg + String arg = null; + if ( ttype == TreePatternLexer.ARG ) { + arg = tokenizer.sval.toString(); + text = arg; + ttype = tokenizer.nextToken(); + } + + // create node + int treeNodeType = wizard.getTokenType(tokenName); + if ( treeNodeType== Token.INVALID_TYPE ) { + return null; + } + Object node; + node = adaptor.create(treeNodeType, text); + if ( label!=null && node.getClass()==TreeWizard.TreePattern.class ) { + ((TreeWizard.TreePattern)node).label = label; + } + if ( arg!=null && node.getClass()==TreeWizard.TreePattern.class ) { + ((TreeWizard.TreePattern)node).hasTextArg = true; + } + return node; + } +} diff --git a/tool/playground/TestU.java b/tool/playground/TestU.java index 045266929..aed059905 100644 --- a/tool/playground/TestU.java +++ b/tool/playground/TestU.java @@ -1,8 +1,5 @@ -import org.antlr.v4.runtime.ANTLRFileStream; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.tree.CommonASTNodeStream; -import org.antlr.v4.runtime.tree.Tree; -import org.antlr.v4.runtime.tree.TreeNodeStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.tree.*; public class TestU { public static void main(String[] args) throws Exception { @@ -14,7 +11,7 @@ public class TestU { System.out.println(((Tree) ctx.tree).toStringTree()); - TreeNodeStream nodes = new CommonASTNodeStream(ctx.tree); + ASTNodeStream nodes = new CommonASTNodeStream(ctx.tree); UWalker walker = new UWalker(nodes); walker.a(); } diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 61944fda6..9f18d55ca 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -154,7 +154,7 @@ public (TokenStream input) { >> treeparser_ctor(p) ::= << -public (TreeNodeStream input) { +public (ASTNodeStream input) { super(input); _interp = new ParserATNSimulator(this,_ATN); } diff --git a/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java b/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java index adde3e3e0..4c166e9dd 100644 --- a/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java +++ b/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java @@ -132,7 +132,7 @@ public class TestTokenTypeAssignment extends BaseTest { String grammar = "grammar P;\n" + "tokens { B='}'; }\n"+ - "a : A B {System.out.println(input);} ;\n"+ + "a : A B {System.out.println(_input);} ;\n"+ "A : 'a' ;\n" + "B : '}' ;\n"+ "WS : (' '|'\\n') {skip();} ;"; @@ -147,7 +147,7 @@ public class TestTokenTypeAssignment extends BaseTest { String grammar = "grammar P;\n" + "tokens { B='}'; }\n"+ - "a : A '}' {System.out.println(input);} ;\n"+ + "a : A '}' {System.out.println(_input);} ;\n"+ "A : 'a' ;\n" + "B : '}' ;\n"+ "WS : (' '|'\\n') {skip();} ;"; diff --git a/tool/test/org/antlr/v4/test/TestTreeNodeStream.java b/tool/test/org/antlr/v4/test/TestTreeNodeStream.java index 6919e532d..35cd16573 100644 --- a/tool/test/org/antlr/v4/test/TestTreeNodeStream.java +++ b/tool/test/org/antlr/v4/test/TestTreeNodeStream.java @@ -37,18 +37,18 @@ public class TestTreeNodeStream extends BaseTest { public static final String UP = " "+Token.UP; /** Build new stream; let's us override to test other streams. */ - public TreeNodeStream newStream(Object t) { + public ASTNodeStream newStream(Object t) { return new CommonASTNodeStream(t); } - public String toTokenTypeString(TreeNodeStream stream) { + public String toTokenTypeString(ASTNodeStream stream) { return ((CommonASTNodeStream)stream).toTokenTypeString(); } @Test public void testSingleNode() throws Exception { BaseAST t = new CommonAST(new CommonToken(101)); - TreeNodeStream stream = newStream(t); + ASTNodeStream stream = newStream(t); String expecting = " 101"; String found = toNodesOnlyString(stream); assertEquals(expecting, found); @@ -65,7 +65,7 @@ public class TestTreeNodeStream extends BaseTest { t.getChild(0).addChild(new CommonAST(new CommonToken(103))); t.addChild(new CommonAST(new CommonToken(104))); - TreeNodeStream stream = newStream(t); + ASTNodeStream stream = newStream(t); String expecting = " 101 102 103 104"; String found = toNodesOnlyString(stream); assertEquals(expecting, found); @@ -88,7 +88,7 @@ public class TestTreeNodeStream extends BaseTest { root.addChild(t); root.addChild(u); - TreeNodeStream stream = newStream(root); + ASTNodeStream stream = newStream(root); String expecting = " 101 102 103 104 105"; String found = toNodesOnlyString(stream); assertEquals(expecting, found); @@ -105,7 +105,7 @@ public class TestTreeNodeStream extends BaseTest { root.addChild(new CommonAST(new CommonToken(102))); root.addChild(new CommonAST(new CommonToken(103))); - TreeNodeStream stream = newStream(root); + ASTNodeStream stream = newStream(root); String expecting = " 101 102 103"; String found = toNodesOnlyString(stream); assertEquals(expecting, found); @@ -120,7 +120,7 @@ public class TestTreeNodeStream extends BaseTest { root.addChild(new CommonAST(new CommonToken(101))); - TreeNodeStream stream = newStream(root); + ASTNodeStream stream = newStream(root); String expecting = " 101"; String found = toNodesOnlyString(stream); assertEquals(expecting, found); @@ -134,7 +134,7 @@ public class TestTreeNodeStream extends BaseTest { BaseAST t = new CommonAST(new CommonToken(101)); t.addChild(new CommonAST(new CommonToken(102))); - TreeNodeStream stream = newStream(t); + ASTNodeStream stream = newStream(t); String expecting = " 101 102"; String found = toNodesOnlyString(stream); assertEquals(expecting, found); @@ -151,7 +151,7 @@ public class TestTreeNodeStream extends BaseTest { t.getChild(0).addChild(new CommonAST(new CommonToken(103))); t.addChild(new CommonAST(new CommonToken(104))); - TreeNodeStream stream = newStream(t); + ASTNodeStream stream = newStream(t); assertEquals(101, ((AST)stream.LT(1)).getType()); assertEquals(Token.DOWN, ((AST)stream.LT(2)).getType()); assertEquals(102, ((AST)stream.LT(3)).getType()); @@ -179,7 +179,7 @@ public class TestTreeNodeStream extends BaseTest { r0.addChild(new CommonAST(new CommonToken(104))); r0.addChild(new CommonAST(new CommonToken(105))); - TreeNodeStream stream = newStream(r0); + ASTNodeStream stream = newStream(r0); int m = stream.mark(); // MARK for (int k=1; k<=13; k++) { // consume til end stream.LT(1); @@ -211,7 +211,7 @@ public class TestTreeNodeStream extends BaseTest { r0.addChild(new CommonAST(new CommonToken(104))); r0.addChild(new CommonAST(new CommonToken(105))); - TreeNodeStream stream = newStream(r0); + ASTNodeStream stream = newStream(r0); for (int k=1; k<=7; k++) { // consume til middle //System.out.println(((AST)stream.LT(1)).getType()); stream.consume(); @@ -257,7 +257,7 @@ public class TestTreeNodeStream extends BaseTest { r0.addChild(new CommonAST(new CommonToken(104))); r0.addChild(new CommonAST(new CommonToken(105))); - TreeNodeStream stream = newStream(r0); + ASTNodeStream stream = newStream(r0); int m = stream.mark(); // MARK at start stream.consume(); // consume 101 stream.consume(); // consume DN @@ -298,7 +298,7 @@ public class TestTreeNodeStream extends BaseTest { r0.addChild(new CommonAST(new CommonToken(104))); r0.addChild(new CommonAST(new CommonToken(105))); - TreeNodeStream stream = newStream(r0); + ASTNodeStream stream = newStream(r0); stream.seek(7); // seek to 107 assertEquals(107, ((AST)stream.LT(1)).getType()); stream.consume(); // consume 107 @@ -321,7 +321,7 @@ public class TestTreeNodeStream extends BaseTest { r0.addChild(new CommonAST(new CommonToken(104))); r0.addChild(new CommonAST(new CommonToken(105))); - TreeNodeStream stream = newStream(r0); + ASTNodeStream stream = newStream(r0); String v = toNodesOnlyString(stream); // scan all stream.reset(); String v2 = toNodesOnlyString(stream); // scan all @@ -356,13 +356,13 @@ public class TestTreeNodeStream extends BaseTest { rules.addChild(rule2); rule2.addChild(id2); - TreeNodeStream stream = newStream(g); + ASTNodeStream stream = newStream(g); String expecting = " 10"+DN+"100 101 20"+DN+"30"+DN+"40 50"+DN+"60"+DN+"70"+UP+""+UP+""+UP+" 80"+DN+"90"+UP+""+UP+""+UP+""; String found = toTokenTypeString(stream); assertEquals(expecting, found); } - public String toNodesOnlyString(TreeNodeStream nodes) { + public String toNodesOnlyString(ASTNodeStream nodes) { ASTAdaptor adaptor = nodes.getTreeAdaptor(); StringBuffer buf = new StringBuffer(); Object o = nodes.LT(1);