Refractor Annotation buffer

This commit is contained in:
jeffcheasey88 2023-10-18 14:22:46 +02:00
parent 4445a76672
commit aab4cd5fd8
10 changed files with 105 additions and 66 deletions

View file

@ -1,5 +1,6 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@ -7,9 +8,11 @@ import be.jeffcheasey88.peeratcode.parser.Token;
public class Annotation extends JavaElement{
public static interface Annotable{
public static interface AnnotableBuffer{
void addAnnotation(Annotation annotation);
void addAnnotationBuffer(Annotation annotation);
List<Annotation> getAnnotationBuffer();
}

View file

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.Annotable;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.AnnotableBuffer;
import be.jeffcheasey88.peeratcode.parser.java.Function.FunctionContainer;
import be.jeffcheasey88.peeratcode.parser.java.Variable.VariableContainer;
@ -14,8 +14,9 @@ interface ClassContainer{
}
public class Class extends JavaElement implements Annotable, ClassContainer, FunctionContainer, VariableContainer{
public class Class extends JavaElement implements AnnotableBuffer, ClassContainer, FunctionContainer, VariableContainer{
private List<Annotation> annotationBuffer;
private List<Annotation> annotations;
@ -25,14 +26,14 @@ public class Class extends JavaElement implements Annotable, ClassContainer, Fun
private List<JavaElement> elements;
public Class(Token name){
public Class(List<Annotation> annotations, Token name){
this.annotations = annotations;
this.name = name;
this.annotations = new ArrayList<>();
this.elements = new ArrayList<>();
}
public Class(Token name, Token extend, Token implement){
this(name);
public Class(List<Annotation> annotations, Token name, Token extend, Token implement){
this(annotations, name);
this.extend = extend;
this.implement = implement;
}
@ -42,11 +43,6 @@ public class Class extends JavaElement implements Annotable, ClassContainer, Fun
this.elements.add(function);
}
@Override
public void addAnnotation(Annotation annotation) {
this.annotations.add(annotation);
}
@Override
public void addVariable(Variable variable) {
this.elements.add(variable);
@ -67,6 +63,18 @@ public class Class extends JavaElement implements Annotable, ClassContainer, Fun
}
return null;
}
@Override
public void addAnnotationBuffer(Annotation annotation){
if(annotationBuffer == null) annotationBuffer = new ArrayList<>();
annotationBuffer.add(annotation);
}
public List<Annotation> getAnnotationBuffer(){
List<Annotation> list = this.annotationBuffer;
this.annotationBuffer = null;
return list;
}
}

View file

@ -4,11 +4,10 @@ import java.util.ArrayList;
import java.util.List;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.Annotable;
import be.jeffcheasey88.peeratcode.parser.java.Operation.OperationContainer;
import be.jeffcheasey88.peeratcode.parser.java.Variable.VariableContainer;
public class Function extends JavaElement implements Annotable, VariableContainer, OperationContainer{
public class Function extends JavaElement implements VariableContainer, OperationContainer{
public static interface FunctionContainer{
@ -27,34 +26,29 @@ public class Function extends JavaElement implements Annotable, VariableContaine
private List<JavaElement> elements;
public Function(int mod, Token type, Token name){
public Function(List<Annotation> annotations, int mod, Token type, Token name){
this.annotations = annotations;
this.mod = mod;
this.type = type;
this.name = name;
this.annotations = new ArrayList<>();
this.elements = new ArrayList<>();
}
public Function(int mod, Token generic, Token type, Token name){
this(mod, type, name);
public Function(List<Annotation> annotations, int mod, Token generic, Token type, Token name){
this(annotations, mod, type, name);
this.generic = generic;
}
public Function(int mod, Token generic, Token type, Token name, List<Variable> parameters){
this(mod, generic, type, name);
public Function(List<Annotation> annotations, int mod, Token generic, Token type, Token name, List<Variable> parameters){
this(annotations, mod, generic, type, name);
this.parameters = parameters;
}
public Function(int mod, Token generic, Token type, Token name, List<Variable> parameters, List<Token> throwables){
this(mod, generic, type, name, parameters);
public Function(List<Annotation> annotations, int mod, Token generic, Token type, Token name, List<Variable> parameters, List<Token> throwables){
this(annotations, mod, generic, type, name, parameters);
this.throwables = throwables;
}
@Override
public void addAnnotation(Annotation annotation) {
this.annotations.add(annotation);
}
@Override
public void addVariable(Variable variable) {
this.elements.add(variable);

View file

@ -6,8 +6,11 @@ import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.Bag;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.AnnotableBuffer;
public class JavaFile extends JavaElement implements ClassContainer{
public class JavaFile extends JavaElement implements ClassContainer, AnnotableBuffer{
private List<Annotation> annotationBuffer;
private List<Token> pack;
private List<List<Token>> imports;
@ -51,4 +54,16 @@ public class JavaFile extends JavaElement implements ClassContainer{
}
return null;
}
@Override
public void addAnnotationBuffer(Annotation annotation){
if(annotationBuffer == null) annotationBuffer = new ArrayList<>();
annotationBuffer.add(annotation);
}
public List<Annotation> getAnnotationBuffer(){
List<Annotation> list = this.annotationBuffer;
this.annotationBuffer = null;
return list;
}
}

View file

@ -21,7 +21,7 @@ import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.TokenType;
import be.jeffcheasey88.peeratcode.parser.TokenValidator;
import be.jeffcheasey88.peeratcode.parser.Tokenizer;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.Annotable;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.AnnotableBuffer;
import be.jeffcheasey88.peeratcode.parser.java.Function.FunctionContainer;
import be.jeffcheasey88.peeratcode.parser.java.Variable.VariableContainer;
import be.jeffcheasey88.peeratcode.parser.state.BuilderStateTree;
@ -450,7 +450,7 @@ public class JavaParser extends Parser<JavaElement> {
annotation_name.end((parent, bag) -> {
Annotation result = new Annotation(bag.get("name"));
bag.set(result);
if(parent instanceof Annotable) ((Annotable)parent).addAnnotation(result);
if(parent instanceof AnnotableBuffer) ((AnnotableBuffer)parent).addAnnotationBuffer(result);
return null;
});
StateTree<JavaElement> annotation_begin = annotation_name.then((validator) -> validator.validate((token) -> token.getValue().equals("(")));
@ -458,7 +458,7 @@ public class JavaParser extends Parser<JavaElement> {
annotation_end.end((parent, bag) -> {
Annotation result = new Annotation(bag.get("name"), bag.get("values"));
bag.set(result);
if(parent instanceof Annotable) ((Annotable)parent).addAnnotation(result);
if(parent instanceof AnnotableBuffer) ((AnnotableBuffer)parent).addAnnotationBuffer(result);
return null;
});
StateTree<JavaElement> annotation_var = annotation_begin.then((validator) -> validator.validate(
@ -490,7 +490,9 @@ public class JavaParser extends Parser<JavaElement> {
Token vtype = bag.get("type");
Map<Token, Value> map = bag.get("vars");
for(Entry<Token, Value> vars : map.entrySet()){
Variable result = new Variable(mod == null ? 0 : mod, vtype, vars.getKey(), false, vars.getValue());
List<Annotation> annotations = null;
if(parent instanceof AnnotableBuffer) annotations = (((AnnotableBuffer)parent).getAnnotationBuffer());
Variable result = new Variable(annotations, mod == null ? 0 : mod, vtype, vars.getKey(), false, vars.getValue());
bag.set(result);
if(parent instanceof VariableContainer) ((VariableContainer)parent).addVariable(result);
}
@ -731,7 +733,7 @@ public class JavaParser extends Parser<JavaElement> {
BiFunction<JavaElement, Bag, JavaElement> function_builder = (parent, bag) -> {
buildVariable(bag);
Integer mod = bag.get("mod");
Function function = new Function(mod == null ? 0 : mod, bag.get("generic"), bag.get("type"), bag.get("name"), bag.get("param"), bag.get("throws"));
Function function = new Function((((AnnotableBuffer)parent).getAnnotationBuffer()), mod == null ? 0 : mod, bag.get("generic"), bag.get("type"), bag.get("name"), bag.get("param"), bag.get("throws"));
if(parent instanceof FunctionContainer) ((FunctionContainer)parent).addFunction(function);
return function;
};
@ -739,7 +741,7 @@ public class JavaParser extends Parser<JavaElement> {
InitialStateTree<JavaElement> function = new InitialStateTree<>();
function.multiple(annotation);
BuilderStateTree<JavaElement,JavaElement> function_static = function.then((validator) -> validator.validate((token) -> token.getValue().equals("static")) && validator.validate((token) -> token.getValue().equals("{"))).end((parent, bag) -> {
Function build = new Function(Modifier.STATIC, null, null);
Function build = new Function((((AnnotableBuffer)parent).getAnnotationBuffer()), Modifier.STATIC, null, null);
if(parent instanceof FunctionContainer) ((FunctionContainer)parent).addFunction(build);
return build;
});
@ -828,27 +830,40 @@ public class JavaParser extends Parser<JavaElement> {
StateTree<JavaElement> clazz_base = clazz.then((validator) -> validator.validate((token) -> token.getValue().equals("class")))
.then(new RedirectStateTree<>(type, (global, local) -> global.set("name", local.get())));
clazz_base.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.<JavaElement>end((JavaElement, bag) -> null)
.<JavaElement>end((parent, bag) -> {
Class current = new Class((((AnnotableBuffer)parent).getAnnotationBuffer()), bag.get("name"));
if(parent instanceof ClassContainer) ((ClassContainer)parent).addClass(current);
return current;
})
.multiple(clazz_container)
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
.end((a,b) -> a);
StateTree<JavaElement> clazz_implement = clazz_base.then((validator) -> validator.validate((token) -> token.getValue().equals("implements")));
StateTree<JavaElement> clazz_implement_name = clazz_implement.then(new RedirectStateTree<>(type, (global, local) -> {
Token token = global.get("implement");
// if(token == null) token = local.get();
// else token = token.concat(local.get());
global.set("implement", token);
if(token == null) global.set("implement", local.get());
else global.set("implement", token.concat(local.get()));
}));
clazz_implement_name.then((validator) -> validator.validate((token) -> token.getValue().equals(","))).then(clazz_implement_name);
clazz_implement_name.then((validator) -> validator.validate(
(token) -> token.getValue().equals(","),
(bag, token) -> bag.set("implement", bag.<Token>get("implement").concat(token)))).then(clazz_implement_name);
clazz_implement_name.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.<JavaElement>end((JavaElement, bag) -> null)
.<JavaElement>end((parent, bag) -> {
Class current = new Class((((AnnotableBuffer)parent).getAnnotationBuffer()), bag.get("name"), bag.get("extend"), bag.get("implements"));
if(parent instanceof ClassContainer) ((ClassContainer)parent).addClass(current);
return current;
})
.multiple(clazz_container)
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
.end((a,b) -> a);
StateTree<JavaElement> clazz_extend = clazz_base.then((validator) -> validator.validate((token) -> token.getValue().equals("extends")))
.then(new RedirectStateTree<>(type, (global, local) -> global.set("extend", local.get())));
clazz_extend.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.<JavaElement>end((JavaElement, bag) -> null)
.<JavaElement>end((parent, bag) -> {
Class current = new Class((((AnnotableBuffer)parent).getAnnotationBuffer()), bag.get("name"), bag.get("extend"), bag.get("implements"));
if(parent instanceof ClassContainer) ((ClassContainer)parent).addClass(current);
return current;
})
.multiple(clazz_container)
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
.end((a,b) -> a);
@ -982,6 +997,7 @@ public class JavaParser extends Parser<JavaElement> {
System.out.println("init");
}
//TAKE ANNOTATIONS ???????
private static void buildVariable(Bag bag){
if(!bag.has("arg_name")) return;
@ -993,7 +1009,7 @@ public class JavaParser extends Parser<JavaElement> {
Integer mod = bag.get("arg_mod");
Variable variable = new Variable(mod == null ? 0 : mod, bag.<Token>get("arg_type"), bag.<Token>get("arg_name"), bag.get("arg_elips") == Boolean.TRUE);
Variable variable = new Variable(null, mod == null ? 0 : mod, bag.<Token>get("arg_type"), bag.<Token>get("arg_name"), bag.get("arg_elips") == Boolean.TRUE);
bag.remove("arg_name");
bag.remove("arg_mod");
bag.remove("arg_type");
@ -1032,6 +1048,13 @@ public class JavaParser extends Parser<JavaElement> {
Parser<JavaElement> parser = new JavaParser();
JavaFile jFile = new JavaFile();
parser.parse(reader, jFile);
System.out.println();
jFile.find((e) -> {
System.out.println(e);
return false;
});
System.out.println((System.currentTimeMillis()-time)+"ms");

View file

@ -5,9 +5,8 @@ import java.util.List;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.Annotable;
public class Variable extends JavaElement implements Annotable{
public class Variable extends JavaElement{
public static interface VariableContainer{
@ -23,20 +22,20 @@ public class Variable extends JavaElement implements Annotable{
private boolean elips;
private Value value;
public Variable(int mod, Token type, Token name){
public Variable(List<Annotation> annotations, int mod, Token type, Token name){
this.mod = mod;
this.type = type;
this.name = name;
this.annotations = new ArrayList<>();
}
public Variable(int mod, Token type, Token name, boolean elips){
this(mod, type, name);
public Variable(List<Annotation> annotations, int mod, Token type, Token name, boolean elips){
this(annotations, mod, type, name);
this.elips = elips;
}
public Variable(int mod, Token type, Token name, boolean elips, Value value){
this(mod, type, name, elips);
public Variable(List<Annotation> annotations, int mod, Token type, Token name, boolean elips, Value value){
this(annotations, mod, type, name, elips);
this.value = value;
}
@ -45,11 +44,6 @@ public class Variable extends JavaElement implements Annotable{
return "Variable[mod="+mod+", type="+type+", name="+name+", elips="+elips+", value="+value+"]";
}
@Override
public void addAnnotation(Annotation annotation){
this.annotations.add(annotation);
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
for(Annotation annotation : this.annotations){

View file

@ -11,8 +11,7 @@ import be.jeffcheasey88.peeratcode.parser.Parser;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.TokenType;
import be.jeffcheasey88.peeratcode.parser.TokenValidator;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.Annotable;
import be.jeffcheasey88.peeratcode.parser.java.Function.FunctionContainer;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.AnnotableBuffer;
import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree;
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
@ -41,7 +40,7 @@ public class AnnotationTests {
annotation_name.end((parent, bag) -> {
Annotation result = new Annotation(bag.get("name"));
bag.set(result);
if(parent instanceof Annotable) ((Annotable)parent).addAnnotation(result);
if(parent instanceof AnnotableBuffer) ((AnnotableBuffer)parent).addAnnotationBuffer(result);
return null;
});
StateTree<JavaElement> annotation_begin = annotation_name.then((validator) -> validator.validate((token) -> token.getValue().equals("(")));
@ -49,7 +48,7 @@ public class AnnotationTests {
annotation_end.end((parent, bag) -> {
Annotation result = new Annotation(bag.get("name"), bag.get("values"));
bag.set(result);
if(parent instanceof Annotable) ((Annotable)parent).addAnnotation(result);
if(parent instanceof AnnotableBuffer) ((AnnotableBuffer)parent).addAnnotationBuffer(result);
return null;
});
StateTree<JavaElement> annotation_var = annotation_begin.then((validator) -> validator.validate(

View file

@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test;
import be.jeffcheasey88.peeratcode.parser.Parser;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.TokenValidator;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.AnnotableBuffer;
import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree;
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
@ -22,7 +23,7 @@ public class ClassTests{
.then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> global.set("name", local.get())));
clazz_base.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.<JavaElement>end((parent, bag) -> {
Class current = new Class(bag.get("name"));
Class current = new Class((((AnnotableBuffer)parent).getAnnotationBuffer()), bag.get("name"));
if(parent instanceof ClassContainer) ((ClassContainer)parent).addClass(current);
return current;
})
@ -40,7 +41,7 @@ public class ClassTests{
(bag, token) -> bag.set("implement", bag.<Token>get("implement").concat(token)))).then(clazz_implement_name);
clazz_implement_name.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.<JavaElement>end((parent, bag) -> {
Class current = new Class(bag.get("name"), bag.get("extend"), bag.get("implements"));
Class current = new Class((((AnnotableBuffer)parent).getAnnotationBuffer()),bag.get("name"), bag.get("extend"), bag.get("implements"));
if(parent instanceof ClassContainer) ((ClassContainer)parent).addClass(current);
return current;
})
@ -51,7 +52,7 @@ public class ClassTests{
.then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> global.set("extend", local.get())));
clazz_extend.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.<JavaElement>end((parent, bag) -> {
Class current = new Class(bag.get("name"), bag.get("extend"), bag.get("implements"));
Class current = new Class((((AnnotableBuffer)parent).getAnnotationBuffer()), bag.get("name"), bag.get("extend"), bag.get("implements"));
if(parent instanceof ClassContainer) ((ClassContainer)parent).addClass(current);
return current;
})

View file

@ -13,6 +13,7 @@ import be.jeffcheasey88.peeratcode.parser.Parser;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.TokenType;
import be.jeffcheasey88.peeratcode.parser.TokenValidator;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.AnnotableBuffer;
import be.jeffcheasey88.peeratcode.parser.java.Function.FunctionContainer;
import be.jeffcheasey88.peeratcode.parser.state.BuilderStateTree;
import be.jeffcheasey88.peeratcode.parser.state.InitialStateTree;
@ -26,7 +27,7 @@ public class FunctionTests {
BiFunction<JavaElement, Bag, JavaElement> function_builder = (parent, bag) -> {
buildVariable(bag);
Integer mod = bag.get("mod");
Function function = new Function(mod == null ? 0 : mod, bag.get("generic"), bag.get("type"), bag.get("name"), bag.get("param"), bag.get("throws"));
Function function = new Function((((AnnotableBuffer)parent).getAnnotationBuffer()), mod == null ? 0 : mod, bag.get("generic"), bag.get("type"), bag.get("name"), bag.get("param"), bag.get("throws"));
if(parent instanceof FunctionContainer) ((FunctionContainer)parent).addFunction(function);
return function;
};
@ -114,7 +115,7 @@ public class FunctionTests {
Integer mod = bag.get("arg_mod");
Variable variable = new Variable(mod == null ? 0 : mod, bag.<Token>get("arg_type"), bag.<Token>get("arg_name"), bag.get("arg_elips") == Boolean.TRUE);
Variable variable = new Variable(null, mod == null ? 0 : mod, bag.<Token>get("arg_type"), bag.<Token>get("arg_name"), bag.get("arg_elips") == Boolean.TRUE);
bag.remove("arg_name");
bag.remove("arg_mod");
bag.remove("arg_type");

View file

@ -14,6 +14,7 @@ import be.jeffcheasey88.peeratcode.parser.Parser;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.TokenType;
import be.jeffcheasey88.peeratcode.parser.TokenValidator;
import be.jeffcheasey88.peeratcode.parser.java.Annotation.AnnotableBuffer;
import be.jeffcheasey88.peeratcode.parser.java.Variable.VariableContainer;
import be.jeffcheasey88.peeratcode.parser.state.InitialStateTree;
import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree;
@ -27,7 +28,7 @@ public class VariableTests{
Token type = bag.get("type");
Map<Token, Value> map = bag.get("vars");
for(Entry<Token, Value> vars : map.entrySet()){
Variable result = new Variable(mod == null ? 0 : mod, type, vars.getKey(), false, vars.getValue());
Variable result = new Variable((((AnnotableBuffer)parent).getAnnotationBuffer()), mod == null ? 0 : mod, type, vars.getKey(), false, vars.getValue());
bag.set(result);
if(parent instanceof VariableContainer) ((VariableContainer)parent).addVariable(result);
}
@ -79,7 +80,7 @@ public class VariableTests{
TokenValidator.TOKENS = 0;
TokenValidator.MAX_VALIDATE = 0;
JavaElement result = new Class(new Token(0, 0, "Test", TokenType.NAME));
JavaElement result = new Class(null, new Token(0, 0, "Test", TokenType.NAME));
parser.parse(value, result);