start Graph Generator + add unit test (UtilsTest + Try Catch Finally (base)) + fix some builders in parser
This commit is contained in:
parent
530aba6d9b
commit
d7add3503b
17 changed files with 312 additions and 20 deletions
|
@ -8,7 +8,7 @@ import dev.peerat.parser.ElementBuilder;
|
||||||
|
|
||||||
public abstract class JavaElement implements ElementBuilder{
|
public abstract class JavaElement implements ElementBuilder{
|
||||||
|
|
||||||
Bag bag; //to remove
|
// Bag bag; //to remove
|
||||||
|
|
||||||
|
|
||||||
public abstract <E extends JavaElement> E find(Function<JavaElement, Boolean> finder);
|
public abstract <E extends JavaElement> E find(Function<JavaElement, Boolean> finder);
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
package dev.peerat.parser.java;
|
package dev.peerat.parser.java;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
@ -15,11 +11,9 @@ import java.util.function.BiConsumer;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
import dev.peerat.parser.Bag;
|
import dev.peerat.parser.Bag;
|
||||||
import dev.peerat.parser.ElementBuilder.Builder;
|
|
||||||
import dev.peerat.parser.Parser;
|
import dev.peerat.parser.Parser;
|
||||||
import dev.peerat.parser.Token;
|
import dev.peerat.parser.Token;
|
||||||
import dev.peerat.parser.TokenType;
|
import dev.peerat.parser.TokenType;
|
||||||
import dev.peerat.parser.TokenValidator;
|
|
||||||
import dev.peerat.parser.Tokenizer;
|
import dev.peerat.parser.Tokenizer;
|
||||||
import dev.peerat.parser.java.Annotation.AnnotableBuffer;
|
import dev.peerat.parser.java.Annotation.AnnotableBuffer;
|
||||||
import dev.peerat.parser.java.Function.FunctionContainer;
|
import dev.peerat.parser.java.Function.FunctionContainer;
|
||||||
|
@ -363,6 +357,7 @@ public class JavaParser extends Parser<JavaElement> {
|
||||||
StateTree<JavaElement> value_name = new StateTree<JavaElement>();
|
StateTree<JavaElement> value_name = new StateTree<JavaElement>();
|
||||||
value.then(value_name);
|
value.then(value_name);
|
||||||
value_instance.then(new RedirectStateTree<>(value_name, (global, local) -> {
|
value_instance.then(new RedirectStateTree<>(value_name, (global, local) -> {
|
||||||
|
System.out.println(local.get()+"");
|
||||||
if(global.has("newit")) global.set(new InstanceValue(global.<Token>get("newit"), local.<MethodCallValue>get().getToken(), local.<MethodCallValue>get().getParameters()));
|
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());
|
else global.set(local.get());
|
||||||
}))
|
}))
|
||||||
|
@ -412,7 +407,11 @@ public class JavaParser extends Parser<JavaElement> {
|
||||||
StateTree<JavaElement> value_array_end = value_array_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("]")));
|
StateTree<JavaElement> value_array_end = value_array_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("]")));
|
||||||
value_array_begin.then(new RedirectStateTree<>(value_container, (global, local) -> global.set("inside", local)))
|
value_array_begin.then(new RedirectStateTree<>(value_container, (global, local) -> global.set("inside", local)))
|
||||||
.then(value_array_end);
|
.then(value_array_end);
|
||||||
value_array_end.end((a,b) -> a)
|
value_array_end.end((parent,bag) -> {
|
||||||
|
//to change
|
||||||
|
bag.set(new MethodCallValue(null, null, bag.get(), null));
|
||||||
|
return parent;
|
||||||
|
})
|
||||||
.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
|
.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
|
||||||
.end((a,b) -> a)
|
.end((a,b) -> a)
|
||||||
.multiple(value_list)
|
.multiple(value_list)
|
||||||
|
|
|
@ -23,4 +23,9 @@ public class CatchOperation extends OperationBag{
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,4 +2,9 @@ package dev.peerat.parser.java.operation;
|
||||||
|
|
||||||
public class DoOperation extends OperationBag{
|
public class DoOperation extends OperationBag{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,4 +2,9 @@ package dev.peerat.parser.java.operation;
|
||||||
|
|
||||||
public class ElseOperation extends OperationBag{
|
public class ElseOperation extends OperationBag{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,4 +2,9 @@ package dev.peerat.parser.java.operation;
|
||||||
|
|
||||||
public class FinallyOperation extends OperationBag{
|
public class FinallyOperation extends OperationBag{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,8 +55,10 @@ public class ForOperation extends OperationBag{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(finder.apply(condition)) return (E)condition;
|
if(finder.apply(condition)) return (E)condition;
|
||||||
for(Value value : this.updates){
|
if(this.updates != null){
|
||||||
if(finder.apply(value)) return (E)value;
|
for(Value value : this.updates){
|
||||||
|
if(finder.apply(value)) return (E)value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return super.find(finder);
|
return super.find(finder);
|
||||||
}
|
}
|
||||||
|
@ -76,10 +78,17 @@ public class ForOperation extends OperationBag{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(finder.apply(condition)) list.add((E)condition);
|
if(finder.apply(condition)) list.add((E)condition);
|
||||||
for(Value value : this.updates){
|
if(this.updates != null){
|
||||||
if(finder.apply(value)) list.add((E)value);
|
for(Value value : this.updates){
|
||||||
value.findAll(finder, list);
|
if(finder.apply(value)) list.add((E)value);
|
||||||
|
value.findAll(finder, list);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
super.findAll(finder, list);
|
super.findAll(finder, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,4 +45,9 @@ public class ForeachOperation extends OperationBag{
|
||||||
iterator.findAll(finder, list);
|
iterator.findAll(finder, list);
|
||||||
super.findAll(finder, list);
|
super.findAll(finder, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,4 +31,9 @@ public class IfOperation extends OperationBag{
|
||||||
condition.findAll(finder, list);
|
condition.findAll(finder, list);
|
||||||
super.findAll(finder, list);
|
super.findAll(finder, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import dev.peerat.parser.java.Variable;
|
||||||
import dev.peerat.parser.java.Operation.OperationContainer;
|
import dev.peerat.parser.java.Operation.OperationContainer;
|
||||||
import dev.peerat.parser.java.Variable.VariableContainer;
|
import dev.peerat.parser.java.Variable.VariableContainer;
|
||||||
|
|
||||||
public class OperationBag extends Operation implements VariableContainer, OperationContainer{
|
public abstract class OperationBag extends Operation implements VariableContainer, OperationContainer{
|
||||||
|
|
||||||
private List<JavaElement> elements;
|
private List<JavaElement> elements;
|
||||||
|
|
||||||
|
@ -52,9 +52,4 @@ public class OperationBag extends Operation implements VariableContainer, Operat
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void build(Builder builder) throws Exception {
|
|
||||||
//todo and super on child
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,4 +31,9 @@ public class SynchronizedOperation extends OperationBag{
|
||||||
value.findAll(finder, list);
|
value.findAll(finder, list);
|
||||||
super.findAll(finder, list);
|
super.findAll(finder, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,4 +36,9 @@ public class TryOperation extends OperationBag{
|
||||||
}
|
}
|
||||||
super.findAll(finder, list);
|
super.findAll(finder, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,4 +30,9 @@ public class WhileOperation extends OperationBag{
|
||||||
condition.findAll(finder, list);
|
condition.findAll(finder, list);
|
||||||
super.findAll(finder, list);
|
super.findAll(finder, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
147
src/dev/peerat/parser/state/generator/GraphGenerator.java
Normal file
147
src/dev/peerat/parser/state/generator/GraphGenerator.java
Normal file
|
@ -0,0 +1,147 @@
|
||||||
|
package dev.peerat.parser.state.generator;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class GraphGenerator {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception{
|
||||||
|
File file = new File("java.generator");
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||||
|
List<String> lines = new ArrayList<>();
|
||||||
|
String line;
|
||||||
|
while((line = reader.readLine()) != null) lines.add(line);
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
Graph graph = new Graph(lines);
|
||||||
|
graph.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Graph{
|
||||||
|
|
||||||
|
private static Pattern PATTERN = Pattern.compile("^(\\d+)\\s+->\s+(.*)$");
|
||||||
|
|
||||||
|
private List<Node> nodes;
|
||||||
|
|
||||||
|
public Graph(List<String> lines){
|
||||||
|
this.nodes = new ArrayList<>();
|
||||||
|
for(String line : lines){
|
||||||
|
Matcher matcher = PATTERN.matcher(line);
|
||||||
|
if(matcher.matches()){
|
||||||
|
Node from = nodes.get(Integer.parseInt(matcher.group(1)));
|
||||||
|
String[] split = matcher.group(2).split("\\s+");
|
||||||
|
for(String s : split){
|
||||||
|
Node to = this.nodes.get(Integer.parseInt(s));
|
||||||
|
from.addOutput(to);
|
||||||
|
to.addInput(from);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
this.nodes.add(new Node(line));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void build(){
|
||||||
|
Set<Node> dones = new HashSet<>();
|
||||||
|
Map<Node, Builder> builders = new HashMap<>();
|
||||||
|
|
||||||
|
for(Node node : this.nodes){
|
||||||
|
if(node.input.size() == 1 && node.input.iterator().next().output.size() == 1) continue;
|
||||||
|
Builder builder = new Builder();
|
||||||
|
builder.stack(node);
|
||||||
|
builders.put(node, builder);
|
||||||
|
}
|
||||||
|
for(Node node : this.nodes){
|
||||||
|
Node before;
|
||||||
|
if(
|
||||||
|
node.
|
||||||
|
input.
|
||||||
|
size() == 1 && (before = node.
|
||||||
|
input.
|
||||||
|
iterator().
|
||||||
|
next()).
|
||||||
|
output.
|
||||||
|
size() == 1) {
|
||||||
|
System.out.println(before.content+" ?");
|
||||||
|
builders.
|
||||||
|
get(before).
|
||||||
|
stack(node);}
|
||||||
|
}
|
||||||
|
Set<Builder> stack = new HashSet<>(builders.values());
|
||||||
|
while(!stack.isEmpty()){
|
||||||
|
Builder builder = null;
|
||||||
|
for(Builder b : stack){
|
||||||
|
if(b.canBeBuild(dones)){
|
||||||
|
builder = b;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(builder == null) throw new IllegalArgumentException("Illegale State");
|
||||||
|
builder.build();
|
||||||
|
dones.addAll(builder.stacks);
|
||||||
|
stack.remove(builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Node{
|
||||||
|
|
||||||
|
private String content;
|
||||||
|
private Set<Node> input;
|
||||||
|
private Set<Node> output;
|
||||||
|
|
||||||
|
public Node(String content){
|
||||||
|
this.content = content;
|
||||||
|
this.input = new HashSet<>();
|
||||||
|
this.output = new HashSet<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addInput(Node node){
|
||||||
|
this.input.add(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addOutput(Node node){
|
||||||
|
this.output.add(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return ".validate("+content+")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder{
|
||||||
|
|
||||||
|
private List<Node> stacks;
|
||||||
|
|
||||||
|
public Builder(){
|
||||||
|
this.stacks = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stack(Node node){
|
||||||
|
this.stacks.add(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean canBeBuild(Set<Node> dones){
|
||||||
|
System.out.println("can ? "+dones+" -> "+stacks.get(0).input);
|
||||||
|
return dones.containsAll(stacks.get(0).input);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void build(){
|
||||||
|
System.out.print("StateTree<?> st = new StateTree<>()");
|
||||||
|
for(Node node : stacks) System.out.print(node);
|
||||||
|
System.out.println(";");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -442,7 +442,7 @@ public class ParserComposantGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void build(){
|
public void build(){
|
||||||
System.out.println("build "+title);
|
System.out.println("build "+title+" | in: out:"+path.elements.size());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
63
test/dev/peerat/parser/java/UtilsTests.java
Normal file
63
test/dev/peerat/parser/java/UtilsTests.java
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
package dev.peerat.parser.java;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import dev.peerat.parser.Parser;
|
||||||
|
|
||||||
|
class UtilsTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void main() throws Exception{
|
||||||
|
List<File> files = load(new File("workspaces.txt"));
|
||||||
|
|
||||||
|
long time = System.currentTimeMillis();
|
||||||
|
|
||||||
|
for(File file : files){
|
||||||
|
if(!file.getName().endsWith(".java")) continue;
|
||||||
|
System.out.println("LOOKING "+file);
|
||||||
|
String result = "";
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||||
|
String line;
|
||||||
|
while((line = reader.readLine()) != null) result+=line+"\n";
|
||||||
|
reader.close();
|
||||||
|
Parser<JavaElement> parser = new JavaParser();
|
||||||
|
JavaFile javaFile = new JavaFile();
|
||||||
|
parser.parse(result, javaFile);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue((System.currentTimeMillis()-time) < 30000);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<File> load(File config) throws Exception{
|
||||||
|
System.out.println("loading "+config.getAbsolutePath());
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(config));
|
||||||
|
List<File> workspaces = new ArrayList<>();
|
||||||
|
String line;
|
||||||
|
while((line = reader.readLine()) != null) workspaces.add(new File(line));
|
||||||
|
reader.close();
|
||||||
|
System.out.println("loaded "+workspaces.size()+" workspaces");
|
||||||
|
List<File> result = new ArrayList<>();
|
||||||
|
for(File workspace : workspaces){
|
||||||
|
result.addAll(files(workspace));
|
||||||
|
System.out.println("Loaded["+result.size()+"] "+workspace.getAbsolutePath());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<File> files(File dir){
|
||||||
|
List<File> result = new ArrayList<>();
|
||||||
|
if(dir.isDirectory()){
|
||||||
|
for(File file : dir.listFiles()) result.addAll(files(file));
|
||||||
|
}else result.add(dir);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
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.ForOperation;
|
||||||
|
|
||||||
|
public class TryCatchFinallyOperationTest extends BaseElementTests{
|
||||||
|
|
||||||
|
{
|
||||||
|
register(
|
||||||
|
"package be.jeffcheasey88;"
|
||||||
|
+ ""
|
||||||
|
+ "public static class Test{ "
|
||||||
|
+ " void test(){ "
|
||||||
|
+ " try{}catch(Exception e){}"
|
||||||
|
+ "}"
|
||||||
|
+ "}",
|
||||||
|
(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);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue