Operation -> For and While Statement (simple case)

This commit is contained in:
jeffcheasey88 2023-05-30 18:46:49 +02:00
parent 0c98fe6e2e
commit b800b88fb4
3 changed files with 116 additions and 61 deletions

View file

@ -0,0 +1,107 @@
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;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class ConditionalOperation extends OperationContainer{
private Pattern pattern;
private String condition;
public ConditionalOperation(Pattern pattern){
this.pattern = pattern;
}
@Override
public int parse(String content, CleanerPool cleaner) throws Exception{
CleanerPool generic = new CleanerPool(
new Cleaner("GENERIC_PARENTHESIS",'(',')'),
new Cleaner("GENERIC_FUNCTION", '{','}'));
content = generic.clean(content);
Matcher matcher = this.pattern.matcher(content);
matcher.matches();
this.condition = generic.unzip(matcher.group(2), (s,p) -> s);
int index = generic.unzip(matcher.group(1), (s,p) -> s).length();
content = generic.unzip(content, (s,p) -> s).substring(index);
int bodysize;
if(content.startsWith("{")){
bodysize = content.indexOf('}')+1;
content = content.substring(1, bodysize-1);
parse(content, cleaner, generic);
}else{
bodysize = parseOne(content, cleaner, generic);
}
return index+bodysize;
}
@Override
public void show(int tab) {
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"Condition??"+condition+"{");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
public static class IfOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*if\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$");
public IfOperation(){
super(PATTERN);
}
@Override
public void show(int tab) {
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"if"+super.condition+"{");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
}
public static class ForOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*for\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$");
public ForOperation(){
super(PATTERN);
}
@Override
public void show(int tab) {
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"for"+super.condition+"{");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
}
public static class WhileOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*while\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$");
public WhileOperation(){
super(PATTERN);
}
@Override
public void show(int tab) {
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"while"+super.condition+"{");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
}
}

View file

@ -1,57 +0,0 @@
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;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
//ContainerOperation
public class IfOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\s*if\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$");
private String condition; //replace by Operation
public IfOperation(){}
@Override
public int parse(String content, CleanerPool cleaner) throws Exception{
System.out.println("IF STATEMENT");
CleanerPool generic = new CleanerPool(
new Cleaner("GENERIC_PARENTHESIS",'(',')'),
new Cleaner("GENERIC_FUNCTION", '{','}'));
content = generic.clean(content);
System.out.println("after clean "+content);
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
this.condition = generic.unzip(matcher.group(2), (s,p) -> s);
int index = generic.unzip(matcher.group(1), (s,p) -> s).length();
content = generic.unzip(content, (s,p) -> s).substring(index);
System.out.println("INSIDE "+content);
int bodysize;
if(content.startsWith("{")){
bodysize = content.indexOf('}')+1;
content = content.substring(1, bodysize-1);
parse(content, cleaner, generic);
}else{
bodysize = parseOne(content, cleaner, generic);
}
return index+bodysize;
}
@Override
public void show(int tab) {
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"if("+condition+"){");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
}

View file

@ -6,9 +6,12 @@ import java.util.Map.Entry;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Variable;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.ForOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.IfOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.WhileOperation;
public class OperationFactory{
@ -25,6 +28,8 @@ public class OperationFactory{
this.patterns.put(Pattern.compile("^\\s*return\\s*[^;]*;.*$"), ReturnOperation.class);
this.patterns.put(Pattern.compile("^\\s*if\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), IfOperation.class);
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*([^;=]+;).*$"), MethodCallOperation.class);
@ -40,14 +45,14 @@ public class OperationFactory{
* for
* do while
* while
* if
* if OK
* switch case
* return OK (without just return)
* return OK
* continue
* break
* try catch finally
* throw
* else
* else OK
* synchronized
* instance of
*