Compare commits
5 commits
f4ff094554
...
3a2e39f223
Author | SHA1 | Date | |
---|---|---|---|
3a2e39f223 | |||
6f8a1b4ceb | |||
eba6a051fd | |||
89fd0553a2 | |||
ab03e73f58 |
11 changed files with 350 additions and 13 deletions
|
@ -89,6 +89,10 @@ public class Class extends JavaElement{
|
|||
return this.name;
|
||||
}
|
||||
|
||||
public List<JavaElement> getChilds(){
|
||||
return this.childs;
|
||||
}
|
||||
|
||||
// public List<Variable> getVariables(){
|
||||
// return this.vars;
|
||||
// }
|
||||
|
|
|
@ -41,13 +41,8 @@ public class Function extends OperationContainer{
|
|||
String body = local.unzipOne(zip, (s,p) -> s);
|
||||
String unzip = body.substring(1, body.indexOf('}'));
|
||||
|
||||
System.out.println("----------before----------");
|
||||
show(0);
|
||||
|
||||
super.parse(local.clean(unzip), global, local);
|
||||
|
||||
System.out.println("----------After----------");
|
||||
show(0);
|
||||
|
||||
return matcher.group(1).length()+local.unzip(unzip, ((s,p) -> s)).length()+1;
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
|
|||
public class JavaParser{
|
||||
|
||||
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\\ExampleClass.java");
|
||||
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Class.java");
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
|
||||
JavaParser parser = new JavaParser(reader);
|
||||
|
|
|
@ -9,6 +9,7 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
|||
public class ConditionalOperation extends OperationContainer{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$");
|
||||
private static Pattern PATTERN_SPACE = Pattern.compile("^(\\s+).*$");
|
||||
|
||||
private Pattern pattern;
|
||||
private String condition;
|
||||
|
@ -23,10 +24,17 @@ public class ConditionalOperation extends OperationContainer{
|
|||
matcher.matches();
|
||||
|
||||
this.condition = local.unzipOne(matcher.group(2), (s,p) -> s);
|
||||
System.out.println("CONDITION "+condition);
|
||||
|
||||
int index = matcher.group(1).length();
|
||||
content = content.substring(index);
|
||||
|
||||
matcher = PATTERN_SPACE.matcher(content);
|
||||
if(matcher.matches()){
|
||||
index+=matcher.group(1).length();
|
||||
content = content.substring(matcher.group(1).length());
|
||||
}
|
||||
|
||||
int bodysize;
|
||||
if(content.startsWith("^")){
|
||||
matcher = PATTERN.matcher(content);
|
||||
|
@ -39,6 +47,8 @@ public class ConditionalOperation extends OperationContainer{
|
|||
content = content.substring(1, content.length()-1);
|
||||
content = local.clean(content);
|
||||
super.parse(content, global, local);
|
||||
}else if(content.startsWith(";")){
|
||||
bodysize=1;
|
||||
}else{
|
||||
bodysize = parseOne(content, global, local);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java.operations;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
||||
|
||||
public class DoOperation extends OperationContainer{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*do\\s*).*$");
|
||||
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$");
|
||||
|
||||
public DoOperation(){}
|
||||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
int index = matcher.group(1).length();
|
||||
content = content.substring(index);
|
||||
|
||||
matcher = PATTERN_CLEANER.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
content = matcher.group(1);
|
||||
|
||||
index += content.length();
|
||||
content = local.unzipOne(content, (s,p) -> s);
|
||||
content = content.substring(1, content.length()-1);
|
||||
content = local.clean(content);
|
||||
super.parse(content, global, local);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show(int tab) {
|
||||
String start = "";
|
||||
for(int i = 0; i < tab; i++) start+="\t";
|
||||
System.out.println(start+"do{");
|
||||
for(JavaElement child : getChilds()) child.show(tab+1);
|
||||
System.out.println(start+"}");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java.operations;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
||||
|
||||
public class LoopAffectOperation extends JavaElement{
|
||||
|
||||
private Pattern pattern;
|
||||
|
||||
public LoopAffectOperation(Pattern pattern){
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = this.pattern.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
return matcher.group(1).length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show(int tab){
|
||||
String start = "";
|
||||
for(int i = 0; i < tab; i++) start+="\t";
|
||||
System.out.println(start+"loop affect??;");
|
||||
}
|
||||
|
||||
public static class ContinueOperation extends LoopAffectOperation{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*continue\\s*;).*$");
|
||||
|
||||
public ContinueOperation(){
|
||||
super(PATTERN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show(int tab){
|
||||
String start = "";
|
||||
for(int i = 0; i < tab; i++) start+="\t";
|
||||
System.out.println(start+"continue;");
|
||||
}
|
||||
}
|
||||
|
||||
public static class BreakOperation extends LoopAffectOperation{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*break\\s*;).*$");
|
||||
|
||||
public BreakOperation(){
|
||||
super(PATTERN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show(int tab){
|
||||
String start = "";
|
||||
for(int i = 0; i < tab; i++) start+="\t";
|
||||
System.out.println(start+"break;");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,7 +16,6 @@ public class MethodCallOperation extends JavaElement{
|
|||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
System.out.println("MethodCallOperation.parse -> "+content);
|
||||
content = local.clean(content);
|
||||
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
|
|
|
@ -40,8 +40,6 @@ public abstract class OperationContainer extends JavaElement{
|
|||
|
||||
System.out.println(operation.getClass().getSimpleName()+" operation = FACTORY.buildOperation();");
|
||||
int index = operation.parse(content, global, local);
|
||||
operation.show(0);
|
||||
System.out.println();
|
||||
content = content.substring(index);
|
||||
if(operation instanceof Variable){
|
||||
if(content.startsWith(",")){
|
||||
|
|
|
@ -12,6 +12,8 @@ import be.jeffcheasey88.peeratcode.parser.java.Variable;
|
|||
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.ForOperation;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.IfOperation;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.WhileOperation;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.BreakOperation;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.ContinueOperation;
|
||||
|
||||
public class OperationFactory{
|
||||
|
||||
|
@ -31,6 +33,12 @@ public class OperationFactory{
|
|||
this.patterns.put(Pattern.compile("^\\s*for\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), ForOperation.class);
|
||||
this.patterns.put(Pattern.compile("^\\s*while\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), WhileOperation.class);
|
||||
this.patterns.put(Pattern.compile("^\\s*else\\s*.*$"), ElseOperation.class);
|
||||
this.patterns.put(Pattern.compile("^\\s*do\\s*\\^GENERIC_FUNCTION\\d+.*$"), DoOperation.class);
|
||||
|
||||
this.patterns.put(Pattern.compile("^\\s*synchronized\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), SynchronizedOperation.class);
|
||||
|
||||
this.patterns.put(Pattern.compile("^\\s*continue\\s*;.*$"), ContinueOperation.class);
|
||||
this.patterns.put(Pattern.compile("^\\s*break\\s*;.*$"), BreakOperation.class);
|
||||
|
||||
this.patterns.put(Pattern.compile("^\\s*[^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+;.*$"), MethodCallOperation.class);
|
||||
|
||||
|
@ -44,17 +52,17 @@ public class OperationFactory{
|
|||
* assignation OK
|
||||
* exec method OK
|
||||
* for OK
|
||||
* do while
|
||||
* do while OK
|
||||
* while OK
|
||||
* if OK
|
||||
* switch case
|
||||
* return OK
|
||||
* continue
|
||||
* break
|
||||
* continue OK
|
||||
* break OK
|
||||
* try catch finally
|
||||
* throw
|
||||
* else OK
|
||||
* synchronized
|
||||
* synchronized OK
|
||||
* instance of
|
||||
*
|
||||
* native operation style i++
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java.operations;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
||||
|
||||
public class SynchronizedOperation extends OperationContainer{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*synchronized\\s*)(\\^GENERIC_PARENTHESIS\\d+)(\\s*).*$");
|
||||
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$");
|
||||
|
||||
private String include;
|
||||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
this.include = local.unzip(matcher.group(2), (s,p) -> s);
|
||||
|
||||
int index = matcher.group(1).length()+matcher.group(2).length()+matcher.group(3).length();
|
||||
content = content.substring(index);
|
||||
|
||||
matcher = PATTERN_CLEANER.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
content = matcher.group(1);
|
||||
|
||||
index += content.length();
|
||||
content = local.unzipOne(content, (s,p) -> s);
|
||||
content = content.substring(1, content.length()-1);
|
||||
content = local.clean(content);
|
||||
super.parse(content, global, local);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show(int tab){
|
||||
String start = "";
|
||||
for(int i = 0; i < tab; i++) start+="\t";
|
||||
System.out.println(start+"synchronized"+this.include+"{");
|
||||
for(JavaElement child : getChilds()) child.show(tab+1);
|
||||
System.out.println(start+"}");
|
||||
}
|
||||
|
||||
}
|
163
test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java
Normal file
163
test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java
Normal file
|
@ -0,0 +1,163 @@
|
|||
package be.jeffcheasey88.peeratcode.parser.java;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.ForOperation;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.WhileOperation;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.DoOperation;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.BreakOperation;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.ContinueOperation;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.MethodCallOperation;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.operations.SynchronizedOperation;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
class OperationTest{
|
||||
|
||||
JavaParser parse(String code) throws Exception{
|
||||
BufferedReader reader = new BufferedReader(new Reader(){public int read(char[] cbuf, int off, int len) throws IOException{return 0;}public void close() throws IOException {}}) {
|
||||
private boolean read = false;
|
||||
@Override
|
||||
public String readLine() throws IOException{
|
||||
if(read) return null;
|
||||
read = true;
|
||||
return code;
|
||||
}
|
||||
};
|
||||
|
||||
JavaParser parser = new JavaParser(reader);
|
||||
parser.parse();
|
||||
return parser;
|
||||
}
|
||||
|
||||
@Test
|
||||
void ifOperation(){
|
||||
try {
|
||||
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0) { return i; } return i; } }");
|
||||
Class clazz = parser.getClazz();
|
||||
|
||||
clazz.show(0);
|
||||
|
||||
assertEquals(1, clazz.getChilds().size());
|
||||
Function function = (Function) clazz.getChilds().get(0);
|
||||
assertEquals(3, function.getChilds().size());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void ifWithNoneOperation(){
|
||||
try {
|
||||
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; if(i == 0); return i; } }");
|
||||
Class clazz = parser.getClazz();
|
||||
|
||||
clazz.show(0);
|
||||
|
||||
assertEquals(1, clazz.getChilds().size());
|
||||
Function function = (Function) clazz.getChilds().get(0);
|
||||
assertEquals(3, function.getChilds().size());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void doWhileOperation(){
|
||||
try {
|
||||
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ int i = 0; do{ System.out.println(\"Hello\"); }while(i == 0); return i; } }");
|
||||
Class clazz = parser.getClazz();
|
||||
|
||||
clazz.show(0);
|
||||
|
||||
assertEquals(1, clazz.getChilds().size());
|
||||
Function function = (Function) clazz.getChilds().get(0);
|
||||
assertEquals(4, function.getChilds().size());
|
||||
|
||||
assertEquals(DoOperation.class, function.getChilds().get(1).getClass());
|
||||
DoOperation doOp = (DoOperation)function.getChilds().get(1);
|
||||
assertEquals(1, doOp.getChilds().size());
|
||||
assertEquals(MethodCallOperation.class, doOp.getChilds().get(0).getClass());
|
||||
|
||||
assertEquals(WhileOperation.class, function.getChilds().get(2).getClass());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void continueOperation(){
|
||||
try {
|
||||
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ for(int i = 0; i < 1; i++) continue; } }");
|
||||
Class clazz = parser.getClazz();
|
||||
|
||||
clazz.show(0);
|
||||
|
||||
assertEquals(1, clazz.getChilds().size());
|
||||
Function function = (Function) clazz.getChilds().get(0);
|
||||
assertEquals(1, function.getChilds().size());
|
||||
|
||||
assertEquals(ForOperation.class, function.getChilds().get(0).getClass());
|
||||
ForOperation f = (ForOperation)function.getChilds().get(0);
|
||||
assertEquals(1, f.getChilds().size());
|
||||
assertEquals(ContinueOperation.class, f.getChilds().get(0).getClass());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void breakOperation(){
|
||||
try {
|
||||
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ for(int i = 0; i < 1; i++) break ; } }");
|
||||
Class clazz = parser.getClazz();
|
||||
|
||||
clazz.show(0);
|
||||
|
||||
assertEquals(1, clazz.getChilds().size());
|
||||
Function function = (Function) clazz.getChilds().get(0);
|
||||
assertEquals(1, function.getChilds().size());
|
||||
|
||||
assertEquals(ForOperation.class, function.getChilds().get(0).getClass());
|
||||
ForOperation f = (ForOperation)function.getChilds().get(0);
|
||||
assertEquals(1, f.getChilds().size());
|
||||
assertEquals(BreakOperation.class, f.getChilds().get(0).getClass());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void synchronizedOperation(){
|
||||
try {
|
||||
JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ synchronized(this) { this.none(); } } }");
|
||||
Class clazz = parser.getClazz();
|
||||
|
||||
clazz.show(0);
|
||||
|
||||
assertEquals(1, clazz.getChilds().size());
|
||||
Function function = (Function) clazz.getChilds().get(0);
|
||||
assertEquals(1, function.getChilds().size());
|
||||
|
||||
assertEquals(SynchronizedOperation.class, function.getChilds().get(0).getClass());
|
||||
SynchronizedOperation sync = (SynchronizedOperation)function.getChilds().get(0);
|
||||
assertEquals(1, sync.getChilds().size());
|
||||
assertEquals(MethodCallOperation.class, sync.getChilds().get(0).getClass());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue