Operation -> Do While

This commit is contained in:
jeffcheasey88 2023-05-31 15:08:45 +02:00
parent ab03e73f58
commit 89fd0553a2
3 changed files with 78 additions and 1 deletions

View file

@ -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+"}");
}
}

View file

@ -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

View file

@ -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();
}
}
}