Compare commits
2 commits
01107c788f
...
af3481fd4d
Author | SHA1 | Date | |
---|---|---|---|
af3481fd4d | |||
cc6142562b |
6 changed files with 97 additions and 26 deletions
|
@ -26,11 +26,14 @@ 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
|
||||||
|
|
|
@ -2,6 +2,7 @@ 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;
|
||||||
|
@ -13,22 +14,41 @@ 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();
|
||||||
|
|
||||||
String[] split = matcher.group(2).split("\\s+");
|
Iterator<String> values = new ArrayIterator<>(matcher.group(2).split("\\s+"));
|
||||||
for(int i = 0; i < split.length-1; i++){
|
String value = null;
|
||||||
this.modifier+=JavaParser.getModifier(split[i]);
|
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<>();
|
this.childs = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -100,6 +120,11 @@ 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("");
|
||||||
|
|
|
@ -29,6 +29,8 @@ 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();
|
||||||
|
|
||||||
|
@ -36,17 +38,16 @@ 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 = new CleanerPool(
|
local = local.group(new CleanerPool(new Cleaner("GENERIC_FUNCTION", '{', '}')));
|
||||||
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>[<|(|\\[|\"|'])");
|
||||||
|
@ -104,17 +105,28 @@ 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) 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;
|
boolean empty = getChilds().size() == 0;
|
||||||
|
|
||||||
super.build(buffer, tab);
|
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("");
|
buffer.add("");
|
||||||
|
|
||||||
if(empty) return;
|
if(empty) return;
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -24,6 +24,11 @@ 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 {
|
||||||
|
@ -90,8 +95,9 @@ 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);
|
||||||
|
|
|
@ -14,14 +14,19 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -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();
|
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);
|
Iterator<String> values = onlyDefine(content, cleaner);
|
||||||
if(!values.hasNext()) return;
|
if(!values.hasNext()) return;
|
||||||
values.next();
|
values.next();
|
||||||
|
@ -67,13 +72,26 @@ public class Variable extends JavaElement{
|
||||||
this.value = new Value(spacesValue);
|
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);
|
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() && (modifier = JavaParser.getModifier(value = values.next())) > 0){
|
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;
|
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;
|
||||||
|
@ -126,10 +144,17 @@ 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("");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue