Compare commits

...

2 commits

Author SHA1 Message Date
af3481fd4d Annotation -> Variable 2023-06-11 18:10:43 +02:00
cc6142562b Annotation -> Class 2023-06-11 17:12:52 +02:00
6 changed files with 97 additions and 26 deletions

View file

@ -26,11 +26,14 @@ public class Annotation extends JavaElement{
return matcher.group(1).length();
}
public String getType(){
return this.type;
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception {
super.build(buffer, tab);
buffer.append((s) -> s+="@"+type+(param == null ? "":param));
buffer.add("");
}
@Override

View file

@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
@ -13,22 +14,41 @@ public class Class extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
private List<Annotation> annotations;
private int modifier;
private String name;
private List<JavaElement> childs;
public Class(){}
public Class(){
this.annotations = new ArrayList<>();
}
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
String[] split = matcher.group(2).split("\\s+");
for(int i = 0; i < split.length-1; i++){
this.modifier+=JavaParser.getModifier(split[i]);
Iterator<String> values = new ArrayIterator<>(matcher.group(2).split("\\s+"));
String value = null;
int modifier;
while(values.hasNext() && ((value = values.next()).charAt(0) == '@')){
Annotation annotation = new Annotation();
annotation.parse(value, global, local);
this.annotations.add(annotation);
}
if((modifier = JavaParser.getModifier(value)) > 0){
do{
this.modifier+=modifier;
}while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0);
}
String type = value; //class interface enum
this.name = values.next();
if(values.hasNext()){
//extends implements
}
this.name = split[split.length-1];
this.childs = new ArrayList<>();
@ -100,6 +120,11 @@ public class Class extends JavaElement{
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
for(Annotation annotation : this.annotations){
annotation.build(buffer, tab);
buffer.add("");
}
super.build(buffer, tab);
buffer.append((s) -> s+=Modifier.toString(modifier)+" "+this.name+"{");
buffer.add("");

View file

@ -29,6 +29,8 @@ public class Function extends OperationContainer{
}
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
local = new CleanerPool(new Cleaner("GENERIC_PARENTHESIS", '(', ')'));
content = local.unzipOne(local.clean(content), (s,p) -> s);
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
@ -36,17 +38,16 @@ public class Function extends OperationContainer{
parameters(matcher.group(3)+";", global, local);
this.exceptions = matcher.group(4).trim();
local = new CleanerPool(
new Cleaner("GENERIC_FUNCTION", '{', '}'),
new Cleaner("GENERIC_PARENTHESIS", '(', ')'));
local = local.group(new CleanerPool(new Cleaner("GENERIC_FUNCTION", '{', '}')));
String zip = local.clean("{"+matcher.group(5));
String body = local.unzipOne(zip, (s,p) -> s);
String unzip = body.substring(1, body.indexOf('}'));
super.parse(local.clean(unzip), global, local);
return matcher.group(1).length()+local.unzip(unzip, ((s,p) -> s)).length()+1;
return local.unzip(matcher.group(1), (s,p) -> s).length()+local.unzip(unzip, ((s,p) -> s)).length()+1;
}
private static Pattern UNZIP_STICK = Pattern.compile("\\s+(?<e>[<|(|\\[|\"|'])");
@ -104,17 +105,28 @@ public class Function extends OperationContainer{
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
for(Annotation annotation : this.annotations) annotation.build(buffer, tab);
for(Annotation annotation : this.annotations){
annotation.build(buffer, tab);
buffer.add("");
}
String param = "";
for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+"";
if(!param.isEmpty()) param = param.substring(1);
final String paramMod = param;
boolean empty = getChilds().size() == 0;
super.build(buffer, tab);
buffer.append((s) -> s+=Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"("+paramMod+") "+exceptions+"{"+((empty ? "}":"")));
buffer.append((s) -> s+=Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"(");
boolean first = true;
for(Variable variable : this.parameters){
for(Annotation annotation : variable.getAnnotations()){
annotation.build(buffer, 0);
buffer.append((s) -> s+=" ");
}
if(first) first = false;
else{
buffer.append((s) -> s+=",");
}
buffer.append((s) -> s+=variable.getType()+" "+variable.getName());
}
buffer.append((s) -> s+=") "+exceptions+"{"+((empty ? "}":"")));
buffer.add("");
if(empty) return;

View file

@ -5,7 +5,7 @@ import java.util.regex.Pattern;
public class Import {
private static Pattern PATTERN = Pattern.compile("^\\s*(import\\s+([^;]*);)");
private static Pattern PATTERN = Pattern.compile("^(\\s*import\\s+([^;]*);)");
public static boolean isImport(String content){
return PATTERN.matcher(content).lookingAt();

View file

@ -24,6 +24,11 @@ public class JavaParser{
parser.parse(reader);
System.out.println("build-----------------");
parser.build(new BufferedWriter(new FileWriter(new File("/home/buildClazzFromParser.txt"))));
reader = new BufferedReader(new FileReader(new File("/home/buildClazzFromParser.txt")));
String line;
while((line = reader.readLine()) != null) System.out.println(line);
reader.close();
}
public static void mainee(String[] args) throws Exception {
@ -90,8 +95,9 @@ public class JavaParser{
line = cleaner.clean(line);
index = line.indexOf("//");
if(index >= 0) line = line.substring(0, index);
content+=line;
content+=line+" ";
}
System.out.println(content);
this.pack = new Package();
index = this.pack.parse(content);

View file

@ -14,14 +14,19 @@ public class Variable extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*)(.*)$");
private List<Annotation> annotations;
private int modifier;
private String name;
private String type;
private Variable value; //Change into operation or JavaElement
public Variable(){}
public Variable(){
this.annotations = new ArrayList<>();
}
public Variable(int modifier, String type){
this();
this.modifier = modifier;
this.type = type;
}
@ -57,7 +62,7 @@ public class Variable extends JavaElement{
return offset+local.unzipOne(body, (s,p) -> (p.equals("^GENERIC_TYPE") || p.equals("^GENERIC_ARRAY")) ? s : null).length();
}
private void assigment(String content, CleanerPool cleaner){
private void assigment(String content, CleanerPool cleaner) throws Exception{
Iterator<String> values = onlyDefine(content, cleaner);
if(!values.hasNext()) return;
values.next();
@ -67,13 +72,26 @@ public class Variable extends JavaElement{
this.value = new Value(spacesValue);
}
private Iterator<String> onlyDefine(String content, CleanerPool cleaner){
private Iterator<String> onlyDefine(String content, CleanerPool cleaner) throws Exception{
content = generiqueTypes(content, cleaner);
Iterator<String> values = new ArrayIterator<>(content.split("\\s+"));
CleanerPool tmp = new CleanerPool(new Cleaner("GENERIC_PARENTHESIS",'(',')'));
content = tmp.clean(content);
String[] array = content.split("\\s+");
// for(int i = 0; i < array.length; i++) array[i] = tmp.unzip(array[i], (s,p) -> s);
Iterator<String> values = new ArrayIterator<>(array);
String value = null;
int modifier;
while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0){
this.modifier+=modifier;
while(values.hasNext() && ((value = values.next()).charAt(0) == '@')){
Annotation annotation = new Annotation();
annotation.parse(value, cleaner, tmp);
this.annotations.add(annotation);
}
if((modifier = JavaParser.getModifier(value)) > 0){
do{
this.modifier+=modifier;
}while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0);
}
if(this.type == null){
this.type = value;
@ -126,10 +144,17 @@ public class Variable extends JavaElement{
return this.value;
}
public List<Annotation> getAnnotations(){
return this.annotations;
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
for(Annotation annotation : this.annotations){
annotation.build(buffer, 0);
buffer.append((s) -> s+=" ");
}
buffer.append((s) -> s+=Modifier.toString(modifier)+(modifier > 0 ? " ":"")+type+" "+name+(value == null ? ";":" = "+value+";"));
buffer.add("");
}