142 lines
4 KiB
Java
142 lines
4 KiB
Java
package be.jeffcheasey88.peeratcode.parser.java;
|
|
|
|
import java.io.BufferedWriter;
|
|
import java.lang.reflect.Modifier;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.function.BiFunction;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import be.jeffcheasey88.peeratcode.parser.java.Variable.MultipleDeclaratedVariable;
|
|
|
|
public class Class extends JavaElement{
|
|
|
|
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
|
|
|
|
private int modifier;
|
|
private String name;
|
|
|
|
private List<JavaElement> childs;
|
|
|
|
public Class(){}
|
|
|
|
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
|
Matcher matcher = PATTERN.matcher(content);
|
|
matcher.matches();
|
|
|
|
String[] split = matcher.group(2).split("\\s+");
|
|
for(int i = 0; i < split.length-1; i++){
|
|
this.modifier+=JavaParser.getModifier(split[i]);
|
|
}
|
|
this.name = split[split.length-1];
|
|
|
|
this.childs = new ArrayList<>();
|
|
|
|
content = matcher.group(3);
|
|
Pattern empty = Pattern.compile("^\\s*$");
|
|
while(!(empty.matcher(content).matches())){
|
|
int quotes = indexOf(content,";");
|
|
int braces = indexOf(content,"\\{");
|
|
int equals = indexOf(content,"=");
|
|
if((quotes < braces && quotes < equals) || (equals < braces)){
|
|
Variable variable = new Variable();
|
|
int index = variable.parse(content, global, local);
|
|
content = content.substring(index);
|
|
boolean quote = content.startsWith(",");
|
|
if(quote){
|
|
content = content.substring(1);
|
|
|
|
MultipleDeclaratedVariable multiple = new MultipleDeclaratedVariable(variable.getModifier(), variable.getType());
|
|
multiple.addVariable(variable.getName(), variable.getValue());
|
|
while(quote){
|
|
variable = new Variable(variable.getModifier(), variable.getType());
|
|
index = variable.parse(content, global, local);
|
|
content = content.substring(index);
|
|
quote = content.startsWith(",");
|
|
if(quote) content = content.substring(1);
|
|
|
|
multiple.addVariable(variable.getName(), variable.getValue());
|
|
}
|
|
|
|
this.childs.add(multiple);
|
|
}else{
|
|
this.childs.add(variable);
|
|
}
|
|
|
|
content = content.substring(1);
|
|
}else{
|
|
// System.out.println("Function "+content);
|
|
Function func = new Function();
|
|
int index = func.parse(content, global, local);
|
|
this.childs.add(func);
|
|
content = content.substring(index);
|
|
// System.out.println("End "+content);
|
|
}
|
|
}
|
|
|
|
// return cleaner.unzip(matcher.group(1), ((s,p) -> s)).length();
|
|
return matcher.group(1).length();
|
|
}
|
|
|
|
private int indexOf(String value, String target){
|
|
return value.split(target)[0].length();
|
|
}
|
|
|
|
public int getModifier(){
|
|
return this.modifier;
|
|
}
|
|
|
|
public String getName(){
|
|
return this.name;
|
|
}
|
|
|
|
public List<JavaElement> getChilds(){
|
|
return this.childs;
|
|
}
|
|
|
|
// public List<Variable> getVariables(){
|
|
// return this.vars;
|
|
// }
|
|
|
|
@Override
|
|
public void build(BufferedWriter writer, int tab) throws Exception{
|
|
super.build(writer, tab);
|
|
writer.write(Modifier.toString(modifier)+" "+this.name+"{\n");
|
|
|
|
for(JavaElement child : this.childs){
|
|
child.build(writer, tab+1);
|
|
writer.write("\n");
|
|
}
|
|
|
|
super.build(writer, tab);
|
|
writer.write("}\n");
|
|
}
|
|
|
|
@Override
|
|
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
|
|
if(search.apply(this)) return (E)this;
|
|
trace.add(this);
|
|
int index = trace.size()-1;
|
|
if(!deep.apply(trace)) return null;
|
|
for(JavaElement element : this.childs){
|
|
E result = element.find(search, deep, trace);
|
|
if(result != null) return result;
|
|
}
|
|
trace.remove(index);
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace){
|
|
trace.add(this);
|
|
int index = trace.size()-1;
|
|
if(search.apply(this, trace)) return (E)this;
|
|
for(JavaElement element : this.childs){
|
|
E result = element.find(search, trace);
|
|
if(result != null) return result;
|
|
}
|
|
trace.remove(index);
|
|
return null;
|
|
}
|
|
}
|