Skip to content

Commit

Permalink
Merge pull request #37 from jdrem/mark-deprecated-items-#36
Browse files Browse the repository at this point in the history
mark-deprecated-items-#36
  • Loading branch information
jdrem committed Mar 19, 2022
2 parents 18447bf + f30be6e commit 3d7e5b9
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 36 deletions.
30 changes: 25 additions & 5 deletions src/main/java/net/remgant/tools/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import org.slf4j.LoggerFactory;

import java.io.PrintStream;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.function.Predicate;

public abstract class Parser {
Expand All @@ -35,24 +38,41 @@ public abstract class Parser {

static class State {
int stateNumber;
Token token;
Predicate<Token> predicate;
int nextStateNumber;
ParserAction action;

/**
* @deprecated Will be removed in 2.0.0
*/
@Deprecated
State(int stateNumber, Predicate<Token> predicate, int nextStateNumber, ParserAction action) {
this.stateNumber = stateNumber;
this.predicate = predicate;
this.nextStateNumber = nextStateNumber;
this.action = action;
}

State(int stateNumber, Token token, int nextStateNumber, ParserAction action) {
this.stateNumber = stateNumber;
this.token = token;
this.predicate = token.getPredicate();
this.nextStateNumber = nextStateNumber;
this.action = action;
}
}

private final List<List<State>> stateList;

/**
* @deprecated Will be removed in 2.0.0
*/
@Deprecated
protected void addState(int state, Predicate<Token> predicate, int nextState, ParserAction action) {
if (stateList.size() < state + 1) {
if (stateList.size() < state + 1)
for (int i=0; i<state + 1; i++)
for (int i = 0; i < state + 1; i++)
stateList.add(null);
List<State> list = new ArrayList<>();
stateList.set(state, list);
Expand All @@ -78,7 +98,7 @@ protected void addState(int state, Token token, int nextState, ParserAction acti
list = new ArrayList<>();
stateList.set(state, list);
}
list.add(new State(state, token.getPredicate(), nextState, action));
list.add(new State(state, token, nextState, action));
}

abstract protected void init();
Expand Down Expand Up @@ -107,8 +127,8 @@ public void printStateDiagram(@SuppressWarnings("SameParameterValue") PrintStrea
continue;
for (State s : l) {
String ts;
if (Token.tokenMap.containsKey(s.predicate))
ts = Token.tokenMap.get(s.predicate);
if (s.token != null)
ts = s.token.toString();
else
ts = s.predicate.toString();
out.printf("%d %s %d%n", s.stateNumber, ts, s.nextStateNumber);
Expand Down
75 changes: 58 additions & 17 deletions src/main/java/net/remgant/tools/parser/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
Expand All @@ -33,18 +34,27 @@
import java.util.stream.Stream;

public abstract class Token {
/**
* @deprecated Will be removed in 2.0.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Deprecated
protected @interface KeywordToken {
}

/**
* @deprecated Will be removed in 2.0.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Deprecated
protected @interface CharToken {
}

/**
* @deprecated Will be removed in 2.0.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Deprecated
Expand Down Expand Up @@ -79,6 +89,11 @@ protected Token() {

}

private Token(String value, Predicate<Token> predicate) {
this.value = value;
this.predicate = predicate;
}

@Override
public String toString() {
return value;
Expand Down Expand Up @@ -125,37 +140,46 @@ public Operator(String v) {
public static class Identifier extends Token {
private static final Pattern instancePattern = Pattern.compile("\\p{Alpha}\\w*");
public static final Predicate<Token> INSTANCE = (t) -> instancePattern.matcher(t.value).matches();
public static final Identifier INSTANCE_ = new Identifier("Identifier.INSTANCE", (t) -> instancePattern.matcher(t.value).matches());

static {
tokenMap.put(INSTANCE, "Identifier.INSTANCE");
}

private Identifier(String v, Predicate<Token> predicate) {
super(v, predicate);
}

public Identifier(String v) {
super(v);
}

public String toString() {
if (value == null)
return "Identifier";
return value.toString();
return value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return true;
return o != null && getClass() == o.getClass();
}
}

public static class CharString extends Token {
private final static Pattern instancePattern = Pattern.compile("((?<![\\\\])[\'\"])((?:.(?!(?<![\\\\])\\1))*.?)\\1");
public final static Predicate<Token> INSTANCE = (t) -> instancePattern.matcher(t.value).matches();
public final static Token INSTANCE_ = new CharString("CharString.INSTNANCE", INSTANCE);

static {
tokenMap.put(INSTANCE, "CharString.INSTANCE");
}

private CharString(String v, Predicate<Token> predicate) {
super(v, predicate);
}

public CharString(String v) {
super(v);
}
Expand All @@ -166,16 +190,21 @@ public CharString() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return true;
return o != null && getClass() == o.getClass();
}
}

public static class BooleanString extends Token {
final public static Pattern instancePattern = Pattern.compile("true|false");
final public static Predicate<Token> INSTANCE = (t) -> instancePattern.matcher(t.value).matches();
final public static Token INSTANCE_ = new BooleanString("BooleanString.INSTANCE", INSTANCE);

private BooleanString(String v, Predicate<Token> predicate) {
super(v, predicate);
}

public BooleanString(String v) {
super(v);
super(v, INSTANCE);
}
}

Expand All @@ -184,12 +213,17 @@ public static class NumericString extends Token {
final private static Pattern anyIntegerPattern = Pattern.compile("\\p{Digit}+");
final public static Predicate<Token> INSTANCE = (t) -> instancePattern.matcher(t.value).matches();
final public static Predicate<Token> ANY_INTEGER = (t) -> anyIntegerPattern.matcher(t.value).matches();
final public static Token INSTANCE_ = new NumericString("NumnericString.INSTANCE", INSTANCE);

static {
tokenMap.put(INSTANCE, "NumericString.INSTANCE");
tokenMap.put(ANY_INTEGER, "NumericString.ANY_INTEGER");
}

private NumericString(String v, Predicate<Token> predicate) {
super(v, predicate);
}

public NumericString(String v) {
super(v);
}
Expand All @@ -200,8 +234,7 @@ public NumericString() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return true;
return o != null && getClass() == o.getClass();
}
}

Expand Down Expand Up @@ -278,12 +311,19 @@ public static Token findToken(String s) {
}

static public class TokenSet implements Predicate<Token> {
Predicate<Token> predicates[];
Token[] tokens;
Predicate<Token>[] predicates;

public TokenSet(Predicate<Token>[] predicates) {
this.predicates = predicates;
}

public TokenSet(Token[] tokens) {
this.tokens = tokens;
//noinspection unchecked
this.predicates = Arrays.stream(tokens).map(Token::getPredicate).toArray(Predicate[]::new);
}

@Override
public boolean test(Token token) {
for (Predicate<Token> p : predicates)
Expand All @@ -293,11 +333,14 @@ public boolean test(Token token) {
}

public String toString() {
if (tokens != null) {
return Stream.of(tokens).map(Token::getValue).collect(Collectors.joining("|"));
}
return Stream.of(predicates)
.map(c -> {
if (c.getClass().getName().toString().contains("Identifier"))
if (c.getClass().getName().contains("Identifier"))
return "Identifier";
if (c.getClass().getName().toString().contains("NumericString"))
if (c.getClass().getName().contains("NumericString"))
return "NumericString";
return tokenMap.get(c);
})
Expand All @@ -308,6 +351,10 @@ public String toString() {
public static TokenSet of(Predicate<Token>... tokens) {
return new TokenSet(tokens);
}

public static TokenSet of(Token... tokens) {
return new TokenSet(tokens);
}
}

public static Predicate<Token> negate(Predicate<Token> predicate) {
Expand Down Expand Up @@ -354,9 +401,6 @@ public static TokenFinder createTokenFinderFromClass(Class<? extends Token> this

protected static void init(Class<?> thisClass) {
Field[] fields = thisClass.getDeclaredFields();
ImmutableSet.Builder<String> charBuilder = ImmutableSet.builder();
ImmutableSet.Builder<String> keywordBuilder = ImmutableSet.builder();
ImmutableSet.Builder<String> operatorBuilder = ImmutableSet.builder();
for (Field f : fields) {
Object o;
try {
Expand All @@ -367,15 +411,12 @@ protected static void init(Class<?> thisClass) {
if (o instanceof Char) {
Char aChar = (Char) o;
tokenMap.put(aChar.getPredicate(), aChar.getValue());
charBuilder.add(aChar.getValue());
} else if (o instanceof Keyword) {
Keyword keyword = (Keyword) o;
tokenMap.put(keyword.getPredicate(), keyword.getValue());
keywordBuilder.add(keyword.getValue());
} else if (o instanceof Operator) {
Operator operator = (Operator) o;
tokenMap.put(operator.getPredicate(), operator.getValue());
operatorBuilder.add(operator.getValue());
}
}
}
Expand Down
41 changes: 27 additions & 14 deletions src/test/java/net/remgant/tools/parser/test/SampleSQLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@

public class SampleSQLParser extends Parser {
public static class SQLToken extends Token {
@KeywordToken
final public static Token SELECT = new Keyword("SELECT");
@KeywordToken
final public static Token FROM = new Keyword("FROM");
@CharToken
final public static Token STAR = new Char("*");
@CharToken
final public static Token COMMA = new Char(",");
@KeywordToken
final public static Token SELECT = new Keyword("SELECT");
@KeywordToken
final public static Token FROM = new Keyword("FROM");
final public static Token WHERE = new Keyword("WHERE");

@CharToken
final public static Token STAR = new Char("*");
@CharToken
final public static Token COMMA = new Char(",");
final public static Token EQ = new Char("=");


static {
init(SQLToken.class);
Expand Down Expand Up @@ -72,7 +76,6 @@ public String toString() {

SampleSQLParser() {
super(ImmutableSet.of('*', ','));
init();
}

@Override
Expand All @@ -85,7 +88,7 @@ protected void init() {
((SelectCommand) c).setProjectAll(true);
return c;
});
addState(1, Token.Identifier.INSTANCE, 3, (s, c) -> {
addState(1, Identifier.INSTANCE_, 3, (s, c) -> {
((SelectCommand) c).addProjectTerm(s);
return c;
});
Expand All @@ -95,14 +98,14 @@ protected void init() {

// State 3
addState(3, FROM, 7, null);
addState(3, Token.Identifier.INSTANCE, 4, (s, c) -> {
addState(3, Identifier.INSTANCE_, 4, (s, c) -> {
((SelectCommand) c).addProjectTerm(s);
return c;
});
addState(3, COMMA, 1, null);

// State 4
addState(4, Identifier.INSTANCE, 6, (s, c) -> {
addState(4, Identifier.INSTANCE_, 6, (s, c) -> {
((SelectCommand) c).addProjectTerm(s);
return c;
});
Expand All @@ -116,14 +119,24 @@ protected void init() {

// State 6
addState(6, FROM, 7, null);
addState(6, Identifier.INSTANCE, 5, null);
addState(6, Identifier.INSTANCE_, 5, null);
addState(6, COMMA, 1, null);

// State 7
addState(7, Identifier.INSTANCE, 8, (s, c) -> {
addState(7, Identifier.INSTANCE_, 8, (s, c) -> {
((SelectCommand) c).addFromTable(s);
return c;
});

// State 8
addState(8, WHERE, 9, null);

// State 9
addState(9, Identifier.INSTANCE_, 10, null);

addState(10, EQ, 11, null);

addState(11, TokenSet.of(CharString.INSTANCE_, NumericString.INSTANCE_), 12, null);
}

public static void main(String[] args) {
Expand Down

0 comments on commit 3d7e5b9

Please sign in to comment.