From 89fd0553a2b63e949b92ede0c5afc700a8fbb931 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 Date: Wed, 31 May 2023 15:08:45 +0200 Subject: [PATCH] Operation -> Do While --- .../parser/java/operations/DoOperation.java | 48 +++++++++++++++++++ .../java/operations/OperationFactory.java | 3 +- .../peeratcode/parser/java/OperationTest.java | 28 +++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/operations/DoOperation.java diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/DoOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/DoOperation.java new file mode 100644 index 0000000..d92dde5 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/DoOperation.java @@ -0,0 +1,48 @@ +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 DoOperation extends OperationContainer{ + + private static Pattern PATTERN = Pattern.compile("^(\\s*do\\s*).*$"); + private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$"); + + public DoOperation(){} + + @Override + public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ + Matcher matcher = PATTERN.matcher(content); + matcher.matches(); + + int index = matcher.group(1).length(); + content = content.substring(index); + + matcher = PATTERN_CLEANER.matcher(content); + matcher.matches(); + + content = matcher.group(1); + + index += content.length(); + content = local.unzipOne(content, (s,p) -> s); + content = content.substring(1, content.length()-1); + content = local.clean(content); + super.parse(content, global, local); + + return index; + } + + @Override + public void show(int tab) { + String start = ""; + for(int i = 0; i < tab; i++) start+="\t"; + System.out.println(start+"do{"); + for(JavaElement child : getChilds()) child.show(tab+1); + System.out.println(start+"}"); + } + +} + diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java index fb50486..1bd233c 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java @@ -31,6 +31,7 @@ public class OperationFactory{ this.patterns.put(Pattern.compile("^\\s*for\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), ForOperation.class); this.patterns.put(Pattern.compile("^\\s*while\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), WhileOperation.class); this.patterns.put(Pattern.compile("^\\s*else\\s*.*$"), ElseOperation.class); + this.patterns.put(Pattern.compile("^\\s*do\\s*\\^GENERIC_FUNCTION\\d+.*$"), DoOperation.class); this.patterns.put(Pattern.compile("^\\s*[^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+;.*$"), MethodCallOperation.class); @@ -44,7 +45,7 @@ public class OperationFactory{ * assignation OK * exec method OK * for OK - * do while + * do while OK * while OK * if OK * switch case diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java b/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java index c9ac45c..ef3c2d3 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java @@ -10,6 +10,10 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; +import be.jeffcheasey88.peeratcode.parser.java.operations.DoOperation; +import be.jeffcheasey88.peeratcode.parser.java.operations.MethodCallOperation; +import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.WhileOperation; + @TestInstance(Lifecycle.PER_CLASS) class OperationTest{ @@ -62,4 +66,28 @@ class OperationTest{ } } + + @Test + void doWhileOperation(){ + try { + JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; do{ System.out.println(\"Hello\"); }while(i == 0); return i; } }"); + Class clazz = parser.getClazz(); + + clazz.show(0); + + assertEquals(1, clazz.getChilds().size()); + Function function = (Function) clazz.getChilds().get(0); + assertEquals(4, function.getChilds().size()); + + assertEquals(DoOperation.class, function.getChilds().get(1).getClass()); + DoOperation doOp = (DoOperation)function.getChilds().get(1); + assertEquals(1, doOp.getChilds().size()); + assertEquals(MethodCallOperation.class, doOp.getChilds().get(0).getClass()); + + assertEquals(WhileOperation.class, function.getChilds().get(2).getClass()); + } catch (Exception e) { + e.printStackTrace(); + } + + } }