From c7b9454ede29368035b3fe16ffa18f607a8f3bbd Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Wed, 31 May 2023 11:47:38 +0200 Subject: [PATCH] Operation -> Fix CleanerPool using --- .../peeratcode/parser/java/Class.java | 8 ++-- .../peeratcode/parser/java/CleanerPool.java | 21 +++++++++- .../peeratcode/parser/java/Function.java | 31 +++++++-------- .../peeratcode/parser/java/JavaElement.java | 2 +- .../peeratcode/parser/java/JavaParser.java | 2 +- .../peeratcode/parser/java/Variable.java | 19 ++++++--- .../java/operations/AssigmentOperation.java | 6 +-- .../java/operations/ConditionalOperation.java | 39 ++++++++++++------- .../parser/java/operations/ElseOperation.java | 36 ++++++++++------- .../java/operations/MethodCallOperation.java | 14 +++---- .../java/operations/OperationContainer.java | 26 +++++++------ .../java/operations/OperationFactory.java | 6 +-- .../java/operations/ReturnOperation.java | 2 +- 13 files changed, 126 insertions(+), 86 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index 0faa732..9e9a653 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -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); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java index 0712dbf..8d8b40f 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java @@ -15,7 +15,26 @@ public class CleanerPool{ private List 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 diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java index aae8a26..da928d9 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -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+(?[<|(|\\[|\"|'])"); @@ -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(","); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java index 5d2f662..d2a126b 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java @@ -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); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index cd5ee83..9288f45 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -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); } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java index 27628d2..2614f3b 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -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){ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java index 39579b9..4817a20 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/AssigmentOperation.java @@ -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(); } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java index c3580f7..73e2047 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java @@ -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; diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ElseOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ElseOperation.java index 86c9412..9eef8f2 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ElseOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ElseOperation.java @@ -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 = ""; diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java index 0ca2395..43008f6 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/MethodCallOperation.java @@ -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 diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java index b8f6924..673a3cc 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationContainer.java @@ -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(","); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java index eeab0e6..fb50486 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java @@ -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> entries : this.patterns.entrySet()){ if(entries.getKey().matcher(content).matches()) return entries.getValue().newInstance(); } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java index 0be0f66..fec14d1 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ReturnOperation.java @@ -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();