package be.jeffcheasey88.peeratcode.parser.java; 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 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 getChilds(){ return this.childs; } // public List getVariables(){ // return this.vars; // } @Override public void build(ArrayBuffer buffer, int tab) throws Exception{ super.build(buffer, tab); buffer.append((s) -> s+=Modifier.toString(modifier)+" "+this.name+"{"); buffer.add(""); for(JavaElement child : this.childs){ child.build(buffer, tab+1); buffer.add(""); } super.build(buffer, tab); buffer.append((s) -> s+="}"); buffer.add(""); } @Override public E find(java.util.function.Function search, java.util.function.Function, Boolean> deep, List 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 find(BiFunction, Boolean> search, List 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; } }