Fix Cleaner

This commit is contained in:
jeffcheasey88 2023-05-02 10:34:48 +02:00
parent d2fd5aeff1
commit 813ebda6cb
6 changed files with 49 additions and 39 deletions

View file

@ -6,7 +6,7 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Class {
public class Class{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
@ -18,7 +18,9 @@ public class Class {
public Class(){}
public int parse(String content) throws Exception{
public int parse(String content, CleanerPool cleaner) throws Exception{
content = cleaner.clean(content);
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
@ -42,7 +44,7 @@ public class Class {
Variable last = null;
do {
Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType());
int index = variable.parse(content);
int index = variable.parse(content, cleaner);
this.vars.add(variable);
content = content.substring(index);
quote = content.startsWith(",");
@ -57,7 +59,7 @@ public class Class {
Variable last = null;
do {
Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType());
int index = variable.parse(content);
int index = variable.parse(content, cleaner);
this.vars.add(variable);
content = content.substring(index);
quote = content.startsWith(",");

View file

@ -57,7 +57,7 @@ public class CleanerPool{
StringBuilder builder = new StringBuilder();
for(int i = 0; i < value.length(); i++){
char c = value.charAt(i);
if(c == '<'){
if(c == open){
i+=cutOpenable(value.substring(i+1), constants, pattern, open, close);
builder.append(pattern+(constants.size()-1));
}else{
@ -69,21 +69,21 @@ public class CleanerPool{
private int cutOpenable(String value, List<String> constants, String pattern, char open, char close){
StringBuilder builder = new StringBuilder();
for(int i = 0; i < value.length(); i++){
int i = 0;
for(;i < value.length(); i++){
char c = value.charAt(i);
if(c == open){
if(c == close){
break;
}else if(c == open){
i+=cutOpenable(value.substring(i+1), constants, pattern, open, close);
builder.append(pattern+(constants.size()-1));
}else if(c == close){
break;
}else{
builder.append(c);
}
}
constants.add(builder.toString());
return builder.length()+1;
return i+1;
}
}

View file

@ -5,8 +5,11 @@ import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class JavaParser {
public static void main(String[] args) throws Exception {
@ -51,7 +54,7 @@ public class JavaParser {
}
this.clazz = new Class();
index = this.clazz.parse(content);
index = this.clazz.parse(content, new CleanerPool(Arrays.asList(new Cleaner("$GENERIC_TYPE_",'<','>'))));
content = content.substring(index);
}

View file

@ -21,7 +21,7 @@ public class Variable {
this.type = type;
}
public int parse(String content) throws Exception{
public int parse(String content, CleanerPool cleaner) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
@ -65,20 +65,7 @@ public class Variable {
}
private String generiqueTypes(String content){
System.out.println(content);
String result = "";
int opened = 0;
for(char c : content.toCharArray()){
if(c == '<') opened++;
else if(c == '>') opened--;
if(opened > 0){
if(Character.isWhitespace(c)) continue;
}
result+=c;
}
result = result.replaceAll("(>\\s*)", "> ").replace("> >", ">>");
return result;
return content;
}
private int indexOf(String value, String target){

View file

@ -13,8 +13,13 @@ public class CleanerTest {
@Test
void cutter(){
CleanerPool cleaner = new CleanerPool(Arrays.asList(new Cleaner("$TEST",'<','>')));
String result = cleaner.clean("test<Test<Lol>>");
assertEquals("test$TEST1", result);
assertEquals("Lol", cleaner.getConstant("$TEST0"));
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

@ -4,13 +4,19 @@ 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;
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;
class VariableTest {
@TestInstance(Lifecycle.PER_CLASS)
class VariableTest{
//int i = 4;
//int i,j,k,l=1;
@ -20,11 +26,18 @@ class VariableTest {
//Test<Test,K,L> j = new Test().schedule(p -> { return true;});
//int i =j=k=l=4;
private CleanerPool cleaner;
@BeforeAll
void init(){
this.cleaner = new CleanerPool(Arrays.asList(new Cleaner("$GENERIC_TYPE_",'<','>')));
}
@Test
void case1(){
try {
Variable variable = new Variable();
variable.parse(" int i = 4 ; ");
variable.parse(cleaner.clean(" int i = 4 ; "), cleaner);
assertEquals(0, variable.getModifier());
assertEquals("int", variable.getType());
@ -39,7 +52,7 @@ class VariableTest {
void case2(){
try {
Variable variable = new Variable();
variable.parse("public static int l ; ");
variable.parse(cleaner.clean("public static int l ; "), cleaner);
assertEquals(JavaParser.getModifier("public")+JavaParser.getModifier("static"), variable.getModifier());
assertEquals("int", variable.getType());
@ -54,7 +67,7 @@ class VariableTest {
void case3(){
try {
Variable variable = new Variable();
variable.parse(" int lm ; ");
variable.parse(cleaner.clean(" int lm ; "), cleaner);
assertEquals(0, variable.getModifier());
assertEquals("int", variable.getType());
@ -69,10 +82,10 @@ class VariableTest {
void case4(){
try {
Variable variable = new Variable();
variable.parse("Testas< List< Map< Test, List< Test >, Test>> >t; ");
variable.parse(cleaner.clean("Test0< List< Map< Test1, List< Test2 >, Test3>> >t; "), cleaner);
assertEquals(0, variable.getModifier());
assertEquals("Testas<List<Map<Test,List<Test>,Test>>>", variable.getType());
assertEquals("Test0<List<Map<Test1,List<Test2>,Test3>>>", variable.getType());
assertEquals("t", variable.getName());
assertNull(variable.getValue());
}catch(Exception e){
@ -84,7 +97,7 @@ class VariableTest {
void case5(){
try {
Variable variable = new Variable();
variable.parse(" int i,j,k,l=1; ");
variable.parse(cleaner.clean(" int i,j,k,l=1; "), cleaner);
assertEquals(0, variable.getModifier());
assertEquals("int", variable.getType());
@ -99,7 +112,7 @@ class VariableTest {
void case6(){
try {
Class clazz = new Class();
clazz.parse("public class Test{ int i ,j,k,l=1; } ");
clazz.parse(cleaner.clean("public class Test{ int i ,j,k,l=1; } "), cleaner);
List<Variable> vars = clazz.getVariables();
assertEquals(vars.size(), 4);
@ -124,7 +137,7 @@ class VariableTest {
void case7(){
try {
Class clazz = new Class();
clazz.parse("public class Test{ int i ,j,k; int l=i=k=l=4; } ");
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",'<','>'))));
List<Variable> vars = clazz.getVariables();
assertEquals(vars.size(), 4);