[Builder] base rebuild
This commit is contained in:
parent
3844a6cf06
commit
2c3400e776
16 changed files with 236 additions and 12 deletions
33
src/be/jeffcheasey88/peeratcode/parser/ElementBuilder.java
Normal file
33
src/be/jeffcheasey88/peeratcode/parser/ElementBuilder.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package be.jeffcheasey88.peeratcode.parser;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public interface ElementBuilder{
|
||||
|
||||
void build(Builder builder) throws Exception;
|
||||
|
||||
public static class Builder{
|
||||
|
||||
private List<Token> tokens;
|
||||
|
||||
public Builder(){
|
||||
this.tokens = new LinkedList<>();
|
||||
this.tokens.add(new Token(1,1,"", TokenType.GROUP));
|
||||
}
|
||||
|
||||
public void append(Token token){
|
||||
this.tokens.add(token);
|
||||
}
|
||||
|
||||
public void append(String value){
|
||||
Token last = tokens.get(tokens.size()-1);
|
||||
append(new Token(last.getLineNumber(), last.getCharacterNumber()+last.getValue().length(), value, TokenType.GROUP));
|
||||
}
|
||||
|
||||
public void build(BufferedReader writer) throws Exception{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.Function;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.Token;
|
||||
|
@ -36,6 +37,21 @@ public class Annotation extends JavaElement{
|
|||
return this.values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
builder.append(name);
|
||||
if(values != null && values.size() > 0){
|
||||
builder.append("(");
|
||||
for(Entry<Token,Value> entry : values.entrySet()){
|
||||
builder.append(entry.getKey());
|
||||
builder.append("=");
|
||||
entry.getValue().build(builder);
|
||||
builder.append(","); //replace by iterator splitter method
|
||||
}
|
||||
builder.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
for(Value value : this.values.values()){
|
||||
|
|
|
@ -20,6 +20,7 @@ public class Class extends JavaElement implements AnnotableBuffer, ClassContaine
|
|||
|
||||
private List<Annotation> annotations;
|
||||
|
||||
//where mod ????
|
||||
private Token name;
|
||||
private Token extend;
|
||||
private Token implement;
|
||||
|
@ -85,6 +86,33 @@ public class Class extends JavaElement implements AnnotableBuffer, ClassContaine
|
|||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
if(annotations != null){
|
||||
for(Annotation annotation : this.annotations){
|
||||
annotation.build(builder);
|
||||
}
|
||||
}
|
||||
|
||||
builder.append("class");
|
||||
builder.append(name);
|
||||
if(extend != null){
|
||||
builder.append(" extends ");
|
||||
builder.append(extend);
|
||||
}
|
||||
if(implement != null){
|
||||
builder.append(" implements ");
|
||||
builder.append(implement);
|
||||
}
|
||||
builder.append("{");
|
||||
if(elements != null){
|
||||
for(JavaElement element : this.elements){
|
||||
element.build(builder);
|
||||
}
|
||||
}
|
||||
builder.append("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
|
||||
if(annotations != null){
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.Token;
|
||||
import be.jeffcheasey88.peeratcode.parser.TokenType;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.Operation.OperationContainer;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.Variable.VariableContainer;
|
||||
|
||||
|
@ -91,6 +93,40 @@ public class Function extends JavaElement implements VariableContainer, Operatio
|
|||
return this.elements;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
if(annotations != null){
|
||||
for(Annotation annotation : this.annotations){
|
||||
annotation.build(builder);
|
||||
}
|
||||
}
|
||||
String mod = Modifier.toString(this.mod);
|
||||
builder.append(new Token(type.getLineNumber(), type.getCharacterNumber()-(mod.length()+1), mod, TokenType.GROUP));
|
||||
if(generic != null) builder.append(generic);
|
||||
builder.append(type);
|
||||
builder.append(name);
|
||||
builder.append("(");
|
||||
if(parameters != null){
|
||||
for(Variable param : this.parameters){
|
||||
param.build(builder);
|
||||
}
|
||||
}
|
||||
builder.append(")");
|
||||
if(throwables != null){
|
||||
builder.append("throws");
|
||||
for(Token token : throwables){
|
||||
builder.append(token);
|
||||
}
|
||||
}
|
||||
builder.append("{");
|
||||
if(elements != null){
|
||||
for(JavaElement content : this.elements){
|
||||
content.build(builder);
|
||||
}
|
||||
}
|
||||
builder.append("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
|
||||
if(annotations != null){
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.Bag;
|
||||
import be.jeffcheasey88.peeratcode.parser.ElementBuilder;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public abstract class JavaElement{
|
||||
public abstract class JavaElement implements ElementBuilder{
|
||||
|
||||
Bag bag; //to remove
|
||||
|
||||
|
|
|
@ -54,15 +54,6 @@ public class JavaFile extends JavaElement implements ClassContainer, AnnotableBu
|
|||
return this.subClazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
if(finder.apply(mainClazz)) return (E) mainClazz;
|
||||
for(Class clazz : subClazz){
|
||||
if(finder.apply(clazz)) return (E) clazz;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAnnotationBuffer(Annotation annotation){
|
||||
if(annotationBuffer == null) annotationBuffer = new ArrayList<>();
|
||||
|
@ -74,4 +65,32 @@ public class JavaFile extends JavaElement implements ClassContainer, AnnotableBu
|
|||
this.annotationBuffer = null;
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception {
|
||||
builder.append("package ");
|
||||
builder.append(pack);
|
||||
builder.append(";");
|
||||
|
||||
for(Import imp : imports){
|
||||
builder.append("import ");
|
||||
if(imp.isStatic()) builder.append("static ");
|
||||
builder.append(imp.getValue());
|
||||
}
|
||||
|
||||
mainClazz.build(builder);
|
||||
for(Class clazz : subClazz){
|
||||
clazz.build(builder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
if(finder.apply(mainClazz)) return (E) mainClazz;
|
||||
for(Class clazz : subClazz){
|
||||
if(finder.apply(clazz)) return (E) clazz;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,6 +32,15 @@ public class Value extends JavaElement{
|
|||
return "Value[token="+token+", content="+content+"]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
if(token != null){
|
||||
builder.append(token);
|
||||
}else{
|
||||
content.build(builder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
return content != null ? finder.apply(content) ? (E) content : null : null;
|
||||
|
@ -66,6 +75,13 @@ public class Value extends JavaElement{
|
|||
return left+" "+action+" "+right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
left.build(builder);
|
||||
builder.append(action);
|
||||
right.build(builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
return finder.apply(left) ? (E) left : finder.apply(right) ? (E) right : null;
|
||||
|
@ -92,6 +108,15 @@ public class Value extends JavaElement{
|
|||
return fail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception {
|
||||
check.build(builder);
|
||||
builder.append("?");
|
||||
success.build(builder);
|
||||
builder.append(":");
|
||||
fail.build(builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
return finder.apply(check) ? (E) check : finder.apply(success) ? (E) success : finder.apply(fail) ? (E) fail : null;
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.Token;
|
||||
import be.jeffcheasey88.peeratcode.parser.TokenType;
|
||||
|
||||
public class Variable extends JavaElement{
|
||||
|
||||
|
@ -68,6 +70,24 @@ public class Variable extends JavaElement{
|
|||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
if(annotations != null){
|
||||
for(Annotation annotation : this.annotations){
|
||||
annotation.build(builder);
|
||||
}
|
||||
}
|
||||
String mod = Modifier.toString(this.mod);
|
||||
builder.append(new Token(type.getLineNumber(), type.getCharacterNumber()-(mod.length()+1), mod, TokenType.GROUP));
|
||||
builder.append(type);
|
||||
if(elips) builder.append("...");
|
||||
builder.append(name);
|
||||
if(value != null){
|
||||
builder.append("=");
|
||||
value.build(builder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
if(annotations != null){
|
||||
|
@ -77,4 +97,5 @@ public class Variable extends JavaElement{
|
|||
}
|
||||
return value != null ? finder.apply(value) ? (E)value : null : null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,6 +24,13 @@ public class AssignOperation extends Operation{
|
|||
return right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
left.build(builder);
|
||||
builder.append("=");
|
||||
right.build(builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
return finder.apply(left) ? (E)left : finder.apply(right) ? (E)right : null;
|
||||
|
|
|
@ -9,6 +9,11 @@ public class BreakOperation extends Operation{
|
|||
|
||||
public BreakOperation(){}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
builder.append("break;");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
return null;
|
||||
|
|
|
@ -7,6 +7,11 @@ import be.jeffcheasey88.peeratcode.parser.java.Operation;
|
|||
|
||||
public class ContinueOperation extends Operation{
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
builder.append("continue;");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
return null;
|
||||
|
|
|
@ -38,10 +38,16 @@ public class MethodCallOperation extends Operation{
|
|||
return this.parameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
if(previous != null) previous.build(builder);
|
||||
//todo
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
||||
if(finder.apply(start)) return (E)start;
|
||||
if(finder.apply(previous)) return (E) previous;
|
||||
if(previous != null) if(finder.apply(previous)) return (E) previous;
|
||||
for(Value param : this.parameters){
|
||||
if(finder.apply(param)) return (E)param;
|
||||
}
|
||||
|
|
|
@ -38,4 +38,9 @@ public class OperationBag extends Operation implements VariableContainer, Operat
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception {
|
||||
//todo and super on child
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,9 +18,15 @@ public class ReturnOperation extends Operation{
|
|||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception {
|
||||
value.build(builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
||||
return value != null ? finder.apply(value) ? (E)value : null : null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,10 @@ import be.jeffcheasey88.peeratcode.parser.java.Operation;
|
|||
|
||||
public class SwitchOperation extends Operation{
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception{
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
||||
return null;
|
||||
|
|
|
@ -18,6 +18,12 @@ public class ThrowOperation extends Operation{
|
|||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build(Builder builder) throws Exception {
|
||||
builder.append(" throw ");
|
||||
value.build(builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
||||
return finder.apply(value) ? (E)value : null;
|
||||
|
|
Loading…
Add table
Reference in a new issue