From d23abb4379894014d81d81a01047f3fac3779316 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Thu, 23 Feb 2023 13:30:29 +0100 Subject: [PATCH] Java Parser V0 --- .../peeratcode/parser/java/Class.java | 64 ++++++++++++++++ .../peeratcode/parser/java/CleanerPool.java | 76 +++++++++++++++++++ .../peeratcode/parser/java/Function.java | 75 ++++++++++++++++++ .../peeratcode/parser/java/Import.java | 28 +++++++ .../peeratcode/parser/java/JavaParser.java | 64 ++++++++++++++++ .../peeratcode/parser/java/Operation.java | 26 +++++++ .../peeratcode/parser/java/Package.java | 24 ++++++ .../peeratcode/parser/java/Variable.java | 41 ++++++++++ 8 files changed, 398 insertions(+) create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/Class.java create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/Function.java create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/Import.java create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/Operation.java create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/Package.java create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/Variable.java diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java new file mode 100644 index 0000000..782613c --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -0,0 +1,64 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Class { + + private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$"); + + private int modifier; + private String name; + + private Listvars; + + public Class(){} + + public int parse(String content) 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]); + } + this.name = split[split.length-1]; + + this.vars = new ArrayList<>(); + + content = matcher.group(3); + Pattern empty = Pattern.compile("^\\s*$"); + while(!(empty.matcher(content).matches())){ + int quotes = content.indexOf(';'); + int braces = content.indexOf('{'); + int equals = content.indexOf('='); + if(quotes < braces && quotes < equals){ + Variable variable = new Variable(); + int index = variable.parse(content); + this.vars.add(variable); + content = content.substring(index); + }else if(equals >= 0 && equals < braces){ + //variable with value + System.out.println("equals < braces"); + break; + }else{ + Function func = new Function(); + int index = func.parse(content); + content = content.substring(index); + } + } + + return matcher.group(1).length(); + } + + public int getModifier(){ + return this.modifier; + } + + public String getName(){ + return this.name; + } + +} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java new file mode 100644 index 0000000..4c422dd --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java @@ -0,0 +1,76 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +public class CleanerPool { + + public static CleanerPool getterToDelete = new CleanerPool(); + + private static String CONSTANT_REPLACER = "$STRING_STATEMENT_CONSTANT_"; + + private List constants; + + private CleanerPool(){ + this.constants = new ArrayList<>(); + getterToDelete = this; + } + + public String clean(String statement){ + char[] chars = statement.toCharArray(); + StringBuilder builder = new StringBuilder(); + for(int i = 0; i < chars.length; i++){ + char current = chars[i]; + if(current== '"'){ + int constantPos = this.constants.size(); + String constant = cutConstant(chars, i); + i+=constant.length()+1; + builder.append(CONSTANT_REPLACER+constantPos); + this.constants.add(constant); + }else{ + builder.append(current); + } + } + + for(String s : constants){ + System.out.println("CONSTANT="+s); + } + return builder.toString(); + } + + public boolean isConstant(String region){ + return region.startsWith(CONSTANT_REPLACER); + } + + public String getConstant(String replacer){ + if(!replacer.startsWith(CONSTANT_REPLACER)) return null; + return this.constants.get(Integer.parseInt(replacer.replace(CONSTANT_REPLACER,""))); + } + + public List getConstants(){ + return this.constants; + } + + private static Pattern parenthesisPattern = Pattern.compile("^\\$SQL_STATEMENT_PARENTHESIS_([0-9]*$)"); + + public boolean isParenthesis(String region){ + return parenthesisPattern.matcher(region).matches(); + } + + private String cutConstant(char[] chars, int pos){ + StringBuilder builder = new StringBuilder(); + for(int i = pos+1; i < chars.length; i++){ + char current = chars[i]; + if(current == '"'){ + if(current == '\\'){ //toChange + builder.append(current); + }else break; + }else{ + builder.append(current); + } + } + return builder.toString(); + } + +} \ No newline at end of file diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java new file mode 100644 index 0000000..84e3125 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -0,0 +1,75 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Function { + + private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$"); + + private int modifier; + private String name; + private String exceptions; + private String parameters; + + public Function(){ + + } + + public int parse(String content) 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); + + String body = matcher.group(5); + int offset = 0; + int index = 0; + do { + System.out.println(); + int end = body.indexOf('}'); + int braces = body.indexOf('{'); + int quotes = body.indexOf(';'); + + if((end < 0) || (end < braces && end < quotes)){ +// System.out.println("no INDEX in "+body); +// if(end > 0) offset+=end; + break; + } + +// System.out.println(toString()+" - "+offset+" | "+end); + if(braces < 0 && quotes < 0){ + System.out.println("OUT "+body); + if(end > 0) offset+=end; + break; + } + + if(braces >= 0 && braces < quotes){ + Function func = new Function(); + index = func.parse(body.substring(0, end+1)); + }else{ + Operation op = new Operation(); + index = op.parse(body.substring(0, end)); + } + offset+=index+1; +// System.out.println("SEEKINDEX "+index+" "+toString()); +// System.out.println("FROM("+body.length()+") "+body); +// System.out.println(); + body = body.substring(index); + }while(offset > -1); +// System.out.println(toString()+": "+(matcher.group(1).length()+offset+1)); +// System.out.println("\t\t\t\t("+content.length()+")\t"+content); + return matcher.group(1).length()+offset; + } + + @Override + public String toString(){ + return "Function[name="+name+",param="+parameters+",exception="+exceptions+"]"; + } +} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Import.java b/src/be/jeffcheasey88/peeratcode/parser/java/Import.java new file mode 100644 index 0000000..283accd --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Import.java @@ -0,0 +1,28 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Import { + + private static Pattern PATTERN = Pattern.compile("^\\s*(import\\s+([^;]*);).*$"); + + public static boolean isImport(String content){ + return PATTERN.matcher(content).matches(); + } + + private String name; + + public Import(){} + + public int parse(String content) throws Exception{ + Matcher matcher = PATTERN.matcher(content); + matcher.matches(); + this.name = matcher.group(2); + return matcher.group(1).length(); + } + + public String getName(){ + return this.name; + } +} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java new file mode 100644 index 0000000..6f81ca3 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -0,0 +1,64 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import java.io.BufferedReader; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; + +public class JavaParser { + + private Package pack; + private List imports; + private Class clazz; + + private BufferedReader reader; + + public JavaParser(BufferedReader reader){ + this.reader = reader; + } + + public void parse() throws Exception{ + String content = ""; + int index; + + String line; + while((line = reader.readLine()) != null) content+=line; + + content = CleanerPool.getterToDelete.clean(content); + + this.pack = new Package(); + index = this.pack.parse(content); + content = content.substring(index); + + this.imports = new ArrayList<>(); + while(Import.isImport(content)){ + Import imp = new Import(); + index = imp.parse(content); + this.imports.add(imp); + content = content.substring(index); + } + + this.clazz = new Class(); + index = this.clazz.parse(content); + content = content.substring(index); + } + + public static int getModifier(String modifier){ + switch(modifier){ + case "public": return Modifier.PUBLIC; + case "private": return Modifier.PRIVATE; + case "protected": return Modifier.PROTECTED; + case "static": return Modifier.STATIC; + case "final": return Modifier.FINAL; + case "synchronized": return Modifier.SYNCHRONIZED; + case "volatile": return Modifier.VOLATILE; + case "transient": return Modifier.TRANSIENT; + case "native": return Modifier.NATIVE; + case "abstract": return Modifier.ABSTRACT; + case "strictfp": return Modifier.STRICT; + default: break; + } + return 0; + } + +} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Operation.java b/src/be/jeffcheasey88/peeratcode/parser/java/Operation.java new file mode 100644 index 0000000..9d33f19 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Operation.java @@ -0,0 +1,26 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Operation { + + private static Pattern VARIABLE_PATTERN = Pattern.compile("^(\\s*([^;]*)).*$"); + + private String tmp; + + public Operation(){ + + } + + public int parse(String content) throws Exception{ + Matcher matcher = VARIABLE_PATTERN.matcher(content); + if(matcher.matches()){ + this.tmp = matcher.group(2); + System.out.println("parsed "+tmp); + return matcher.group(1).length()+1; + } + return 0; + } + +} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Package.java b/src/be/jeffcheasey88/peeratcode/parser/java/Package.java new file mode 100644 index 0000000..b160919 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Package.java @@ -0,0 +1,24 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Package { + + private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);).*$"); + + private String name; + + public Package(){} + + public int parse(String content) throws Exception{ + Matcher matcher = PATTERN.matcher(content); + matcher.matches(); + this.name = matcher.group(2); + return matcher.group(1).length(); + } + + public String getName(){ + return this.name; + } +} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java new file mode 100644 index 0000000..63646b6 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -0,0 +1,41 @@ +package be.jeffcheasey88.peeratcode.parser.java; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Variable { + + private static Pattern PATTERN = Pattern.compile("^(\\s*([^;]*);).*$"); + + private int modifier; + private String name; + private String type; + + public Variable(){} + + public int parse(String content) 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.type = split[split.length-2]; + + return matcher.group(1).length(); + } + + public int getModifier(){ + return this.modifier; + } + + public String getName(){ + return this.name; + } + + public String getType(){ + return this.type; + } +}