Java Parser V0

This commit is contained in:
jeffcheasey88 2023-02-23 13:30:29 +01:00
parent b1674d44a1
commit d23abb4379
8 changed files with 398 additions and 0 deletions

View file

@ -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 List<Variable>vars;
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;
}
}

View file

@ -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<String> 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<String> 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();
}
}

View file

@ -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+"]";
}
}

View file

@ -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;
}
}

View file

@ -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<Import> 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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}