106 lines
2.9 KiB
Java
106 lines
2.9 KiB
Java
package be.jeffcheasey88.peeratcode.parser.java;
|
|
|
|
import java.lang.reflect.Modifier;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import be.jeffcheasey88.peeratcode.parser.java.Variable.MultipleDeclaratedVariable;
|
|
|
|
public class Class{
|
|
|
|
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
|
|
|
|
private int modifier;
|
|
private String name;
|
|
|
|
private List<Variable> vars;
|
|
private List<Function> functions;
|
|
|
|
public Class(){}
|
|
|
|
public int parse(String content, CleanerPool cleaner) throws Exception{
|
|
content = cleaner.clean(content);
|
|
|
|
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.vars = new ArrayList<>();
|
|
this.functions = 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, cleaner);
|
|
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, cleaner);
|
|
content = content.substring(index);
|
|
quote = content.startsWith(",");
|
|
if(quote) content = content.substring(1);
|
|
|
|
multiple.addVariable(variable.getName(), variable.getValue());
|
|
}
|
|
|
|
this.vars.add(multiple);
|
|
}else{
|
|
this.vars.add(variable);
|
|
}
|
|
|
|
content = content.substring(1);
|
|
}else{
|
|
// System.out.println("Function "+content);
|
|
Function func = new Function();
|
|
int index = func.parse(content, cleaner);
|
|
this.functions.add(func);
|
|
content = content.substring(index);
|
|
// System.out.println("End "+content);
|
|
}
|
|
}
|
|
|
|
return cleaner.unzip(matcher.group(1), ((s,p) -> s)).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<Variable> getVariables(){
|
|
return this.vars;
|
|
}
|
|
|
|
public void show(){
|
|
System.out.println(Modifier.toString(modifier)+" "+this.name+"{");
|
|
for(Variable v : this.vars) v.show(1);
|
|
System.out.println();
|
|
for(Function f : this.functions) f.show(1);
|
|
System.out.println("}");
|
|
}
|
|
}
|