Operation -> OperationContainer (Recursive {} body system)
This commit is contained in:
parent
56ce2b7235
commit
a231434ecd
4 changed files with 128 additions and 52 deletions
|
@ -8,12 +8,9 @@ import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
||||||
import be.jeffcheasey88.peeratcode.parser.java.Variable.MultipleDeclaratedVariable;
|
import be.jeffcheasey88.peeratcode.parser.java.operations.OperationContainer;
|
||||||
import be.jeffcheasey88.peeratcode.parser.java.operations.OperationFactory;
|
|
||||||
|
|
||||||
public class Function extends JavaElement{
|
public class Function extends OperationContainer{
|
||||||
|
|
||||||
private static OperationFactory FACTORY = new OperationFactory();
|
|
||||||
|
|
||||||
private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$");
|
private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$");
|
||||||
|
|
||||||
|
@ -25,11 +22,8 @@ public class Function extends JavaElement{
|
||||||
|
|
||||||
private boolean constructor;
|
private boolean constructor;
|
||||||
|
|
||||||
private List<JavaElement> childs;
|
|
||||||
|
|
||||||
public Function(){
|
public Function(){
|
||||||
this.parameters = new ArrayList<>();
|
this.parameters = new ArrayList<>();
|
||||||
this.childs = new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||||
|
@ -44,7 +38,7 @@ public class Function extends JavaElement{
|
||||||
String zip = generic.clean("{"+matcher.group(5));
|
String zip = generic.clean("{"+matcher.group(5));
|
||||||
String body = generic.unzipOne(zip, (s,p) -> s);
|
String body = generic.unzipOne(zip, (s,p) -> s);
|
||||||
String unzip = body.substring(1, body.indexOf('}'));
|
String unzip = body.substring(1, body.indexOf('}'));
|
||||||
body(unzip, generic);
|
parse(unzip, cleaner, generic);
|
||||||
|
|
||||||
return matcher.group(1).length()+generic.unzip(unzip, ((s,p) -> s)).length()+1;
|
return matcher.group(1).length()+generic.unzip(unzip, ((s,p) -> s)).length()+1;
|
||||||
}
|
}
|
||||||
|
@ -95,43 +89,6 @@ public class Function extends JavaElement{
|
||||||
}while(quote);
|
}while(quote);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void body(String content, CleanerPool cleaner) throws Exception{
|
|
||||||
content = cleaner.unzip(content, (s,p) -> s);
|
|
||||||
while(!(content.replaceAll("\\s+", "").isEmpty())){
|
|
||||||
System.out.println("BODY "+content);
|
|
||||||
|
|
||||||
JavaElement operation = FACTORY.buildOperation(content);
|
|
||||||
|
|
||||||
System.out.println("got "+operation.getClass().getSimpleName());
|
|
||||||
int index = operation.parse(content, cleaner);
|
|
||||||
content = content.substring(index);
|
|
||||||
if(operation instanceof Variable){
|
|
||||||
if(content.startsWith(",")){
|
|
||||||
Variable variable = (Variable)operation;
|
|
||||||
MultipleDeclaratedVariable multiple = new MultipleDeclaratedVariable(variable.getModifier(), variable.getType());
|
|
||||||
multiple.addVariable(variable.getName(), variable.getValue());
|
|
||||||
|
|
||||||
operation = multiple;
|
|
||||||
|
|
||||||
boolean quote;
|
|
||||||
do{
|
|
||||||
variable = new Variable(variable.getModifier(), variable.getType());
|
|
||||||
index = variable.parse(content, cleaner);
|
|
||||||
multiple.addVariable(variable.getName(), variable.getValue());
|
|
||||||
content = content.substring(index);
|
|
||||||
quote = content.startsWith(",");
|
|
||||||
content = content.substring(1);
|
|
||||||
}while(quote);
|
|
||||||
}else content = content.substring(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.childs.add(operation);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void show(int tab){
|
public void show(int tab){
|
||||||
String start = "";
|
String start = "";
|
||||||
for(int i = 0; i < tab; i++) start+="\t";
|
for(int i = 0; i < tab; i++) start+="\t";
|
||||||
|
@ -139,7 +96,7 @@ public class Function extends JavaElement{
|
||||||
for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+"";
|
for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+"";
|
||||||
if(!param.isEmpty()) param = param.substring(1);
|
if(!param.isEmpty()) param = param.substring(1);
|
||||||
System.out.println(start+Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"("+param+") "+exceptions+"{");
|
System.out.println(start+Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"("+param+") "+exceptions+"{");
|
||||||
for(JavaElement child : this.childs) child.show(tab+1);
|
for(JavaElement child : getChilds()) child.show(tab+1);
|
||||||
System.out.println(start+"}");
|
System.out.println(start+"}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
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
|
||||||
|
private String body;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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.unzipOne(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 = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return index+bodysize;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void show(int tab) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.parser.java.operations;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
|
||||||
|
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
||||||
|
import be.jeffcheasey88.peeratcode.parser.java.Variable;
|
||||||
|
import be.jeffcheasey88.peeratcode.parser.java.Variable.MultipleDeclaratedVariable;
|
||||||
|
|
||||||
|
public abstract class OperationContainer extends JavaElement{
|
||||||
|
|
||||||
|
private static OperationFactory FACTORY = OperationFactory.getFactory();
|
||||||
|
|
||||||
|
private List<JavaElement> childs;
|
||||||
|
|
||||||
|
public OperationContainer(){
|
||||||
|
this.childs = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||||
|
content = local.unzip(content, (s,p) -> s);
|
||||||
|
System.out.println("CONTAINER "+content);
|
||||||
|
while(!(content.replaceAll("\\s+", "").isEmpty())){
|
||||||
|
System.out.println("BODY "+content);
|
||||||
|
|
||||||
|
JavaElement operation = FACTORY.buildOperation(content);
|
||||||
|
|
||||||
|
System.out.println("got "+operation.getClass().getSimpleName());
|
||||||
|
int index = operation.parse(content, local);
|
||||||
|
content = content.substring(index);
|
||||||
|
if(operation instanceof Variable){
|
||||||
|
if(content.startsWith(",")){
|
||||||
|
Variable variable = (Variable)operation;
|
||||||
|
MultipleDeclaratedVariable multiple = new MultipleDeclaratedVariable(variable.getModifier(), variable.getType());
|
||||||
|
multiple.addVariable(variable.getName(), variable.getValue());
|
||||||
|
|
||||||
|
operation = multiple;
|
||||||
|
|
||||||
|
boolean quote;
|
||||||
|
do{
|
||||||
|
variable = new Variable(variable.getModifier(), variable.getType());
|
||||||
|
index = variable.parse(content, local);
|
||||||
|
multiple.addVariable(variable.getName(), variable.getValue());
|
||||||
|
content = content.substring(index);
|
||||||
|
quote = content.startsWith(",");
|
||||||
|
content = content.substring(1);
|
||||||
|
}while(quote);
|
||||||
|
}else content = content.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.childs.add(operation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<JavaElement> getChilds(){
|
||||||
|
return this.childs;
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,15 +10,22 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
||||||
import be.jeffcheasey88.peeratcode.parser.java.Variable;
|
import be.jeffcheasey88.peeratcode.parser.java.Variable;
|
||||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
||||||
|
|
||||||
//Singleton
|
|
||||||
public class OperationFactory{
|
public class OperationFactory{
|
||||||
|
|
||||||
|
private static final OperationFactory SINGLETON = new OperationFactory();
|
||||||
|
|
||||||
|
public static OperationFactory getFactory(){
|
||||||
|
return SINGLETON;
|
||||||
|
}
|
||||||
|
|
||||||
private Map<Pattern, Class<? extends JavaElement>> patterns;
|
private Map<Pattern, Class<? extends JavaElement>> patterns;
|
||||||
|
|
||||||
public OperationFactory(){
|
private OperationFactory(){
|
||||||
this.patterns = new LinkedHashMap<>();
|
this.patterns = new LinkedHashMap<>();
|
||||||
|
|
||||||
this.patterns.put(Pattern.compile("^\\s*return\\s*[^;]*;.*$"), ReturnOperation.class);
|
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*([^;=]+;).*$"), MethodCallOperation.class);
|
this.patterns.put(Pattern.compile("^\\s*([^;=]+;).*$"), MethodCallOperation.class);
|
||||||
|
|
||||||
this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s+[^\\s]+\\s*=\\s*[^;]+;.*$"), Variable.class);
|
this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s+[^\\s]+\\s*=\\s*[^;]+;.*$"), Variable.class);
|
||||||
|
@ -52,10 +59,10 @@ public class OperationFactory{
|
||||||
|
|
||||||
public JavaElement buildOperation(String content) throws Exception{
|
public JavaElement buildOperation(String content) throws Exception{
|
||||||
CleanerPool generic = new CleanerPool(
|
CleanerPool generic = new CleanerPool(
|
||||||
new Cleaner("GENERIC_TYPE_",'<','>'),
|
|
||||||
new Cleaner("GENERIC_ARRAY",'[',']'),
|
|
||||||
new Cleaner("GENERIC_FUNCTION", '{','}'),
|
new Cleaner("GENERIC_FUNCTION", '{','}'),
|
||||||
new Cleaner("GENERIC_PARENTHESIS",'(',')'));
|
new Cleaner("GENERIC_PARENTHESIS",'(',')'),
|
||||||
|
new Cleaner("GENERIC_ARRAY",'[',']'),
|
||||||
|
new Cleaner("GENERIC_TYPE_",'<','>'));
|
||||||
content = generic.clean(content);
|
content = generic.clean(content);
|
||||||
generiqueTypes(content, generic);
|
generiqueTypes(content, generic);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue