rename TreeNodeStream
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9075]
This commit is contained in:
parent
93581cfa98
commit
e8a2a738cf
|
@ -29,8 +29,7 @@
|
||||||
package org.antlr.v4.runtime;
|
package org.antlr.v4.runtime;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.misc.IntervalSet;
|
import org.antlr.v4.runtime.misc.IntervalSet;
|
||||||
import org.antlr.v4.runtime.tree.ASTAdaptor;
|
import org.antlr.v4.runtime.tree.*;
|
||||||
import org.antlr.v4.runtime.tree.TreeNodeStream;
|
|
||||||
|
|
||||||
/** The root of the ANTLR exception hierarchy.
|
/** The root of the ANTLR exception hierarchy.
|
||||||
*
|
*
|
||||||
|
@ -127,7 +126,7 @@ public class RecognitionException extends RuntimeException {
|
||||||
this.line = token.getLine();
|
this.line = token.getLine();
|
||||||
this.charPositionInLine = token.getCharPositionInLine();
|
this.charPositionInLine = token.getCharPositionInLine();
|
||||||
}
|
}
|
||||||
if ( input instanceof TreeNodeStream) {
|
if ( input instanceof ASTNodeStream) {
|
||||||
//extractInformationFromTreeNodeStream(input);
|
//extractInformationFromTreeNodeStream(input);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -186,8 +185,8 @@ public class RecognitionException extends RuntimeException {
|
||||||
if ( recognizer.getInputStream() instanceof TokenStream) {
|
if ( recognizer.getInputStream() instanceof TokenStream) {
|
||||||
return token.getType();
|
return token.getType();
|
||||||
}
|
}
|
||||||
else if ( recognizer.getInputStream() instanceof TreeNodeStream) {
|
else if ( recognizer.getInputStream() instanceof ASTNodeStream) {
|
||||||
TreeNodeStream nodes = (TreeNodeStream)recognizer.getInputStream();
|
ASTNodeStream nodes = (ASTNodeStream)recognizer.getInputStream();
|
||||||
ASTAdaptor adaptor = nodes.getTreeAdaptor();
|
ASTAdaptor adaptor = nodes.getTreeAdaptor();
|
||||||
return adaptor.getType(node);
|
return adaptor.getType(node);
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,8 +122,8 @@ public class Recognizer<ATNInterpreter> {
|
||||||
msg = "mismatched input "+getTokenErrorDisplay(e.token)+
|
msg = "mismatched input "+getTokenErrorDisplay(e.token)+
|
||||||
" expecting "+tokenName;
|
" expecting "+tokenName;
|
||||||
}
|
}
|
||||||
else if ( e instanceof MismatchedTreeNodeException ) {
|
else if ( e instanceof MismatchedASTNodeException) {
|
||||||
MismatchedTreeNodeException mtne = (MismatchedTreeNodeException)e;
|
MismatchedASTNodeException mtne = (MismatchedASTNodeException)e;
|
||||||
String tokenName="<unknown>";
|
String tokenName="<unknown>";
|
||||||
if ( mtne.expecting.member(Token.EOF) ) {
|
if ( mtne.expecting.member(Token.EOF) ) {
|
||||||
tokenName = "EOF";
|
tokenName = "EOF";
|
||||||
|
|
|
@ -34,16 +34,16 @@ import org.antlr.v4.runtime.tree.*;
|
||||||
* and a debug listener. As node stream calls come in, debug events
|
* and a debug listener. As node stream calls come in, debug events
|
||||||
* are triggered.
|
* are triggered.
|
||||||
*/
|
*/
|
||||||
public class DebugTreeNodeStream implements TreeNodeStream {
|
public class DebugTreeNodeStream implements ASTNodeStream {
|
||||||
protected DebugEventListener dbg;
|
protected DebugEventListener dbg;
|
||||||
protected ASTAdaptor adaptor;
|
protected ASTAdaptor adaptor;
|
||||||
protected TreeNodeStream input;
|
protected ASTNodeStream input;
|
||||||
protected boolean initialStreamState = true;
|
protected boolean initialStreamState = true;
|
||||||
|
|
||||||
/** Track the last mark() call result value for use in rewind(). */
|
/** Track the last mark() call result value for use in rewind(). */
|
||||||
protected int lastMarker;
|
protected int lastMarker;
|
||||||
|
|
||||||
public DebugTreeNodeStream(TreeNodeStream input,
|
public DebugTreeNodeStream(ASTNodeStream input,
|
||||||
DebugEventListener dbg)
|
DebugEventListener dbg)
|
||||||
{
|
{
|
||||||
this.input = input;
|
this.input = input;
|
||||||
|
|
|
@ -28,8 +28,7 @@
|
||||||
package org.antlr.v4.runtime.debug;
|
package org.antlr.v4.runtime.debug;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.RecognitionException;
|
import org.antlr.v4.runtime.RecognitionException;
|
||||||
import org.antlr.v4.runtime.tree.TreeNodeStream;
|
import org.antlr.v4.runtime.tree.*;
|
||||||
import org.antlr.v4.runtime.tree.TreeParser;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
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
|
/** Create a normal parser except wrap the token stream in a debug
|
||||||
* proxy that fires consume events.
|
* 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));
|
super(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,dbg));
|
||||||
setDebugListener(dbg);
|
setDebugListener(dbg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DebugTreeParser(TreeNodeStream input) {
|
public DebugTreeParser(ASTNodeStream input) {
|
||||||
super(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,null));
|
super(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,8 +32,7 @@ package org.antlr.v4.runtime.tree;
|
||||||
import org.antlr.v4.runtime.*;
|
import org.antlr.v4.runtime.*;
|
||||||
|
|
||||||
/** A stream of tree nodes, accessing nodes from a tree of some kind */
|
/** A stream of tree nodes, accessing nodes from a tree of some kind */
|
||||||
// TODO: rename to ASTNodeStream?
|
public interface ASTNodeStream extends IntStream {
|
||||||
public interface TreeNodeStream extends IntStream {
|
|
||||||
/** Get a tree node at an absolute index i; 0..n-1.
|
/** 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
|
* If you don't want to buffer up nodes, then this method makes no
|
||||||
* sense for you.
|
* sense for you.
|
|
@ -34,7 +34,7 @@ import org.antlr.v4.runtime.misc.LookaheadStream;
|
||||||
|
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
public class CommonASTNodeStream extends LookaheadStream<Object> implements TreeNodeStream {
|
public class CommonASTNodeStream extends LookaheadStream<Object> implements ASTNodeStream {
|
||||||
public static final int DEFAULT_INITIAL_BUFFER_SIZE = 100;
|
public static final int DEFAULT_INITIAL_BUFFER_SIZE = 100;
|
||||||
public static final int INITIAL_CALL_STACK_SIZE = 10;
|
public static final int INITIAL_CALL_STACK_SIZE = 10;
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ public class CommonErrorNode extends CommonAST {
|
||||||
badText = ((TokenStream)input).toString(i, j);
|
badText = ((TokenStream)input).toString(i, j);
|
||||||
}
|
}
|
||||||
else if ( start instanceof Tree ) {
|
else if ( start instanceof Tree ) {
|
||||||
badText = ((TreeNodeStream)input).toString(start, stop);
|
badText = ((ASTNodeStream)input).toString(start, stop);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// people should subclass if they alter the tree type so this
|
// people should subclass if they alter the tree type so this
|
||||||
|
|
|
@ -29,8 +29,7 @@
|
||||||
|
|
||||||
package org.antlr.v4.runtime.tree;
|
package org.antlr.v4.runtime.tree;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.RecognitionException;
|
import org.antlr.v4.runtime.*;
|
||||||
import org.antlr.v4.runtime.TokenStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Cut-n-paste from material I'm not using in the book anymore (edit later
|
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 TokenStream originalTokenStream;
|
||||||
protected ASTAdaptor originalAdaptor;
|
protected ASTAdaptor originalAdaptor;
|
||||||
|
|
||||||
public TreeFilter(TreeNodeStream input) {
|
public TreeFilter(ASTNodeStream input) {
|
||||||
super(input);
|
super(input);
|
||||||
originalAdaptor = (ASTAdaptor) input.getTreeAdaptor();
|
originalAdaptor = (ASTAdaptor) input.getTreeAdaptor();
|
||||||
originalTokenStream = input.getTokenStream();
|
originalTokenStream = input.getTokenStream();
|
||||||
|
|
|
@ -31,8 +31,7 @@ package org.antlr.v4.runtime.tree;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.*;
|
import org.antlr.v4.runtime.*;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.*;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/** A parser for a stream of tree nodes. "tree grammars" result in a subclass
|
/** 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
|
* 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 dotdotPattern = Pattern.compile(dotdot);
|
||||||
static Pattern doubleEtcPattern = Pattern.compile(doubleEtc);
|
static Pattern doubleEtcPattern = Pattern.compile(doubleEtc);
|
||||||
|
|
||||||
protected TreeNodeStream _input;
|
protected ASTNodeStream _input;
|
||||||
|
|
||||||
public TreeParser(TreeNodeStream input) {
|
public TreeParser(ASTNodeStream input) {
|
||||||
super(input);
|
super(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,10 +63,10 @@ public class TreeParser extends BaseRecognizer {
|
||||||
protected Object getCurrentInputSymbol() { return _input.LT(1); }
|
protected Object getCurrentInputSymbol() { return _input.LT(1); }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TreeNodeStream getInputStream() { return _input; }
|
public ASTNodeStream getInputStream() { return _input; }
|
||||||
|
|
||||||
@Override
|
@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.
|
/** Always called by generated parsers upon entry to a rule.
|
||||||
* This occurs after the new context has been pushed. Access field
|
* 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) {
|
protected Object getCurrentInputSymbol(IntStream input) {
|
||||||
return ((TreeNodeStream)input).LT(1);
|
return ((ASTNodeStream)input).LT(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Object getMissingSymbol(IntStream input,
|
protected Object getMissingSymbol(IntStream input,
|
||||||
|
@ -96,7 +95,7 @@ public class TreeParser extends BaseRecognizer {
|
||||||
{
|
{
|
||||||
String tokenText =
|
String tokenText =
|
||||||
"<missing "+getTokenNames()[expectedTokenType]+">";
|
"<missing "+getTokenNames()[expectedTokenType]+">";
|
||||||
ASTAdaptor adaptor = ((TreeNodeStream)e.input).getTreeAdaptor();
|
ASTAdaptor adaptor = ((ASTNodeStream)e.input).getTreeAdaptor();
|
||||||
return adaptor.create(new CommonToken(expectedTokenType, tokenText));
|
return adaptor.create(new CommonToken(expectedTokenType, tokenText));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,7 +155,7 @@ public class TreeParser extends BaseRecognizer {
|
||||||
*/
|
*/
|
||||||
public String getErrorMessage(RecognitionException e, String[] tokenNames) {
|
public String getErrorMessage(RecognitionException e, String[] tokenNames) {
|
||||||
if ( this instanceof TreeParser ) {
|
if ( this instanceof TreeParser ) {
|
||||||
ASTAdaptor adaptor = ((TreeNodeStream)e.input).getTreeAdaptor();
|
ASTAdaptor adaptor = ((ASTNodeStream)e.input).getTreeAdaptor();
|
||||||
e.token = adaptor.getToken(e.node);
|
e.token = adaptor.getToken(e.node);
|
||||||
if ( e.token==null ) { // could be an UP/DOWN node
|
if ( e.token==null ) { // could be an UP/DOWN node
|
||||||
e.token = new CommonToken(adaptor.getType(e.node),
|
e.token = new CommonToken(adaptor.getType(e.node),
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,5 @@
|
||||||
import org.antlr.v4.runtime.ANTLRFileStream;
|
import org.antlr.v4.runtime.*;
|
||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
import org.antlr.v4.runtime.tree.*;
|
||||||
import org.antlr.v4.runtime.tree.CommonASTNodeStream;
|
|
||||||
import org.antlr.v4.runtime.tree.Tree;
|
|
||||||
import org.antlr.v4.runtime.tree.TreeNodeStream;
|
|
||||||
|
|
||||||
public class TestU {
|
public class TestU {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
@ -14,7 +11,7 @@ public class TestU {
|
||||||
|
|
||||||
System.out.println(((Tree) ctx.tree).toStringTree());
|
System.out.println(((Tree) ctx.tree).toStringTree());
|
||||||
|
|
||||||
TreeNodeStream nodes = new CommonASTNodeStream(ctx.tree);
|
ASTNodeStream nodes = new CommonASTNodeStream(ctx.tree);
|
||||||
UWalker walker = new UWalker(nodes);
|
UWalker walker = new UWalker(nodes);
|
||||||
walker.a();
|
walker.a();
|
||||||
}
|
}
|
||||||
|
|
|
@ -154,7 +154,7 @@ public <p.name>(TokenStream input) {
|
||||||
>>
|
>>
|
||||||
|
|
||||||
treeparser_ctor(p) ::= <<
|
treeparser_ctor(p) ::= <<
|
||||||
public <p.name>(TreeNodeStream input) {
|
public <p.name>(ASTNodeStream input) {
|
||||||
super(input);
|
super(input);
|
||||||
_interp = new ParserATNSimulator(this,_ATN);
|
_interp = new ParserATNSimulator(this,_ATN);
|
||||||
}
|
}
|
||||||
|
|
|
@ -132,7 +132,7 @@ public class TestTokenTypeAssignment extends BaseTest {
|
||||||
String grammar =
|
String grammar =
|
||||||
"grammar P;\n" +
|
"grammar P;\n" +
|
||||||
"tokens { B='}'; }\n"+
|
"tokens { B='}'; }\n"+
|
||||||
"a : A B {System.out.println(input);} ;\n"+
|
"a : A B {System.out.println(_input);} ;\n"+
|
||||||
"A : 'a' ;\n" +
|
"A : 'a' ;\n" +
|
||||||
"B : '}' ;\n"+
|
"B : '}' ;\n"+
|
||||||
"WS : (' '|'\\n') {skip();} ;";
|
"WS : (' '|'\\n') {skip();} ;";
|
||||||
|
@ -147,7 +147,7 @@ public class TestTokenTypeAssignment extends BaseTest {
|
||||||
String grammar =
|
String grammar =
|
||||||
"grammar P;\n" +
|
"grammar P;\n" +
|
||||||
"tokens { B='}'; }\n"+
|
"tokens { B='}'; }\n"+
|
||||||
"a : A '}' {System.out.println(input);} ;\n"+
|
"a : A '}' {System.out.println(_input);} ;\n"+
|
||||||
"A : 'a' ;\n" +
|
"A : 'a' ;\n" +
|
||||||
"B : '}' ;\n"+
|
"B : '}' ;\n"+
|
||||||
"WS : (' '|'\\n') {skip();} ;";
|
"WS : (' '|'\\n') {skip();} ;";
|
||||||
|
|
|
@ -37,18 +37,18 @@ public class TestTreeNodeStream extends BaseTest {
|
||||||
public static final String UP = " "+Token.UP;
|
public static final String UP = " "+Token.UP;
|
||||||
|
|
||||||
/** Build new stream; let's us override to test other streams. */
|
/** 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);
|
return new CommonASTNodeStream(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toTokenTypeString(TreeNodeStream stream) {
|
public String toTokenTypeString(ASTNodeStream stream) {
|
||||||
return ((CommonASTNodeStream)stream).toTokenTypeString();
|
return ((CommonASTNodeStream)stream).toTokenTypeString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void testSingleNode() throws Exception {
|
@Test public void testSingleNode() throws Exception {
|
||||||
BaseAST t = new CommonAST(new CommonToken(101));
|
BaseAST t = new CommonAST(new CommonToken(101));
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(t);
|
ASTNodeStream stream = newStream(t);
|
||||||
String expecting = " 101";
|
String expecting = " 101";
|
||||||
String found = toNodesOnlyString(stream);
|
String found = toNodesOnlyString(stream);
|
||||||
assertEquals(expecting, found);
|
assertEquals(expecting, found);
|
||||||
|
@ -65,7 +65,7 @@ public class TestTreeNodeStream extends BaseTest {
|
||||||
t.getChild(0).addChild(new CommonAST(new CommonToken(103)));
|
t.getChild(0).addChild(new CommonAST(new CommonToken(103)));
|
||||||
t.addChild(new CommonAST(new CommonToken(104)));
|
t.addChild(new CommonAST(new CommonToken(104)));
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(t);
|
ASTNodeStream stream = newStream(t);
|
||||||
String expecting = " 101 102 103 104";
|
String expecting = " 101 102 103 104";
|
||||||
String found = toNodesOnlyString(stream);
|
String found = toNodesOnlyString(stream);
|
||||||
assertEquals(expecting, found);
|
assertEquals(expecting, found);
|
||||||
|
@ -88,7 +88,7 @@ public class TestTreeNodeStream extends BaseTest {
|
||||||
root.addChild(t);
|
root.addChild(t);
|
||||||
root.addChild(u);
|
root.addChild(u);
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(root);
|
ASTNodeStream stream = newStream(root);
|
||||||
String expecting = " 101 102 103 104 105";
|
String expecting = " 101 102 103 104 105";
|
||||||
String found = toNodesOnlyString(stream);
|
String found = toNodesOnlyString(stream);
|
||||||
assertEquals(expecting, found);
|
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(102)));
|
||||||
root.addChild(new CommonAST(new CommonToken(103)));
|
root.addChild(new CommonAST(new CommonToken(103)));
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(root);
|
ASTNodeStream stream = newStream(root);
|
||||||
String expecting = " 101 102 103";
|
String expecting = " 101 102 103";
|
||||||
String found = toNodesOnlyString(stream);
|
String found = toNodesOnlyString(stream);
|
||||||
assertEquals(expecting, found);
|
assertEquals(expecting, found);
|
||||||
|
@ -120,7 +120,7 @@ public class TestTreeNodeStream extends BaseTest {
|
||||||
|
|
||||||
root.addChild(new CommonAST(new CommonToken(101)));
|
root.addChild(new CommonAST(new CommonToken(101)));
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(root);
|
ASTNodeStream stream = newStream(root);
|
||||||
String expecting = " 101";
|
String expecting = " 101";
|
||||||
String found = toNodesOnlyString(stream);
|
String found = toNodesOnlyString(stream);
|
||||||
assertEquals(expecting, found);
|
assertEquals(expecting, found);
|
||||||
|
@ -134,7 +134,7 @@ public class TestTreeNodeStream extends BaseTest {
|
||||||
BaseAST t = new CommonAST(new CommonToken(101));
|
BaseAST t = new CommonAST(new CommonToken(101));
|
||||||
t.addChild(new CommonAST(new CommonToken(102)));
|
t.addChild(new CommonAST(new CommonToken(102)));
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(t);
|
ASTNodeStream stream = newStream(t);
|
||||||
String expecting = " 101 102";
|
String expecting = " 101 102";
|
||||||
String found = toNodesOnlyString(stream);
|
String found = toNodesOnlyString(stream);
|
||||||
assertEquals(expecting, found);
|
assertEquals(expecting, found);
|
||||||
|
@ -151,7 +151,7 @@ public class TestTreeNodeStream extends BaseTest {
|
||||||
t.getChild(0).addChild(new CommonAST(new CommonToken(103)));
|
t.getChild(0).addChild(new CommonAST(new CommonToken(103)));
|
||||||
t.addChild(new CommonAST(new CommonToken(104)));
|
t.addChild(new CommonAST(new CommonToken(104)));
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(t);
|
ASTNodeStream stream = newStream(t);
|
||||||
assertEquals(101, ((AST)stream.LT(1)).getType());
|
assertEquals(101, ((AST)stream.LT(1)).getType());
|
||||||
assertEquals(Token.DOWN, ((AST)stream.LT(2)).getType());
|
assertEquals(Token.DOWN, ((AST)stream.LT(2)).getType());
|
||||||
assertEquals(102, ((AST)stream.LT(3)).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(104)));
|
||||||
r0.addChild(new CommonAST(new CommonToken(105)));
|
r0.addChild(new CommonAST(new CommonToken(105)));
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(r0);
|
ASTNodeStream stream = newStream(r0);
|
||||||
int m = stream.mark(); // MARK
|
int m = stream.mark(); // MARK
|
||||||
for (int k=1; k<=13; k++) { // consume til end
|
for (int k=1; k<=13; k++) { // consume til end
|
||||||
stream.LT(1);
|
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(104)));
|
||||||
r0.addChild(new CommonAST(new CommonToken(105)));
|
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
|
for (int k=1; k<=7; k++) { // consume til middle
|
||||||
//System.out.println(((AST)stream.LT(1)).getType());
|
//System.out.println(((AST)stream.LT(1)).getType());
|
||||||
stream.consume();
|
stream.consume();
|
||||||
|
@ -257,7 +257,7 @@ public class TestTreeNodeStream extends BaseTest {
|
||||||
r0.addChild(new CommonAST(new CommonToken(104)));
|
r0.addChild(new CommonAST(new CommonToken(104)));
|
||||||
r0.addChild(new CommonAST(new CommonToken(105)));
|
r0.addChild(new CommonAST(new CommonToken(105)));
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(r0);
|
ASTNodeStream stream = newStream(r0);
|
||||||
int m = stream.mark(); // MARK at start
|
int m = stream.mark(); // MARK at start
|
||||||
stream.consume(); // consume 101
|
stream.consume(); // consume 101
|
||||||
stream.consume(); // consume DN
|
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(104)));
|
||||||
r0.addChild(new CommonAST(new CommonToken(105)));
|
r0.addChild(new CommonAST(new CommonToken(105)));
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(r0);
|
ASTNodeStream stream = newStream(r0);
|
||||||
stream.seek(7); // seek to 107
|
stream.seek(7); // seek to 107
|
||||||
assertEquals(107, ((AST)stream.LT(1)).getType());
|
assertEquals(107, ((AST)stream.LT(1)).getType());
|
||||||
stream.consume(); // consume 107
|
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(104)));
|
||||||
r0.addChild(new CommonAST(new CommonToken(105)));
|
r0.addChild(new CommonAST(new CommonToken(105)));
|
||||||
|
|
||||||
TreeNodeStream stream = newStream(r0);
|
ASTNodeStream stream = newStream(r0);
|
||||||
String v = toNodesOnlyString(stream); // scan all
|
String v = toNodesOnlyString(stream); // scan all
|
||||||
stream.reset();
|
stream.reset();
|
||||||
String v2 = toNodesOnlyString(stream); // scan all
|
String v2 = toNodesOnlyString(stream); // scan all
|
||||||
|
@ -356,13 +356,13 @@ public class TestTreeNodeStream extends BaseTest {
|
||||||
rules.addChild(rule2);
|
rules.addChild(rule2);
|
||||||
rule2.addChild(id2);
|
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 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);
|
String found = toTokenTypeString(stream);
|
||||||
assertEquals(expecting, found);
|
assertEquals(expecting, found);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toNodesOnlyString(TreeNodeStream nodes) {
|
public String toNodesOnlyString(ASTNodeStream nodes) {
|
||||||
ASTAdaptor adaptor = nodes.getTreeAdaptor();
|
ASTAdaptor adaptor = nodes.getTreeAdaptor();
|
||||||
StringBuffer buf = new StringBuffer();
|
StringBuffer buf = new StringBuffer();
|
||||||
Object o = nodes.LT(1);
|
Object o = nodes.LT(1);
|
||||||
|
|
Loading…
Reference in New Issue