From b800b88fb45d5a29431b411f37934ab1a326c88d Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Tue, 30 May 2023 18:46:49 +0200 Subject: [PATCH] Operation -> For and While Statement (simple case) --- .../java/operations/ConditionalOperation.java | 107 ++++++++++++++++++ .../parser/java/operations/IfOperation.java | 57 ---------- .../java/operations/OperationFactory.java | 13 ++- 3 files changed, 116 insertions(+), 61 deletions(-) create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java delete mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/operations/IfOperation.java diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java new file mode 100644 index 0000000..c3580f7 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/ConditionalOperation.java @@ -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+"}"); + } + } +} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/IfOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/IfOperation.java deleted file mode 100644 index 4a13002..0000000 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/IfOperation.java +++ /dev/null @@ -1,57 +0,0 @@ -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; - -//ContainerOperation -public class IfOperation extends OperationContainer{ - - private static Pattern PATTERN = Pattern.compile("^(\\s*if\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$"); - - private String condition; //replace by Operation - - public IfOperation(){} - - @Override - public int parse(String content, CleanerPool cleaner) throws Exception{ - System.out.println("IF STATEMENT"); - CleanerPool generic = new CleanerPool( - new Cleaner("GENERIC_PARENTHESIS",'(',')'), - new Cleaner("GENERIC_FUNCTION", '{','}')); - content = generic.clean(content); - System.out.println("after clean "+content); - - Matcher matcher = 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); - 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+"if("+condition+"){"); - for(JavaElement child : getChilds()) child.show(tab+1); - System.out.println(start+"}"); - } - -} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java index c4b9ddd..9eca205 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java @@ -6,9 +6,12 @@ import java.util.Map.Entry; import java.util.regex.Pattern; 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.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{ @@ -25,6 +28,8 @@ public class OperationFactory{ this.patterns.put(Pattern.compile("^\\s*return\\s*[^;]*;.*$"), ReturnOperation.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); this.patterns.put(Pattern.compile("^\\s*([^;=]+;).*$"), MethodCallOperation.class); @@ -40,14 +45,14 @@ public class OperationFactory{ * for * do while * while - * if + * if OK * switch case - * return OK (without just return) + * return OK * continue * break * try catch finally * throw - * else + * else OK * synchronized * instance of *