forked from jasder/antlr
rm any/all pattern
This commit is contained in:
parent
8cace4e182
commit
f99cc52d39
|
@ -1,4 +1,4 @@
|
||||||
package org.antlr.v4.runtime.tree.pattern;
|
package org.antlr.v4.runtime;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
import org.antlr.v4.runtime.CommonToken;
|
import org.antlr.v4.runtime.CommonToken;
|
|
@ -1,63 +0,0 @@
|
||||||
package org.antlr.v4.runtime.tree.pattern;
|
|
||||||
|
|
||||||
import org.antlr.v4.runtime.CharStream;
|
|
||||||
import org.antlr.v4.runtime.Token;
|
|
||||||
import org.antlr.v4.runtime.TokenSource;
|
|
||||||
|
|
||||||
/** <...> tag matches anything until what follows or end of string.
|
|
||||||
* "<...> . <ID>" matches anything until an ID.
|
|
||||||
* "x = <...>"
|
|
||||||
*
|
|
||||||
* matches .+ not .*
|
|
||||||
*/
|
|
||||||
public class AnythingTagToken implements Token {
|
|
||||||
@Override
|
|
||||||
public int getChannel() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getText() {
|
|
||||||
return "...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getType() {
|
|
||||||
return Token.INVALID_TYPE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getLine() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getCharPositionInLine() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getTokenIndex() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getStartIndex() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getStopIndex() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TokenSource getTokenSource() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CharStream getInputStream() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -80,7 +80,6 @@ public class ParseTreePatternErrorStrategy extends DefaultErrorStrategy {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean isPatternTag(Token t) {
|
protected boolean isPatternTag(Token t) {
|
||||||
return t instanceof RuleTagToken ||
|
return t instanceof RuleTagToken;
|
||||||
t instanceof WildcardTagToken;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ 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.CommonTokenStream;
|
||||||
import org.antlr.v4.runtime.Lexer;
|
import org.antlr.v4.runtime.Lexer;
|
||||||
|
import org.antlr.v4.runtime.ListTokenSource;
|
||||||
import org.antlr.v4.runtime.Parser;
|
import org.antlr.v4.runtime.Parser;
|
||||||
import org.antlr.v4.runtime.ParserRuleContext;
|
import org.antlr.v4.runtime.ParserRuleContext;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
@ -53,10 +54,6 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class ParseTreePatternMatcher {
|
public class ParseTreePatternMatcher {
|
||||||
|
|
||||||
public static final String ANYTHING = "...";
|
|
||||||
public static final String WILDCARD = ".";
|
|
||||||
|
|
||||||
public static class CannotCreateLexerOrParser extends RuntimeException {
|
public static class CannotCreateLexerOrParser extends RuntimeException {
|
||||||
public CannotCreateLexerOrParser(Throwable e) {
|
public CannotCreateLexerOrParser(Throwable e) {
|
||||||
super(e);
|
super(e);
|
||||||
|
@ -135,9 +132,6 @@ public class ParseTreePatternMatcher {
|
||||||
if ( tree==null || patternTree==null ) {
|
if ( tree==null || patternTree==null ) {
|
||||||
return new ParseTreeMatchFailed(tree, null, pattern);
|
return new ParseTreeMatchFailed(tree, null, pattern);
|
||||||
}
|
}
|
||||||
if ( patternTree instanceof WildcardTagToken ) {
|
|
||||||
return new ParseTreeMatch(tree, pattern); // <.> matches any single node/subtree
|
|
||||||
}
|
|
||||||
// x and <ID>, x and y, or x and x; or could be mismatched types
|
// x and <ID>, x and y, or x and x; or could be mismatched types
|
||||||
if ( tree instanceof TerminalNode && patternTree instanceof TerminalNode ) {
|
if ( tree instanceof TerminalNode && patternTree instanceof TerminalNode ) {
|
||||||
TerminalNode t1 = (TerminalNode)tree;
|
TerminalNode t1 = (TerminalNode)tree;
|
||||||
|
@ -250,13 +244,7 @@ public class ParseTreePatternMatcher {
|
||||||
if ( chunk instanceof TagChunk ) {
|
if ( chunk instanceof TagChunk ) {
|
||||||
TagChunk tagChunk = (TagChunk)chunk;
|
TagChunk tagChunk = (TagChunk)chunk;
|
||||||
// add special rule token or conjure up new token from name
|
// add special rule token or conjure up new token from name
|
||||||
// if ( tagChunk.tag.equals(ANYTHING) ) {
|
if ( Character.isUpperCase(tagChunk.tag.charAt(0)) ) {
|
||||||
// tokens.add(new AnythingTagToken());
|
|
||||||
// }
|
|
||||||
if ( tagChunk.tag.equals(WILDCARD) ) {
|
|
||||||
tokens.add(new WildcardTagToken());
|
|
||||||
}
|
|
||||||
else if ( Character.isUpperCase(tagChunk.tag.charAt(0)) ) {
|
|
||||||
tokens.add(new TokenTagToken(tagChunk.tag, tokenNameToType.get(tagChunk.tag)));
|
tokens.add(new TokenTagToken(tagChunk.tag, tokenNameToType.get(tagChunk.tag)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -293,7 +281,7 @@ public class ParseTreePatternMatcher {
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Split "<ID> = <e:expr> ;" into 4 chunks */
|
/** Split "<ID> = <e:expr> ;" into 4 chunks for tokenizing by tokenize() */
|
||||||
public List<Chunk> split(String pattern) {
|
public List<Chunk> split(String pattern) {
|
||||||
int p = 0;
|
int p = 0;
|
||||||
int n = pattern.length();
|
int n = pattern.length();
|
||||||
|
@ -368,7 +356,7 @@ public class ParseTreePatternMatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// strip out the escape sequences
|
// strip out the escape sequences from text chunks but not tags
|
||||||
for (Chunk c : chunks) {
|
for (Chunk c : chunks) {
|
||||||
if ( c instanceof TextChunk ) {
|
if ( c instanceof TextChunk ) {
|
||||||
TextChunk tc = (TextChunk)c;
|
TextChunk tc = (TextChunk)c;
|
||||||
|
|
|
@ -1,62 +0,0 @@
|
||||||
package org.antlr.v4.runtime.tree.pattern;
|
|
||||||
|
|
||||||
import org.antlr.v4.runtime.CharStream;
|
|
||||||
import org.antlr.v4.runtime.Token;
|
|
||||||
import org.antlr.v4.runtime.TokenSource;
|
|
||||||
|
|
||||||
public class WildcardTagToken implements Token {
|
|
||||||
@Override
|
|
||||||
public int getChannel() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getText() {
|
|
||||||
return "<.>";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getType() {
|
|
||||||
return Token.INVALID_TYPE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getLine() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getCharPositionInLine() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getTokenIndex() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getStartIndex() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getStopIndex() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TokenSource getTokenSource() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CharStream getInputStream() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "<.>";
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue