split apart

This commit is contained in:
Terence Parr 2013-09-04 12:37:04 -07:00
parent 5f60ad859e
commit 56cd7cdf2c
9 changed files with 99 additions and 80 deletions

View File

@ -0,0 +1,6 @@
package org.antlr.v4.runtime.tree.pattern;
class Chunk {
public Chunk() {
}
}

View File

@ -0,0 +1,11 @@
package org.antlr.v4.runtime.tree.pattern;
import org.antlr.v4.runtime.misc.Pair;
import org.antlr.v4.runtime.tree.ParseTree;
import java.util.List;
public class Match {
protected ParseTree subtree;
protected List<Pair<String,? extends ParseTree>> labels;
}

View File

@ -0,0 +1,19 @@
package org.antlr.v4.runtime.tree.pattern;
import java.util.Iterator;
public class MatchIterator implements Iterator<Match> {
@Override
public boolean hasNext() {
return false;
}
@Override
public Match next() {
return null;
}
@Override
public void remove() {
}
}

View File

@ -28,7 +28,7 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.antlr.v4.runtime.tree; package org.antlr.v4.runtime.tree.pattern;
import org.antlr.v4.runtime.DefaultErrorStrategy; import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.Parser;

View File

@ -28,7 +28,7 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.antlr.v4.runtime.tree; package org.antlr.v4.runtime.tree.pattern;
import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;

View File

@ -28,23 +28,23 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.antlr.v4.runtime.tree; package org.antlr.v4.runtime.tree.pattern;
import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream; import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.misc.Pair; import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.tree.RuleNode;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
public class ParseTreePatternMatcher { public class ParseTreePatternMatcher {
@ -54,78 +54,6 @@ public class ParseTreePatternMatcher {
protected String start = "<", stop=">"; protected String start = "<", stop=">";
protected String escape = "\\"; // e.g., \< and \> must escape BOTH! protected String escape = "\\"; // e.g., \< and \> must escape BOTH!
protected class Chunk {
}
protected class TextChunk extends Chunk {
public String text;
public TextChunk(String text) {
this.text = text;
}
@Override
public String toString() {
return "'"+text+"'";
}
}
protected class TagChunk extends Chunk { // <e:expr> or <ID>
public String tag;
public String label;
public TagChunk(String tag) {
this.tag = tag;
}
public TagChunk(String label, String tag) {
this.label = label;
this.tag = tag;
}
@Override
public String toString() {
if ( label!=null ) return label+":"+tag;
return tag;
}
}
public static class Pattern {
protected String pattern;
public Pattern(String pattern) {
this.pattern = pattern;
}
public boolean matches(ParseTree t) {
return false;
}
}
public static final Pattern WildcardPattern =
new Pattern("...") {
public boolean matches(ParseTree t) {
return true;
}
};
public static class Match {
protected ParseTree subtree;
protected List<Pair<String,? extends ParseTree>> labels;
}
public static class MatchIterator implements Iterator<Match> {
@Override
public boolean hasNext() {
return false;
}
@Override
public Match next() {
return null;
}
@Override
public void remove() {
}
}
public ParseTreePatternMatcher() { } public ParseTreePatternMatcher() { }
public ParseTreePatternMatcher(Class<? extends Lexer> lexerClass, public ParseTreePatternMatcher(Class<? extends Lexer> lexerClass,
@ -159,7 +87,7 @@ public class ParseTreePatternMatcher {
String[] tokenNames = parser.getTokenNames(); String[] tokenNames = parser.getTokenNames();
String[] ruleNames = parser.getRuleNames(); String[] ruleNames = parser.getRuleNames();
// make maps for quick look up // make maps for quick look up
// split pattern into chunks: sea (raw input) and islands (<ID>, <expr>) // split pattern into chunks: sea (raw input) and islands (<ID>, <expr>)
List<Chunk> chunks = split(pattern); List<Chunk> chunks = split(pattern);
@ -306,7 +234,7 @@ public class ParseTreePatternMatcher {
if ( pattern.matches(t) ) { if ( pattern.matches(t) ) {
subtrees.add(t); subtrees.add(t);
} }
if ( t instanceof RuleNode ) { if ( t instanceof RuleNode) {
RuleNode r = (RuleNode)t; RuleNode r = (RuleNode)t;
int n = r.getChildCount(); int n = r.getChildCount();
for (int i = 0; i<n; i++) { for (int i = 0; i<n; i++) {

View File

@ -0,0 +1,22 @@
package org.antlr.v4.runtime.tree.pattern;
import org.antlr.v4.runtime.tree.ParseTree;
public class Pattern {
public static final Pattern WildcardPattern =
new Pattern("...") {
public boolean matches(ParseTree t) {
return true;
}
};
protected String pattern;
public Pattern(String pattern) {
this.pattern = pattern;
}
public boolean matches(ParseTree t) {
return false;
}
}

View File

@ -0,0 +1,20 @@
package org.antlr.v4.runtime.tree.pattern;
class TagChunk extends Chunk { // <e:expr> or <ID>
public String tag;
public String label;
public TagChunk(String tag) {
this.tag = tag;
}
public TagChunk(String label, String tag) {
this.label = label;
this.tag = tag;
}
@Override
public String toString() {
if ( label!=null ) return label+":"+tag;
return tag;
}
}

View File

@ -0,0 +1,13 @@
package org.antlr.v4.runtime.tree.pattern;
class TextChunk extends Chunk {
public String text;
public TextChunk(String text) {
this.text = text;
}
@Override
public String toString() {
return "'"+text+"'";
}
}