Try Catch Finally

This commit is contained in:
jeffcheasey88 2023-06-10 22:19:25 +02:00
parent a77404952f
commit 31fbc33fa6
3 changed files with 167 additions and 1 deletions

View file

@ -24,6 +24,14 @@ public class Class extends JavaElement{
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
try {
}catch(Exception e) {
}finally {
System.out.println("Hello");
}
String[] split = matcher.group(2).split("\\s+");
for(int i = 0; i < split.length-1; i++){
this.modifier+=JavaParser.getModifier(split[i]);

View file

@ -14,6 +14,9 @@ import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.I
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;
import be.jeffcheasey88.peeratcode.parser.java.operations.TryCatchOperation.CatchOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.TryCatchOperation.FinallyOperation;
import be.jeffcheasey88.peeratcode.parser.java.operations.TryCatchOperation.TryOperation;
public class OperationFactory{
@ -42,6 +45,10 @@ public class OperationFactory{
this.patterns.put(Pattern.compile("^\\s*throw\\s+[^;]+"), ThrowOperation.class);
this.patterns.put(Pattern.compile("^\\s*try\\s*\\^"), TryOperation.class);
this.patterns.put(Pattern.compile("^\\s*catch\\s*\\^GENERIC_PARENTHESIS\\d+"), CatchOperation.class);
this.patterns.put(Pattern.compile("^\\s*finally\\s*\\^GENERIC_FUNCTION\\d+"), FinallyOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+;"), MethodCallOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s+[^\\s]+\\s*=\\s*[^;]+;"), Variable.class);
@ -61,7 +68,7 @@ public class OperationFactory{
* return OK
* continue OK
* break OK
* try catch finally
* try catch finally OK
* throw OK
* else OK
* synchronized OK

View file

@ -0,0 +1,151 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class TryCatchOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
private static Pattern PATTERN_SPACE = Pattern.compile("^(\\s+)");
public TryCatchOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
content = matcher.group(1);
int bodysize = 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 bodysize;
}
public static class TryOperation extends TryCatchOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*try\\s*(\\^GENERIC_PARENTHESIS\\d+)?)");
private String resource;
public TryOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = TryOperation.PATTERN.matcher(content);
matcher.lookingAt();
if(matcher.group(2) != null) this.resource = local.unzipOne(matcher.group(2), (s,p) -> s);
int index = matcher.group(1).length();
content = content.substring(index);
matcher = PATTERN_SPACE.matcher(content);
if(matcher.lookingAt()){
index+=matcher.group(1).length();
content = content.substring(matcher.group(1).length());
}
return index+super.parse(content, global, local);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="try"+(resource == null ? "":resource)+"{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}
public static class CatchOperation extends TryCatchOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*catch\\s*(\\^GENERIC_PARENTHESIS\\d+))");
private String exceptions;
public CatchOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = CatchOperation.PATTERN.matcher(content);
matcher.lookingAt();
this.exceptions = local.unzipOne(matcher.group(2), (s,p) -> s);
int index = matcher.group(1).length();
content = content.substring(index);
matcher = PATTERN_SPACE.matcher(content);
if(matcher.lookingAt()){
index+=matcher.group(1).length();
content = content.substring(matcher.group(1).length());
}
return index+super.parse(content, global, local);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="catch"+exceptions+"{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}
public static class FinallyOperation extends TryCatchOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*finally\\s*)");
public FinallyOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = FinallyOperation.PATTERN.matcher(content);
matcher.lookingAt();
int index = matcher.group(1).length();
content = content.substring(index);
matcher = PATTERN_SPACE.matcher(content);
if(matcher.lookingAt()){
index+=matcher.group(1).length();
content = content.substring(matcher.group(1).length());
}
return index+super.parse(content, global, local);
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="finally{");
buffer.add("");
for(JavaElement child : getChilds()) child.build(buffer, tab+1);
super.build(buffer, tab);
buffer.append((s) -> s+="}");
buffer.add("");
}
}
}