builder/JavaImportBuilder + Builder System -> normalize (clear all token positions)
This commit is contained in:
parent
dd899da348
commit
e98ad587c1
8 changed files with 253 additions and 1 deletions
|
@ -28,6 +28,18 @@ public class JavaAnnotationBuilder extends JavaBuilder<Annotation>{
|
|||
return parameter(null, value);
|
||||
}
|
||||
|
||||
public JavaAnnotationBuilder normalize(Annotation annotation){
|
||||
setName(annotation.getName().getValue());
|
||||
Map<Token, Value> parameters = annotation.getParameters();
|
||||
if(parameters != null){
|
||||
if(this.values == null) this.values = new HashMap<>();
|
||||
for(Entry<Token, Value> entry : parameters.entrySet()){
|
||||
this.values.put(entry.getKey() == null ? null : entry.getKey().getValue(), JavaValueBuilder.normalize(entry.getValue()));
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Annotation build(){
|
||||
Map<Token, Value> map = null;
|
||||
|
|
|
@ -16,6 +16,15 @@ public abstract class JavaBuilder<T extends JavaElement>{
|
|||
return ofFile().setPackage(pack);
|
||||
}
|
||||
|
||||
public static JavaImportBuilder ofImport(){
|
||||
return new JavaImportBuilder();
|
||||
}
|
||||
|
||||
public static JavaImportBuilder ofImport(boolean isStatic, String value){
|
||||
JavaImportBuilder builder = ofImport().setImport(value);
|
||||
return isStatic ? builder.setStatic() : builder;
|
||||
}
|
||||
|
||||
public static JavaClassBuilder ofClass(){
|
||||
return new JavaClassBuilder();
|
||||
}
|
||||
|
|
|
@ -45,6 +45,37 @@ public class JavaClassBuilder extends JavaModifiableBuilder<JavaClassBuilder, de
|
|||
return this;
|
||||
}
|
||||
|
||||
public JavaClassBuilder normalize(dev.peerat.parser.java.Class clazz){
|
||||
normalizeAnnotations(clazz);
|
||||
normalizeSignature(clazz);
|
||||
//elements
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaClassBuilder normalizeAnnotations(dev.peerat.parser.java.Class clazz){
|
||||
List<Annotation> annotations = clazz.getAnnotations();
|
||||
if(annotations != null && annotations.size() > 0){
|
||||
if(this.annotations == null) this.annotations = new ArrayList<>();
|
||||
for(Annotation annotation : annotations){
|
||||
this.annotations.add(JavaBuilder.ofAnnotation().normalize(annotation).build());
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaClassBuilder normalizeSignature(dev.peerat.parser.java.Class clazz){
|
||||
this.mod = clazz.getModifier();
|
||||
this.name = clazz.getName().getValue();
|
||||
if(clazz.getExtension() != null) this.extend = clazz.getExtension().getValue();
|
||||
if(clazz.getImplementations() != null) this.implement = this.implement == null ? clazz.getImplementations().getValue() : ","+clazz.getImplementations().getValue();
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaClassBuilder clearAnnotations(){
|
||||
this.annotations = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class build(){
|
||||
dev.peerat.parser.java.Class result = new dev.peerat.parser.java.Class(this.annotations, this.mod, new Token(0,0,name, null), extend == null ? null : new Token(0,0,extend, null), implement == null ? null : new Token(0,0, implement, null));
|
||||
|
|
|
@ -6,7 +6,6 @@ import java.util.List;
|
|||
import dev.peerat.parser.Token;
|
||||
import dev.peerat.parser.java.ClassBase;
|
||||
import dev.peerat.parser.java.Import;
|
||||
import dev.peerat.parser.java.JavaElement;
|
||||
import dev.peerat.parser.java.JavaFile;
|
||||
|
||||
public class JavaFileBuilder extends JavaBuilder<JavaFile>{
|
||||
|
@ -32,6 +31,17 @@ public class JavaFileBuilder extends JavaBuilder<JavaFile>{
|
|||
return this;
|
||||
}
|
||||
|
||||
public JavaFileBuilder normalize(JavaFile file){
|
||||
if(file.getPackage() != null) this.pack = file.getPackage().getValue();
|
||||
List<Import> imports = file.getImports();
|
||||
if(imports != null && imports.size() > 0){
|
||||
if(this.imports == null) this.imports = new ArrayList<>();
|
||||
for(Import imp : imports) this.imports.add(JavaBuilder.ofImport().normalize(imp).build());
|
||||
}
|
||||
//classes
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JavaFile build(){
|
||||
|
|
|
@ -59,6 +59,47 @@ public class JavaFunctionBuilder extends JavaModifiableBuilder<JavaFunctionBuild
|
|||
return this;
|
||||
}
|
||||
|
||||
public JavaFunctionBuilder normalize(Function func){
|
||||
normalizeAnnotations(func);
|
||||
normalizeSignature(func);
|
||||
//elements
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaFunctionBuilder normalizeAnnotations(Function func){
|
||||
List<Annotation> annotations = func.getAnnotations();
|
||||
if(annotations != null && annotations.size() > 0){
|
||||
if(this.annotations == null) this.annotations = new ArrayList<>();
|
||||
for(Annotation annotation : annotations){
|
||||
this.annotations.add(JavaBuilder.ofAnnotation().normalize(annotation).build());
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaFunctionBuilder normalizeSignature(Function func){
|
||||
this.mod = func.getModifier();
|
||||
if(func.getGeneric() != null) this.generic = func.getGeneric().getValue();
|
||||
if(func.getReturnType() != null) this.type = func.getReturnType().getValue();
|
||||
if(func.getName() != null) this.name = func.getName().getValue();
|
||||
List<Variable> parameters = func.getParameters();
|
||||
if(parameters != null && parameters.size() > 0){
|
||||
if(this.parameters == null) this.parameters = new ArrayList<>();
|
||||
for(Variable param : parameters) this.parameters.add(JavaBuilder.ofVariable().normalize(param).build());
|
||||
}
|
||||
List<Token> throwables = func.getThrowables();
|
||||
if(throwables != null && throwables.size() > 0){
|
||||
if(this.throwables == null) this.throwables = new ArrayList<>();
|
||||
for(Token token : throwables) this.throwables.add(new Token(0,0, token.getValue(), null));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaFunctionBuilder clearAnnotations(){
|
||||
this.annotations = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function build(){
|
||||
Function result = new Function(
|
||||
|
|
32
src/dev/peerat/parser/java/builder/JavaImportBuilder.java
Normal file
32
src/dev/peerat/parser/java/builder/JavaImportBuilder.java
Normal file
|
@ -0,0 +1,32 @@
|
|||
package dev.peerat.parser.java.builder;
|
||||
|
||||
import dev.peerat.parser.Token;
|
||||
import dev.peerat.parser.java.Import;
|
||||
|
||||
public class JavaImportBuilder extends JavaBuilder<Import>{
|
||||
|
||||
private boolean isStatic;
|
||||
private String value;
|
||||
|
||||
public JavaImportBuilder setStatic(){
|
||||
this.isStatic = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaImportBuilder setImport(String value){
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaImportBuilder normalize(Import imp){
|
||||
this.isStatic = imp.isStatic();
|
||||
this.value = imp.getValue().getValue();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Import build(){
|
||||
return new Import(this.isStatic, new Token(0,0,this.value, null));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +1,27 @@
|
|||
package dev.peerat.parser.java.builder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import dev.peerat.parser.Parser;
|
||||
import dev.peerat.parser.Token;
|
||||
import dev.peerat.parser.java.JavaElement;
|
||||
import dev.peerat.parser.java.JavaParser;
|
||||
import dev.peerat.parser.java.tree.JavaTreeType;
|
||||
import dev.peerat.parser.java.value.ArrayAccessValue;
|
||||
import dev.peerat.parser.java.value.ArrayValue;
|
||||
import dev.peerat.parser.java.value.BiValue;
|
||||
import dev.peerat.parser.java.value.CastValue;
|
||||
import dev.peerat.parser.java.value.InstanceValue;
|
||||
import dev.peerat.parser.java.value.LambdaValue;
|
||||
import dev.peerat.parser.java.value.MethodCallValue;
|
||||
import dev.peerat.parser.java.value.ModifierValue;
|
||||
import dev.peerat.parser.java.value.LambdaValue.LambdaParameter;
|
||||
import dev.peerat.parser.java.value.StaticValue;
|
||||
import dev.peerat.parser.java.value.TriValue;
|
||||
import dev.peerat.parser.java.value.Value;
|
||||
import dev.peerat.parser.java.value.VariableAccessValue;
|
||||
import dev.peerat.parser.java.value.Value.ValueContainer;
|
||||
import dev.peerat.parser.java.visitor.JavaVisitor;
|
||||
import dev.peerat.parser.visitor.VisitorBag;
|
||||
|
@ -16,6 +30,85 @@ public class JavaValueBuilder extends JavaBuilder<Value>{
|
|||
|
||||
private static Parser<JavaElement> PARSER = new JavaParser(JavaTreeType.VALUE);
|
||||
|
||||
static Value normalize(Value value){
|
||||
if(value == null) return null;
|
||||
if(value instanceof StaticValue) return normalizeStatic((StaticValue) value);
|
||||
if(value instanceof BiValue) return normalizeBi((BiValue) value);
|
||||
if(value instanceof TriValue) return normalizeTri((TriValue) value);
|
||||
if(value instanceof ArrayAccessValue) return normalizeArrayAccess((ArrayAccessValue) value);
|
||||
if(value instanceof ArrayValue) return normalizeArray((ArrayValue) value);
|
||||
if(value instanceof CastValue) return normalizeCast((CastValue) value);
|
||||
if(value instanceof InstanceValue) return normalizeInstance((InstanceValue) value);
|
||||
if(value instanceof LambdaValue) return normalizeLambda((LambdaValue) value);
|
||||
if(value instanceof MethodCallValue) return normalizeMethodCall((MethodCallValue) value);
|
||||
if(value instanceof ModifierValue) return normalizeModifier((ModifierValue) value);
|
||||
if(value instanceof VariableAccessValue) return normalizeVariableAccess((VariableAccessValue) value);
|
||||
return null;
|
||||
}
|
||||
|
||||
static Value normalizeStatic(StaticValue value){
|
||||
return new StaticValue(new Token(0,0,value.getToken().getValue(), null));
|
||||
}
|
||||
|
||||
static Value normalizeBi(BiValue value){
|
||||
return new BiValue(normalize(value.left()), value.getAction(), normalize(value.right()));
|
||||
}
|
||||
|
||||
static Value normalizeTri(TriValue value){
|
||||
return new TriValue(normalize(value.getChecker()), normalize(value.success()), normalize(value.fail()));
|
||||
}
|
||||
|
||||
static Value normalizeArrayAccess(ArrayAccessValue value){
|
||||
return new ArrayAccessValue(normalize(value.base()), normalize(value.getAccessor()));
|
||||
}
|
||||
|
||||
static Value normalizeArray(ArrayValue value){
|
||||
Value[] values = value.getValues();
|
||||
Value[] result = new Value[values.length];
|
||||
for(int i = 0; i < values.length; i++){
|
||||
result[i] = normalize(values[i]);
|
||||
}
|
||||
return new ArrayValue(result);
|
||||
}
|
||||
|
||||
static Value normalizeCast(CastValue value){
|
||||
return new CastValue(new Token(0,0,value.getType().getValue(), null), normalize(value.getValue()));
|
||||
}
|
||||
|
||||
static Value normalizeInstance(InstanceValue value){
|
||||
List<Value> params = value.getParameters();
|
||||
List<Value> nparams = new ArrayList<>();
|
||||
for(Value param : params) nparams.add(normalize(param));
|
||||
InstanceValue result = new InstanceValue(new Token(0,0,value.getToken().getValue(), null), nparams);
|
||||
//elements
|
||||
return result;
|
||||
}
|
||||
|
||||
static Value normalizeLambda(LambdaValue value){
|
||||
List<LambdaParameter> params = value.getParameters();
|
||||
List<LambdaParameter> nparams = new ArrayList<>();
|
||||
for(LambdaParameter param : params) nparams.add(new LambdaParameter(new Token(0,0,param.getType().getValue(), null), new Token(0,0,param.getName().getValue(), null)));
|
||||
LambdaValue result = new LambdaValue(nparams);
|
||||
//elements
|
||||
return result;
|
||||
}
|
||||
|
||||
static Value normalizeMethodCall(MethodCallValue value){
|
||||
List<Value> params = value.getParameters();
|
||||
List<Value> nparams = new ArrayList<>();
|
||||
for(Value param : params) nparams.add(normalize(param));
|
||||
return new MethodCallValue(normalize(value.base()), value.getGeneric() == null ? null : new Token(0,0,value.getGeneric().getValue(), null), new Token(0,0,value.getToken().getValue(), null), nparams);
|
||||
}
|
||||
|
||||
static Value normalizeModifier(ModifierValue value){
|
||||
return new ModifierValue(new Token(0,0,value.getModifier().getValue(), null), normalize(value.getValue()));
|
||||
}
|
||||
|
||||
static Value normalizeVariableAccess(VariableAccessValue value){
|
||||
return new VariableAccessValue(normalize(value.base()), new Token(0,0, value.getVariable().getValue(), null));
|
||||
}
|
||||
|
||||
|
||||
private Value value;
|
||||
|
||||
public JavaValueBuilder(String value) throws Exception{
|
||||
|
|
|
@ -37,6 +37,30 @@ public class JavaVariableBuilder extends JavaModifiableBuilder<JavaVariableBuild
|
|||
return this;
|
||||
}
|
||||
|
||||
public JavaVariableBuilder normalize(Variable variable){
|
||||
normalizeAnnotations(variable);
|
||||
setType(variable.getType().getValue());
|
||||
setName(variable.getName().getValue());
|
||||
setValue(JavaValueBuilder.normalize(variable.getValue()));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaVariableBuilder normalizeAnnotations(Variable variable){
|
||||
List<Annotation> annotations = variable.getAnnotations();
|
||||
if(annotations != null && annotations.size() > 0){
|
||||
if(this.annotations == null) this.annotations = new ArrayList<>();
|
||||
for(Annotation annotation : annotations){
|
||||
this.annotations.add(JavaBuilder.ofAnnotation().normalize(annotation).build());
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaVariableBuilder clearAnnotations(){
|
||||
this.annotations = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Variable build(){
|
||||
return new Variable(annotations, mod, new Token(0,0, type, null), new Token(0,0, name, null), elipse, value);
|
||||
|
|
Loading…
Add table
Reference in a new issue