[AST] MethodCall, new Instance, Lambda, fix left&right values
This commit is contained in:
parent
3f4d824b33
commit
9b87621a35
4 changed files with 229 additions and 32 deletions
|
@ -25,10 +25,15 @@ import dev.peerat.parser.java.Annotation.AnnotableBuffer;
|
|||
import dev.peerat.parser.java.Function.FunctionContainer;
|
||||
import dev.peerat.parser.java.Operation.OperationContainer;
|
||||
import dev.peerat.parser.java.Value.BiValue;
|
||||
import dev.peerat.parser.java.Value.InstanceValue;
|
||||
import dev.peerat.parser.java.Value.LambdaValue;
|
||||
import dev.peerat.parser.java.Value.MethodCallValue;
|
||||
import dev.peerat.parser.java.Variable.VariableContainer;
|
||||
import dev.peerat.parser.java.operation.AssignOperation;
|
||||
import dev.peerat.parser.java.operation.BreakOperation;
|
||||
import dev.peerat.parser.java.operation.ContinueOperation;
|
||||
import dev.peerat.parser.java.operation.IfOperation;
|
||||
import dev.peerat.parser.java.operation.MethodCallOperation;
|
||||
import dev.peerat.parser.java.operation.ReturnOperation;
|
||||
import dev.peerat.parser.java.operation.SynchronizedOperation;
|
||||
import dev.peerat.parser.java.operation.ThrowOperation;
|
||||
|
@ -225,9 +230,11 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
|
||||
//VALUE
|
||||
BiFunction<JavaElement, Bag, JavaElement> value_builder = (parent, bag) -> {
|
||||
System.out.println(bag);
|
||||
if(bag.has("right")){
|
||||
|
||||
if(!(bag.get("left") instanceof Value)) bag.set("left", new Value(bag.<Token>get("left")));
|
||||
if(!(bag.get("right") instanceof Value)) bag.set("right", new Value(bag.<Token>get("right")));
|
||||
|
||||
BiValue result = new BiValue(
|
||||
|
||||
bag.get("left"),
|
||||
|
@ -244,6 +251,7 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
global.set("right", local.get("left"));
|
||||
};
|
||||
|
||||
//STRING CONCAT
|
||||
StateTree<JavaElement> value = new StateTree<>();
|
||||
|
||||
StateTree<JavaElement> value_container = new StateTree<>();
|
||||
|
@ -269,10 +277,15 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
return null;
|
||||
});
|
||||
|
||||
StateTree<JavaElement> value_instance = value.then((validator) -> validator.validate((token) -> token.getValue().equals("new")));
|
||||
StateTree<JavaElement> value_instance = value.then((validator) -> validator.validate(
|
||||
(token) -> token.getValue().equals("new"),
|
||||
(bag, token) -> bag.set("newit", token)));
|
||||
StateTree<JavaElement> value_name = new StateTree<JavaElement>();
|
||||
value.then(value_name);
|
||||
value_instance.then(new RedirectStateTree<>(value_name, (global, local) -> global.set(local.get())))
|
||||
value_instance.then(new RedirectStateTree<>(value_name, (global, local) -> {
|
||||
if(global.has("newit")) global.set(new InstanceValue(global.<Token>get("newit"), local.<MethodCallValue>get().getToken(), local.<MethodCallValue>get().getParameters()));
|
||||
else global.set(local.get());
|
||||
}))
|
||||
.end((a,b) -> a)
|
||||
.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
|
||||
.end((a,b) -> a)
|
||||
|
@ -304,9 +317,26 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
value_array_end.then(value_call);
|
||||
value_array_end.then(value_array_begin);
|
||||
|
||||
StateTree<JavaElement> value_arg_begin = value_name.then((validator) -> validator.validate((token) -> token.getValue().equals("(")));
|
||||
StateTree<JavaElement> value_arg_end = value_arg_begin.then((validator) -> validator.validate((token) -> token.getValue().equals(")")));
|
||||
value_arg_end.end((a,b) -> a);
|
||||
StateTree<JavaElement> value_arg_begin = value_name.then((validator) -> validator.validate(
|
||||
(token) -> token.getValue().equals("("),
|
||||
(bag, token) -> bag.set(bag.<Token>get().concat(token))));
|
||||
StateTree<JavaElement> value_arg_end = value_arg_begin.then((validator) -> validator.validate(
|
||||
(token) -> token.getValue().equals(")"),
|
||||
(bag, token) -> bag.set(bag.<Token>get().concat(token))));
|
||||
value_arg_end.end((parent,bag) -> {
|
||||
Integer paramters = bag.get("args");
|
||||
List<Value> list = null;
|
||||
if(paramters != null){
|
||||
list = new ArrayList<>();
|
||||
for(int i = 0; i < paramters; i++) list.add(bag.<Bag>get("arg"+i).get());
|
||||
}
|
||||
|
||||
MethodCallValue methodCall = new MethodCallValue(bag.get(), list);
|
||||
//take arguments from bag
|
||||
System.out.println("MethodCall "+bag);
|
||||
bag.set(methodCall);
|
||||
return null;
|
||||
});
|
||||
value_arg_end.then(value_call);
|
||||
value_arg_end.then(value_array_begin);
|
||||
StateTree<JavaElement> value_generic_begin = value_name.then((validator) -> validator.validate((token) -> token.getValue().equals("<")));
|
||||
|
@ -324,7 +354,6 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
if(count == null) count = 0;
|
||||
global.set("arg"+count, local);
|
||||
global.set("args", (count+1));
|
||||
System.out.println("ah "+local);
|
||||
}));
|
||||
value_arg.then((validator) -> validator.validate((token) -> token.getValue().equals(",")))
|
||||
.then(value_arg);
|
||||
|
@ -345,10 +374,16 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
validator.validate((token) -> token.getValue().equals("-")) &&
|
||||
validator.validate((token) -> token.getValue().equals(">")));
|
||||
value_lambda.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
|
||||
.end((a,b) -> a).end((a,b) -> a)
|
||||
.<JavaElement>end((a,bag) -> {
|
||||
bag.set(new LambdaValue(null, null));
|
||||
return a;
|
||||
})
|
||||
.multiple(function_container)
|
||||
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))).end((a,b) -> a);
|
||||
value_lambda.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null))).end((a,b) -> a);
|
||||
value_lambda.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null))).end((a,bag) ->{
|
||||
bag.set(new LambdaValue(null, null));
|
||||
return a;
|
||||
});
|
||||
value_lambda_arg.then(value_lambda);
|
||||
StateTree<JavaElement> value_parenthesis_end = value_parenthesis.then(new RedirectStateTree<>(value_container, (global, local) -> global.set("left", local.get())))
|
||||
.then((validator) -> validator.validate((token) -> token.getValue().equals(")")));
|
||||
|
@ -360,7 +395,6 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
value_container.then((validator) -> validator.validate((token) -> token.getValue().equals("+"))).then(value_container);
|
||||
value_container.then((validator) -> validator.validate((token) -> token.getValue().equals("~"))).then(value_container);
|
||||
StateTree<JavaElement> value_inside = value_container.then(new RedirectStateTree<>(value, (global, local) -> {
|
||||
System.out.println("captured "+local);
|
||||
global.set("left", local.get());
|
||||
}));
|
||||
value_inside.then((validator) -> validator.validate((token) -> token.getValue().equals("[")))
|
||||
|
@ -369,7 +403,8 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
.then(value_left);
|
||||
value_inside.then(value_left);
|
||||
value_left.end((parent, bag) -> {
|
||||
if(bag.get() == null) bag.set(bag.get("left"));
|
||||
if(bag.get("left") instanceof Value) bag.set(bag.get("left"));
|
||||
else bag.set(new Value(bag.<Token>get("left")));
|
||||
return null;
|
||||
});
|
||||
StateTree<JavaElement> value_equals = value_left.then((validator) -> validator.validate((token) -> token.getValue().equals("="), (bag, token) -> bag.set("action", "=")));
|
||||
|
@ -535,7 +570,7 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
variable_split.then(variable_name);
|
||||
StateTree<JavaElement> variable_value = variable_name.then((validator) -> validator.validate((token) -> token.getValue().equals("=")))
|
||||
.then(new RedirectStateTree<>(value_container, (global, local) -> {
|
||||
// global.<Map<Token, Value>>get("vars").put(global.get("last"), local.get());
|
||||
global.<Map<Token, Value>>get("vars").put(global.get("last"), local.get());
|
||||
}));
|
||||
variable_value.then(variable_split);
|
||||
variable_value.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
|
||||
|
@ -544,12 +579,25 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
|
||||
//OPERATION
|
||||
StateTree<JavaElement> operation = new StateTree<>();
|
||||
StateTree<JavaElement> operation_name = operation.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null)));
|
||||
operation_name.then((validator) -> validator.validate((token) -> token.getValue().equals(";"))).end((a,b) -> a);
|
||||
operation_name.then((validator) -> validator.validate((token) -> token.getValue().equals("=")))
|
||||
.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null)))
|
||||
.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
|
||||
.end((a,b) -> a);
|
||||
StateTree<JavaElement> operation_name = operation.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(local.get())));
|
||||
operation_name.then((validator) -> validator.validate((token) -> token.getValue().equals(";"))).end((parent,bag) -> {
|
||||
Value action = bag.get();
|
||||
if(action instanceof BiValue){
|
||||
BiValue assign = (BiValue)action;
|
||||
AssignOperation op = new AssignOperation(assign.left(), assign.right());
|
||||
if(parent instanceof OperationContainer) ((OperationContainer)parent).addOperation(op);
|
||||
}
|
||||
if(action instanceof MethodCallValue){
|
||||
MethodCallValue call = (MethodCallValue)action;
|
||||
MethodCallOperation op = new MethodCallOperation((Value)null, call.getToken(), call.getParameters());
|
||||
if(parent instanceof OperationContainer) ((OperationContainer)parent).addOperation(op);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
// operation_name.then((validator) -> validator.validate((token) -> token.getValue().equals("=")))
|
||||
// .then(new RedirectStateTree<>(value_container, (global, local) -> global.set("newer", local.get())))
|
||||
// .then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
|
||||
// .end((parent, bag) -> parent);
|
||||
StateTree<JavaElement> operation_call = operation_name.then((validator) -> validator.validate((token) -> token.getValue().equals(".")));
|
||||
operation_call.then(operation_name);
|
||||
StateTree<JavaElement> operation_begin = operation_name.then((validator) -> validator.validate((token) -> token.getValue().equals("(")));
|
||||
|
@ -657,6 +705,7 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(local.get())))
|
||||
.then((validator) -> validator.validate((token) -> token.getValue().equals(")")))
|
||||
.end((parent,bag) -> {
|
||||
System.out.println("if "+bag);
|
||||
IfOperation op = new IfOperation(bag.get());
|
||||
if(parent instanceof OperationContainer) ((OperationContainer)parent).addOperation(op);
|
||||
return op;
|
||||
|
@ -1122,12 +1171,11 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
JavaFile jFile = new JavaFile();
|
||||
parser.parse(reader, jFile);
|
||||
|
||||
System.out.println();
|
||||
List<Value> values = new ArrayList<>();
|
||||
jFile.findAll((e) -> e instanceof Value, values);
|
||||
System.out.println("Find "+values.size()+" values");
|
||||
for(Value v : values) System.out.println(v);
|
||||
|
||||
jFile.find((e) -> {
|
||||
System.out.println(e);
|
||||
return false;
|
||||
});
|
||||
|
||||
System.out.println((System.currentTimeMillis()-time)+"ms");
|
||||
|
||||
|
|
|
@ -150,4 +150,146 @@ public class Value extends JavaElement{
|
|||
}
|
||||
}
|
||||
|
||||
public static class MethodCallValue extends Value{
|
||||
|
||||
private Token token;
|
||||
private List<Value> parameters;
|
||||
|
||||
public MethodCallValue(Token token, List<Value> parameters){
|
||||
this.token = token;
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public Token getToken(){
|
||||
return token;
|
||||
}
|
||||
|
||||
public List<Value> getParameters(){
|
||||
return this.parameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return token.getValue()+((parameters == null ? "":parameters));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
builder.append(token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
if(parameters != null){
|
||||
for(Value value : parameters){
|
||||
if(finder.apply(value)) return (E) value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> void findAll(Function<JavaElement, Boolean> finder, List<E> list){
|
||||
if(parameters != null){
|
||||
for(Value value : parameters){
|
||||
if(finder.apply(value)) list.add((E) value);
|
||||
value.findAll(finder, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class InstanceValue extends Value{
|
||||
|
||||
private Token newer;
|
||||
private Token token;
|
||||
private List<Value> parameters;
|
||||
|
||||
public InstanceValue(Token newer, Token token, List<Value> parameters){
|
||||
this.newer = newer;
|
||||
this.token = token;
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public Token getNewer(){
|
||||
return this.newer;
|
||||
}
|
||||
|
||||
public Token getToken(){
|
||||
return token;
|
||||
}
|
||||
|
||||
public List<Value> getParameters(){
|
||||
return this.parameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return newer.getValue()+" "+token.getValue()+((parameters == null ? "":parameters));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
builder.append(newer);
|
||||
builder.append(token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
if(parameters != null){
|
||||
for(Value value : parameters){
|
||||
if(finder.apply(value)) return (E) value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> void findAll(Function<JavaElement, Boolean> finder, List<E> list){
|
||||
if(parameters != null){
|
||||
for(Value value : parameters){
|
||||
if(finder.apply(value)) list.add((E) value);
|
||||
value.findAll(finder, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TO FILL
|
||||
public static class LambdaValue extends Value{
|
||||
|
||||
private List<Token> parameters;
|
||||
private List<JavaElement> operations;
|
||||
|
||||
public LambdaValue(List<Token> parameters, List<JavaElement> operations){
|
||||
this.parameters = parameters;
|
||||
this.operations = operations;
|
||||
}
|
||||
|
||||
public List<Token> getParameters(){
|
||||
return this.parameters;
|
||||
}
|
||||
|
||||
public List<JavaElement> getOperations(){
|
||||
return this.operations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "() -> {}";
|
||||
}
|
||||
|
||||
@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){
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ public class IfOperation extends OperationBag{
|
|||
|
||||
public IfOperation(Value condition){
|
||||
super();
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
public Value getCondition(){
|
||||
|
@ -28,6 +29,6 @@ public class IfOperation extends OperationBag{
|
|||
public <E extends JavaElement> void findAll(Function<JavaElement, Boolean> finder, List<E> list){
|
||||
if(finder.apply(condition)) list.add((E)condition);
|
||||
condition.findAll(finder, list);
|
||||
super.find(finder);
|
||||
super.findAll(finder, list);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,25 +46,31 @@ public class MethodCallOperation extends Operation{
|
|||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
||||
if(finder.apply(start)) return (E)start;
|
||||
if(start != null && finder.apply(start)) return (E)start;
|
||||
if(previous != null) if(finder.apply(previous)) return (E) previous;
|
||||
if(this.parameters != null){
|
||||
for(Value param : this.parameters){
|
||||
if(finder.apply(param)) return (E)param;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> void findAll(Function<JavaElement, Boolean> finder, List<E> list){
|
||||
if(start != null){
|
||||
if(finder.apply(start)) list.add((E)start);
|
||||
start.findAll(finder, list);
|
||||
}
|
||||
if(previous != null){
|
||||
if(finder.apply(previous)) list.add((E) previous);
|
||||
previous.findAll(finder, list);
|
||||
}
|
||||
if(this.parameters != null){
|
||||
for(Value param : this.parameters){
|
||||
if(finder.apply(param)) list.add((E)param);
|
||||
param.findAll(finder, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue