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

View file

@ -54,7 +54,7 @@ public class JavaParser {
} }
this.clazz = new Class(); 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); content = content.substring(index);
} }

View file

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

View file

@ -12,14 +12,8 @@ public class CleanerTest {
@Test @Test
void cutter(){ 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>> > "); String result = cleaner.clean("Test0< List< Map< Test1, List< Test2 >, Test3>> > ");
assertEquals("Test0$TEST3 ", result); 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 @BeforeAll
void init(){ void init(){
this.cleaner = new CleanerPool(Arrays.asList(new Cleaner("$GENERIC_TYPE_",'<','>'))); this.cleaner = new CleanerPool();
} }
@Test @Test
@ -137,7 +137,7 @@ class VariableTest{
void case7(){ void case7(){
try { try {
Class clazz = new Class(); 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(); List<Variable> vars = clazz.getVariables();
assertEquals(vars.size(), 4); assertEquals(vars.size(), 4);