From 57132f320a4c2176c90cd9e96a7e9215cdb3e395 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Tue, 9 May 2023 09:02:36 +0200 Subject: [PATCH] Remove unwanted space & Fix space case on array define --- .../peeratcode/parser/java/Class.java | 20 ++--------- .../peeratcode/parser/java/Function.java | 2 +- .../peeratcode/parser/java/JavaParser.java | 30 +++++++++++++++-- .../peeratcode/parser/java/Variable.java | 30 ++++++++++------- .../peeratcode/parser/java/VariableTest.java | 33 +++++++++++++++++-- 5 files changed, 81 insertions(+), 34 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index f041fc6..7f7c34b 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -39,7 +39,7 @@ public class Class{ int quotes = indexOf(content,";"); int braces = indexOf(content,"\\{"); int equals = indexOf(content,"="); - if(quotes < braces && quotes < equals){ + if((quotes < braces && quotes < equals) || (equals < braces)){ boolean quote = false; Variable variable = null; do { @@ -51,27 +51,13 @@ public class Class{ if(quote) content = content.substring(1); }while(quote); content = content.substring(1); - }else if(equals < braces){ - //variable with value - boolean quote = false; - Variable variable = null; - do { - variable = (variable == null) ? new Variable() : new Variable(variable.getModifier(), variable.getType()); - int index = variable.parse(content, cleaner); - this.vars.add(variable); - content = content.substring(index); - System.out.println("CONTENT IS NOW "+content); - quote = content.startsWith(","); - if(quote) content = content.substring(1); - }while(quote); - content = content.substring(1); }else{ - System.out.println("Function "+content); +// System.out.println("Function "+content); Function func = new Function(); int index = func.parse(content); this.functions.add(func); content = content.substring(index); - System.out.println("End "+content); +// System.out.println("End "+content); } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java index f849436..98b3cc8 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -71,7 +71,7 @@ public class Function { 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+" {"); + 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); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index 5df2042..975733f 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -10,9 +10,9 @@ import java.util.List; 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 maine(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)); @@ -22,6 +22,32 @@ public class JavaParser { parser.show(); } + public static void main(String[] args) throws Exception { + File dir = new File("C:\\Users\\jeffc\\eclipse-workspace\\"); + show(dir); + } + + private static void show(File dir) throws Exception{ + if(dir.isFile()){ + if(!dir.getName().endsWith(".java")) return; + System.out.println("| "+dir.getAbsolutePath()); + BufferedReader reader = new BufferedReader(new FileReader(dir)); + + try { + JavaParser parser = new JavaParser(reader); + parser.parse(); + + Class clazz = parser.getClazz(); + System.out.println(clazz.getName()); + for(Variable variable : clazz.getVariables()){ + variable.show(1); + } + }catch(Exception e) {} + }else{ + for(File file : dir.listFiles()) show(file); + } + } + private Package pack; private List imports; private Class clazz; diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java index fcab8a3..6110daf 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -31,7 +31,6 @@ public class Variable { new Cleaner("GENERIC_FUNCTION", '{','}'), new Cleaner("GENERIC_PARENTHESIS",'(',')')); content = generic.clean(content); - System.out.println("clean "+content); Matcher matcher = PATTERN.matcher(content); matcher.matches(); @@ -50,16 +49,14 @@ public class Variable { }else{ onlyDefine(body, generic); } - show(1); + return offset+generic.unzip(body, ((s,p) -> s)).length(); } private void assigment(String content, CleanerPool cleaner){ System.out.println("assigment "+content); Iterator values = onlyDefine(content, cleaner); - System.out.println("b"); if(!values.hasNext()) return; - System.out.println("ah"); values.next(); if(!values.hasNext()) return; String spacesValue = values.next(); @@ -68,6 +65,7 @@ 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; @@ -86,15 +84,25 @@ public class Variable { return values; } + 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 static Pattern UNZIP_EQUALS_LEFT = Pattern.compile("(?[^=\\s])="); + private static Pattern UNZIP_EQUALS_RIGHT = Pattern.compile("=(?[^=\\s])"); + private String generiqueTypes(String content, CleanerPool cleaner){ - return cleaner. - unzip(content, (value, pattern) -> { + System.out.println("generic "+content); + String unzip = cleaner.unzip(content, (value, pattern) -> { return (pattern.equals("$GENERIC_FUNCTION")) ? null : value.replaceAll("\\s+", ""); - }). - replaceAll(">(?[^>\\d,;])", "> ${varname}"). - replaceAll("](?[^\\[\\d,;])", "] ${varname}"). - replaceAll("(?[^=\\s])=","${varname} ="). - replaceAll("=(?[^=\\s])","= ${varname}"); + }); + 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; } private int indexOf(String value, String target){ diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java b/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java index 52ae405..53edfdb 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; -import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.BeforeAll; @@ -12,7 +11,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; -import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner; import be.jeffcheasey88.peeratcode.parser.java.Variable.Value; @TestInstance(Lifecycle.PER_CLASS) @@ -196,7 +194,7 @@ class VariableTest{ assertEquals(0, variable.getModifier()); assertEquals("boolean", variable.getType()); assertEquals("i", variable.getName()); - assertEquals("j << 3 == 0", ((Value)variable.getValue()).value()); + assertEquals("j<< 3 == 0", ((Value)variable.getValue()).value()); }catch(Exception e){ fail(e); } @@ -277,4 +275,33 @@ class VariableTest{ } } + @Test + void case15(){ + try { + Variable variable = new Variable(); + variable.parse(cleaner.clean(" List list = new ArrayList <> () ; "), cleaner); + + assertEquals(0, variable.getModifier()); + assertEquals("List", variable.getType()); + assertEquals("list", variable.getName()); + assertEquals("new ArrayList<>()", ((Value)variable.getValue()).value()); + }catch(Exception e){ + fail(e); + } + } + + @Test + void case16(){ + try { + Variable variable = new Variable(); + variable.parse(cleaner.clean(" List a [] = new ArrayList [ 0] ; "), cleaner); + + assertEquals(0, variable.getModifier()); + assertEquals("List", variable.getType()); + assertEquals("a[]", variable.getName()); + assertEquals("new ArrayList[0]", ((Value)variable.getValue()).value()); + }catch(Exception e){ + fail(e); + } + } }