From 4b1b83930eb9de8ba2181cd9275f844bf40a713c Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Mon, 15 May 2023 19:11:27 +0200 Subject: [PATCH] Function signature & delimitation --- .../peeratcode/parser/java/Class.java | 2 +- .../peeratcode/parser/java/CleanerPool.java | 56 ++++++-- .../peeratcode/parser/java/Function.java | 120 ++++++++++-------- .../peeratcode/parser/java/JavaParser.java | 6 +- .../peeratcode/parser/java/Variable.java | 7 +- 5 files changed, 117 insertions(+), 74 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index 851f9b1..1304a41 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -54,7 +54,7 @@ public class Class{ }else{ // System.out.println("Function "+content); Function func = new Function(); - int index = func.parse(content); + int index = func.parse(content, cleaner); this.functions.add(func); content = content.substring(index); // System.out.println("End "+content); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java index 894e8c4..c2bbbfa 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java @@ -2,7 +2,10 @@ package be.jeffcheasey88.peeratcode.parser.java; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.function.BiFunction; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -20,21 +23,46 @@ public class CleanerPool{ return value; } - - public String unzip(String value, BiFunction modifier){ - boolean 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; + public String unzipOne(String value, BiFunction modifier){ + boolean edited; + String tmp = value; + Map map = new HashMap<>(); + do{ + edited = false; + for(Cleaner cleaner : this.cleaners){ + Matcher matcher = cleaner.getMatcher(tmp); + if(matcher.matches()){ + String key = matcher.group(2); + String zip = cleaner.getConstant(key); + map.put(key, cleaner.open+zip+cleaner.close); + tmp = matcher.group(1)+cleaner.open+zip+cleaner.close+matcher.group(3); + edited = true; + } } - } - if(edited) return unzip(value, modifier); + }while(edited); + + + for(Entry unzip : map.entrySet()) value = value.replaceAll("\\"+unzip.getKey()+"(?([^\\d]|$))", unzip.getValue()+"${e}"); + + return value; + } + + public String unzip(String value, BiFunction 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; } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java index 98b3cc8..be61179 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -2,84 +2,104 @@ package be.jeffcheasey88.peeratcode.parser.java; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner; + public class Function { private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$"); private int modifier; + private String returnType; private String name; + private List parameters; private String exceptions; - private String parameters; - private List functions; + private List variables; private List operations; public Function(){ - this.functions = new ArrayList<>(); + this.parameters = new ArrayList<>(); + this.variables = 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.matches(); - String[] split = matcher.group(2).split("\\s+"); - for(int i = 0; i < split.length-2; i++){ - this.modifier+=JavaParser.getModifier(split[i]); - } - this.name = split[split.length-1]; - this.parameters = matcher.group(3); - this.exceptions = matcher.group(4); + attribute(matcher.group(2)); + parameters(matcher.group(3)+";", cleaner); + this.exceptions = matcher.group(4).trim(); - String body = matcher.group(5); - int offset = 0; - int index = 0; + 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+(?[<|(|\\[|\"|'])"); + private static Pattern UNZIP_MAJ = Pattern.compile(">(?[^>\\d,;(])"); + private static Pattern UNZIP_ARRAY = Pattern.compile("](?[^\\[\\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 values = new ArrayIterator<>(unzip.split("\\s+")); + String value = null; + int modifier; + while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0){ + this.modifier+=modifier; + } + if(this.returnType == null){ + this.returnType = value; + if(values.hasNext()) value = values.next(); + } + if(this.name == null){ + this.name = value; + } + } + + private void parameters(String content, CleanerPool cleaner) throws Exception{ + if(content.length() == 1) return; + boolean quote = false; do { - int end = body.indexOf('}'); - int braces = body.indexOf('{'); - int quotes = body.indexOf(';'); - - if((end < 0) || (end < braces && end < quotes)){ - 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; + Variable variable = new Variable(); + int index = variable.parse(content, cleaner); + this.parameters.add(variable); + content = content.substring(index); + quote = content.startsWith(","); + if(quote) content = content.substring(1); + }while(quote); + } + + private void body(String content, CleanerPool cleaner) throws Exception{ } public void show(int tab){ String start = ""; for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+Modifier.toString(modifier)+" "+name+"("+parameters+") "+exceptions+"{"); - for(Operation o : this.operations) o.show(tab+1); - System.out.println(); - for(Function f : this.functions) f.show(tab+1); + String param = ""; + for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+""; + if(!param.isEmpty()) param = param.substring(1); + System.out.println(start+Modifier.toString(modifier)+" "+returnType+" "+name+"("+param+") "+exceptions+"{"); System.out.println(start+"}"); } - - @Override - public String toString(){ - return "Function[name="+name+",param="+parameters+",exception="+exceptions+"]"; - } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index cec4554..fecb634 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -14,7 +14,7 @@ import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner; 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"); BufferedReader reader = new BufferedReader(new FileReader(file)); @@ -29,8 +29,8 @@ public class JavaParser{ show(dir); } - public static void main(String[] args) throws Exception{ - String variable = "Future future = {new Runnable()};"; + public static void mainf(String[] args) throws Exception{ + String variable = "var myName = \"Test\";"; String clazz = "package test; public class Test{ "+variable+" }"; BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/home/tmp.txt"))); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java index 20c0af2..e05ea6c 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java; import java.lang.reflect.Modifier; import java.util.Iterator; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -24,7 +25,6 @@ public class Variable { } public int parse(String content, CleanerPool cleaner) throws Exception{ - System.out.println("parse "+content); CleanerPool generic = new CleanerPool( new Cleaner("GENERIC_TYPE_",'<','>'), new Cleaner("GENERIC_ARRAY",'[',']'), @@ -54,7 +54,6 @@ public class Variable { } private void assigment(String content, CleanerPool cleaner){ - System.out.println("assigment "+content); Iterator values = onlyDefine(content, cleaner); if(!values.hasNext()) return; values.next(); @@ -65,7 +64,6 @@ public class Variable { } private Iterator onlyDefine(String content, CleanerPool cleaner){ - System.out.println("define "+content); content = generiqueTypes(content, cleaner); Iterator values = new ArrayIterator<>(content.split("\\s+")); String value = null; @@ -91,19 +89,16 @@ public class Variable { private static Pattern UNZIP_EQUALS_RIGHT = Pattern.compile("=(?[^=\\s])"); private String generiqueTypes(String content, CleanerPool cleaner){ - System.out.println("generic "+content); String unzip = cleaner.unzip(content, (value, pattern) -> { if(pattern.equals("^GENERIC_FUNCTION")) return null; if(pattern.equals("^GENERIC_PARENTHESIS")) return value.replace("\\s+", " "); return value.replaceAll("\\s+", ""); }); - System.out.println("unzip "+unzip); unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}"); unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}"); unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}"); unzip = UNZIP_EQUALS_LEFT.matcher(unzip).replaceAll("${e} ="); unzip = UNZIP_EQUALS_RIGHT.matcher(unzip).replaceAll("= ${e}"); - System.out.println("matcher "+unzip); return unzip; }