Refractor Parser (begin) & Route Extracter

This commit is contained in:
jeffcheasey88 2023-04-15 21:25:55 +02:00
parent 39a56ca16c
commit 4db724e2af
5 changed files with 91 additions and 101 deletions

View file

@ -11,6 +11,7 @@ import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import be.jeffcheasey88.peeratcode.bonus.discord.Bot;
import be.jeffcheasey88.peeratcode.bonus.extract.RouteExtracter;
import be.jeffcheasey88.peeratcode.framework.Client;
import be.jeffcheasey88.peeratcode.framework.HttpReader;
import be.jeffcheasey88.peeratcode.framework.HttpUtil;
@ -68,6 +69,8 @@ public class Main{
});
initRoutes(router, config);
// RouteExtracter extracter = new RouteExtracter(router);
// extracter.extract();
startWebServer(config, router);
}

View file

@ -0,0 +1,33 @@
package be.jeffcheasey88.peeratcode.bonus.extract;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Map.Entry;
import be.jeffcheasey88.peeratcode.framework.RequestType;
import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.Router;
//A noter que le but est d'extraire des informations sans modifier le code source,
//comme les tests unitaire, ici je veux générer un élément textuel qui me donnera l'état des routes,
//je ne vais donc pas modifier la classe router pour donner un accès au route.
public class RouteExtracter {
private Router router;
public RouteExtracter(Router router){
this.router = router;
}
public void extract() throws Exception{
Field field = Router.class.getDeclaredField("responses");
field.setAccessible(true);
Map<RequestType, Map<Response, Route>> responses = (Map<RequestType, Map<Response, Route>>) field.get(this.router);
for(Entry<RequestType, Map<Response, Route>> types : responses.entrySet()){
for(Entry<Response, Route> routes : types.getValue().entrySet()){
System.out.println("["+types.getKey()+"] "+routes.getValue().path());
}
}
}
}

View file

@ -13,8 +13,8 @@ public class Class {
private int modifier;
private String name;
private List<Variable>vars;
private List<Function>functions;
private List<Variable> vars;
private List<Function> functions;
public Class(){}

View file

@ -1,6 +1,7 @@
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;
@ -20,118 +21,71 @@ public class Variable {
this.type = type;
}
//int i = 4;
//int i,j,k,l=1;
//int lm ;
//public static int l;
//Test<Test>t;
//Test<Test,K,L> j = new Test().schedule(p -> { return true;});
//int i =j=k=l=4;
public int parse(String content) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
int offset = matcher.group(1).length();
boolean hasEquals = false;
boolean fromMinus = false;
String body = matcher.group(2);
while(body.length() > 0){
while(indexOf(body, "\\s+") == 0){
body = body.substring(1);
offset++;
}
int space = indexOf(body, "\\s+");
int equals = indexOf(body, "=");
int quote = indexOf(body,",");
int quotes = indexOf(body, ";");
int minus = indexOf(body, "<");
int min = min(space, equals, quote, quotes, minus);
String value = body.substring(0,min);
if(hasEquals){
this.value = new Value(value);
body = body.substring(value.length()+1);
offset+=value.length()+1;
break;
}else if(fromMinus){
this.name = value;
body = body.substring(value.length()+1);
offset+=value.length()+1;
break;
} else if(min == space){
int mod = JavaParser.getModifier(value);
if(mod > 0){
this.modifier+=mod;
}else{
if(type == null){
this.type = value;
}else if(name == null){
this.name = value;
}
}
body = body.substring(value.length()+1);
offset+=value.length()+1;
}else if(min == equals){
if(this.name == null) this.name = value;
hasEquals = true;
body = body.substring(value.length()+1);
offset+=value.length()+1;
}else if(min == minus){
value = value+"<";
int maxus = 1;
while(maxus > 0){
char current = body.charAt(value.length());
value+=current;
if(current == '<'){
maxus++;
}
if(current == '>'){
maxus--;
}
}
this.type = value;
body = body.substring(value.length());
offset+=value.length();
while(indexOf(body, "\\s+") == 0){
body = body.substring(1);
offset++;
}
fromMinus = true;
}else if(min == quote || min == quotes){
if(this.name != null) break;
this.name = value;
body = body.substring(value.length());
offset+=value.length();
if(min == quotes){
body = body.substring(1);
offset+=1;
}
break;
}else {
offset+=value.length()+1;
break;
}
int equals = indexOf(body, "=");
int quote = indexOf(body,",");
int quotes = indexOf(body, ";");
int min = Math.min(quote, quotes);
body = body.substring(0, min);
if(equals < quote && equals < quotes){
assigment(body);
}else{
onlyDefine(body);
}
return offset;
return offset+min;
}
private void assigment(String content){
}
private void onlyDefine(String content){
content = generiqueTypes(content);
System.out.println(content);
String[] values = content.split("\\s+");
for(String value : values){
int modifier = JavaParser.getModifier(value);
if(modifier > 0){
this.modifier+=modifier;
continue;
}
if(this.type == null){
this.type = value;
continue;
}
this.name = value;
}
}
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;
}
System.out.println(result);
result = result.replaceAll("(>\\s*)", "> ").replace("> >", ">>");
return result;
}
private int indexOf(String value, String target){
return value.split(target)[0].length();
}
private int min(int... mins){
int result = mins[0];
for(int min : mins){
if(min < result) result = min;
}
return result;
}
public int getModifier(){
return this.modifier;
}

View file

@ -69,10 +69,10 @@ class VariableTest {
void case4(){
try {
Variable variable = new Variable();
variable.parse("Testas<Test>t; ");
variable.parse("Testas< List< Map< Test, List< Test >, Test>> >t; ");
assertEquals(0, variable.getModifier());
assertEquals("Testas<Test>", variable.getType());
assertEquals("Testas<List<Map<Test,List<Test>,Test>>>", variable.getType());
assertEquals("t", variable.getName());
assertNull(variable.getValue());
}catch(Exception e){