Function signature & delimitation
This commit is contained in:
parent
0111df23f5
commit
4b1b83930e
5 changed files with 117 additions and 74 deletions
|
@ -54,7 +54,7 @@ public class Class{
|
|||
}else{
|
||||
// System.out.println("Function "+content);
|
||||
Function func = new Function();
|
||||
int index = func.parse(content);
|
||||
int index = func.parse(content, cleaner);
|
||||
this.functions.add(func);
|
||||
content = content.substring(index);
|
||||
// System.out.println("End "+content);
|
||||
|
|
|
@ -2,7 +2,10 @@ package be.jeffcheasey88.peeratcode.parser.java;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -20,21 +23,46 @@ public class CleanerPool{
|
|||
return value;
|
||||
}
|
||||
|
||||
|
||||
public String unzip(String value, BiFunction<String,String, String> modifier){
|
||||
boolean edited = false;
|
||||
for(Cleaner cleaner : this.cleaners){
|
||||
Matcher matcher = cleaner.getMatcher(value);
|
||||
if(matcher.matches()){
|
||||
String key = matcher.group(2);
|
||||
String zip = cleaner.getConstant(key);
|
||||
String modified = modifier.apply(zip, cleaner.getPattern());
|
||||
if(modified == null) modified = zip;
|
||||
value = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3);
|
||||
edited = true;
|
||||
public String unzipOne(String value, BiFunction<String, String, String> modifier){
|
||||
boolean edited;
|
||||
String tmp = value;
|
||||
Map<String, String> map = new HashMap<>();
|
||||
do{
|
||||
edited = false;
|
||||
for(Cleaner cleaner : this.cleaners){
|
||||
Matcher matcher = cleaner.getMatcher(tmp);
|
||||
if(matcher.matches()){
|
||||
String key = matcher.group(2);
|
||||
String zip = cleaner.getConstant(key);
|
||||
map.put(key, cleaner.open+zip+cleaner.close);
|
||||
tmp = matcher.group(1)+cleaner.open+zip+cleaner.close+matcher.group(3);
|
||||
edited = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(edited) return unzip(value, modifier);
|
||||
}while(edited);
|
||||
|
||||
|
||||
for(Entry<String, String> unzip : map.entrySet()) value = value.replaceAll("\\"+unzip.getKey()+"(?<e>([^\\d]|$))", unzip.getValue()+"${e}");
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public String unzip(String value, BiFunction<String, String, String> modifier){
|
||||
boolean edited;
|
||||
do{
|
||||
edited = false;
|
||||
for(Cleaner cleaner : this.cleaners){
|
||||
Matcher matcher = cleaner.getMatcher(value);
|
||||
if(matcher.matches()){
|
||||
String key = matcher.group(2);
|
||||
String zip = cleaner.getConstant(key);
|
||||
String modified = modifier.apply(zip, cleaner.getPattern());
|
||||
if(modified == null) modified = zip;
|
||||
value = matcher.group(1)+cleaner.open+modified+cleaner.close+matcher.group(3);
|
||||
edited = true;
|
||||
}
|
||||
}
|
||||
}while(edited);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,84 +2,104 @@ 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.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
||||
|
||||
public class Function {
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$");
|
||||
|
||||
private int modifier;
|
||||
private String returnType;
|
||||
private String name;
|
||||
private List<Variable> parameters;
|
||||
private String exceptions;
|
||||
private String parameters;
|
||||
|
||||
private List<Function> functions;
|
||||
private List<Variable> variables;
|
||||
private List<Operation> operations;
|
||||
|
||||
public Function(){
|
||||
this.functions = new ArrayList<>();
|
||||
this.parameters = new ArrayList<>();
|
||||
this.variables = new ArrayList<>();
|
||||
this.operations = new ArrayList<>();
|
||||
}
|
||||
|
||||
public int parse(String content) throws Exception{
|
||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
String[] split = matcher.group(2).split("\\s+");
|
||||
for(int i = 0; i < split.length-2; i++){
|
||||
this.modifier+=JavaParser.getModifier(split[i]);
|
||||
}
|
||||
this.name = split[split.length-1];
|
||||
this.parameters = matcher.group(3);
|
||||
this.exceptions = matcher.group(4);
|
||||
attribute(matcher.group(2));
|
||||
parameters(matcher.group(3)+";", cleaner);
|
||||
this.exceptions = matcher.group(4).trim();
|
||||
|
||||
String body = matcher.group(5);
|
||||
int offset = 0;
|
||||
int index = 0;
|
||||
CleanerPool generic = new CleanerPool(new Cleaner("GENERIC_FUNCTION", '{', '}'));
|
||||
String zip = generic.clean("{"+matcher.group(5));
|
||||
String body = generic.unzipOne(zip, (s,p) -> s);
|
||||
String unzip = body.substring(1, body.indexOf('}'));
|
||||
body(unzip, generic);
|
||||
|
||||
return matcher.group(1).length()+generic.unzip(unzip, ((s,p) -> s)).length()+1;
|
||||
}
|
||||
|
||||
private static Pattern UNZIP_STICK = Pattern.compile("\\s+(?<e>[<|(|\\[|\"|'])");
|
||||
private static Pattern UNZIP_MAJ = Pattern.compile(">(?<e>[^>\\d,;(])");
|
||||
private static Pattern UNZIP_ARRAY = Pattern.compile("](?<e>[^\\[\\d,;])");
|
||||
|
||||
private void attribute(String content){
|
||||
CleanerPool generic = new CleanerPool(
|
||||
new Cleaner("GENERIC_TYPE_",'<','>'),
|
||||
new Cleaner("GENERIC_ARRAY",'[',']'));
|
||||
String zip = generic.clean(content);
|
||||
String unzip = generic.unzip(zip, (value, pattern) -> {
|
||||
return value.replaceAll("\\s+", "");
|
||||
});
|
||||
|
||||
unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}");
|
||||
unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}");
|
||||
unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}");
|
||||
|
||||
Iterator<String> values = new ArrayIterator<>(unzip.split("\\s+"));
|
||||
String value = null;
|
||||
int modifier;
|
||||
while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0){
|
||||
this.modifier+=modifier;
|
||||
}
|
||||
if(this.returnType == null){
|
||||
this.returnType = value;
|
||||
if(values.hasNext()) value = values.next();
|
||||
}
|
||||
if(this.name == null){
|
||||
this.name = value;
|
||||
}
|
||||
}
|
||||
|
||||
private void parameters(String content, CleanerPool cleaner) throws Exception{
|
||||
if(content.length() == 1) return;
|
||||
boolean quote = false;
|
||||
do {
|
||||
int end = body.indexOf('}');
|
||||
int braces = body.indexOf('{');
|
||||
int quotes = body.indexOf(';');
|
||||
|
||||
if((end < 0) || (end < braces && end < quotes)){
|
||||
if(end > 0) offset+=end;
|
||||
break;
|
||||
}
|
||||
|
||||
if(braces < 0 && quotes < 0){
|
||||
if(end > 0) offset+=end;
|
||||
break;
|
||||
}
|
||||
|
||||
if(braces >= 0 && braces < quotes){
|
||||
Function func = new Function();
|
||||
index = func.parse(body.substring(0, end+1));
|
||||
this.functions.add(func);
|
||||
}else{
|
||||
Operation op = new Operation();
|
||||
index = op.parse(body.substring(0, end+1));
|
||||
this.operations.add(op);
|
||||
}
|
||||
offset+=index+1;
|
||||
body = body.substring(index);
|
||||
}while(offset > -1);
|
||||
return matcher.group(1).length()+offset;
|
||||
Variable variable = new Variable();
|
||||
int index = variable.parse(content, cleaner);
|
||||
this.parameters.add(variable);
|
||||
content = content.substring(index);
|
||||
quote = content.startsWith(",");
|
||||
if(quote) content = content.substring(1);
|
||||
}while(quote);
|
||||
}
|
||||
|
||||
private void body(String content, CleanerPool cleaner) throws Exception{
|
||||
}
|
||||
|
||||
public void show(int tab){
|
||||
String start = "";
|
||||
for(int i = 0; i < tab; i++) start+="\t";
|
||||
System.out.println(start+Modifier.toString(modifier)+" "+name+"("+parameters+") "+exceptions+"{");
|
||||
for(Operation o : this.operations) o.show(tab+1);
|
||||
System.out.println();
|
||||
for(Function f : this.functions) f.show(tab+1);
|
||||
String param = "";
|
||||
for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+"";
|
||||
if(!param.isEmpty()) param = param.substring(1);
|
||||
System.out.println(start+Modifier.toString(modifier)+" "+returnType+" "+name+"("+param+") "+exceptions+"{");
|
||||
System.out.println(start+"}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "Function[name="+name+",param="+parameters+",exception="+exceptions+"]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
|||
|
||||
public class JavaParser{
|
||||
|
||||
public static void maine(String[] args) throws Exception {
|
||||
public static void main(String[] args) throws Exception {
|
||||
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Import.java");
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
|
||||
|
@ -29,8 +29,8 @@ public class JavaParser{
|
|||
show(dir);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
String variable = "Future future = {new Runnable()};";
|
||||
public static void mainf(String[] args) throws Exception{
|
||||
String variable = "var myName = \"Test\";";
|
||||
|
||||
String clazz = "package test; public class Test{ "+variable+" }";
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/home/tmp.txt")));
|
||||
|
|
|
@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java;
|
|||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -24,7 +25,6 @@ public class Variable {
|
|||
}
|
||||
|
||||
public int parse(String content, CleanerPool cleaner) throws Exception{
|
||||
System.out.println("parse "+content);
|
||||
CleanerPool generic = new CleanerPool(
|
||||
new Cleaner("GENERIC_TYPE_",'<','>'),
|
||||
new Cleaner("GENERIC_ARRAY",'[',']'),
|
||||
|
@ -54,7 +54,6 @@ public class Variable {
|
|||
}
|
||||
|
||||
private void assigment(String content, CleanerPool cleaner){
|
||||
System.out.println("assigment "+content);
|
||||
Iterator<String> values = onlyDefine(content, cleaner);
|
||||
if(!values.hasNext()) return;
|
||||
values.next();
|
||||
|
@ -65,7 +64,6 @@ public class Variable {
|
|||
}
|
||||
|
||||
private Iterator<String> onlyDefine(String content, CleanerPool cleaner){
|
||||
System.out.println("define "+content);
|
||||
content = generiqueTypes(content, cleaner);
|
||||
Iterator<String> values = new ArrayIterator<>(content.split("\\s+"));
|
||||
String value = null;
|
||||
|
@ -91,19 +89,16 @@ public class Variable {
|
|||
private static Pattern UNZIP_EQUALS_RIGHT = Pattern.compile("=(?<e>[^=\\s])");
|
||||
|
||||
private String generiqueTypes(String content, CleanerPool cleaner){
|
||||
System.out.println("generic "+content);
|
||||
String unzip = cleaner.unzip(content, (value, pattern) -> {
|
||||
if(pattern.equals("^GENERIC_FUNCTION")) return null;
|
||||
if(pattern.equals("^GENERIC_PARENTHESIS")) return value.replace("\\s+", " ");
|
||||
return value.replaceAll("\\s+", "");
|
||||
});
|
||||
System.out.println("unzip "+unzip);
|
||||
unzip = UNZIP_STICK.matcher(unzip).replaceAll("${e}");
|
||||
unzip = UNZIP_MAJ.matcher(unzip).replaceAll("> ${e}");
|
||||
unzip = UNZIP_ARRAY.matcher(unzip).replaceAll("] ${e}");
|
||||
unzip = UNZIP_EQUALS_LEFT.matcher(unzip).replaceAll("${e} =");
|
||||
unzip = UNZIP_EQUALS_RIGHT.matcher(unzip).replaceAll("= ${e}");
|
||||
System.out.println("matcher "+unzip);
|
||||
return unzip;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue