Fix Cleaner
This commit is contained in:
parent
d2fd5aeff1
commit
813ebda6cb
6 changed files with 49 additions and 39 deletions
|
@ -6,7 +6,7 @@ import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class Class {
|
public class Class{
|
||||||
|
|
||||||
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
|
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
|
||||||
|
|
||||||
|
@ -18,7 +18,9 @@ public class Class {
|
||||||
|
|
||||||
public 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 matcher = PATTERN.matcher(content);
|
||||||
matcher.matches();
|
matcher.matches();
|
||||||
|
|
||||||
|
@ -42,7 +44,7 @@ public class Class {
|
||||||
Variable last = null;
|
Variable last = null;
|
||||||
do {
|
do {
|
||||||
Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType());
|
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);
|
this.vars.add(variable);
|
||||||
content = content.substring(index);
|
content = content.substring(index);
|
||||||
quote = content.startsWith(",");
|
quote = content.startsWith(",");
|
||||||
|
@ -57,7 +59,7 @@ public class Class {
|
||||||
Variable last = null;
|
Variable last = null;
|
||||||
do {
|
do {
|
||||||
Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType());
|
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);
|
this.vars.add(variable);
|
||||||
content = content.substring(index);
|
content = content.substring(index);
|
||||||
quote = content.startsWith(",");
|
quote = content.startsWith(",");
|
||||||
|
|
|
@ -57,7 +57,7 @@ public class CleanerPool{
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
for(int i = 0; i < value.length(); i++){
|
for(int i = 0; i < value.length(); i++){
|
||||||
char c = value.charAt(i);
|
char c = value.charAt(i);
|
||||||
if(c == '<'){
|
if(c == open){
|
||||||
i+=cutOpenable(value.substring(i+1), constants, pattern, open, close);
|
i+=cutOpenable(value.substring(i+1), constants, pattern, open, close);
|
||||||
builder.append(pattern+(constants.size()-1));
|
builder.append(pattern+(constants.size()-1));
|
||||||
}else{
|
}else{
|
||||||
|
@ -69,21 +69,21 @@ public class CleanerPool{
|
||||||
|
|
||||||
private int cutOpenable(String value, List<String> constants, String pattern, char open, char close){
|
private int cutOpenable(String value, List<String> constants, String pattern, char open, char close){
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
|
int i = 0;
|
||||||
for(int i = 0; i < value.length(); i++){
|
for(;i < value.length(); i++){
|
||||||
char c = value.charAt(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);
|
i+=cutOpenable(value.substring(i+1), constants, pattern, open, close);
|
||||||
builder.append(pattern+(constants.size()-1));
|
builder.append(pattern+(constants.size()-1));
|
||||||
}else if(c == close){
|
|
||||||
break;
|
|
||||||
}else{
|
}else{
|
||||||
builder.append(c);
|
builder.append(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
constants.add(builder.toString());
|
constants.add(builder.toString());
|
||||||
return builder.length()+1;
|
return i+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,11 @@ import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
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 main(String[] args) throws Exception {
|
||||||
|
@ -51,7 +54,7 @@ public class JavaParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.clazz = new Class();
|
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);
|
content = content.substring(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ public class Variable {
|
||||||
this.type = type;
|
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 matcher = PATTERN.matcher(content);
|
||||||
matcher.matches();
|
matcher.matches();
|
||||||
|
|
||||||
|
@ -65,20 +65,7 @@ public class Variable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String generiqueTypes(String content){
|
private String generiqueTypes(String content){
|
||||||
System.out.println(content);
|
return 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int indexOf(String value, String target){
|
private int indexOf(String value, String target){
|
||||||
|
|
|
@ -13,8 +13,13 @@ public class CleanerTest {
|
||||||
@Test
|
@Test
|
||||||
void cutter(){
|
void cutter(){
|
||||||
CleanerPool cleaner = new CleanerPool(Arrays.asList(new Cleaner("$TEST",'<','>')));
|
CleanerPool cleaner = new CleanerPool(Arrays.asList(new Cleaner("$TEST",'<','>')));
|
||||||
String result = cleaner.clean("test<Test<Lol>>");
|
String result = cleaner.clean("Test0< List< Map< Test1, List< Test2 >, Test3>> > ");
|
||||||
assertEquals("test$TEST1", result);
|
assertEquals("Test0$TEST3 ", result);
|
||||||
assertEquals("Lol", cleaner.getConstant("$TEST0"));
|
}
|
||||||
|
@Test
|
||||||
|
void cutterLittle(){
|
||||||
|
CleanerPool cleaner = new CleanerPool(Arrays.asList(new Cleaner("$TEST",'<','>')));
|
||||||
|
String result = cleaner.clean("Test0<Keske, wtf<mais>> ");
|
||||||
|
assertEquals("Test0$TEST1 ", result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.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.Test;
|
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;
|
import be.jeffcheasey88.peeratcode.parser.java.Variable.Value;
|
||||||
|
|
||||||
class VariableTest {
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
|
class VariableTest{
|
||||||
|
|
||||||
//int i = 4;
|
//int i = 4;
|
||||||
//int i,j,k,l=1;
|
//int i,j,k,l=1;
|
||||||
|
@ -20,11 +26,18 @@ class VariableTest {
|
||||||
//Test<Test,K,L> j = new Test().schedule(p -> { return true;});
|
//Test<Test,K,L> j = new Test().schedule(p -> { return true;});
|
||||||
//int i =j=k=l=4;
|
//int i =j=k=l=4;
|
||||||
|
|
||||||
|
private CleanerPool cleaner;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
void init(){
|
||||||
|
this.cleaner = new CleanerPool(Arrays.asList(new Cleaner("$GENERIC_TYPE_",'<','>')));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void case1(){
|
void case1(){
|
||||||
try {
|
try {
|
||||||
Variable variable = new Variable();
|
Variable variable = new Variable();
|
||||||
variable.parse(" int i = 4 ; ");
|
variable.parse(cleaner.clean(" int i = 4 ; "), cleaner);
|
||||||
|
|
||||||
assertEquals(0, variable.getModifier());
|
assertEquals(0, variable.getModifier());
|
||||||
assertEquals("int", variable.getType());
|
assertEquals("int", variable.getType());
|
||||||
|
@ -39,7 +52,7 @@ class VariableTest {
|
||||||
void case2(){
|
void case2(){
|
||||||
try {
|
try {
|
||||||
Variable variable = new Variable();
|
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(JavaParser.getModifier("public")+JavaParser.getModifier("static"), variable.getModifier());
|
||||||
assertEquals("int", variable.getType());
|
assertEquals("int", variable.getType());
|
||||||
|
@ -54,7 +67,7 @@ class VariableTest {
|
||||||
void case3(){
|
void case3(){
|
||||||
try {
|
try {
|
||||||
Variable variable = new Variable();
|
Variable variable = new Variable();
|
||||||
variable.parse(" int lm ; ");
|
variable.parse(cleaner.clean(" int lm ; "), cleaner);
|
||||||
|
|
||||||
assertEquals(0, variable.getModifier());
|
assertEquals(0, variable.getModifier());
|
||||||
assertEquals("int", variable.getType());
|
assertEquals("int", variable.getType());
|
||||||
|
@ -69,10 +82,10 @@ class VariableTest {
|
||||||
void case4(){
|
void case4(){
|
||||||
try {
|
try {
|
||||||
Variable variable = new Variable();
|
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(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());
|
assertEquals("t", variable.getName());
|
||||||
assertNull(variable.getValue());
|
assertNull(variable.getValue());
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
|
@ -84,7 +97,7 @@ class VariableTest {
|
||||||
void case5(){
|
void case5(){
|
||||||
try {
|
try {
|
||||||
Variable variable = new Variable();
|
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(0, variable.getModifier());
|
||||||
assertEquals("int", variable.getType());
|
assertEquals("int", variable.getType());
|
||||||
|
@ -99,7 +112,7 @@ class VariableTest {
|
||||||
void case6(){
|
void case6(){
|
||||||
try {
|
try {
|
||||||
Class clazz = new Class();
|
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();
|
List<Variable> vars = clazz.getVariables();
|
||||||
assertEquals(vars.size(), 4);
|
assertEquals(vars.size(), 4);
|
||||||
|
@ -124,7 +137,7 @@ class VariableTest {
|
||||||
void case7(){
|
void case7(){
|
||||||
try {
|
try {
|
||||||
Class clazz = new Class();
|
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();
|
List<Variable> vars = clazz.getVariables();
|
||||||
assertEquals(vars.size(), 4);
|
assertEquals(vars.size(), 4);
|
||||||
|
|
Loading…
Add table
Reference in a new issue