[Complete Tests] Assign and loop operations + builder in good order
This commit is contained in:
parent
1b20de6eae
commit
530aba6d9b
9 changed files with 263 additions and 40 deletions
|
@ -31,14 +31,15 @@ public interface ElementBuilder{
|
|||
int character = 1;
|
||||
int line = 1;
|
||||
for(Token token : tokens){
|
||||
while(character < token.getCharacterNumber()){
|
||||
writer.write(" ");
|
||||
character++;
|
||||
}
|
||||
while(line < token.getLineNumber()){
|
||||
writer.write("\n");
|
||||
line++;
|
||||
}
|
||||
while(character < token.getCharacterNumber()){
|
||||
writer.write(" ");
|
||||
character++;
|
||||
}
|
||||
|
||||
writer.write(token.getValue());
|
||||
character+=token.getValue().length();
|
||||
}
|
||||
|
|
|
@ -32,11 +32,13 @@ 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.ForOperation;
|
||||
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;
|
||||
import dev.peerat.parser.java.operation.WhileOperation;
|
||||
import dev.peerat.parser.state.BuilderStateTree;
|
||||
import dev.peerat.parser.state.InitialStateTree;
|
||||
import dev.peerat.parser.state.RedirectStateTree;
|
||||
|
@ -678,35 +680,10 @@ 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(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("(")));
|
||||
StateTree<JavaElement> operation_end = operation_begin.then((validator) -> validator.validate((token) -> token.getValue().equals(")")));
|
||||
operation_end.then(operation_call);
|
||||
operation_end.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
|
||||
.end((a,b) -> a);
|
||||
StateTree<JavaElement> operation_value = operation_begin.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null)));
|
||||
operation_value.then(operation_end);
|
||||
operation_value.then((validator) -> validator.validate((token) -> token.getValue().equals(","))).then(operation_value);
|
||||
|
||||
StateTree<JavaElement> operation_return = operation.then((validator) -> validator.validate((token) -> token.getValue().equals("return")));
|
||||
operation_return.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(local.get())))
|
||||
|
@ -781,7 +758,6 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
.end((a,b) -> a);
|
||||
operation_catch.then(operation_finally);
|
||||
operation_catch.then(operation_catch_named);
|
||||
|
||||
|
||||
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("continue")))
|
||||
.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
|
||||
|
@ -794,6 +770,7 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("break")))
|
||||
.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
|
||||
.end((parent,bag) -> {
|
||||
System.out.println("break");
|
||||
BreakOperation op = new BreakOperation();
|
||||
if(parent instanceof OperationContainer) ((OperationContainer)parent).addOperation(op);
|
||||
return null;
|
||||
|
@ -874,10 +851,19 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
.then(operation_for_second_part);
|
||||
|
||||
StateTree<JavaElement> operation_for_end = operation_for_second_part.then((validator) -> validator.validate((token) -> token.getValue().equals(")")));
|
||||
operation_for_end.end((a,b) -> a);
|
||||
operation_for_end.<JavaElement>end((parent,bag) -> {
|
||||
ForOperation op = new ForOperation(null, null, null, null);
|
||||
if(parent instanceof OperationContainer) ((OperationContainer)parent).addOperation(op);
|
||||
System.out.println("FOOOR");
|
||||
return op;
|
||||
}).then(operation);
|
||||
|
||||
operation_for_end.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
|
||||
.end((a,b) -> a)
|
||||
.<JavaElement>end((parent,bag) -> {
|
||||
ForOperation op = new ForOperation(null, null, null, null);
|
||||
if(parent instanceof OperationContainer) ((OperationContainer)parent).addOperation(op);
|
||||
return op;
|
||||
})
|
||||
.multiple(function_container)
|
||||
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
|
||||
.end((a,b) -> a);
|
||||
|
@ -896,16 +882,20 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null)))
|
||||
.then((validator) -> validator.validate((token) -> token.getValue().equals(")")));
|
||||
operation_while.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
|
||||
.end((a,b) -> a)
|
||||
.<JavaElement>end((parent,bag) -> {
|
||||
WhileOperation op = new WhileOperation(null);
|
||||
if(parent instanceof OperationContainer) ((OperationContainer)parent).addOperation(op);
|
||||
return op;
|
||||
})
|
||||
.multiple(function_container)
|
||||
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
|
||||
.end((a,b) -> a);
|
||||
|
||||
operation_while.then(new RedirectStateTree<>(operation, (global, local) -> global.set(null)))
|
||||
.end((a,b) -> a);
|
||||
|
||||
operation_while.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
|
||||
.end((a,b) -> a);
|
||||
operation_while.<JavaElement>end((parent,bag) -> {
|
||||
WhileOperation op = new WhileOperation(null);
|
||||
if(parent instanceof OperationContainer) ((OperationContainer)parent).addOperation(op);
|
||||
return op;
|
||||
}).then(operation);
|
||||
|
||||
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("synchronized")))
|
||||
.then((validator) -> validator.validate((token) -> token.getValue().equals("(")))
|
||||
|
@ -921,6 +911,33 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
.unique((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;
|
||||
});
|
||||
|
||||
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("(")));
|
||||
StateTree<JavaElement> operation_end = operation_begin.then((validator) -> validator.validate((token) -> token.getValue().equals(")")));
|
||||
operation_end.then(operation_call);
|
||||
operation_end.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
|
||||
.end((a,b) -> a);
|
||||
StateTree<JavaElement> operation_value = operation_begin.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null)));
|
||||
operation_value.then(operation_end);
|
||||
operation_value.then((validator) -> validator.validate((token) -> token.getValue().equals(","))).then(operation_value);
|
||||
|
||||
function_container.then(variable);
|
||||
function_container.then(operation);
|
||||
|
||||
|
@ -960,6 +977,7 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
(token) -> token.getType().equals(TokenType.NAME),
|
||||
(bag, token) -> bag.set("name", token)));
|
||||
function_type.then(function_name);
|
||||
function_mod.then(function_name);
|
||||
StateTree<JavaElement> function_begin = function_name.then((validator) -> validator.validate((token) -> token.getValue().equals("(")));
|
||||
StateTree<JavaElement> function_end = function_begin.then((validator) -> validator.validate((token) -> token.getValue().equals(")")));
|
||||
function_end.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
|
||||
|
|
|
@ -27,6 +27,10 @@ public class OperationBag extends Operation implements VariableContainer, Operat
|
|||
public void addOperation(Operation operation){
|
||||
this.elements.add(operation);
|
||||
}
|
||||
|
||||
public List<JavaElement> getElements(){
|
||||
return this.elements;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
|
|
|
@ -6,7 +6,6 @@ import static org.junit.Assert.assertTrue;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -19,6 +18,7 @@ import dev.peerat.parser.java.Interface;
|
|||
import dev.peerat.parser.java.JavaElement;
|
||||
import dev.peerat.parser.java.JavaFile;
|
||||
import dev.peerat.parser.java.JavaParser;
|
||||
import dev.peerat.parser.java.Operation;
|
||||
import dev.peerat.parser.java.Variable;
|
||||
|
||||
public class BaseElementTests{
|
||||
|
@ -46,6 +46,7 @@ public class BaseElementTests{
|
|||
System.out.println("["+getClass().getSimpleName()+"] passed "+size+" tests");
|
||||
}catch(Exception|AssertionError e){
|
||||
System.err.println("["+getClass().getSimpleName()+"] failed "+text+" for cause "+e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,4 +83,9 @@ public class BaseElementTests{
|
|||
return (Variable)element;
|
||||
}
|
||||
|
||||
public Operation checkOperation(JavaElement element){
|
||||
assertNotNull(element);
|
||||
assertTrue(element instanceof Operation);
|
||||
return (Operation)element;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@ import dev.peerat.parser.java.element.clazz.InterfaceWithoutMod;
|
|||
import dev.peerat.parser.java.element.function.ConstructorFunction;
|
||||
import dev.peerat.parser.java.element.function.NormalFunction;
|
||||
import dev.peerat.parser.java.element.function.StaticFunction;
|
||||
import dev.peerat.parser.java.element.operation.AssignOperationTest;
|
||||
import dev.peerat.parser.java.element.operation.LoopOperationTests;
|
||||
import dev.peerat.parser.java.element.variable.VariableInClass;
|
||||
import dev.peerat.parser.java.element.variable.VariableInMethod;
|
||||
|
||||
|
@ -42,7 +44,9 @@ public class CITests{
|
|||
new StaticFunction(),
|
||||
new NormalFunction(),
|
||||
new VariableInClass(),
|
||||
new VariableInMethod()
|
||||
new VariableInMethod(),
|
||||
new AssignOperationTest(),
|
||||
new LoopOperationTests()
|
||||
);
|
||||
|
||||
@Test
|
||||
|
|
|
@ -29,6 +29,7 @@ public class ConstructorFunction extends BaseElementTests{
|
|||
assertEquals(1, elements.size());
|
||||
Function function = checkFunction(elements.get(0));
|
||||
assertNotNull(function.getName());
|
||||
assertEquals(function.getName().getValue(), function.getReturnType().getValue());
|
||||
assertEquals(0, function.getElements().size());
|
||||
assertEquals(0, function.getModifier());
|
||||
assertNull(function.getParameters());
|
||||
|
|
|
@ -102,6 +102,32 @@ public class NormalFunction extends BaseElementTests{
|
|||
assertNotNull(function.getThrowables());
|
||||
assertEquals("Exception", function.getThrowables().get(0).getValue());
|
||||
});
|
||||
|
||||
register(
|
||||
"package be.jeffcheasey88;"
|
||||
+ ""
|
||||
+ "public static final class Test{ "
|
||||
+ " void test(){} void test1(){}"
|
||||
+ "}",
|
||||
(javafile) -> {
|
||||
Class clazz = checkClass(javafile);
|
||||
List<JavaElement> elements = clazz.getElements();
|
||||
assertNotNull(elements);
|
||||
assertEquals(2, elements.size());
|
||||
Function function = checkFunction(elements.get(0));
|
||||
assertEquals("test", function.getName().getValue());
|
||||
assertEquals("void", function.getReturnType().getValue());
|
||||
assertEquals(0, function.getElements().size());
|
||||
assertEquals(0, function.getModifier());
|
||||
assertNull(function.getParameters());
|
||||
|
||||
function = checkFunction(elements.get(1));
|
||||
assertEquals("test1", function.getName().getValue());
|
||||
assertEquals("void", function.getReturnType().getValue());
|
||||
assertEquals(0, function.getElements().size());
|
||||
assertEquals(0, function.getModifier());
|
||||
assertNull(function.getParameters());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package dev.peerat.parser.java.element.operation;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import dev.peerat.parser.java.Function;
|
||||
import dev.peerat.parser.java.Operation;
|
||||
import dev.peerat.parser.java.element.BaseElementTests;
|
||||
import dev.peerat.parser.java.operation.AssignOperation;
|
||||
|
||||
public class AssignOperationTest extends BaseElementTests{
|
||||
|
||||
{
|
||||
register(
|
||||
"package be.jeffcheasey88;"
|
||||
+ ""
|
||||
+ "public static class Test{ "
|
||||
+ " void test(){ "
|
||||
+ " int i = 3;"
|
||||
+ " i = 4;"
|
||||
+ "}"
|
||||
+ "}",
|
||||
(javafile) -> {
|
||||
Function function = checkFunction(checkClass(javafile).getElements().get(0));
|
||||
assertEquals(2, function.getElements().size());
|
||||
checkVariable(function.getElements().get(0));
|
||||
Operation op = checkOperation(function.getElements().get(1));
|
||||
assertTrue(op instanceof AssignOperation);
|
||||
AssignOperation assign = (AssignOperation)op;
|
||||
assertEquals("i", assign.left().getToken().getValue());
|
||||
assertEquals("4", assign.right().getToken().getValue());
|
||||
});
|
||||
|
||||
register(
|
||||
"package be.jeffcheasey88;"
|
||||
+ ""
|
||||
+ "public static class Test{ "
|
||||
+ " void test(){ "
|
||||
+ " int i = 3, j = 4;"
|
||||
+ " i = j;"
|
||||
+ "}"
|
||||
+ "}",
|
||||
(javafile) -> {
|
||||
Function function = checkFunction(checkClass(javafile).getElements().get(0));
|
||||
assertEquals(3, function.getElements().size());
|
||||
checkVariable(function.getElements().get(0));
|
||||
checkVariable(function.getElements().get(1));
|
||||
Operation op = checkOperation(function.getElements().get(2));
|
||||
assertTrue(op instanceof AssignOperation);
|
||||
AssignOperation assign = (AssignOperation)op;
|
||||
assertEquals("i", assign.left().getToken().getValue());
|
||||
assertEquals("j", assign.right().getToken().getValue());
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
package dev.peerat.parser.java.element.operation;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import dev.peerat.parser.java.Function;
|
||||
import dev.peerat.parser.java.Operation;
|
||||
import dev.peerat.parser.java.element.BaseElementTests;
|
||||
import dev.peerat.parser.java.operation.BreakOperation;
|
||||
import dev.peerat.parser.java.operation.ContinueOperation;
|
||||
import dev.peerat.parser.java.operation.ForOperation;
|
||||
import dev.peerat.parser.java.operation.ReturnOperation;
|
||||
import dev.peerat.parser.java.operation.WhileOperation;
|
||||
|
||||
public class LoopOperationTests extends BaseElementTests{
|
||||
|
||||
{
|
||||
register(
|
||||
"package be.jeffcheasey88;"
|
||||
+ ""
|
||||
+ "public static class Test{ "
|
||||
+ " void test(){ "
|
||||
+ " for(;;) break;"
|
||||
+ "}"
|
||||
+ "}",
|
||||
(javafile) -> {
|
||||
Function function = checkFunction(checkClass(javafile).getElements().get(0));
|
||||
assertEquals(1, function.getElements().size());
|
||||
Operation op = checkOperation(function.getElements().get(0));
|
||||
assertTrue(op instanceof ForOperation);
|
||||
ForOperation forOp = (ForOperation)op;
|
||||
Operation o = checkOperation(forOp.getElements().get(0));
|
||||
assertTrue(o instanceof BreakOperation);
|
||||
});
|
||||
|
||||
register(
|
||||
"package be.jeffcheasey88;"
|
||||
+ ""
|
||||
+ "public static class Test{ "
|
||||
+ " void test(){ "
|
||||
+ " for(;;){ break;}"
|
||||
+ "}"
|
||||
+ "}",
|
||||
(javafile) -> {
|
||||
Function function = checkFunction(checkClass(javafile).getElements().get(0));
|
||||
assertEquals(1, function.getElements().size());
|
||||
Operation op = checkOperation(function.getElements().get(0));
|
||||
assertTrue(op instanceof ForOperation);
|
||||
ForOperation forOp = (ForOperation)op;
|
||||
Operation o = checkOperation(forOp.getElements().get(0));
|
||||
assertTrue(o instanceof BreakOperation);
|
||||
});
|
||||
|
||||
register(
|
||||
"package be.jeffcheasey88;"
|
||||
+ ""
|
||||
+ "public static class Test{ "
|
||||
+ " void test(){ "
|
||||
+ " for(;;) continue;"
|
||||
+ "}"
|
||||
+ "}",
|
||||
(javafile) -> {
|
||||
Function function = checkFunction(checkClass(javafile).getElements().get(0));
|
||||
assertEquals(1, function.getElements().size());
|
||||
Operation op = checkOperation(function.getElements().get(0));
|
||||
assertTrue(op instanceof ForOperation);
|
||||
ForOperation forOp = (ForOperation)op;
|
||||
Operation o = checkOperation(forOp.getElements().get(0));
|
||||
assertTrue(o instanceof ContinueOperation);
|
||||
});
|
||||
|
||||
register(
|
||||
"package be.jeffcheasey88;"
|
||||
+ ""
|
||||
+ "public static class Test{ "
|
||||
+ " void test(){ "
|
||||
+ " while(true) continue;"
|
||||
+ "}"
|
||||
+ "}",
|
||||
(javafile) -> {
|
||||
Function function = checkFunction(checkClass(javafile).getElements().get(0));
|
||||
assertEquals(1, function.getElements().size());
|
||||
Operation op = checkOperation(function.getElements().get(0));
|
||||
assertTrue(op instanceof WhileOperation);
|
||||
WhileOperation whileOp = (WhileOperation)op;
|
||||
Operation o = checkOperation(whileOp.getElements().get(0));
|
||||
assertTrue(o instanceof ContinueOperation);
|
||||
});
|
||||
|
||||
register(
|
||||
"package be.jeffcheasey88;"
|
||||
+ ""
|
||||
+ "public static class Test{ "
|
||||
+ " void test(){ "
|
||||
+ " while(true) return;"
|
||||
+ "}"
|
||||
+ "}",
|
||||
(javafile) -> {
|
||||
Function function = checkFunction(checkClass(javafile).getElements().get(0));
|
||||
assertEquals(1, function.getElements().size());
|
||||
Operation op = checkOperation(function.getElements().get(0));
|
||||
assertTrue(op instanceof WhileOperation);
|
||||
WhileOperation whileOp = (WhileOperation)op;
|
||||
Operation o = checkOperation(whileOp.getElements().get(0));
|
||||
assertTrue(o instanceof ReturnOperation);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue