Compare commits

..

7 commits

Author SHA1 Message Date
jeffcheasey88
800ad9a4e9 Fix Cleaner when modifier return null case 2023-05-30 19:31:57 +02:00
jeffcheasey88
b800b88fb4 Operation -> For and While Statement (simple case) 2023-05-30 18:46:49 +02:00
jeffcheasey88
0c98fe6e2e Operation -> Fix empty return case 2023-05-30 18:38:44 +02:00
jeffcheasey88
9a19c54e34 Operation -> Else Statement 2023-05-30 18:21:59 +02:00
jeffcheasey88
8f37f9882b Operation -> If Statement 2023-05-30 18:13:55 +02:00
jeffcheasey88
a231434ecd Operation -> OperationContainer (Recursive {} body system) 2023-05-30 18:06:50 +02:00
jeffcheasey88
56ce2b7235 Operation -> Assigment 2023-05-30 11:19:55 +02:00
10 changed files with 328 additions and 70 deletions

View file

@ -34,6 +34,8 @@ public class CleanerPool{
if(matcher.matches()){ if(matcher.matches()){
String key = matcher.group(2); String key = matcher.group(2);
String zip = cleaner.getConstant(key); String zip = cleaner.getConstant(key);
String modified = modifier.apply(zip, cleaner.getPattern());
if(modified == null) continue;
map.put(key, cleaner.open+zip+cleaner.close); map.put(key, cleaner.open+zip+cleaner.close);
tmp = matcher.group(1)+cleaner.open+zip+cleaner.close+matcher.group(3); tmp = matcher.group(1)+cleaner.open+zip+cleaner.close+matcher.group(3);
edited = true; edited = true;
@ -57,7 +59,7 @@ public class CleanerPool{
String key = matcher.group(2); String key = matcher.group(2);
String zip = cleaner.getConstant(key); String zip = cleaner.getConstant(key);
String modified = modifier.apply(zip, cleaner.getPattern()); String modified = modifier.apply(zip, cleaner.getPattern());
if(modified == null) modified = zip; if(modified == null) continue;
value = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3); value = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3);
edited = true; edited = true;
} }

View file

@ -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*([^{]*)\\{)(.*)$");
@ -23,11 +20,10 @@ public class Function extends JavaElement{
private List<Variable> parameters; private List<Variable> parameters;
private String exceptions; private String exceptions;
private List<JavaElement> childs; private boolean constructor;
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{
@ -42,7 +38,13 @@ 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); 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; return matcher.group(1).length()+generic.unzip(unzip, ((s,p) -> s)).length()+1;
} }
@ -73,6 +75,7 @@ public class Function extends JavaElement{
if(this.returnType == null){ if(this.returnType == null){
this.returnType = value; this.returnType = value;
if(values.hasNext()) value = values.next(); if(values.hasNext()) value = values.next();
else constructor = true;
} }
if(this.name == null){ if(this.name == null){
this.name = value; this.name = value;
@ -92,50 +95,14 @@ public class Function extends JavaElement{
}while(quote); }while(quote);
} }
private void body(String content, CleanerPool cleaner) throws Exception{
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";
String param = ""; String param = "";
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)+" "+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+"}");
} }
} }

View file

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

View file

@ -16,7 +16,7 @@ public class Variable extends JavaElement{
private int modifier; private int modifier;
private String name; private String name;
private String type; private String type;
private Variable value; //Change into operation private Variable value; //Change into operation or JavaElement
public Variable(){} public Variable(){}
@ -126,7 +126,7 @@ public class Variable extends JavaElement{
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";
System.out.println(start+Modifier.toString(modifier)+" "+type+" "+name+(value == null ? ";":" = "+value+";")); System.out.println(start+Modifier.toString(modifier)+(modifier > 0 ? " ":"")+type+" "+name+(value == null ? ";":" = "+value+";"));
} }
public static class Value extends Variable{ public static class Value extends Variable{

View file

@ -0,0 +1,37 @@
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;
public class AssigmentOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\s]+)\\s*=\\s*([^;]+);).*$");
private String variable;
private String value;
public AssigmentOperation(){}
@Override
public int parse(String content, CleanerPool cleaner) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
this.variable = matcher.group(2);
this.value = matcher.group(3);
return matcher.group(1).length();
}
@Override
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+variable+" = "+value+";");
}
}

View file

@ -0,0 +1,107 @@
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;
public class ConditionalOperation extends OperationContainer{
private Pattern pattern;
private String condition;
public ConditionalOperation(Pattern pattern){
this.pattern = pattern;
}
@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);
Matcher matcher = this.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.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);
}else{
bodysize = parseOne(content, cleaner, generic);
}
return index+bodysize;
}
@Override
public void show(int tab) {
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"Condition??"+condition+"{");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
public static class IfOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*if\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$");
public IfOperation(){
super(PATTERN);
}
@Override
public void show(int tab) {
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"if"+super.condition+"{");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
}
public static class ForOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*for\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$");
public ForOperation(){
super(PATTERN);
}
@Override
public void show(int tab) {
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"for"+super.condition+"{");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
}
public static class WhileOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*while\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$");
public WhileOperation(){
super(PATTERN);
}
@Override
public void show(int tab) {
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"while"+super.condition+"{");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
}
}

View file

@ -0,0 +1,52 @@
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;
public class ElseOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\s*else\\s*).*$");
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);
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 bodysize;
if(content.startsWith("{")){
bodysize = content.indexOf('}')+1;
content = content.substring(1, bodysize-1);
parse(content, cleaner, generic);
}else{
bodysize = parseOne(content, cleaner, generic);
}
return index+bodysize;
}
@Override
public void show(int tab) {
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+"else{");
for(JavaElement child : getChilds()) child.show(tab+1);
System.out.println(start+"}");
}
}

View file

@ -0,0 +1,68 @@
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())) content = internalParse(content, global, local);
}
public int parseOne(String content, CleanerPool global, CleanerPool local) throws Exception{
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);
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);
return content;
}
public List<JavaElement> getChilds(){
return this.childs;
}
}

View file

@ -6,39 +6,53 @@ import java.util.Map.Entry;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool; import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement; 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.operations.ConditionalOperation.ForOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.IfOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.WhileOperation;
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*([^;=]*;).*$"), MethodCallOperation.class); this.patterns.put(Pattern.compile("^\\s*if\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), IfOperation.class);
this.patterns.put(Pattern.compile("^\\s*for\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), ForOperation.class);
this.patterns.put(Pattern.compile("^\\s*while\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), WhileOperation.class);
this.patterns.put(Pattern.compile("^\\s*else\\s*.*$"), ElseOperation.class);
//if not types ?? this.patterns.put(Pattern.compile("^\\s*([^;=]+;).*$"), MethodCallOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^\\s]*\\s+[^\\s]*\\s+=\\s+[^\\s]*;.*$"), Variable.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);
} }
/* /*
* variable * variable OK
* assignation * assignation OK
* exec method * exec method OK
* for * for OK
* do while * do while
* while * while OK
* if * if OK
* switch case * switch case
* return * return OK
* continue * continue
* break * break
* try catch finally * try catch finally
* throw * throw
* else * else OK
* synchronized * synchronized
* instance of * instance of
* *
@ -46,17 +60,19 @@ public class OperationFactory{
* *
* assert ?? * assert ??
* Goto ?? * Goto ??
* Const ??
*/ */
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); content = generiqueTypes(content, generic);
System.out.println("look for "+content);
for(Entry<Pattern, Class<? extends JavaElement>> entries : this.patterns.entrySet()){ for(Entry<Pattern, Class<? extends JavaElement>> entries : this.patterns.entrySet()){
if(entries.getKey().matcher(content).matches()) return entries.getValue().newInstance(); if(entries.getKey().matcher(content).matches()) return entries.getValue().newInstance();
} }
@ -71,16 +87,25 @@ public class OperationFactory{
private static Pattern UNZIP_EQUALS_RIGHT = Pattern.compile("=(?<e>[^=\\s])"); private static Pattern UNZIP_EQUALS_RIGHT = Pattern.compile("=(?<e>[^=\\s])");
private String generiqueTypes(String content, CleanerPool cleaner){ private String generiqueTypes(String content, CleanerPool cleaner){
System.out.println("base on "+content);
String unzip = cleaner.unzip(content, (value, pattern) -> { String unzip = cleaner.unzip(content, (value, pattern) -> {
System.out.println("unzip pattern "+pattern);
if(pattern.equals("^GENERIC_FUNCTION")) return null; if(pattern.equals("^GENERIC_FUNCTION")) return null;
if(pattern.equals("^GENERIC_PARENTHESIS")) return value.replace("\\s+", " "); if(pattern.equals("^GENERIC_PARENTHESIS")) return null;
System.out.println("let pass "+pattern);
return value.replaceAll("\\s+", ""); return value.replaceAll("\\s+", "");
}); });
System.out.println("generiqueTypes on "+unzip);
unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}"); unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}");
System.out.println("g => "+unzip);
unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}"); unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}");
System.out.println("g => "+unzip);
unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}"); unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}");
System.out.println("g => "+unzip);
unzip = UNZIP_EQUALS_LEFT.matcher(unzip).replaceAll("${e} ="); unzip = UNZIP_EQUALS_LEFT.matcher(unzip).replaceAll("${e} =");
System.out.println("g => "+unzip);
unzip = UNZIP_EQUALS_RIGHT.matcher(unzip).replaceAll("= ${e}"); unzip = UNZIP_EQUALS_RIGHT.matcher(unzip).replaceAll("= ${e}");
System.out.println("g => "+unzip);
return unzip; return unzip;
} }
} }

View file

@ -8,7 +8,7 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class ReturnOperation extends JavaElement{ public class ReturnOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*return\\s+([^;]*);)\\s*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*return\\s*([^;]*);).*$");
private String value; private String value;