Remove unwanted space & Fix space case on array define

This commit is contained in:
jeffcheasey88 2023-05-09 09:02:36 +02:00
parent 5754720fe5
commit 57132f320a
5 changed files with 81 additions and 34 deletions

View file

@ -39,7 +39,7 @@ public class Class{
int quotes = indexOf(content,";"); int quotes = indexOf(content,";");
int braces = indexOf(content,"\\{"); int braces = indexOf(content,"\\{");
int equals = indexOf(content,"="); int equals = indexOf(content,"=");
if(quotes < braces && quotes < equals){ if((quotes < braces && quotes < equals) || (equals < braces)){
boolean quote = false; boolean quote = false;
Variable variable = null; Variable variable = null;
do { do {
@ -51,27 +51,13 @@ public class Class{
if(quote) content = content.substring(1); if(quote) content = content.substring(1);
}while(quote); }while(quote);
content = content.substring(1); 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{ }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);
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

@ -71,7 +71,7 @@ public class Function {
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+" {"); System.out.println(start+Modifier.toString(modifier)+" "+name+"("+parameters+") "+exceptions+"{");
for(Operation o : this.operations) o.show(tab+1); for(Operation o : this.operations) o.show(tab+1);
System.out.println(); System.out.println();
for(Function f : this.functions) f.show(tab+1); for(Function f : this.functions) f.show(tab+1);

View file

@ -10,9 +10,9 @@ import java.util.List;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner; 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"); 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));
@ -22,6 +22,32 @@ public class JavaParser {
parser.show(); 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 Package pack;
private List<Import> imports; private List<Import> imports;
private Class clazz; private Class clazz;

View file

@ -31,7 +31,6 @@ public class Variable {
new Cleaner("GENERIC_FUNCTION", '{','}'), new Cleaner("GENERIC_FUNCTION", '{','}'),
new Cleaner("GENERIC_PARENTHESIS",'(',')')); new Cleaner("GENERIC_PARENTHESIS",'(',')'));
content = generic.clean(content); content = generic.clean(content);
System.out.println("clean "+content);
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.matches();
@ -50,16 +49,14 @@ public class Variable {
}else{ }else{
onlyDefine(body, generic); onlyDefine(body, generic);
} }
show(1);
return offset+generic.unzip(body, ((s,p) -> s)).length(); return offset+generic.unzip(body, ((s,p) -> s)).length();
} }
private void assigment(String content, CleanerPool cleaner){ private void assigment(String content, CleanerPool cleaner){
System.out.println("assigment "+content); System.out.println("assigment "+content);
Iterator<String> values = onlyDefine(content, cleaner); Iterator<String> values = onlyDefine(content, cleaner);
System.out.println("b");
if(!values.hasNext()) return; if(!values.hasNext()) return;
System.out.println("ah");
values.next(); values.next();
if(!values.hasNext()) return; if(!values.hasNext()) return;
String spacesValue = values.next(); String spacesValue = values.next();
@ -68,6 +65,7 @@ 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;
@ -86,15 +84,25 @@ public class Variable {
return values; return values;
} }
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 static Pattern UNZIP_EQUALS_LEFT = 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){
return cleaner. System.out.println("generic "+content);
unzip(content, (value, pattern) -> { String unzip = cleaner.unzip(content, (value, pattern) -> {
return (pattern.equals("$GENERIC_FUNCTION")) ? null : value.replaceAll("\\s+", ""); return (pattern.equals("$GENERIC_FUNCTION")) ? null : value.replaceAll("\\s+", "");
}). });
replaceAll(">(?<varname>[^>\\d,;])", "> ${varname}"). System.out.println("unzip "+unzip);
replaceAll("](?<varname>[^\\[\\d,;])", "] ${varname}"). unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}");
replaceAll("(?<varname>[^=\\s])=","${varname} ="). unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}");
replaceAll("=(?<varname>[^=\\s])","= ${varname}"); 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){ private int indexOf(String value, String target){

View file

@ -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.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.BeforeAll; 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;
import org.junit.jupiter.api.TestInstance.Lifecycle; import org.junit.jupiter.api.TestInstance.Lifecycle;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
import be.jeffcheasey88.peeratcode.parser.java.Variable.Value; import be.jeffcheasey88.peeratcode.parser.java.Variable.Value;
@TestInstance(Lifecycle.PER_CLASS) @TestInstance(Lifecycle.PER_CLASS)
@ -196,7 +194,7 @@ class VariableTest{
assertEquals(0, variable.getModifier()); assertEquals(0, variable.getModifier());
assertEquals("boolean", variable.getType()); assertEquals("boolean", variable.getType());
assertEquals("i", variable.getName()); assertEquals("i", variable.getName());
assertEquals("j << 3 == 0", ((Value)variable.getValue()).value()); assertEquals("j<< 3 == 0", ((Value)variable.getValue()).value());
}catch(Exception e){ }catch(Exception e){
fail(e); fail(e);
} }
@ -277,4 +275,33 @@ class VariableTest{
} }
} }
@Test
void case15(){
try {
Variable variable = new Variable();
variable.parse(cleaner.clean(" List <String> list = new ArrayList <> () ; "), cleaner);
assertEquals(0, variable.getModifier());
assertEquals("List<String>", 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 <String> a [] = new ArrayList [ 0] ; "), cleaner);
assertEquals(0, variable.getModifier());
assertEquals("List<String>", variable.getType());
assertEquals("a[]", variable.getName());
assertEquals("new ArrayList[0]", ((Value)variable.getValue()).value());
}catch(Exception e){
fail(e);
}
}
} }