dev localy
This commit is contained in:
parent
e0716ef657
commit
02c1adbcbf
4 changed files with 82 additions and 26 deletions
|
@ -1,8 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="test"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
|
@ -28,7 +28,7 @@ public class GraphGenerator {
|
|||
|
||||
public static class Graph{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\d+)\\s+->\s+(.*)$");
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\d+)\\s+->\\s+(.*)$");
|
||||
|
||||
private List<Node> nodes;
|
||||
|
||||
|
|
|
@ -18,9 +18,7 @@ public class Operator{
|
|||
VisitorBag vbag = visitor.visit(element);
|
||||
if(!vbag.isValidated()) return new VisitorBag();
|
||||
bag.merge(vbag);
|
||||
for(Object computed : vbag.computeList()){
|
||||
set.add(computed);
|
||||
}
|
||||
set.add(vbag.computed());
|
||||
}
|
||||
for(Object obj : set) bag.compute(obj);
|
||||
return bag;
|
||||
|
@ -53,9 +51,7 @@ public class Operator{
|
|||
VisitorBag vbag = visitor.visit(element);
|
||||
if(!vbag.isValidated()) return new VisitorBag();
|
||||
bag.merge(vbag);
|
||||
for(Object computed : vbag.computeList()){
|
||||
bag.compute(computed);
|
||||
}
|
||||
bag.compute(vbag.computed());
|
||||
return bag;
|
||||
}
|
||||
return bag;
|
||||
|
@ -94,17 +90,32 @@ public class Operator{
|
|||
|
||||
@Override
|
||||
public boolean canPropagate(){
|
||||
return true;
|
||||
return visitor.canPropagate();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static <T extends Visitor<?>> T key(Visitor<?> visitor){
|
||||
return null;
|
||||
public static <E> Visitor<E> key(Visitor<E> visitor){
|
||||
return new Visitor<E>(){
|
||||
@Override
|
||||
public VisitorBag visit(E element){
|
||||
return visitor.visit(element).key(element);
|
||||
}
|
||||
|
||||
public static <T extends Visitor<?>> T value(Visitor<?> visitor){
|
||||
return null;
|
||||
@Override
|
||||
public boolean canVisit(Class<?> type){
|
||||
return visitor.canVisit(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPropagate(){
|
||||
return visitor.canPropagate();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static <E> Visitor<E> value(Visitor<E> visitor){
|
||||
return collect(visitor);
|
||||
}
|
||||
|
||||
public static <E> Visitor<E> collect(Visitor<E> visitor){
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package dev.peerat.parser.visitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
//only one pass in parameter or visit ?
|
||||
public class VisitorBag{
|
||||
|
@ -9,36 +12,36 @@ public class VisitorBag{
|
|||
private static Object VALIDATE_BAG = new Object();
|
||||
|
||||
private List<Object> collect;
|
||||
private List<Object> list;
|
||||
private Object computed;
|
||||
|
||||
public VisitorBag(){
|
||||
this.collect = new ArrayList<>();
|
||||
this.list = new ArrayList<>();
|
||||
this.collect = new ArrayList<>(0);
|
||||
}
|
||||
|
||||
public void compute(Object element){
|
||||
// System.out.println("compute("+collect+", "+list+") "+element+" <- "+Thread.currentThread().getStackTrace()[2]);
|
||||
this.list.add(element);
|
||||
this.computed = element;
|
||||
}
|
||||
|
||||
public void merge(VisitorBag bag){
|
||||
// System.out.println("merge("+collect+", "+list+") <- "+Thread.currentThread().getStackTrace()[2]);
|
||||
this.collect.addAll(bag.collect);
|
||||
if(!this.collect.isEmpty()) list.add(VALIDATE_BAG);
|
||||
// System.out.println("merged("+collect+", "+list+") <- "+Thread.currentThread().getStackTrace()[2]);
|
||||
if((!this.collect.isEmpty()) && this.computed == null) this.computed = VALIDATE_BAG;
|
||||
}
|
||||
|
||||
public Object computed(){
|
||||
return this.computed;
|
||||
}
|
||||
|
||||
public boolean isValidated(){
|
||||
return !this.list.isEmpty();
|
||||
return this.computed != null;
|
||||
}
|
||||
|
||||
public List<Object> computeList(){
|
||||
return this.list;
|
||||
VisitorBag key(Object key){
|
||||
this.collect = Arrays.asList(new Pair<>(key, this.collect));
|
||||
return this;
|
||||
}
|
||||
|
||||
VisitorBag collect(){
|
||||
this.collect.addAll(this.list);
|
||||
// System.out.println("collected("+collect+", "+list+") <- "+Thread.currentThread().getStackTrace()[2]);
|
||||
if(this.computed != null) this.collect.add(this.computed);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -49,4 +52,46 @@ public class VisitorBag{
|
|||
public <T> List<T> toList(){
|
||||
return (List<T>) this.collect;
|
||||
}
|
||||
|
||||
public <K, V> Map<K, V> toMap(){
|
||||
Map<K, V> map = new HashMap<>();
|
||||
for(Object obj : this.collect){
|
||||
Pair<K, V> pair = (Pair<K, V>)obj;
|
||||
map.put(pair.getKey(), pair.getValue());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public <K, V> Map<K, List<V>> toMapValues(){
|
||||
Map<K, List<V>> map = new HashMap<>();
|
||||
for(Object obj : this.collect){
|
||||
Pair<K, V> pair = (Pair<K, V>)obj;
|
||||
map.put(pair.getKey(), pair.getValues());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static class Pair<K, V>{
|
||||
|
||||
private Object key;
|
||||
private List<Object> values;
|
||||
|
||||
public Pair(Object key, List<Object> values){
|
||||
this.key = key;
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
public K getKey(){
|
||||
return (K) this.key;
|
||||
}
|
||||
|
||||
public V getValue(){
|
||||
return (V)this.values.get(0);
|
||||
}
|
||||
|
||||
public List<V> getValues(){
|
||||
return (List<V>) this.values;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue