diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index 9e9a653..a191983 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -89,6 +89,10 @@ public class Class extends JavaElement{ return this.name; } + public List getChilds(){ + return this.childs; + } + // public List getVariables(){ // return this.vars; // } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java index da928d9..942e7d0 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -41,13 +41,8 @@ public class Function extends OperationContainer{ String body = local.unzipOne(zip, (s,p) -> s); String unzip = body.substring(1, body.indexOf('}')); - System.out.println("----------before----------"); - show(0); - super.parse(local.clean(unzip), global, local); - System.out.println("----------After----------"); - show(0); return matcher.group(1).length()+local.unzip(unzip, ((s,p) -> s)).length()+1; } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index 7ac6688..9288f45 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\\ExampleClass.java"); + File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Class.java"); BufferedReader reader = new BufferedReader(new FileReader(file)); JavaParser parser = new JavaParser(reader); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java index cd9f4f0..4c658c7 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java @@ -9,6 +9,7 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement; public class ConditionalOperation extends OperationContainer{ private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$"); + private static Pattern PATTERN_SPACE = Pattern.compile("^(\\s+).*$"); private Pattern pattern; private String condition; @@ -23,9 +24,16 @@ public class ConditionalOperation extends OperationContainer{ matcher.matches(); this.condition = local.unzipOne(matcher.group(2), (s,p) -> s); + System.out.println("CONDITION "+condition); int index = matcher.group(1).length(); content = content.substring(index); + + matcher = PATTERN_SPACE.matcher(content); + if(matcher.matches()){ + index+=matcher.group(1).length(); + content = content.substring(matcher.group(1).length()); + } int bodysize; if(content.startsWith("^")){ @@ -39,6 +47,8 @@ public class ConditionalOperation extends OperationContainer{ content = content.substring(1, content.length()-1); content = local.clean(content); super.parse(content, global, local); + }else if(content.startsWith(";")){ + bodysize=1; }else{ bodysize = parseOne(content, global, local); } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java index 43008f6..6fb5e26 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java @@ -16,7 +16,6 @@ public class MethodCallOperation extends JavaElement{ @Override public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ - System.out.println("MethodCallOperation.parse -> "+content); content = local.clean(content); Matcher matcher = PATTERN.matcher(content); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java index 673a3cc..ff2cd7a 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java @@ -40,8 +40,6 @@ public abstract class OperationContainer extends JavaElement{ System.out.println(operation.getClass().getSimpleName()+" operation = FACTORY.buildOperation();"); int index = operation.parse(content, global, local); - operation.show(0); - System.out.println(); content = content.substring(index); if(operation instanceof Variable){ if(content.startsWith(",")){ diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java b/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java new file mode 100644 index 0000000..c9ac45c --- /dev/null +++ b/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java @@ -0,0 +1,65 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.Reader; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + +@TestInstance(Lifecycle.PER_CLASS) +class OperationTest{ + + JavaParser parse(String code) throws Exception{ + BufferedReader reader = new BufferedReader(new Reader(){public int read(char[] cbuf, int off, int len) throws IOException{return 0;}public void close() throws IOException {}}) { + private boolean read = false; + @Override + public String readLine() throws IOException{ + if(read) return null; + read = true; + return code; + } + }; + + JavaParser parser = new JavaParser(reader); + parser.parse(); + return parser; + } + + @Test + void ifOperation(){ + try { + JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0) { return i; } return i; } }"); + Class clazz = parser.getClazz(); + + clazz.show(0); + + assertEquals(1, clazz.getChilds().size()); + Function function = (Function) clazz.getChilds().get(0); + assertEquals(3, function.getChilds().size()); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + @Test + void ifWithNoneOperation(){ + try { + JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0); return i; } }"); + Class clazz = parser.getClazz(); + + clazz.show(0); + + assertEquals(1, clazz.getChilds().size()); + Function function = (Function) clazz.getChilds().get(0); + assertEquals(3, function.getChilds().size()); + } catch (Exception e) { + e.printStackTrace(); + } + + } +}