121 lines
3.3 KiB
Java
121 lines
3.3 KiB
Java
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 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;
|
|
|
|
public ConditionalOperation(Pattern pattern){
|
|
this.pattern = pattern;
|
|
}
|
|
|
|
@Override
|
|
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
|
Matcher matcher = this.pattern.matcher(content);
|
|
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("^")){
|
|
matcher = PATTERN.matcher(content);
|
|
matcher.matches();
|
|
|
|
content = matcher.group(1);
|
|
|
|
bodysize = 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);
|
|
}else if(content.startsWith(";")){
|
|
bodysize=1;
|
|
}else{
|
|
bodysize = parseOne(content, global, local);
|
|
}
|
|
|
|
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+"}");
|
|
}
|
|
}
|
|
}
|