diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java index 975c119..f17165a 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -23,6 +23,8 @@ public class Function extends JavaElement{ private List parameters; private String exceptions; + private boolean constructor; + private List childs; public Function(){ @@ -73,6 +75,7 @@ public class Function extends JavaElement{ if(this.returnType == null){ this.returnType = value; if(values.hasNext()) value = values.next(); + else constructor = true; } if(this.name == null){ this.name = value; @@ -95,6 +98,7 @@ public class Function extends JavaElement{ private void body(String content, CleanerPool cleaner) throws Exception{ + content = cleaner.unzip(content, (s,p) -> s); while(!(content.replaceAll("\\s+", "").isEmpty())){ System.out.println("BODY "+content); @@ -134,7 +138,7 @@ public class Function extends JavaElement{ String param = ""; for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+""; if(!param.isEmpty()) param = param.substring(1); - System.out.println(start+Modifier.toString(modifier)+" "+returnType+" "+name+"("+param+") "+exceptions+"{"); + System.out.println(start+Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"("+param+") "+exceptions+"{"); for(JavaElement child : this.childs) child.show(tab+1); System.out.println(start+"}"); } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index 089550d..224dd4f 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -14,7 +14,7 @@ import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner; public class JavaParser{ public static void main(String[] args) throws Exception { - File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Import.java"); + File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Variable.java"); BufferedReader reader = new BufferedReader(new FileReader(file)); JavaParser parser = new JavaParser(reader); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java index 67f9334..27628d2 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -16,7 +16,7 @@ public class Variable extends JavaElement{ private int modifier; private String name; private String type; - private Variable value; //Change into operation + private Variable value; //Change into operation or JavaElement public Variable(){} @@ -126,7 +126,7 @@ public class Variable extends JavaElement{ public void show(int tab){ String start = ""; for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+Modifier.toString(modifier)+" "+type+" "+name+(value == null ? ";":" = "+value+";")); + System.out.println(start+Modifier.toString(modifier)+(modifier > 0 ? " ":"")+type+" "+name+(value == null ? ";":" = "+value+";")); } public static class Value extends Variable{ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java new file mode 100644 index 0000000..39579b9 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java @@ -0,0 +1,37 @@ +package be.jeffcheasey88.peeratcode.parser.java.operations; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import be.jeffcheasey88.peeratcode.parser.java.CleanerPool; +import be.jeffcheasey88.peeratcode.parser.java.JavaElement; + +public class AssigmentOperation extends JavaElement{ + + private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\s]+)\\s*=\\s*([^;]+);).*$"); + + private String variable; + private String value; + + public AssigmentOperation(){} + + @Override + public int parse(String content, CleanerPool cleaner) throws Exception{ + Matcher matcher = PATTERN.matcher(content); + matcher.matches(); + + this.variable = matcher.group(2); + this.value = matcher.group(3); + + return matcher.group(1).length(); + } + + @Override + public void show(int tab){ + String start = ""; + for(int i = 0; i < tab; i++) start+="\t"; + System.out.println(start+variable+" = "+value+";"); + } + + +} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java index 80c496e..4ef3bd0 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java @@ -10,6 +10,7 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement; import be.jeffcheasey88.peeratcode.parser.java.Variable; import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner; +//Singleton public class OperationFactory{ private Map> patterns; @@ -17,23 +18,23 @@ public class OperationFactory{ public OperationFactory(){ this.patterns = new LinkedHashMap<>(); - this.patterns.put(Pattern.compile("^\\s*return\\s+[^;]*;.*$"), ReturnOperation.class); - this.patterns.put(Pattern.compile("^\\s*([^;=]*;).*$"), MethodCallOperation.class); + this.patterns.put(Pattern.compile("^\\s*return\\s*[^;]*;.*$"), ReturnOperation.class); + this.patterns.put(Pattern.compile("^\\s*([^;=]+;).*$"), MethodCallOperation.class); - //if not types ?? - this.patterns.put(Pattern.compile("^\\s*[^\\s]*\\s+[^\\s]*\\s+=\\s+[^\\s]*;.*$"), Variable.class); + this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s+[^\\s]+\\s*=\\s*[^;]+;.*$"), Variable.class); + this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s*=\\s*[^;]+;.*$"), AssigmentOperation.class); } /* - * variable - * assignation - * exec method + * variable OK + * assignation OK + * exec method OK * for * do while * while * if * switch case - * return + * return OK (without just return) * continue * break * try catch finally @@ -46,6 +47,7 @@ public class OperationFactory{ * * assert ?? * Goto ?? + * Const ?? */ public JavaElement buildOperation(String content) throws Exception{ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java index 236d583..8ea6b07 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java @@ -8,7 +8,7 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement; public class ReturnOperation extends JavaElement{ - private static Pattern PATTERN = Pattern.compile("^(\\s*return\\s+([^;]*);)\\s*$"); + private static Pattern PATTERN = Pattern.compile("^(\\s*return\\s*([^;]*);)\\s*$"); private String value;