Refractor Parser (begin) & Route Extracter
This commit is contained in:
parent
39a56ca16c
commit
4db724e2af
5 changed files with 91 additions and 101 deletions
|
@ -11,6 +11,7 @@ import javax.net.ssl.SSLServerSocket;
|
||||||
import javax.net.ssl.SSLServerSocketFactory;
|
import javax.net.ssl.SSLServerSocketFactory;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.bonus.discord.Bot;
|
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.Client;
|
||||||
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||||
import be.jeffcheasey88.peeratcode.framework.HttpUtil;
|
import be.jeffcheasey88.peeratcode.framework.HttpUtil;
|
||||||
|
@ -68,6 +69,8 @@ public class Main{
|
||||||
});
|
});
|
||||||
|
|
||||||
initRoutes(router, config);
|
initRoutes(router, config);
|
||||||
|
// RouteExtracter extracter = new RouteExtracter(router);
|
||||||
|
// extracter.extract();
|
||||||
|
|
||||||
startWebServer(config, router);
|
startWebServer(config, router);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,8 +13,8 @@ public class Class {
|
||||||
private int modifier;
|
private int modifier;
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private List<Variable>vars;
|
private List<Variable> vars;
|
||||||
private List<Function>functions;
|
private List<Function> functions;
|
||||||
|
|
||||||
public Class(){}
|
public Class(){}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
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;
|
||||||
|
|
||||||
|
@ -20,118 +21,71 @@ public class Variable {
|
||||||
this.type = type;
|
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{
|
public int parse(String content) throws Exception{
|
||||||
Matcher matcher = PATTERN.matcher(content);
|
Matcher matcher = PATTERN.matcher(content);
|
||||||
matcher.matches();
|
matcher.matches();
|
||||||
|
|
||||||
int offset = matcher.group(1).length();
|
int offset = matcher.group(1).length();
|
||||||
|
|
||||||
boolean hasEquals = false;
|
|
||||||
boolean fromMinus = false;
|
|
||||||
String body = matcher.group(2);
|
String body = matcher.group(2);
|
||||||
while(body.length() > 0){
|
int equals = indexOf(body, "=");
|
||||||
while(indexOf(body, "\\s+") == 0){
|
int quote = indexOf(body,",");
|
||||||
body = body.substring(1);
|
int quotes = indexOf(body, ";");
|
||||||
offset++;
|
int min = Math.min(quote, quotes);
|
||||||
}
|
body = body.substring(0, min);
|
||||||
int space = indexOf(body, "\\s+");
|
|
||||||
int equals = indexOf(body, "=");
|
if(equals < quote && equals < quotes){
|
||||||
int quote = indexOf(body,",");
|
assigment(body);
|
||||||
int quotes = indexOf(body, ";");
|
}else{
|
||||||
int minus = indexOf(body, "<");
|
onlyDefine(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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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){
|
private int indexOf(String value, String target){
|
||||||
return value.split(target)[0].length();
|
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(){
|
public int getModifier(){
|
||||||
return this.modifier;
|
return this.modifier;
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,10 +69,10 @@ class VariableTest {
|
||||||
void case4(){
|
void case4(){
|
||||||
try {
|
try {
|
||||||
Variable variable = new Variable();
|
Variable variable = new Variable();
|
||||||
variable.parse("Testas<Test>t; ");
|
variable.parse("Testas< List< Map< Test, List< Test >, Test>> >t; ");
|
||||||
|
|
||||||
assertEquals(0, variable.getModifier());
|
assertEquals(0, variable.getModifier());
|
||||||
assertEquals("Testas<Test>", variable.getType());
|
assertEquals("Testas<List<Map<Test,List<Test>,Test>>>", variable.getType());
|
||||||
assertEquals("t", variable.getName());
|
assertEquals("t", variable.getName());
|
||||||
assertNull(variable.getValue());
|
assertNull(variable.getValue());
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
|
|
Loading…
Add table
Reference in a new issue