Compare commits
No commits in common. "b87e25c8da5ca3280639e057ab69d07b8c0c977d" and "ce19dae60ea4f078c529b9f17dee18e186d1ede7" have entirely different histories.
b87e25c8da
...
ce19dae60e
21 changed files with 147 additions and 513 deletions
|
@ -11,33 +11,34 @@ public class Bag{
|
||||||
|
|
||||||
private Object value;
|
private Object value;
|
||||||
|
|
||||||
// private String path = "";
|
private String path = "";
|
||||||
|
|
||||||
public Bag(){
|
public Bag(){
|
||||||
this.map = new HashMap<>();
|
this.map = new HashMap<>();
|
||||||
// path = Thread.currentThread().getStackTrace()[2].toString();
|
path = Thread.currentThread().getStackTrace()[2].toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bag(Bag bag){
|
public Bag(Bag bag){
|
||||||
this.map = new HashMap<>(bag.map);
|
this.map = new HashMap<>(bag.map);
|
||||||
this.value = bag.value;
|
this.value = bag.value;
|
||||||
// this.path = bag.path;
|
this.path = bag.path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E> E get(){
|
public <E> E get(){
|
||||||
return (E) this.value;
|
return (E) this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public void addPath(String s){
|
public void addPath(String s){
|
||||||
// path+=s;
|
path+=s;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// public String path(){
|
public String path(){
|
||||||
// return this.path;
|
return this.path;
|
||||||
// }
|
}
|
||||||
|
|
||||||
public void set(Object value){
|
public void set(Object value){
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
// System.out.println("edit Bag "+this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E> E get(String key){
|
public <E> E get(String key){
|
||||||
|
@ -62,6 +63,6 @@ public class Bag{
|
||||||
String map = "";
|
String map = "";
|
||||||
for(Entry<String, Object> entry : this.map.entrySet()) map+=","+(entry.getKey())+" -> "+entry.getValue();
|
for(Entry<String, Object> entry : this.map.entrySet()) map+=","+(entry.getKey())+" -> "+entry.getValue();
|
||||||
if(map.length() > 0) map = map.substring(1);
|
if(map.length() > 0) map = map.substring(1);
|
||||||
return "([bag "+hashCode()+"] "+(value != null ? "| value="+value+" ":"")+"| map["+Arrays.toString(this.map.keySet().toArray())+"]="+map+" )";
|
return "([bag] "+(value != null ? "| value="+value+" ":"")+"| map["+Arrays.toString(this.map.keySet().toArray())+"]="+map+" )";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,11 @@ public class TokenValidator{
|
||||||
}
|
}
|
||||||
this.validated++;
|
this.validated++;
|
||||||
|
|
||||||
|
// StackTraceElement[] trace = Thread.currentThread().getStackTrace();
|
||||||
|
// int current = 0;
|
||||||
|
// while((current < trace.length) && (!(trace[++current].getClassName().endsWith("JavaParser"))));
|
||||||
|
// System.out.println("[validate] "+trace[current]);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -1,31 +1,22 @@
|
||||||
package dev.peerat.parser.java.tree;
|
package dev.peerat.parser.java.tree;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.BiConsumer;
|
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
import dev.peerat.parser.Bag;
|
import dev.peerat.parser.Bag;
|
||||||
import dev.peerat.parser.Token;
|
|
||||||
import dev.peerat.parser.TokenType;
|
import dev.peerat.parser.TokenType;
|
||||||
import dev.peerat.parser.java.JavaElement;
|
import dev.peerat.parser.java.JavaElement;
|
||||||
import dev.peerat.parser.java.value.ArrayAccessValue;
|
|
||||||
import dev.peerat.parser.java.value.BiValue;
|
import dev.peerat.parser.java.value.BiValue;
|
||||||
import dev.peerat.parser.java.value.MethodCallValue;
|
import dev.peerat.parser.java.value.MethodCallValue;
|
||||||
import dev.peerat.parser.java.value.ModifierValue;
|
import dev.peerat.parser.java.value.ModifierValue;
|
||||||
import dev.peerat.parser.java.value.StaticValue;
|
import dev.peerat.parser.java.value.StaticValue;
|
||||||
import dev.peerat.parser.java.value.TriValue;
|
import dev.peerat.parser.java.value.TriValue;
|
||||||
import dev.peerat.parser.java.value.Value;
|
|
||||||
import dev.peerat.parser.java.value.Value.ValueContainer;
|
import dev.peerat.parser.java.value.Value.ValueContainer;
|
||||||
import dev.peerat.parser.java.value.VariableAccessValue;
|
|
||||||
import dev.peerat.parser.state.RedirectStateTree;
|
import dev.peerat.parser.state.RedirectStateTree;
|
||||||
import dev.peerat.parser.state.StateTree;
|
import dev.peerat.parser.state.StateTree;
|
||||||
import dev.peerat.parser.tree.SyntaxTree;
|
import dev.peerat.parser.tree.SyntaxTree;
|
||||||
import dev.peerat.parser.tree.SyntaxTreeRegistery;
|
import dev.peerat.parser.tree.SyntaxTreeRegistery;
|
||||||
|
|
||||||
public class ValueTree extends SyntaxTree<JavaElement> { //TODO order tree by more used branch ? to not check a whole tree to go on the last branch...
|
public class ValueTree extends SyntaxTree<JavaElement> {
|
||||||
|
|
||||||
public ValueTree(SyntaxTreeRegistery<JavaElement> registery) {
|
public ValueTree(SyntaxTreeRegistery<JavaElement> registery) {
|
||||||
super(registery);
|
super(registery);
|
||||||
|
@ -38,25 +29,63 @@ public class ValueTree extends SyntaxTree<JavaElement> { //TODO order tree by mo
|
||||||
StateTree<JavaElement> function_container = this.registery.getNode(FunctionContainerTree.class);
|
StateTree<JavaElement> function_container = this.registery.getNode(FunctionContainerTree.class);
|
||||||
StateTree<JavaElement> class_container = this.registery.getNode(ClassContainerTree.class);
|
StateTree<JavaElement> class_container = this.registery.getNode(ClassContainerTree.class);
|
||||||
// VALUE
|
// VALUE
|
||||||
StateTree<JavaElement> value = this.current.then((validator) -> {
|
StateTree<JavaElement> value = this.current;
|
||||||
validator.getBag().set(new ValueChainBuilder());
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
StateTree<JavaElement> unary_value = new StateTree<>();
|
StateTree<JavaElement> unary_value = new StateTree<>();
|
||||||
StateTree<JavaElement> value_operation = new StateTree<>();
|
StateTree<JavaElement> value_operation = new StateTree<>();
|
||||||
|
|
||||||
StateTree<JavaElement> redirectRight = new RedirectStateTree<>(this.current, (global, local) -> {
|
value.then((v) -> {
|
||||||
global.<ValueChainBuilder>get().right(local.get());
|
return v.validate((t) -> {
|
||||||
|
System.out.println("entry value "+t);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
BiFunction<JavaElement, Bag, JavaElement> operationBuilder = (parent, bag) -> {
|
||||||
|
System.out.println("operationBuilder "+bag);
|
||||||
|
if (bag.has("current")) {
|
||||||
|
bag.set(new BiValue(new StaticValue(bag.get("current")), bag.get("action"), bag.get("right")));
|
||||||
|
} else {
|
||||||
|
bag.set(new BiValue(bag.get(), bag.get("action"), bag.get("right")));
|
||||||
|
}
|
||||||
|
System.out.println("value_op " + bag);
|
||||||
|
if(parent instanceof ValueContainer) ((ValueContainer)parent).addValue(bag.get());
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
BiFunction<JavaElement, Bag, JavaElement> staticValueBuilder = (parent, bag) -> {
|
||||||
|
System.out.println("staticValueBuilder "+bag);
|
||||||
|
StaticValue result = new StaticValue(bag.get("current"));
|
||||||
|
bag.set(result);
|
||||||
|
if(parent instanceof ValueContainer) ((ValueContainer)parent).addValue(result);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
BiFunction<JavaElement, Bag, JavaElement> modifierValueBuilder = (parent, bag) -> {
|
||||||
|
System.out.println("modifierValueBuilder "+bag);
|
||||||
|
ModifierValue result = new ModifierValue(bag.get("modifier"), bag.get());
|
||||||
|
bag.set(result);
|
||||||
|
if(parent instanceof ValueContainer) ((ValueContainer)parent).addValue(result);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
BiFunction<JavaElement, Bag, JavaElement> methodCallValueBuilder = (parent, bag) -> {
|
||||||
|
System.out.println("methodCallValueBuilder "+bag);
|
||||||
|
MethodCallValue result = new MethodCallValue(null, null, bag.get("current"), null);
|
||||||
|
bag.set(result);
|
||||||
|
if(parent instanceof ValueContainer) ((ValueContainer)parent).addValue(result);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
StateTree<JavaElement> redirectRight = new RedirectStateTree<>(value, (global, local) -> {
|
||||||
|
System.out.println("redirectRight g="+global+" ,l="+local);
|
||||||
|
global.set("right", local.get());
|
||||||
});
|
});
|
||||||
|
|
||||||
StateTree<JavaElement> value_instance = unary_value.then((validator) -> {
|
StateTree<JavaElement> value_instance = unary_value.equals("new");
|
||||||
validator.getBag().set(new ValueChainBuilder());
|
|
||||||
return true;
|
|
||||||
}).equals("new");
|
|
||||||
StateTree<JavaElement> value_instance_type = value_instance.redirect(type);
|
StateTree<JavaElement> value_instance_type = value_instance.redirect(type);
|
||||||
StateTree<JavaElement> value_instance_params = value_instance_type.equals("(");
|
StateTree<JavaElement> value_instance_params = value_instance_type.equals("(");
|
||||||
StateTree<JavaElement> value_instance_params_end = value_instance_params.equals(")");
|
StateTree<JavaElement> value_instance_params_end = value_instance_params.equals(")");
|
||||||
StateTree<JavaElement> value_instance_param = value_instance_params.redirect(this.current);
|
StateTree<JavaElement> value_instance_param = value_instance_params.redirect(value);
|
||||||
value_instance_param.then(value_instance_params_end);
|
value_instance_param.then(value_instance_params_end);
|
||||||
value_instance_param.equals(",").then(value_instance_param);
|
value_instance_param.equals(",").then(value_instance_param);
|
||||||
value_instance_params_end.<JavaElement>end();
|
value_instance_params_end.<JavaElement>end();
|
||||||
|
@ -65,85 +94,77 @@ public class ValueTree extends SyntaxTree<JavaElement> { //TODO order tree by mo
|
||||||
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))).end();
|
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))).end();
|
||||||
|
|
||||||
StateTree<JavaElement> value_instance_array_init = value_instance_type.equals("{");
|
StateTree<JavaElement> value_instance_array_init = value_instance_type.equals("{");
|
||||||
StateTree<JavaElement> value_instance_array_init_value = value_instance_array_init.redirect(this.current);
|
StateTree<JavaElement> value_instance_array_init_value = value_instance_array_init.redirect(value);
|
||||||
value_instance_array_init_value.equals(",").then(value_instance_array_init_value);
|
value_instance_array_init_value.equals(",").then(value_instance_array_init_value);
|
||||||
value_instance_array_init_value.equals("}").end();
|
value_instance_array_init_value.equals("}").end();
|
||||||
|
|
||||||
StateTree<JavaElement> value_instance_array = value_instance_type.equals("[");
|
StateTree<JavaElement> value_instance_array = value_instance_type.equals("[");
|
||||||
StateTree<JavaElement> value_instance_array_value = value_instance_array.redirect(this.current);
|
StateTree<JavaElement> value_instance_array_value = value_instance_array.redirect(value);
|
||||||
StateTree<JavaElement> value_instance_array_end = value_instance_array_value.equals("]");
|
StateTree<JavaElement> value_instance_array_end = value_instance_array_value.equals("]");
|
||||||
value_instance_array.then(value_instance_array_end);
|
value_instance_array.then(value_instance_array_end);
|
||||||
value_instance_array_end.end();
|
value_instance_array_end.end();
|
||||||
value_instance_array_end.then(value_instance_array);
|
value_instance_array_end.then(value_instance_array);
|
||||||
|
|
||||||
StateTree<JavaElement> value_name = unary_value.then((validator) -> {
|
StateTree<JavaElement> value_name = unary_value
|
||||||
validator.getBag().set(new ValueChainBuilder());
|
|
||||||
return true;
|
|
||||||
})
|
|
||||||
.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME),
|
.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME),
|
||||||
(bag, token) ->{
|
(bag, token) -> bag.set("current", token)));
|
||||||
bag.<ValueChainBuilder>get().nameValue(token);
|
value_name.end(staticValueBuilder);
|
||||||
}));
|
|
||||||
value_name.end(ValueChainBuilder.build((builder) -> builder.buildStatic()));
|
|
||||||
value_name.then(value_operation);
|
value_name.then(value_operation);
|
||||||
|
|
||||||
unary_value.then((validator) -> {
|
unary_value.then((validator) -> validator.validate(
|
||||||
validator.getBag().set(new ValueChainBuilder());
|
|
||||||
return true;
|
|
||||||
}).then((validator) -> validator.validate(
|
|
||||||
(token) -> token.getType().equals(TokenType.STRING) || token.getType().equals(TokenType.CHAR),
|
(token) -> token.getType().equals(TokenType.STRING) || token.getType().equals(TokenType.CHAR),
|
||||||
(bag, token) -> bag.<ValueChainBuilder>get().constantValue(token)))
|
(bag, token) -> bag.set("current", token)))
|
||||||
.end(ValueChainBuilder.build((builder) -> builder.buildStatic()));
|
.end(staticValueBuilder);
|
||||||
|
|
||||||
value_operation.equals(">", action(">")).then(redirectRight).end(ValueChainBuilder.build((builder) -> builder.buildOperation()));
|
value_operation.equals(">","action",">").then(redirectRight).end(operationBuilder);
|
||||||
value_operation.equals("<",action("<")).then(redirectRight).end(ValueChainBuilder.build((builder) -> builder.buildOperation()));
|
value_operation.equals("<","action","<").then(redirectRight).end(operationBuilder);
|
||||||
value_operation.equals("<",action("<")).equals("<","action","<<").then(redirectRight).end(ValueChainBuilder.build((builder) -> builder.buildOperation()));
|
value_operation.equals("<","action","<").equals("<","action","<<").then(redirectRight).end(operationBuilder);
|
||||||
value_operation.equals("+",action("+")).then(redirectRight).end(ValueChainBuilder.build((builder) -> builder.buildOperation()));
|
value_operation.equals("+","action","+").then(redirectRight).end(operationBuilder);
|
||||||
StateTree<JavaElement> value_operation_bit_and = value_operation.equals("&",action("&"));
|
StateTree<JavaElement> value_operation_bit_and = value_operation.equals("&","action","&");
|
||||||
value_operation_bit_and.then(redirectRight).end(ValueChainBuilder.build((builder) -> builder.buildOperation()));
|
value_operation_bit_and.then(redirectRight).end(operationBuilder);
|
||||||
value_operation_bit_and.equals("&",action("&&")).then(redirectRight).end(ValueChainBuilder.build((builder) -> builder.buildOperation()));
|
value_operation_bit_and.equals("&","action","&&").then(redirectRight).end(operationBuilder);
|
||||||
StateTree<JavaElement> value_operation_bit_or = value_operation.equals("|",action("|"));
|
StateTree<JavaElement> value_operation_bit_or = value_operation.equals("|","action","|");
|
||||||
value_operation_bit_or.then(redirectRight).end(ValueChainBuilder.build((builder) -> builder.buildOperation()));
|
value_operation_bit_or.then(redirectRight).end(operationBuilder);
|
||||||
value_operation_bit_or.equals("|",action("||")).then(redirectRight).end(ValueChainBuilder.build((builder) -> builder.buildOperation()));
|
value_operation_bit_or.equals("|","action","||").then(redirectRight).end(operationBuilder);
|
||||||
|
|
||||||
value.equals("!",modifier()).redirect(this.current, (global, local) -> global.<ValueChainBuilder>get().rebase(local.get())).end(ValueChainBuilder.build((builder) -> builder.buildModifier()));
|
value.equals("!","modifier").redirect(value).end(modifierValueBuilder);
|
||||||
|
|
||||||
StateTree<JavaElement> left_value = value.redirect(unary_value, (global, local) -> global.<ValueChainBuilder>get().rebase(local.get()));
|
StateTree<JavaElement> left_value = value.redirect(unary_value);
|
||||||
left_value.end(ValueChainBuilder.build());
|
left_value.end((parent, bag) -> {
|
||||||
|
if(parent instanceof ValueContainer) ((ValueContainer)parent).addValue(bag.get());
|
||||||
|
return null;
|
||||||
|
});
|
||||||
left_value.equals("?")
|
left_value.equals("?")
|
||||||
.redirect(this.current, (global, local) -> global.set("success", local.get()))
|
.redirect(value, (global, local) -> global.set("success", local.get()))
|
||||||
.equals(":")
|
.equals(":")
|
||||||
.redirect(this.current, (global, local) -> global.set("fail", local.get()))
|
.redirect(value, (global, local) -> global.set("fail", local.get()))
|
||||||
.end((parent, bag) -> {
|
.end((parent, bag) -> {
|
||||||
bag.set(new TriValue(bag.<ValueChainBuilder>get().base, bag.get("success"), bag.get("fail")));
|
System.out.println("tri value "+bag);
|
||||||
|
bag.set(new TriValue(bag.get(), bag.get("success"), bag.get("fail")));
|
||||||
if(parent instanceof ValueContainer) ((ValueContainer)parent).addValue(bag.get());
|
if(parent instanceof ValueContainer) ((ValueContainer)parent).addValue(bag.get());
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
StateTree<JavaElement> value_array = left_value.equals("[");
|
StateTree<JavaElement> value_array = left_value.equals("[");
|
||||||
StateTree<JavaElement> value_array_value = value_array.redirect(this.current, (global, local) -> global.<ValueChainBuilder>get().arrayAccess(local.get()));
|
StateTree<JavaElement> value_array_value = value_array.redirect(value);
|
||||||
StateTree<JavaElement> value_array_end = value_array_value.equals("]");
|
StateTree<JavaElement> value_array_end = value_array_value.equals("]");
|
||||||
value_array_end.end(ValueChainBuilder.build());
|
value_array_end.end();
|
||||||
value_array_end.then(value_array);
|
value_array_end.then(value_array);
|
||||||
value_array_end.equals(".", (bag, token) -> bag.<ValueChainBuilder>get().dot()).then(value_name);
|
|
||||||
|
|
||||||
StateTree<JavaElement> operation_call = value_name.equals(".", (bag, token) -> bag.<ValueChainBuilder>get().dot());
|
StateTree<JavaElement> operation_call = value_name.equals(".");
|
||||||
operation_call.then(value_name);
|
operation_call.then(value_name);
|
||||||
StateTree<JavaElement> operation_begin = value_name.equals("(", (bag, token) -> bag.<ValueChainBuilder>get().parameter());
|
StateTree<JavaElement> operation_begin = value_name.equals("(");
|
||||||
StateTree<JavaElement> operation_end = operation_begin.equals(")", (bag, token) -> bag.<ValueChainBuilder>get().parameter());
|
StateTree<JavaElement> operation_end = operation_begin.equals(")");
|
||||||
operation_end.then(operation_call);
|
operation_end.then(operation_call);
|
||||||
operation_end.end(ValueChainBuilder.build((builder) -> builder.buildMethodCall()));
|
operation_end.end(methodCallValueBuilder);
|
||||||
StateTree<JavaElement> operation_value = operation_begin.redirect(this.current, (global, local) -> global.<ValueChainBuilder>get().parameter(local.get()));
|
StateTree<JavaElement> operation_value = operation_begin.redirect(value);
|
||||||
operation_value.then(operation_end);
|
operation_value.then(operation_end);
|
||||||
operation_value.equals(",")
|
operation_value.equals(",")
|
||||||
.then(operation_value);
|
.then(operation_value);
|
||||||
|
|
||||||
StateTree<JavaElement> value_parenthesis = unary_value.then((validator) -> {
|
StateTree<JavaElement> value_parenthesis = unary_value.equals("(");
|
||||||
validator.getBag().set(new ValueChainBuilder());
|
|
||||||
return true;
|
|
||||||
}).equals("(");
|
|
||||||
StateTree<JavaElement> value_parenthesis_close = value_parenthesis.equals(")");
|
StateTree<JavaElement> value_parenthesis_close = value_parenthesis.equals(")");
|
||||||
StateTree<JavaElement> value_inside = value_parenthesis.redirect(this.current);
|
StateTree<JavaElement> value_inside = value_parenthesis.redirect(value);
|
||||||
StateTree<JavaElement> value_inside_type = value_parenthesis.redirect(type);
|
StateTree<JavaElement> value_inside_type = value_parenthesis.redirect(type);
|
||||||
|
|
||||||
value_inside_type.then(value_inside);
|
value_inside_type.then(value_inside);
|
||||||
|
@ -164,143 +185,5 @@ public class ValueTree extends SyntaxTree<JavaElement> { //TODO order tree by mo
|
||||||
value_lambda.redirect(unary_value).end();
|
value_lambda.redirect(unary_value).end();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private BiConsumer<Bag, Token> action(String action){
|
|
||||||
return (bag, token) -> bag.<ValueChainBuilder>get().action(action);
|
|
||||||
}
|
|
||||||
|
|
||||||
private BiConsumer<Bag, Token> modifier(){
|
|
||||||
return (bag, token) -> bag.<ValueChainBuilder>get().modifier(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ValueChainBuilder{
|
|
||||||
|
|
||||||
static BiFunction<JavaElement, Bag, JavaElement> build(Consumer<ValueChainBuilder> builder){
|
|
||||||
return (parent, bag) -> {
|
|
||||||
ValueChainBuilder chainBuilder = bag.get();
|
|
||||||
builder.accept(chainBuilder);
|
|
||||||
chainBuilder.build(parent, bag);
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static BiFunction<JavaElement, Bag, JavaElement> build(){
|
|
||||||
return (parent, bag) -> {
|
|
||||||
ValueChainBuilder chainBuilder = bag.get();
|
|
||||||
chainBuilder.build(parent, bag);
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO fall back branch problem cause one instance in multiple branch's bag ?
|
|
||||||
private Value base;
|
|
||||||
|
|
||||||
private Token current;
|
|
||||||
|
|
||||||
private Token modifier;
|
|
||||||
|
|
||||||
private String action;
|
|
||||||
private Value right;
|
|
||||||
|
|
||||||
private List<Value> parameters;
|
|
||||||
|
|
||||||
ValueChainBuilder(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void rebase(Value base){
|
|
||||||
this.base = base;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void nameValue(Token token) {
|
|
||||||
this.current = token;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void constantValue(Token token){
|
|
||||||
this.current = token;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void action(String action){
|
|
||||||
this.action = action;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void right(Value value){
|
|
||||||
this.right = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void modifier(Token token){
|
|
||||||
this.modifier = token;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void dot(){
|
|
||||||
if(this.base == null){
|
|
||||||
buildStatic();
|
|
||||||
this.current = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(this.current != null){
|
|
||||||
buildVariableAccess();
|
|
||||||
this.current = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void parameter(Value value){
|
|
||||||
this.parameters.add(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void parameter(){
|
|
||||||
if(this.parameters == null){
|
|
||||||
this.parameters = new ArrayList<>();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
buildMethodCall();
|
|
||||||
this.current = null;
|
|
||||||
this.parameters = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void arrayAccess(Value value){
|
|
||||||
this.base = new ArrayAccessValue(this.base, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void buildModifier(){
|
|
||||||
if(this.base == null) return; //TODO throws exception in this case
|
|
||||||
this.base = new ModifierValue(this.modifier, this.base);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void buildStatic(){
|
|
||||||
if(this.current == null) return;
|
|
||||||
this.base = new StaticValue(this.current);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void buildOperation(){
|
|
||||||
this.base = new BiValue(current == null ? base : new StaticValue(current), action, right);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void buildMethodCall(){
|
|
||||||
if(this.current == null) return;
|
|
||||||
this.base = new MethodCallValue(this.base, null, this.current, this.parameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void buildVariableAccess(){
|
|
||||||
this.base = new VariableAccessValue(this.base, this.current);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void build(JavaElement parent, Bag bag){
|
|
||||||
bag.set(base);
|
|
||||||
if(parent instanceof ValueContainer) ((ValueContainer)parent).addValue(base);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString(){
|
|
||||||
String result = "ValueChainBuilder{";
|
|
||||||
try{
|
|
||||||
for(Field field : getClass().getDeclaredFields()){
|
|
||||||
result+="\n\t"+field.getName()+"="+field.get(this);
|
|
||||||
}
|
|
||||||
}catch(Exception ex){}
|
|
||||||
|
|
||||||
return result+"\n}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,48 +0,0 @@
|
||||||
package dev.peerat.parser.java.value;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import dev.peerat.parser.java.JavaElement;
|
|
||||||
import dev.peerat.parser.java.visitor.JavaVisitor;
|
|
||||||
import dev.peerat.parser.visitor.VisitorBag;
|
|
||||||
|
|
||||||
public class ArrayAccessValue extends Value{
|
|
||||||
|
|
||||||
private Value base;
|
|
||||||
private Value access;
|
|
||||||
|
|
||||||
public ArrayAccessValue(Value base, Value access){
|
|
||||||
this.base = base;
|
|
||||||
this.access = access;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Value base(){
|
|
||||||
return this.base;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Value getAccessor(){
|
|
||||||
return this.access;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void build(Builder builder) throws Exception {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <E extends JavaElement> void findAll(Function<JavaElement, Boolean> finder, List<E> list) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public VisitorBag visit(JavaVisitor<?> visitor) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -65,8 +65,4 @@ public class MethodCallValue extends Value{
|
||||||
return bag;
|
return bag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString(){
|
|
||||||
return "[MethodCallValue] "+base+".<"+generic+">"+token+"("+parameters+")";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,9 +49,6 @@ public class VariableAccessValue extends Value{
|
||||||
return bag;
|
return bag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString(){
|
|
||||||
return "[VariableAccessValue] "+base+"."+variable;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
package dev.peerat.parser.java.visitor;
|
|
||||||
|
|
||||||
import dev.peerat.parser.java.AnnotationClass;
|
|
||||||
import dev.peerat.parser.visitor.VisitorBag;
|
|
||||||
|
|
||||||
public class JavaAnnotationClassVisitor extends JavaVisitor<AnnotationClass>{
|
|
||||||
|
|
||||||
public JavaAnnotationClassVisitor(){
|
|
||||||
super(AnnotationClass.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public VisitorBag visitElement(AnnotationClass element) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -27,16 +27,6 @@ public class JavaAnnotationVisitor extends JavaVisitor<Annotation>{
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JavaAnnotationVisitor noValues(){
|
|
||||||
//TODO
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JavaAnnotationVisitor hasValues(){
|
|
||||||
//TODO
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JavaAnnotationVisitor hasKey(Predicate<String> validator){
|
public JavaAnnotationVisitor hasKey(Predicate<String> validator){
|
||||||
this.keyFilter = (this.keyFilter == null) ? validator : this.keyFilter.and(validator);
|
this.keyFilter = (this.keyFilter == null) ? validator : this.keyFilter.and(validator);
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
package dev.peerat.parser.java.visitor;
|
|
||||||
|
|
||||||
import dev.peerat.parser.java.Enumeration;
|
|
||||||
import dev.peerat.parser.visitor.VisitorBag;
|
|
||||||
|
|
||||||
public class JavaEnumerationVisitor extends JavaVisitor<Enumeration>{
|
|
||||||
|
|
||||||
public JavaEnumerationVisitor(){
|
|
||||||
super(Enumeration.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public VisitorBag visitElement(Enumeration element){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
package dev.peerat.parser.java.visitor;
|
|
||||||
|
|
||||||
import dev.peerat.parser.java.Import;
|
|
||||||
import dev.peerat.parser.visitor.VisitorBag;
|
|
||||||
|
|
||||||
public class JavaImportVisitor extends JavaVisitor<Import>{
|
|
||||||
|
|
||||||
public JavaImportVisitor(){
|
|
||||||
super(Import.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public VisitorBag visitElement(Import element){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
package dev.peerat.parser.java.visitor;
|
|
||||||
|
|
||||||
import dev.peerat.parser.java.Interface;
|
|
||||||
import dev.peerat.parser.visitor.VisitorBag;
|
|
||||||
|
|
||||||
public class JavaInterfaceVisitor extends JavaVisitor<Interface>{
|
|
||||||
|
|
||||||
public JavaInterfaceVisitor(){
|
|
||||||
super(Interface.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public VisitorBag visitElement(Interface element){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,15 +1,15 @@
|
||||||
package dev.peerat.parser.java.visitor;
|
package dev.peerat.parser.java.visitor;
|
||||||
|
|
||||||
|
import dev.peerat.parser.java.AnnotationClass;
|
||||||
|
import dev.peerat.parser.java.Import;
|
||||||
import dev.peerat.parser.java.JavaElement;
|
import dev.peerat.parser.java.JavaElement;
|
||||||
import dev.peerat.parser.java.visitor.operation.JavaOperationVisitor;
|
import dev.peerat.parser.java.Operation;
|
||||||
import dev.peerat.parser.java.visitor.value.JavaValueVisitor;
|
import dev.peerat.parser.java.value.Value;
|
||||||
import dev.peerat.parser.visitor.Visitor;
|
import dev.peerat.parser.visitor.Visitor;
|
||||||
import dev.peerat.parser.visitor.VisitorBag;
|
import dev.peerat.parser.visitor.VisitorBag;
|
||||||
|
|
||||||
public abstract class JavaVisitor<T extends JavaElement> extends Visitor<JavaElement>{
|
public abstract class JavaVisitor<T extends JavaElement> extends Visitor<JavaElement>{
|
||||||
|
|
||||||
//TODO current system don't do litteraly ALL, juste DIRECT SUB ELEMENT
|
|
||||||
|
|
||||||
public static <T extends JavaElement> JavaVisitor<T> base(Visitor<T> visitor){
|
public static <T extends JavaElement> JavaVisitor<T> base(Visitor<T> visitor){
|
||||||
return new JavaVisitor<T>(null){
|
return new JavaVisitor<T>(null){
|
||||||
|
|
||||||
|
@ -47,16 +47,16 @@ public abstract class JavaVisitor<T extends JavaElement> extends Visitor<JavaEle
|
||||||
return new JavaClassVisitor();
|
return new JavaClassVisitor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaInterfaceVisitor allInterface(){
|
public static JavaVisitor<JavaElement> allInterface(){
|
||||||
return new JavaInterfaceVisitor();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaEnumerationVisitor allEnum(){
|
public static JavaVisitor<JavaElement> allEnum(){
|
||||||
return new JavaEnumerationVisitor();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaAnnotationClassVisitor allAnnotationClass(){
|
public static JavaVisitor<AnnotationClass> allAnnotationClass(){
|
||||||
return new JavaAnnotationClassVisitor();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaAnnotationVisitor allAnnotation(){
|
public static JavaAnnotationVisitor allAnnotation(){
|
||||||
|
@ -71,21 +71,21 @@ public abstract class JavaVisitor<T extends JavaElement> extends Visitor<JavaEle
|
||||||
return new JavaFunctionVisitor();
|
return new JavaFunctionVisitor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaValueVisitor allValue(){
|
public static JavaVisitor<Value> allValue(){
|
||||||
return new JavaValueVisitor();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaOperationVisitor allOperation(){
|
public static JavaVisitor<Operation> allOperation(){
|
||||||
return new JavaOperationVisitor();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaImportVisitor allImport(){
|
public static JavaVisitor<Import> allImport(){
|
||||||
return new JavaImportVisitor();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Class<?> type;
|
private Class<?> type;
|
||||||
|
|
||||||
public JavaVisitor(Class<?> type){
|
JavaVisitor(Class<?> type){
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
package dev.peerat.parser.java.visitor.operation;
|
|
||||||
|
|
||||||
import dev.peerat.parser.java.Operation;
|
|
||||||
import dev.peerat.parser.java.visitor.JavaVisitor;
|
|
||||||
import dev.peerat.parser.visitor.VisitorBag;
|
|
||||||
|
|
||||||
public class JavaOperationVisitor extends JavaVisitor<Operation>{
|
|
||||||
|
|
||||||
public JavaOperationVisitor(){
|
|
||||||
super(Operation.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public VisitorBag visitElement(Operation element){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
package dev.peerat.parser.java.visitor.value;
|
|
||||||
|
|
||||||
import dev.peerat.parser.java.value.Value;
|
|
||||||
import dev.peerat.parser.java.visitor.JavaVisitor;
|
|
||||||
import dev.peerat.parser.visitor.VisitorBag;
|
|
||||||
|
|
||||||
public class JavaValueVisitor extends JavaVisitor<Value>{
|
|
||||||
|
|
||||||
public JavaValueVisitor(){
|
|
||||||
super(Value.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public VisitorBag visitElement(Value element){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,6 +10,8 @@ public class RedirectStateTree<E, T extends E> extends StateTree<T>{
|
||||||
private StateTree<E> redirect;
|
private StateTree<E> redirect;
|
||||||
private BiConsumer<Bag, Bag> group;
|
private BiConsumer<Bag, Bag> group;
|
||||||
|
|
||||||
|
// private StackTraceElement calling;
|
||||||
|
|
||||||
public RedirectStateTree(StateTree<E> redirect, BiConsumer<Bag, Bag> group){
|
public RedirectStateTree(StateTree<E> redirect, BiConsumer<Bag, Bag> group){
|
||||||
super();
|
super();
|
||||||
this.redirect = redirect;
|
this.redirect = redirect;
|
||||||
|
@ -17,8 +19,8 @@ public class RedirectStateTree<E, T extends E> extends StateTree<T>{
|
||||||
|
|
||||||
// StackTraceElement[] trace = Thread.currentThread().getStackTrace();
|
// StackTraceElement[] trace = Thread.currentThread().getStackTrace();
|
||||||
// int current = 0;
|
// int current = 0;
|
||||||
// while((current < trace.length) && (!(trace[++current].getClassName().contains("Value"))));
|
// while((current < trace.length) && (!(trace[++current].getClassName().endsWith("JavaParser"))));
|
||||||
//
|
|
||||||
// this.calling = trace[current];
|
// this.calling = trace[current];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,6 +35,8 @@ public class RedirectStateTree<E, T extends E> extends StateTree<T>{
|
||||||
if(builded == null) return null;
|
if(builded == null) return null;
|
||||||
builded.build(branch, element);
|
builded.build(branch, element);
|
||||||
|
|
||||||
|
// System.out.println("[redirect done] "+calling);
|
||||||
|
|
||||||
this.group.accept(currentBag, localBag);
|
this.group.accept(currentBag, localBag);
|
||||||
branch.setBag(currentBag);
|
branch.setBag(currentBag);
|
||||||
validator.merge(branch);
|
validator.merge(branch);
|
||||||
|
|
|
@ -7,7 +7,6 @@ import java.util.function.BiFunction;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import dev.peerat.parser.Bag;
|
import dev.peerat.parser.Bag;
|
||||||
import dev.peerat.parser.Token;
|
|
||||||
import dev.peerat.parser.TokenValidator;
|
import dev.peerat.parser.TokenValidator;
|
||||||
|
|
||||||
public class StateTree<E>{
|
public class StateTree<E>{
|
||||||
|
@ -17,7 +16,6 @@ public class StateTree<E>{
|
||||||
Function<TokenValidator, Boolean> checker;
|
Function<TokenValidator, Boolean> checker;
|
||||||
private BuilderStateTree<E, ?> builder;
|
private BuilderStateTree<E, ?> builder;
|
||||||
|
|
||||||
//TODO checker in constructeur ?
|
|
||||||
public StateTree(){
|
public StateTree(){
|
||||||
this.childs = new ArrayList<>();
|
this.childs = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
@ -93,10 +91,6 @@ public class StateTree<E>{
|
||||||
return then((validator) -> validator.validate((token) -> token.getValue().equals(value), (bag, token) -> bag.set(key, val)));
|
return then((validator) -> validator.validate((token) -> token.getValue().equals(value), (bag, token) -> bag.set(key, val)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public StateTree<E> equals(String value, BiConsumer<Bag, Token> consumer){
|
|
||||||
return then((validator) -> validator.validate((token) -> token.getValue().equals(value), consumer));
|
|
||||||
}
|
|
||||||
|
|
||||||
public StateTree<E> redirect(StateTree<E> target){
|
public StateTree<E> redirect(StateTree<E> target){
|
||||||
return then(new RedirectStateTree<>(target, (global, local) -> global.set(local.get())));
|
return then(new RedirectStateTree<>(target, (global, local) -> global.set(local.get())));
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,8 @@ package dev.peerat.parser.visitor;
|
||||||
|
|
||||||
import static dev.peerat.parser.java.visitor.JavaVisitor.base;
|
import static dev.peerat.parser.java.visitor.JavaVisitor.base;
|
||||||
import static dev.peerat.parser.java.visitor.JavaVisitor.allClass;
|
import static dev.peerat.parser.java.visitor.JavaVisitor.allClass;
|
||||||
import static dev.peerat.parser.java.visitor.JavaVisitor.allAnnotation;
|
|
||||||
import static dev.peerat.parser.java.visitor.JavaVisitor.allJavaElement;
|
import static dev.peerat.parser.java.visitor.JavaVisitor.allJavaElement;
|
||||||
import static dev.peerat.parser.visitor.Operator.collect;
|
import static dev.peerat.parser.visitor.Operator.collect;
|
||||||
import static dev.peerat.parser.visitor.Operator.and;
|
|
||||||
import static dev.peerat.parser.visitor.Operator.or;
|
|
||||||
|
|
||||||
import dev.peerat.parser.java.JavaFile;
|
import dev.peerat.parser.java.JavaFile;
|
||||||
import dev.peerat.parser.java.JavaParser;
|
import dev.peerat.parser.java.JavaParser;
|
||||||
|
@ -28,7 +25,7 @@ public class Example {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
System.out.println("allJavaElement in class : "+result.toList());
|
System.out.println("result: "+result.toList());
|
||||||
|
|
||||||
result = container.visit(base(collect(
|
result = container.visit(base(collect(
|
||||||
allClass()
|
allClass()
|
||||||
|
@ -38,24 +35,7 @@ public class Example {
|
||||||
)
|
)
|
||||||
)));
|
)));
|
||||||
|
|
||||||
System.out.println("all class: "+result.toList());
|
System.out.println("result: "+result.toList());
|
||||||
|
|
||||||
result = container.visit(base(
|
|
||||||
collect(
|
|
||||||
allClass()
|
|
||||||
.isPublic()
|
|
||||||
.elements(
|
|
||||||
or(
|
|
||||||
allAnnotation().nameFilter((name) -> name.startsWith("T")),
|
|
||||||
allAnnotation().valueFilter((name, value) -> name.startsWith("t"))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
;
|
|
||||||
|
|
||||||
System.out.println("and op : "+result.toList());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,83 +1,46 @@
|
||||||
package dev.peerat.parser.visitor;
|
package dev.peerat.parser.visitor;
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class Operator{
|
public class Operator{
|
||||||
|
|
||||||
//Success ? Fail ?
|
//Success ? Fail ?
|
||||||
public static <E> Visitor<E> and(Visitor<E>... visitors){
|
public static <E,T extends Visitor<E>> T and(T... visitors){
|
||||||
return new Visitor<E>(){
|
return (T) new Visitor<E>(){
|
||||||
@Override
|
@Override
|
||||||
public VisitorBag visit(E element){
|
public VisitorBag visit(E element){
|
||||||
VisitorBag bag = new VisitorBag();
|
VisitorBag bag = new VisitorBag();
|
||||||
Set<Object> set = new HashSet<>();
|
for(T visitor : visitors){
|
||||||
for(Visitor<E> visitor : visitors){
|
|
||||||
VisitorBag vbag = visitor.visit(element);
|
VisitorBag vbag = visitor.visit(element);
|
||||||
if(!vbag.isValidated()) return new VisitorBag();
|
|
||||||
bag.merge(vbag);
|
bag.merge(vbag);
|
||||||
for(Object computed : vbag.computeList()){
|
if(!vbag.isValidated()) return new VisitorBag();
|
||||||
set.add(computed);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for(Object obj : set) bag.compute(obj);
|
|
||||||
return bag;
|
return bag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canVisit(Class<?> type){
|
public boolean canVisit(Class<?> type){
|
||||||
for(Visitor<E> visitor : visitors){
|
return true;
|
||||||
if(visitor.canVisit(type)) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
//Quid de si c'est empty ?
|
//Quid de si c'est empty ?
|
||||||
//Quid de si on a un collect dans les 2 ? ou dans 1 qui sourtout ne sera peux être pas appeler ?
|
public static <E, T extends Visitor<E>> T or(T... visitors){
|
||||||
public static <E> Visitor<E> or(Visitor<E>... visitors){
|
return (T) new Visitor<E>(){
|
||||||
return new Visitor<E>(){
|
|
||||||
@Override
|
@Override
|
||||||
public VisitorBag visit(E element){
|
public VisitorBag visit(E element){
|
||||||
VisitorBag bag = new VisitorBag();
|
VisitorBag bag = new VisitorBag();
|
||||||
for(Visitor<E> visitor : visitors){
|
for(T visitor : visitors){
|
||||||
VisitorBag vbag = visitor.visit(element);
|
VisitorBag vbag = visitor.visit(element);
|
||||||
if(!vbag.isValidated()) return new VisitorBag();
|
|
||||||
bag.merge(vbag);
|
bag.merge(vbag);
|
||||||
for(Object computed : vbag.computeList()){
|
|
||||||
bag.compute(computed);
|
|
||||||
}
|
|
||||||
return bag;
|
|
||||||
}
|
}
|
||||||
return bag;
|
return bag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canVisit(Class<?> type) {
|
public boolean canVisit(Class<?> type) {
|
||||||
for(Visitor<E> visitor : visitors){
|
return true;
|
||||||
if(visitor.canVisit(type)) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <E> Visitor<E> not(Visitor<E> visitor){
|
|
||||||
return new Visitor<E>(){
|
|
||||||
@Override
|
|
||||||
public VisitorBag visit(E element){
|
|
||||||
VisitorBag result = visitor.visit(element);
|
|
||||||
if(result.isValidated()) return null;
|
|
||||||
VisitorBag bag = new VisitorBag();
|
|
||||||
bag.compute(element);
|
|
||||||
return bag;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canVisit(Class<?> type){
|
|
||||||
return visitor.canVisit(type);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package dev.peerat.parser.visitor;
|
||||||
|
|
||||||
public abstract class Visitor<T>{
|
public abstract class Visitor<T>{
|
||||||
|
|
||||||
public abstract boolean canVisit(Class<?> type); //TODO change "Class<?> type" into "Object element" ?
|
public abstract boolean canVisit(Class<?> type); //change "Class<?> type" into "Object element" ?
|
||||||
|
|
||||||
public abstract VisitorBag visit(T element);
|
public abstract VisitorBag visit(T element);
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,6 @@ import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||||
|
|
||||||
import dev.peerat.parser.Parser;
|
import dev.peerat.parser.Parser;
|
||||||
import dev.peerat.parser.java.tree.JavaTreeType;
|
import dev.peerat.parser.java.tree.JavaTreeType;
|
||||||
import dev.peerat.parser.java.value.ArrayAccessValue;
|
|
||||||
import dev.peerat.parser.java.value.BiValue;
|
import dev.peerat.parser.java.value.BiValue;
|
||||||
import dev.peerat.parser.java.value.MethodCallValue;
|
import dev.peerat.parser.java.value.MethodCallValue;
|
||||||
import dev.peerat.parser.java.value.ModifierValue;
|
import dev.peerat.parser.java.value.ModifierValue;
|
||||||
|
@ -22,9 +21,6 @@ import dev.peerat.parser.java.value.StaticValue;
|
||||||
import dev.peerat.parser.java.value.TriValue;
|
import dev.peerat.parser.java.value.TriValue;
|
||||||
import dev.peerat.parser.java.value.Value;
|
import dev.peerat.parser.java.value.Value;
|
||||||
import dev.peerat.parser.java.value.Value.ValueContainer;
|
import dev.peerat.parser.java.value.Value.ValueContainer;
|
||||||
import dev.peerat.parser.java.value.VariableAccessValue;
|
|
||||||
import dev.peerat.parser.java.visitor.JavaVisitor;
|
|
||||||
import dev.peerat.parser.visitor.VisitorBag;
|
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class ValueTypesTests {
|
public class ValueTypesTests {
|
||||||
|
@ -63,15 +59,12 @@ public class ValueTypesTests {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends JavaElement> void findAll(Function<JavaElement, Boolean> finder, List<E> list){}
|
public <E extends JavaElement> void findAll(Function<JavaElement, Boolean> finder, List<E> list){}
|
||||||
|
|
||||||
@Override
|
|
||||||
public VisitorBag visit(JavaVisitor<?> visitor){return null;}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> T assertInstance(Object obj, java.lang.Class<? extends T> type){
|
private <T> T assertInstance(Object obj, java.lang.Class<? extends T> type){
|
||||||
if(type.isAssignableFrom(obj.getClass())) return (T) obj;
|
assertTrue(type.isAssignableFrom(obj.getClass()));
|
||||||
throw new IllegalArgumentException(obj.getClass()+" is not assignable to type "+type);
|
return (T) obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -173,9 +166,7 @@ public class ValueTypesTests {
|
||||||
assertNull(v.base());
|
assertNull(v.base());
|
||||||
assertEquals("test", v.getToken().getValue());
|
assertEquals("test", v.getToken().getValue());
|
||||||
assertNull(v.getGeneric());
|
assertNull(v.getGeneric());
|
||||||
assertEquals(0, v.getParameters().size());
|
assertNull(v.getParameters());
|
||||||
|
|
||||||
v = assertInstance(parse("Test.test()"), MethodCallValue.class);
|
|
||||||
|
|
||||||
v = assertInstance(parse("JavaParserTest.test(8+8).equals(16)"), MethodCallValue.class);
|
v = assertInstance(parse("JavaParserTest.test(8+8).equals(16)"), MethodCallValue.class);
|
||||||
assertEquals("equals", v.getToken().getValue());
|
assertEquals("equals", v.getToken().getValue());
|
||||||
|
@ -188,27 +179,8 @@ public class ValueTypesTests {
|
||||||
assertEquals("+", bv.getAction());
|
assertEquals("+", bv.getAction());
|
||||||
assertEquals("8", assertInstance(bv.right(), StaticValue.class).getToken().getValue());
|
assertEquals("8", assertInstance(bv.right(), StaticValue.class).getToken().getValue());
|
||||||
assertEquals("test", v.getToken().getValue());
|
assertEquals("test", v.getToken().getValue());
|
||||||
assertEquals("JavaParserTest", assertInstance(v.base(), StaticValue.class).getToken().getValue());
|
assertEquals("JavaParserTest", assertInstance(v.base(), StaticValue.class).getToken().getClass());
|
||||||
|
|
||||||
v = assertInstance(parse("main.test.here.localvar.run(6)[0].validate(7)"), MethodCallValue.class); //main.test.here.localvar.run(6)[0].validate(7)
|
|
||||||
assertEquals("validate", v.getToken().getValue());
|
|
||||||
assertEquals(1, v.getParameters().size());
|
|
||||||
StaticValue param = assertInstance(v.getParameters().get(0), StaticValue.class);
|
|
||||||
assertEquals("7", param.getToken().getValue());
|
|
||||||
ArrayAccessValue array = assertInstance(v.base(), ArrayAccessValue.class); //main.test.here.localvar.run(6)[0]
|
|
||||||
param = assertInstance(array.getAccessor(), StaticValue.class);
|
|
||||||
assertEquals("0", param.getToken().getValue());
|
|
||||||
v = assertInstance(array.base(), MethodCallValue.class); //main.test.here.localvar.run(6)
|
|
||||||
assertEquals("run", v.getToken().getValue());
|
|
||||||
param = assertInstance(v.getParameters().get(0), StaticValue.class);
|
|
||||||
assertEquals("6", param.getToken().getValue());
|
|
||||||
VariableAccessValue access = assertInstance(v.base(), VariableAccessValue.class);
|
|
||||||
assertEquals("localvar", access.getVariable().getValue());
|
|
||||||
access = assertInstance(access.base(), VariableAccessValue.class);
|
|
||||||
assertEquals("here", access.getVariable().getValue());
|
|
||||||
access = assertInstance(access.base(), VariableAccessValue.class);
|
|
||||||
assertEquals("test", access.getVariable().getValue());
|
|
||||||
assertEquals("main", assertInstance(access.base(), StaticValue.class).getToken().getValue());
|
|
||||||
|
|
||||||
|
v = assertInstance(parse("main.test.here.localvar.run(6)[0].validate(7)"), MethodCallValue.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ public class BrowserLambdaTests{
|
||||||
|
|
||||||
Class clazz = (Class) javaFile.getMainClass();
|
Class clazz = (Class) javaFile.getMainClass();
|
||||||
System.out.println(clazz);
|
System.out.println(clazz);
|
||||||
// clazz.stream().functionFilter((func) -> func.getName().getValue().equals("test"));
|
clazz.stream().functionFilter((func) -> func.getName().getValue().equals("test"));
|
||||||
}catch(Exception ex){
|
}catch(Exception ex){
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ public class BrowserLambdaTests{
|
||||||
|
|
||||||
Class clazz = (Class) javaFile.getMainClass();
|
Class clazz = (Class) javaFile.getMainClass();
|
||||||
System.out.println(clazz);
|
System.out.println(clazz);
|
||||||
// clazz.stream().variableFilter((v) -> Modifier.isPrivate(v.getModifier()));
|
clazz.stream().variableFilter((v) -> Modifier.isPrivate(v.getModifier()));
|
||||||
}catch(Exception ex){
|
}catch(Exception ex){
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue