Function signature & delimitation

This commit is contained in:
jeffcheasey88 2023-05-15 19:11:27 +02:00
parent 0111df23f5
commit 4b1b83930e
5 changed files with 117 additions and 74 deletions

View file

@ -54,7 +54,7 @@ public class Class{
}else{ }else{
// System.out.println("Function "+content); // System.out.println("Function "+content);
Function func = new Function(); Function func = new Function();
int index = func.parse(content); int index = func.parse(content, cleaner);
this.functions.add(func); this.functions.add(func);
content = content.substring(index); content = content.substring(index);
// System.out.println("End "+content); // System.out.println("End "+content);

View file

@ -2,7 +2,10 @@ package be.jeffcheasey88.peeratcode.parser.java;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -20,21 +23,46 @@ public class CleanerPool{
return value; return value;
} }
public String unzipOne(String value, BiFunction<String, String, String> modifier){
public String unzip(String value, BiFunction<String,String, String> modifier){ boolean edited;
boolean edited = false; String tmp = value;
for(Cleaner cleaner : this.cleaners){ Map<String, String> map = new HashMap<>();
Matcher matcher = cleaner.getMatcher(value); do{
if(matcher.matches()){ edited = false;
String key = matcher.group(2); for(Cleaner cleaner : this.cleaners){
String zip = cleaner.getConstant(key); Matcher matcher = cleaner.getMatcher(tmp);
String modified = modifier.apply(zip, cleaner.getPattern()); if(matcher.matches()){
if(modified == null) modified = zip; String key = matcher.group(2);
value = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3); String zip = cleaner.getConstant(key);
edited = true; map.put(key, cleaner.open+zip+cleaner.close);
tmp = matcher.group(1)+cleaner.open+zip+cleaner.close+matcher.group(3);
edited = true;
}
} }
} }while(edited);
if(edited) return unzip(value, modifier);
for(Entry<String, String> unzip : map.entrySet()) value = value.replaceAll("\\"+unzip.getKey()+"(?<e>([^\\d]|$))", unzip.getValue()+"${e}");
return value;
}
public String unzip(String value, BiFunction<String, String, String> modifier){
boolean edited;
do{
edited = false;
for(Cleaner cleaner : this.cleaners){
Matcher matcher = cleaner.getMatcher(value);
if(matcher.matches()){
String key = matcher.group(2);
String zip = cleaner.getConstant(key);
String modified = modifier.apply(zip, cleaner.getPattern());
if(modified == null) modified = zip;
value = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3);
edited = true;
}
}
}while(edited);
return value; return value;
} }

View file

@ -2,84 +2,104 @@ package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class Function { public class Function {
private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$"); private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$");
private int modifier; private int modifier;
private String returnType;
private String name; private String name;
private List<Variable> parameters;
private String exceptions; private String exceptions;
private String parameters;
private List<Function> functions; private List<Variable> variables;
private List<Operation> operations; private List<Operation> operations;
public Function(){ public Function(){
this.functions = new ArrayList<>(); this.parameters = new ArrayList<>();
this.variables = new ArrayList<>();
this.operations = new ArrayList<>(); this.operations = new ArrayList<>();
} }
public int parse(String content) throws Exception{ public int parse(String content, CleanerPool cleaner) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.matches();
String[] split = matcher.group(2).split("\\s+"); attribute(matcher.group(2));
for(int i = 0; i < split.length-2; i++){ parameters(matcher.group(3)+";", cleaner);
this.modifier+=JavaParser.getModifier(split[i]); 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);
String unzip = body.substring(1, body.indexOf('}'));
body(unzip, generic);
return matcher.group(1).length()+generic.unzip(unzip, ((s,p) -> s)).length()+1;
}
private static Pattern UNZIP_STICK = Pattern.compile("\\s+(?<e>[<|(|\\[|\"|'])");
private static Pattern UNZIP_MAJ = Pattern.compile(">(?<e>[^>\\d,;(])");
private static Pattern UNZIP_ARRAY = Pattern.compile("](?<e>[^\\[\\d,;])");
private void attribute(String content){
CleanerPool generic = new CleanerPool(
new Cleaner("GENERIC_TYPE_",'<','>'),
new Cleaner("GENERIC_ARRAY",'[',']'));
String zip = generic.clean(content);
String unzip = generic.unzip(zip, (value, pattern) -> {
return value.replaceAll("\\s+", "");
});
unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}");
unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}");
unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}");
Iterator<String> values = new ArrayIterator<>(unzip.split("\\s+"));
String value = null;
int modifier;
while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0){
this.modifier+=modifier;
} }
this.name = split[split.length-1]; if(this.returnType == null){
this.parameters = matcher.group(3); this.returnType = value;
this.exceptions = matcher.group(4); if(values.hasNext()) value = values.next();
}
if(this.name == null){
this.name = value;
}
}
String body = matcher.group(5); private void parameters(String content, CleanerPool cleaner) throws Exception{
int offset = 0; if(content.length() == 1) return;
int index = 0; boolean quote = false;
do { do {
int end = body.indexOf('}'); Variable variable = new Variable();
int braces = body.indexOf('{'); int index = variable.parse(content, cleaner);
int quotes = body.indexOf(';'); this.parameters.add(variable);
content = content.substring(index);
quote = content.startsWith(",");
if(quote) content = content.substring(1);
}while(quote);
}
if((end < 0) || (end < braces && end < quotes)){ private void body(String content, CleanerPool cleaner) throws Exception{
if(end > 0) offset+=end;
break;
}
if(braces < 0 && quotes < 0){
if(end > 0) offset+=end;
break;
}
if(braces >= 0 && braces < quotes){
Function func = new Function();
index = func.parse(body.substring(0, end+1));
this.functions.add(func);
}else{
Operation op = new Operation();
index = op.parse(body.substring(0, end+1));
this.operations.add(op);
}
offset+=index+1;
body = body.substring(index);
}while(offset > -1);
return matcher.group(1).length()+offset;
} }
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)+" "+name+"("+parameters+") "+exceptions+"{"); String param = "";
for(Operation o : this.operations) o.show(tab+1); for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+"";
System.out.println(); if(!param.isEmpty()) param = param.substring(1);
for(Function f : this.functions) f.show(tab+1); System.out.println(start+Modifier.toString(modifier)+" "+returnType+" "+name+"("+param+") "+exceptions+"{");
System.out.println(start+"}"); System.out.println(start+"}");
} }
@Override
public String toString(){
return "Function[name="+name+",param="+parameters+",exception="+exceptions+"]";
}
} }

View file

@ -14,7 +14,7 @@ import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class JavaParser{ public class JavaParser{
public static void maine(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\\Import.java");
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = new BufferedReader(new FileReader(file));
@ -29,8 +29,8 @@ public class JavaParser{
show(dir); show(dir);
} }
public static void main(String[] args) throws Exception{ public static void mainf(String[] args) throws Exception{
String variable = "Future future = {new Runnable()};"; String variable = "var myName = \"Test\";";
String clazz = "package test; public class Test{ "+variable+" }"; String clazz = "package test; public class Test{ "+variable+" }";
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/home/tmp.txt"))); BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/home/tmp.txt")));

View file

@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -24,7 +25,6 @@ public class Variable {
} }
public int parse(String content, CleanerPool cleaner) throws Exception{ public int parse(String content, CleanerPool cleaner) throws Exception{
System.out.println("parse "+content);
CleanerPool generic = new CleanerPool( CleanerPool generic = new CleanerPool(
new Cleaner("GENERIC_TYPE_",'<','>'), new Cleaner("GENERIC_TYPE_",'<','>'),
new Cleaner("GENERIC_ARRAY",'[',']'), new Cleaner("GENERIC_ARRAY",'[',']'),
@ -54,7 +54,6 @@ public class Variable {
} }
private void assigment(String content, CleanerPool cleaner){ private void assigment(String content, CleanerPool cleaner){
System.out.println("assigment "+content);
Iterator<String> values = onlyDefine(content, cleaner); Iterator<String> values = onlyDefine(content, cleaner);
if(!values.hasNext()) return; if(!values.hasNext()) return;
values.next(); values.next();
@ -65,7 +64,6 @@ public class Variable {
} }
private Iterator<String> onlyDefine(String content, CleanerPool cleaner){ private Iterator<String> onlyDefine(String content, CleanerPool cleaner){
System.out.println("define "+content);
content = generiqueTypes(content, cleaner); content = generiqueTypes(content, cleaner);
Iterator<String> values = new ArrayIterator<>(content.split("\\s+")); Iterator<String> values = new ArrayIterator<>(content.split("\\s+"));
String value = null; String value = null;
@ -91,19 +89,16 @@ public class Variable {
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("generic "+content);
String unzip = cleaner.unzip(content, (value, pattern) -> { String unzip = cleaner.unzip(content, (value, 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 value.replace("\\s+", " ");
return value.replaceAll("\\s+", ""); return value.replaceAll("\\s+", "");
}); });
System.out.println("unzip "+unzip);
unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}"); unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}");
unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}"); unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}");
unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}"); unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}");
unzip = UNZIP_EQUALS_LEFT.matcher(unzip).replaceAll("${e} ="); unzip = UNZIP_EQUALS_LEFT.matcher(unzip).replaceAll("${e} =");
unzip = UNZIP_EQUALS_RIGHT.matcher(unzip).replaceAll("= ${e}"); unzip = UNZIP_EQUALS_RIGHT.matcher(unzip).replaceAll("= ${e}");
System.out.println("matcher "+unzip);
return unzip; return unzip;
} }