Annotation -> Function

This commit is contained in:
jeffcheasey88 2023-06-10 22:42:16 +02:00
parent 31fbc33fa6
commit 01107c788f
7 changed files with 70 additions and 19 deletions

View file

@ -0,0 +1,48 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Annotation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*@\\s*([^\\s\\^]*)\\s*(\\^GENERIC_PARENTHESIS\\d+)?)");
private String type;
private String param;
public Annotation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception {
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
this.type = matcher.group(2);
this.param = matcher.group(3);
return matcher.group(1).length();
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception {
super.build(buffer, tab);
buffer.append((s) -> s+="@"+type+(param == null ? "":param));
buffer.add("");
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> search, Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace) {
return null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace) {
return null;
}
}

View file

@ -24,14 +24,6 @@ public class Class extends JavaElement{
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
try {
}catch(Exception e) {
}finally {
System.out.println("Hello");
}
String[] split = matcher.group(2).split("\\s+");
for(int i = 0; i < split.length-1; i++){
this.modifier+=JavaParser.getModifier(split[i]);

View file

@ -14,6 +14,7 @@ public class Function extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$");
private List<Annotation> annotations;
private int modifier;
private String returnType;
private String name;
@ -23,6 +24,7 @@ public class Function extends OperationContainer{
private boolean constructor;
public Function(){
this.annotations = new ArrayList<>();
this.parameters = new ArrayList<>();
}
@ -51,7 +53,7 @@ public class Function extends OperationContainer{
private static Pattern UNZIP_MAJ = Pattern.compile(">(?<e>[^>\\d,;(])");
private static Pattern UNZIP_ARRAY = Pattern.compile("](?<e>[^\\[\\d,;])");
private void attribute(String content){
private void attribute(String content) throws Exception{
CleanerPool generic = new CleanerPool(
new Cleaner("GENERIC_TYPE_",'<','>'),
new Cleaner("GENERIC_ARRAY",'[',']'));
@ -67,8 +69,15 @@ public class Function extends OperationContainer{
Iterator<String> values = new ArrayIterator<>(unzip.split("\\s+"));
String value = null;
int modifier;
while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0){
this.modifier+=modifier;
while(values.hasNext() && ((value = values.next()).charAt(0) == '@')){
Annotation annotation = new Annotation();
annotation.parse(value, null, generic);
this.annotations.add(annotation);
}
if((modifier = JavaParser.getModifier(value)) > 0){
do{
this.modifier+=modifier;
}while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0);
}
if(this.returnType == null){
this.returnType = value;
@ -95,15 +104,16 @@ public class Function extends OperationContainer{
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
for(Annotation annotation : this.annotations) annotation.build(buffer, tab);
String param = "";
for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+"";
if(!param.isEmpty()) param = param.substring(1);
super.build(buffer, tab);
final String paramMod = param;
boolean empty = getChilds().size() == 0;
super.build(buffer, tab);
buffer.append((s) -> s+=Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"("+paramMod+") "+exceptions+"{"+((empty ? "}":"")));
buffer.add("");

View file

@ -72,6 +72,7 @@ public class JavaParser{
private List<Import> imports;
private Class clazz;
//later, maybe put string in the element
private CleanerPool cleaner;
public JavaParser(){}

View file

@ -25,7 +25,7 @@ public class ConditionalOperation extends OperationContainer{
matcher.lookingAt();
this.condition = local.unzipOne(matcher.group(2), (s,p) -> s);
System.out.println("CONDITION "+condition);
// System.out.println("CONDITION "+condition);
int index = matcher.group(1).length();
content = content.substring(index);

View file

@ -21,22 +21,22 @@ public abstract class OperationContainer extends JavaElement{
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
System.out.println("OperationContainer.parse -> "+content);
// System.out.println("OperationContainer.parse -> "+content);
while(!(content.replaceAll("\\s+", "").isEmpty())) content = internalParse(content, global, local);
return 0;
}
public int parseOne(String content, CleanerPool global, CleanerPool local) throws Exception{
System.out.println("OperationContainer.parseOne -> "+content);
// System.out.println("OperationContainer.parseOne -> "+content);
String modify = internalParse(content, global, local);
return content.length()-modify.length();
}
private String internalParse(String content, CleanerPool global, CleanerPool local) throws Exception{
System.out.println("OperationContainer.internalParse -> "+content);
// System.out.println("OperationContainer.internalParse -> "+content);
JavaElement operation = FACTORY.buildOperation(content);
System.out.println(operation.getClass().getSimpleName()+" operation = FACTORY.buildOperation();");
// System.out.println(operation.getClass().getSimpleName()+" operation = FACTORY.buildOperation();");
int index = operation.parse(content, global, local);
content = content.substring(index);
if(operation instanceof Variable){

View file

@ -82,7 +82,7 @@ public class OperationFactory{
*/
public JavaElement buildOperation(String content) throws Exception{
System.out.println("Factory.buildOperation -> "+content);
// System.out.println("Factory.buildOperation -> "+content);
CleanerPool generic = new CleanerPool(
new Cleaner("GENERIC_ARRAY",'[',']'),
new Cleaner("GENERIC_TYPE_",'<','>'));