Compare commits

..

No commits in common. "af3481fd4d3b1c3cd77caded947c21b74cdfec7d" and "01107c788f450c5f8d31d6356db9a3bc939730fa" have entirely different histories.

6 changed files with 26 additions and 97 deletions

View file

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

View file

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

View file

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

View file

@ -5,7 +5,7 @@ import java.util.regex.Pattern;
public class Import { 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){ public static boolean isImport(String content){
return PATTERN.matcher(content).lookingAt(); return PATTERN.matcher(content).lookingAt();

View file

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

View file

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