Compare commits

...

4 commits

Author SHA1 Message Date
jeffcheasey88
f4ff094554 Operation -> Return statement -> get value 2023-05-31 12:00:38 +02:00
jeffcheasey88
c7b9454ede Operation -> Fix CleanerPool using 2023-05-31 11:47:38 +02:00
jeffcheasey88
ad430cb659 Operation -> Method Call 2023-05-30 21:46:40 +02:00
jeffcheasey88
be289b8d7f Fix Cleaner unzip one, fix logic 2023-05-30 21:35:55 +02:00
13 changed files with 155 additions and 101 deletions

View file

@ -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);

View file

@ -15,7 +15,34 @@ 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
public String toString(){
String s = "CleanerPool[\n";
for(Cleaner cleaner : cleaners) s+=cleaner.pattern+"['"+cleaner.open+"' -> '"+cleaner.close+"']\n";
s+="\n]";
return s;
}
public String clean(String value){
@ -36,15 +63,18 @@ public class CleanerPool{
String zip = cleaner.getConstant(key);
String modified = modifier.apply(zip, cleaner.getPattern());
if(modified == null) continue;
map.put(key, cleaner.open+zip+cleaner.close);
tmp = matcher.group(1)+cleaner.open+zip+cleaner.close+matcher.group(3);
map.put(key, cleaner.open+modified+cleaner.close);
tmp = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3);
edited = true;
}
}
}while(edited);
for(Entry<String, String> unzip : map.entrySet()) value = value.replaceAll("\\"+unzip.getKey()+"(?<e>([^\\d]|$))", unzip.getValue()+"${e}");
String base = value;
for(Entry<String, String> unzip : map.entrySet()){
Pattern pattern = Pattern.compile("\\"+unzip.getKey()+"(?<e>([^\\d]|$))");
if(pattern.matcher(base).find()) value = pattern.matcher(value).replaceAll(unzip.getValue()+"${e}");
}
return value;
}

View file

@ -26,27 +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();
CleanerPool generic = new CleanerPool(new Cleaner("GENERIC_FUNCTION", '{', '}'));
String zip = generic.clean("{"+matcher.group(5));
String body = generic.unzipOne(zip, (s,p) -> s);
local = new CleanerPool(
new Cleaner("GENERIC_FUNCTION", '{', '}'),
new Cleaner("GENERIC_PARENTHESIS", '(', ')'));
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------------");
show(0);
System.out.println("----------------------------");
parse(unzip, cleaner, generic);
System.out.println("----------After------------");
show(0);
System.out.println("---------------------------");
return matcher.group(1).length()+generic.unzip(unzip, ((s,p) -> s)).length()+1;
System.out.println("----------before----------");
show(0);
super.parse(local.clean(unzip), global, local);
System.out.println("----------After----------");
show(0);
return matcher.group(1).length()+local.unzip(unzip, ((s,p) -> s)).length()+1;
}
private static Pattern UNZIP_STICK = Pattern.compile("\\s+(?<e>[<|(|\\[|\"|'])");
@ -82,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(",");

View file

@ -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);

View file

@ -14,7 +14,7 @@ import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class JavaParser{
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Variable.java");
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\ExampleClass.java");
BufferedReader reader = new BufferedReader(new FileReader(file));
JavaParser parser = new JavaParser(reader);
@ -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);
}

View file

@ -25,13 +25,15 @@ 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{
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);
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
@ -46,12 +48,12 @@ 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();
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){

View file

@ -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();
}

View file

@ -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;
@ -17,26 +18,29 @@ public class ConditionalOperation extends OperationContainer{
}
@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{
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);
int index = matcher.group(1).length();
content = content.substring(index);
int index = generic.unzip(matcher.group(1), (s,p) -> s).length();
content = generic.unzip(content, (s,p) -> s).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.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;

View file

@ -10,30 +10,32 @@ 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{
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;

View file

@ -8,18 +8,21 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class MethodCallOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\(]*\\([^\\)]*\\));).*$");
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+);).*$");
private String value;
public MethodCallOperation(){}
@Override
public int parse(String content, CleanerPool cleaner) throws Exception{
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();
this.value = matcher.group(2);
this.value = local.unzip(matcher.group(2), (s,p) -> s);
return matcher.group(1).length();
}

View file

@ -18,25 +18,30 @@ public abstract class OperationContainer extends JavaElement{
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);
@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(",")){
@ -49,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(",");

View file

@ -32,10 +32,11 @@ public class OperationFactory{
this.patterns.put(Pattern.compile("^\\s*while\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), WhileOperation.class);
this.patterns.put(Pattern.compile("^\\s*else\\s*.*$"), ElseOperation.class);
this.patterns.put(Pattern.compile("^\\s*([^;=]+;).*$"), MethodCallOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+;.*$"), MethodCallOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s+[^\\s]+\\s*=\\s*[^;]+;.*$"), Variable.class);
this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s*=\\s*[^;]+;.*$"), AssigmentOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^;]+;.*$"), Variable.class);
}
/*
@ -64,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();
}
@ -87,25 +86,15 @@ public class OperationFactory{
private static Pattern UNZIP_EQUALS_RIGHT = Pattern.compile("=(?<e>[^=\\s])");
private String generiqueTypes(String content, CleanerPool cleaner){
System.out.println("base on "+content);
String unzip = cleaner.unzip(content, (value, pattern) -> {
System.out.println("unzip pattern "+pattern);
if(pattern.equals("^GENERIC_FUNCTION")) return null;
if(pattern.equals("^GENERIC_PARENTHESIS")) return null;
System.out.println("let pass "+pattern);
if(pattern.equals("^GENERIC_FUNCTION") || pattern.equals("^GENERIC_PARENTHESIS")) return null;
return value.replaceAll("\\s+", "");
});
System.out.println("generiqueTypes on "+unzip);
unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}");
System.out.println("g => "+unzip);
unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}");
System.out.println("g => "+unzip);
unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}");
System.out.println("g => "+unzip);
unzip = UNZIP_EQUALS_LEFT.matcher(unzip).replaceAll("${e} =");
System.out.println("g => "+unzip);
unzip = UNZIP_EQUALS_RIGHT.matcher(unzip).replaceAll("= ${e}");
System.out.println("g => "+unzip);
return unzip;
}
}

View file

@ -10,16 +10,26 @@ public class ReturnOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*return\\s*([^;]*);).*$");
private String value;
private static OperationFactory FACTORY = OperationFactory.getFactory();
private String toChange;
private JavaElement value;
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();
this.value = matcher.group(2);
//To update for native obj
this.toChange = matcher.group(2);
JavaElement operation = FACTORY.buildOperation(toChange+";");
if(operation != null){
System.out.println(operation.getClass().getSimpleName());
value = operation;
value.parse(toChange+";", global, local);
}
return matcher.group(1).length();
}
@ -28,7 +38,13 @@ public class ReturnOperation extends JavaElement{
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"return "+value+";");
if(value != null){
System.out.println("return");
value.show(tab+1);
System.out.println(";");
return;
}
System.out.println(start+"return "+toChange+";");
}
}