Operation -> Fix CleanerPool using
This commit is contained in:
parent
ad430cb659
commit
c7b9454ede
13 changed files with 126 additions and 86 deletions
|
@ -19,7 +19,7 @@ public class Class extends JavaElement{
|
|||
|
||||
public Class(){}
|
||||
|
||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
|
@ -39,7 +39,7 @@ public class Class extends JavaElement{
|
|||
int equals = indexOf(content,"=");
|
||||
if((quotes < braces && quotes < equals) || (equals < braces)){
|
||||
Variable variable = new Variable();
|
||||
int index = variable.parse(content, cleaner);
|
||||
int index = variable.parse(content, global, local);
|
||||
content = content.substring(index);
|
||||
boolean quote = content.startsWith(",");
|
||||
if(quote){
|
||||
|
@ -49,7 +49,7 @@ public class Class extends JavaElement{
|
|||
multiple.addVariable(variable.getName(), variable.getValue());
|
||||
while(quote){
|
||||
variable = new Variable(variable.getModifier(), variable.getType());
|
||||
index = variable.parse(content, cleaner);
|
||||
index = variable.parse(content, global, local);
|
||||
content = content.substring(index);
|
||||
quote = content.startsWith(",");
|
||||
if(quote) content = content.substring(1);
|
||||
|
@ -66,7 +66,7 @@ public class Class extends JavaElement{
|
|||
}else{
|
||||
// System.out.println("Function "+content);
|
||||
Function func = new Function();
|
||||
int index = func.parse(content, cleaner);
|
||||
int index = func.parse(content, global, local);
|
||||
this.childs.add(func);
|
||||
content = content.substring(index);
|
||||
// System.out.println("End "+content);
|
||||
|
|
|
@ -15,7 +15,26 @@ public class CleanerPool{
|
|||
private List<Cleaner> cleaners;
|
||||
|
||||
public CleanerPool(Cleaner... cleaners){
|
||||
this.cleaners = Arrays.asList(cleaners);
|
||||
this.cleaners = new ArrayList<>(Arrays.asList(cleaners));
|
||||
}
|
||||
|
||||
public CleanerPool group(CleanerPool... pools){
|
||||
CleanerPool pool = new CleanerPool();
|
||||
pool.cleaners.addAll(cleaners);
|
||||
for(CleanerPool other : pools){
|
||||
for(Cleaner cleaner : other.cleaners){
|
||||
boolean contains = false;
|
||||
for(Cleaner include : pool.cleaners){
|
||||
if(include.getPattern().equals(cleaner.getPattern())){
|
||||
contains = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(contains) continue;
|
||||
pool.cleaners.add(cleaner);
|
||||
}
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -26,33 +26,30 @@ public class Function extends OperationContainer{
|
|||
this.parameters = new ArrayList<>();
|
||||
}
|
||||
|
||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
attribute(matcher.group(2));
|
||||
parameters(matcher.group(3)+";", cleaner);
|
||||
parameters(matcher.group(3)+";", global, local);
|
||||
this.exceptions = matcher.group(4).trim();
|
||||
|
||||
System.out.println("body IS {"+matcher.group(5));
|
||||
CleanerPool generic = new CleanerPool(
|
||||
local = new CleanerPool(
|
||||
new Cleaner("GENERIC_FUNCTION", '{', '}'),
|
||||
new Cleaner("GENERIC_PARENTHESIS", '(', ')'));
|
||||
String zip = generic.clean("{"+matcher.group(5));
|
||||
System.out.println("clean it "+zip);
|
||||
String body = generic.unzipOne(zip, (s,p) -> s);
|
||||
String zip = local.clean("{"+matcher.group(5));
|
||||
String body = local.unzipOne(zip, (s,p) -> s);
|
||||
String unzip = body.substring(1, body.indexOf('}'));
|
||||
System.out.println("----------Before------------");
|
||||
|
||||
System.out.println("----------before----------");
|
||||
show(0);
|
||||
System.out.println("----------------------------");
|
||||
|
||||
super.parse(local.clean(unzip), global, local);
|
||||
|
||||
System.out.println("BODY LIKE "+body);
|
||||
parse(unzip, cleaner, generic);
|
||||
System.out.println("----------After------------");
|
||||
System.out.println("----------After----------");
|
||||
show(0);
|
||||
System.out.println("---------------------------");
|
||||
|
||||
return matcher.group(1).length()+generic.unzip(unzip, ((s,p) -> s)).length()+1;
|
||||
|
||||
return matcher.group(1).length()+local.unzip(unzip, ((s,p) -> s)).length()+1;
|
||||
}
|
||||
|
||||
private static Pattern UNZIP_STICK = Pattern.compile("\\s+(?<e>[<|(|\\[|\"|'])");
|
||||
|
@ -88,12 +85,12 @@ public class Function extends OperationContainer{
|
|||
}
|
||||
}
|
||||
|
||||
private void parameters(String content, CleanerPool cleaner) throws Exception{
|
||||
private void parameters(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
if(content.length() == 1) return;
|
||||
boolean quote = false;
|
||||
do {
|
||||
Variable variable = new Variable();
|
||||
int index = variable.parse(content, cleaner);
|
||||
int index = variable.parse(content, global, local);
|
||||
this.parameters.add(variable);
|
||||
content = content.substring(index);
|
||||
quote = content.startsWith(",");
|
||||
|
|
|
@ -2,7 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java;
|
|||
|
||||
public abstract class JavaElement {
|
||||
|
||||
public abstract int parse(String content, CleanerPool cleaner) throws Exception;
|
||||
public abstract int parse(String content, CleanerPool global, CleanerPool local) throws Exception;
|
||||
|
||||
//Only for development
|
||||
public abstract void show(int tab);
|
||||
|
|
|
@ -105,7 +105,7 @@ public class JavaParser{
|
|||
}
|
||||
|
||||
this.clazz = new Class();
|
||||
index = this.clazz.parse(content, cleaner);
|
||||
index = this.clazz.parse(content, cleaner, null);
|
||||
content = content.substring(index);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,13 +25,18 @@ public class Variable extends JavaElement{
|
|||
this.type = type;
|
||||
}
|
||||
|
||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
System.out.println("local ? "+local);
|
||||
System.out.println("Variable.parse -> "+content);
|
||||
CleanerPool generic = new CleanerPool(
|
||||
new Cleaner("GENERIC_TYPE_",'<','>'),
|
||||
new Cleaner("GENERIC_TYPE",'<','>'),
|
||||
new Cleaner("GENERIC_ARRAY",'[',']'),
|
||||
new Cleaner("GENERIC_FUNCTION", '{','}'),
|
||||
new Cleaner("GENERIC_PARENTHESIS",'(',')'));
|
||||
content = generic.clean(content);
|
||||
if(local == null) local = generic;
|
||||
else local = local.group(generic);
|
||||
content = local.clean(content);
|
||||
System.out.println("Variable.parse -> "+content);
|
||||
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
|
@ -46,12 +51,14 @@ public class Variable extends JavaElement{
|
|||
body = body.substring(0, min);
|
||||
|
||||
if(equals < quote && equals < quotes){
|
||||
assigment(body, generic);
|
||||
assigment(body, local);
|
||||
}else{
|
||||
onlyDefine(body, generic);
|
||||
onlyDefine(body, local);
|
||||
}
|
||||
|
||||
return offset+generic.unzip(body, ((s,p) -> s)).length();
|
||||
System.out.println("skip var in "+body);
|
||||
|
||||
return offset+local.unzipOne(body, (s,p) -> (p.equals("^GENERIC_TYPE") || p.equals("^GENERIC_ARRAY")) ? s : null).length();
|
||||
}
|
||||
|
||||
private void assigment(String content, CleanerPool cleaner){
|
||||
|
|
|
@ -16,12 +16,12 @@ public class AssigmentOperation extends JavaElement{
|
|||
public AssigmentOperation(){}
|
||||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
this.variable = matcher.group(2);
|
||||
this.value = matcher.group(3);
|
||||
this.variable = local.unzip(matcher.group(2), (s,p) -> s);
|
||||
this.value = local.unzip(matcher.group(3), (s,p) -> s);
|
||||
|
||||
return matcher.group(1).length();
|
||||
}
|
||||
|
|
|
@ -5,10 +5,11 @@ 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;
|
||||
|
||||
public class ConditionalOperation extends OperationContainer{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$");
|
||||
|
||||
private Pattern pattern;
|
||||
private String condition;
|
||||
|
||||
|
@ -16,27 +17,37 @@ public class ConditionalOperation extends OperationContainer{
|
|||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
//loss global cleaner
|
||||
@Override
|
||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||
CleanerPool generic = new CleanerPool(
|
||||
new Cleaner("GENERIC_PARENTHESIS",'(',')'),
|
||||
new Cleaner("GENERIC_FUNCTION", '{','}'));
|
||||
content = generic.clean(content);
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
// content = local.clean(content);
|
||||
System.out.println("ConditionalOperation.parse -> "+content);
|
||||
|
||||
Matcher matcher = this.pattern.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
this.condition = generic.unzip(matcher.group(2), (s,p) -> s);
|
||||
this.condition = local.unzipOne(matcher.group(2), (s,p) -> s);
|
||||
System.out.println("condition -> "+condition);
|
||||
|
||||
int index = generic.unzip(matcher.group(1), (s,p) -> s).length();
|
||||
content = generic.unzip(content, (s,p) -> s).substring(index);
|
||||
|
||||
int index = matcher.group(1).length();
|
||||
content = content.substring(index);
|
||||
System.out.println("body is around "+content);
|
||||
|
||||
int bodysize;
|
||||
if(content.startsWith("{")){
|
||||
bodysize = content.indexOf('}')+1;
|
||||
content = content.substring(1, bodysize-1);
|
||||
parse(content, cleaner, generic);
|
||||
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{
|
||||
bodysize = parseOne(content, cleaner, generic);
|
||||
bodysize = parseOne(content, global, local);
|
||||
}
|
||||
|
||||
return index+bodysize;
|
||||
|
|
|
@ -10,35 +10,41 @@ import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
|||
public class ElseOperation extends OperationContainer{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*else\\s*).*$");
|
||||
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$");
|
||||
|
||||
public ElseOperation(){}
|
||||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||
System.out.println("ELSE STATEMENT");
|
||||
CleanerPool generic = new CleanerPool(
|
||||
new Cleaner("GENERIC_PARENTHESIS",'(',')'),
|
||||
new Cleaner("GENERIC_FUNCTION", '{','}'));
|
||||
content = generic.clean(content);
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
System.out.println("ElseOperation.parse -> "+content);
|
||||
|
||||
// content = global.clean(content);
|
||||
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
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 index = matcher.group(1).length();
|
||||
content = content.substring(index);
|
||||
|
||||
int bodysize;
|
||||
if(content.startsWith("{")){
|
||||
bodysize = content.indexOf('}')+1;
|
||||
content = content.substring(1, bodysize-1);
|
||||
parse(content, cleaner, generic);
|
||||
if(content.startsWith("^")){
|
||||
matcher = PATTERN_CLEANER.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{
|
||||
bodysize = parseOne(content, cleaner, generic);
|
||||
bodysize = parseOne(content, global, local);
|
||||
}
|
||||
|
||||
return index+bodysize;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void show(int tab) {
|
||||
String start = "";
|
||||
|
|
|
@ -15,18 +15,16 @@ public class MethodCallOperation extends JavaElement{
|
|||
public MethodCallOperation(){}
|
||||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||
System.out.println("method call "+cleaner);
|
||||
System.out.println("parse method "+content);
|
||||
content = cleaner.clean(content);
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
System.out.println("MethodCallOperation.parse -> "+content);
|
||||
content = local.clean(content);
|
||||
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
System.out.println("method call "+content);
|
||||
|
||||
this.value = local.unzip(matcher.group(2), (s,p) -> s);
|
||||
|
||||
this.value = cleaner.unzip(matcher.group(2), (s,p) -> s);
|
||||
|
||||
return cleaner.unzip(matcher.group(1), (s,p) -> s).length();
|
||||
return matcher.group(1).length();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -18,26 +18,30 @@ public abstract class OperationContainer extends JavaElement{
|
|||
this.childs = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
System.out.println("RECEIVE CONAINER "+content);
|
||||
content = local.unzip(content, (s,p) -> s);
|
||||
System.out.println("CONTAINER "+content);
|
||||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
System.out.println("OperationContainer.parse -> "+content);
|
||||
// content = local.unzipOne(content, (s,p) -> s);
|
||||
|
||||
while(!(content.replaceAll("\\s+", "").isEmpty())) content = internalParse(content, global, local);
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int parseOne(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
content = local.unzip(content, (s,p) -> s);
|
||||
System.out.println("OperationContainer.parseOne -> "+content);
|
||||
// content = local.unzip(content, (s,p) -> s);
|
||||
String modify = internalParse(content, global, local);
|
||||
return content.length()-modify.length();
|
||||
}
|
||||
|
||||
private String internalParse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
System.out.println("BODY "+content);
|
||||
|
||||
System.out.println("OperationContainer.internalParse -> "+content);
|
||||
JavaElement operation = FACTORY.buildOperation(content);
|
||||
|
||||
System.out.println("got "+operation.getClass().getSimpleName());
|
||||
int index = operation.parse(content, local);
|
||||
|
||||
System.out.println(operation.getClass().getSimpleName()+" operation = FACTORY.buildOperation();");
|
||||
int index = operation.parse(content, global, local);
|
||||
operation.show(0);
|
||||
System.out.println();
|
||||
content = content.substring(index);
|
||||
if(operation instanceof Variable){
|
||||
if(content.startsWith(",")){
|
||||
|
@ -50,7 +54,7 @@ public abstract class OperationContainer extends JavaElement{
|
|||
boolean quote;
|
||||
do{
|
||||
variable = new Variable(variable.getModifier(), variable.getType());
|
||||
index = variable.parse(content, local);
|
||||
index = variable.parse(content, global, local);
|
||||
multiple.addVariable(variable.getName(), variable.getValue());
|
||||
content = content.substring(index);
|
||||
quote = content.startsWith(",");
|
||||
|
|
|
@ -65,15 +65,13 @@ public class OperationFactory{
|
|||
*/
|
||||
|
||||
public JavaElement buildOperation(String content) throws Exception{
|
||||
System.out.println("Factory.buildOperation -> "+content);
|
||||
CleanerPool generic = new CleanerPool(
|
||||
new Cleaner("GENERIC_FUNCTION", '{','}'),
|
||||
new Cleaner("GENERIC_PARENTHESIS",'(',')'),
|
||||
new Cleaner("GENERIC_ARRAY",'[',']'),
|
||||
new Cleaner("GENERIC_TYPE_",'<','>'));
|
||||
content = generic.clean(content);
|
||||
content = generiqueTypes(content, generic);
|
||||
|
||||
System.out.println("look for "+content);
|
||||
|
||||
for(Entry<Pattern, Class<? extends JavaElement>> entries : this.patterns.entrySet()){
|
||||
if(entries.getKey().matcher(content).matches()) return entries.getValue().newInstance();
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ public class ReturnOperation extends JavaElement{
|
|||
public ReturnOperation(){}
|
||||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue