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 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);
}
}

View file

@ -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);

View file

@ -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<Import> imports;
private Class clazz;

View file

@ -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<String> 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<String> onlyDefine(String content, CleanerPool cleaner){
System.out.println("define "+content);
content = generiqueTypes(content, cleaner);
Iterator<String> 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+(?<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){
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(">(?<varname>[^>\\d,;])", "> ${varname}").
replaceAll("](?<varname>[^\\[\\d,;])", "] ${varname}").
replaceAll("(?<varname>[^=\\s])=","${varname} =").
replaceAll("=(?<varname>[^=\\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){

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.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 <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);
}
}
}