diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java b/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java new file mode 100644 index 0000000..7ab225b --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java @@ -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 buffer, int tab) throws Exception { + super.build(buffer, tab); + buffer.append((s) -> s+="@"+type+(param == null ? "":param)); + buffer.add(""); + } + + @Override + public E find(Function search, Function, Boolean> deep, List trace) { + return null; + } + + @Override + public E find(BiFunction, Boolean> search, List trace) { + return null; + } + + + +} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index 1907f82..b2ca569 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -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]); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java index 223684a..fa99106 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -14,6 +14,7 @@ public class Function extends OperationContainer{ private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$"); + private List 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(">(?[^>\\d,;(])"); private static Pattern UNZIP_ARRAY = Pattern.compile("](?[^\\[\\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 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 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(""); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index 3d12502..3058fae 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -72,6 +72,7 @@ public class JavaParser{ private List imports; private Class clazz; + //later, maybe put string in the element private CleanerPool cleaner; public JavaParser(){} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java index e62b091..45e4a24 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java @@ -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); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java index 6354318..a7ec230 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java @@ -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){ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java index 6b20494..c8198a6 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java @@ -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_",'<','>'));