[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.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.parser.Token;
|
import be.jeffcheasey88.peeratcode.parser.Token;
|
||||||
|
@ -35,6 +36,21 @@ public class Annotation extends JavaElement{
|
||||||
public Map<Token, Value> getParameters(){
|
public Map<Token, Value> getParameters(){
|
||||||
return this.values;
|
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
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||||
|
|
|
@ -20,6 +20,7 @@ public class Class extends JavaElement implements AnnotableBuffer, ClassContaine
|
||||||
|
|
||||||
private List<Annotation> annotations;
|
private List<Annotation> annotations;
|
||||||
|
|
||||||
|
//where mod ????
|
||||||
private Token name;
|
private Token name;
|
||||||
private Token extend;
|
private Token extend;
|
||||||
private Token implement;
|
private Token implement;
|
||||||
|
@ -84,6 +85,33 @@ public class Class extends JavaElement implements AnnotableBuffer, ClassContaine
|
||||||
this.annotationBuffer = null;
|
this.annotationBuffer = null;
|
||||||
return list;
|
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
|
@Override
|
||||||
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
|
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
package be.jeffcheasey88.peeratcode.parser.java;
|
||||||
|
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.parser.Token;
|
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.Operation.OperationContainer;
|
||||||
import be.jeffcheasey88.peeratcode.parser.java.Variable.VariableContainer;
|
import be.jeffcheasey88.peeratcode.parser.java.Variable.VariableContainer;
|
||||||
|
|
||||||
|
@ -90,6 +92,40 @@ public class Function extends JavaElement implements VariableContainer, Operatio
|
||||||
public List<JavaElement> getElements(){
|
public List<JavaElement> getElements(){
|
||||||
return this.elements;
|
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
|
@Override
|
||||||
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
|
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
package be.jeffcheasey88.peeratcode.parser.java;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.parser.Bag;
|
import be.jeffcheasey88.peeratcode.parser.Bag;
|
||||||
|
import be.jeffcheasey88.peeratcode.parser.ElementBuilder;
|
||||||
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public abstract class JavaElement{
|
public abstract class JavaElement implements ElementBuilder{
|
||||||
|
|
||||||
Bag bag; //to remove
|
Bag bag; //to remove
|
||||||
|
|
||||||
|
|
|
@ -53,16 +53,7 @@ public class JavaFile extends JavaElement implements ClassContainer, AnnotableBu
|
||||||
public List<Class> getSubClasses(){
|
public List<Class> getSubClasses(){
|
||||||
return this.subClazz;
|
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
|
@Override
|
||||||
public void addAnnotationBuffer(Annotation annotation){
|
public void addAnnotationBuffer(Annotation annotation){
|
||||||
if(annotationBuffer == null) annotationBuffer = new ArrayList<>();
|
if(annotationBuffer == null) annotationBuffer = new ArrayList<>();
|
||||||
|
@ -74,4 +65,32 @@ public class JavaFile extends JavaElement implements ClassContainer, AnnotableBu
|
||||||
this.annotationBuffer = null;
|
this.annotationBuffer = null;
|
||||||
return list;
|
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+"]";
|
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
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||||
return content != null ? finder.apply(content) ? (E) content : null : null;
|
return content != null ? finder.apply(content) ? (E) content : null : null;
|
||||||
|
@ -66,6 +75,13 @@ public class Value extends JavaElement{
|
||||||
return left+" "+action+" "+right;
|
return left+" "+action+" "+right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception{
|
||||||
|
left.build(builder);
|
||||||
|
builder.append(action);
|
||||||
|
right.build(builder);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||||
return finder.apply(left) ? (E) left : finder.apply(right) ? (E) right : null;
|
return finder.apply(left) ? (E) left : finder.apply(right) ? (E) right : null;
|
||||||
|
@ -92,6 +108,15 @@ public class Value extends JavaElement{
|
||||||
return fail;
|
return fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
check.build(builder);
|
||||||
|
builder.append("?");
|
||||||
|
success.build(builder);
|
||||||
|
builder.append(":");
|
||||||
|
fail.build(builder);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
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;
|
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;
|
package be.jeffcheasey88.peeratcode.parser.java;
|
||||||
|
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.parser.Token;
|
import be.jeffcheasey88.peeratcode.parser.Token;
|
||||||
|
import be.jeffcheasey88.peeratcode.parser.TokenType;
|
||||||
|
|
||||||
public class Variable extends JavaElement{
|
public class Variable extends JavaElement{
|
||||||
|
|
||||||
|
@ -68,6 +70,24 @@ public class Variable extends JavaElement{
|
||||||
return this.value;
|
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
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||||
if(annotations != null){
|
if(annotations != null){
|
||||||
|
@ -77,4 +97,5 @@ public class Variable extends JavaElement{
|
||||||
}
|
}
|
||||||
return value != null ? finder.apply(value) ? (E)value : null : null;
|
return value != null ? finder.apply(value) ? (E)value : null : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,13 @@ public class AssignOperation extends Operation{
|
||||||
public Value right(){
|
public Value right(){
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception{
|
||||||
|
left.build(builder);
|
||||||
|
builder.append("=");
|
||||||
|
right.build(builder);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||||
|
|
|
@ -8,6 +8,11 @@ import be.jeffcheasey88.peeratcode.parser.java.Operation;
|
||||||
public class BreakOperation extends Operation{
|
public class BreakOperation extends Operation{
|
||||||
|
|
||||||
public BreakOperation(){}
|
public BreakOperation(){}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception{
|
||||||
|
builder.append("break;");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||||
|
|
|
@ -6,6 +6,11 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
||||||
import be.jeffcheasey88.peeratcode.parser.java.Operation;
|
import be.jeffcheasey88.peeratcode.parser.java.Operation;
|
||||||
|
|
||||||
public class ContinueOperation extends Operation{
|
public class ContinueOperation extends Operation{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception{
|
||||||
|
builder.append("continue;");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||||
|
|
|
@ -37,11 +37,17 @@ public class MethodCallOperation extends Operation{
|
||||||
public List<Value> getParameters(){
|
public List<Value> getParameters(){
|
||||||
return this.parameters;
|
return this.parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception{
|
||||||
|
if(previous != null) previous.build(builder);
|
||||||
|
//todo
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
||||||
if(finder.apply(start)) return (E)start;
|
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){
|
for(Value param : this.parameters){
|
||||||
if(finder.apply(param)) return (E)param;
|
if(finder.apply(param)) return (E)param;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,4 +38,9 @@ public class OperationBag extends Operation implements VariableContainer, Operat
|
||||||
return null;
|
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;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
value.build(builder);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
||||||
return value != null ? finder.apply(value) ? (E)value : null : null;
|
return value != null ? finder.apply(value) ? (E)value : null : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,10 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
||||||
import be.jeffcheasey88.peeratcode.parser.java.Operation;
|
import be.jeffcheasey88.peeratcode.parser.java.Operation;
|
||||||
|
|
||||||
public class SwitchOperation extends Operation{
|
public class SwitchOperation extends Operation{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception{
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
||||||
|
|
|
@ -17,6 +17,12 @@ public class ThrowOperation extends Operation{
|
||||||
public Value getValue(){
|
public Value getValue(){
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Builder builder) throws Exception {
|
||||||
|
builder.append(" throw ");
|
||||||
|
value.build(builder);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue