This commit is contained in:
jeffcheasey88 2023-05-02 14:31:21 +02:00
parent 813ebda6cb
commit 5d27475a9b
5 changed files with 46 additions and 29 deletions

View file

@ -1,14 +1,18 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CleanerPool{
private List<Cleaner> cleaners;
public CleanerPool(List<Cleaner> cleaners){
this.cleaners = new ArrayList<>(cleaners);
public CleanerPool(Cleaner... cleaners){
this.cleaners = Arrays.asList(cleaners);
}
public String clean(String value){
@ -16,22 +20,29 @@ public class CleanerPool{
return value;
}
public boolean isConstant(String value){
for(Cleaner cleaner : this.cleaners) if(value.startsWith(cleaner.getPattern())) return true;
return false;
public String unzip(String value){
return unzip(value, (s) -> s);
}
public String getConstant(String value){
public String unzip(String value, Function<String, String> modifier){
boolean edited = false;
for(Cleaner cleaner : this.cleaners){
if(value.startsWith(cleaner.getPattern())){
return cleaner.getConstant(value);
Matcher matcher = cleaner.getMatcher(value);
if(matcher.matches()){
String key = matcher.group(2);
String zip = cleaner.getConstant(key);
value = matcher.group(1)+cleaner.open+modifier.apply(zip)+cleaner.close+matcher.group(3);
edited = true;
}
}
return null;
if(edited) return unzip(value, modifier);
return value;
}
public static class Cleaner{
private Pattern rPattern;
private String pattern;
private char open;
private char close;
@ -39,12 +50,17 @@ public class CleanerPool{
public List<String> constants;
public Cleaner(String pattern, char open, char close){
this.pattern = pattern;
this.pattern = "$"+pattern;
this.open = open;
this.close = close;
this.rPattern = Pattern.compile("^(.*)(\\$"+pattern+"\\d+)(.*)$");
this.constants = new ArrayList<>();
}
public Matcher getMatcher(String value){
return this.rPattern.matcher(value);
}
public String getPattern(){
return this.pattern;
}

View file

@ -54,7 +54,7 @@ public class JavaParser {
}
this.clazz = new Class();
index = this.clazz.parse(content, new CleanerPool(Arrays.asList(new Cleaner("$GENERIC_TYPE_",'<','>'))));
index = this.clazz.parse(content, new CleanerPool());
content = content.substring(index);
}

View file

@ -1,10 +1,11 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class Variable {
private static Pattern PATTERN = Pattern.compile("^(\\s*)(.*)$");
@ -22,6 +23,11 @@ public class Variable {
}
public int parse(String content, CleanerPool cleaner) throws Exception{
System.out.println("parse "+content);
CleanerPool generic = new CleanerPool(new Cleaner("GENERIC_TYPE_",'<','>'));
content = generic.clean(content);
System.out.println("clean "+content);
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
@ -35,19 +41,20 @@ public class Variable {
body = body.substring(0, min);
if(equals < quote && equals < quotes){
assigment(body);
assigment(body, generic);
}else{
onlyDefine(body);
onlyDefine(body, generic);
}
return offset+min;
}
private void assigment(String content){
private void assigment(String content, CleanerPool cleaner){
}
private void onlyDefine(String content){
content = generiqueTypes(content);
private void onlyDefine(String content, CleanerPool cleaner){
System.out.println("onlyDefine "+content);
content = generiqueTypes(content, cleaner);
System.out.println(content);
String[] values = content.split("\\s+");
for(String value : values){
@ -64,8 +71,8 @@ public class Variable {
}
}
private String generiqueTypes(String content){
return content;
private String generiqueTypes(String content, CleanerPool cleaner){
return cleaner.unzip(content, (s) -> s.replaceAll("\\s+", "")).replaceAll(">(?<varname>[^>\\d,;])", "> ${varname}");
}
private int indexOf(String value, String target){

View file

@ -12,14 +12,8 @@ public class CleanerTest {
@Test
void cutter(){
CleanerPool cleaner = new CleanerPool(Arrays.asList(new Cleaner("$TEST",'<','>')));
CleanerPool cleaner = new CleanerPool();
String result = cleaner.clean("Test0< List< Map< Test1, List< Test2 >, Test3>> > ");
assertEquals("Test0$TEST3 ", result);
}
@Test
void cutterLittle(){
CleanerPool cleaner = new CleanerPool(Arrays.asList(new Cleaner("$TEST",'<','>')));
String result = cleaner.clean("Test0<Keske, wtf<mais>> ");
assertEquals("Test0$TEST1 ", result);
}
}

View file

@ -30,7 +30,7 @@ class VariableTest{
@BeforeAll
void init(){
this.cleaner = new CleanerPool(Arrays.asList(new Cleaner("$GENERIC_TYPE_",'<','>')));
this.cleaner = new CleanerPool();
}
@Test
@ -137,7 +137,7 @@ class VariableTest{
void case7(){
try {
Class clazz = new Class();
clazz.parse(cleaner.clean("public class Test{ int i ,j,k; int l=i=k=l=4; } "), new CleanerPool(Arrays.asList(new Cleaner("$GENERIC_TYPE",'<','>'))));
clazz.parse(cleaner.clean("public class Test{ int i ,j,k; int l=i=k=l=4; } "), new CleanerPool());
List<Variable> vars = clazz.getVariables();
assertEquals(vars.size(), 4);