Only one instance of Parser for multiple parsing

This commit is contained in:
jeffcheasey88 2023-08-11 12:19:33 +02:00
parent f2d9c8f5db
commit b97909101b
4 changed files with 32 additions and 11 deletions

View file

@ -21,6 +21,7 @@ public class Parser<E>{
} }
public final void parse(BufferedReader reader, E container) throws Exception{ public final void parse(BufferedReader reader, E container) throws Exception{
this.tokenizer.reset();
this.tokenizer.parse(reader); this.tokenizer.parse(reader);
this.state.seed(this.tokenizer, container); this.state.seed(this.tokenizer, container);

View file

@ -29,6 +29,10 @@ public class Tokenizer {
return this.tokens; return this.tokens;
} }
public void reset(){
this.tokens.clear();
}
public void parse(BufferedReader reader) throws Exception{ public void parse(BufferedReader reader) throws Exception{
int lineNumber = 0; int lineNumber = 0;
String line; String line;

View file

@ -31,13 +31,6 @@ public class JavaParser extends Parser<JavaElement> {
public JavaParser(){ public JavaParser(){
Tokenizer tokenizer = new Tokenizer(){ Tokenizer tokenizer = new Tokenizer(){
private List<Token> tokens = new ArrayList<>();
@Override
public List<Token> getTokens(){
return this.tokens;
}
@Override @Override
public void parse(BufferedReader reader) throws Exception{ public void parse(BufferedReader reader) throws Exception{
int lineNumber = 0; int lineNumber = 0;
@ -63,7 +56,7 @@ public class JavaParser extends Parser<JavaElement> {
if(c == '/' && (i == line.length()-1 || line.charAt(i+1) == '/')) break; if(c == '/' && (i == line.length()-1 || line.charAt(i+1) == '/')) break;
token = new Token(lineNumber, i+1, ""+c, TokenType.DELIMITER); token = new Token(lineNumber, i+1, ""+c, TokenType.DELIMITER);
} }
this.tokens.add(token); getTokens().add(token);
} }
} }
} }
@ -203,7 +196,6 @@ public class JavaParser extends Parser<JavaElement> {
if(current == null) current = token; if(current == null) current = token;
else current = current.concat(token); else current = current.concat(token);
bag.set(current); bag.set(current);
System.out.println("set bag "+bag);
})); }));
value_name.end((a,b) -> a); value_name.end((a,b) -> a);
@ -349,17 +341,40 @@ public class JavaParser extends Parser<JavaElement> {
.end((a,b) -> a); .end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("else"))) operation.then((validator) -> validator.validate((token) -> token.getValue().equals("else")))
.end((a,b) -> a); .end((a,b) -> a);
StateTree<JavaElement> operation_try = operation.then((validator) -> validator.validate((token) -> token.getValue().equals("try"))); StateTree<JavaElement> operation_try = operation.then((validator) -> validator.validate((token) -> token.getValue().equals("try")));
StateTree<JavaElement> operation_try_base = operation_try.then((validator) -> validator.validate((token) -> token.getValue().equals("{"))); StateTree<JavaElement> operation_try_base = operation_try.then((validator) -> validator.validate((token) -> token.getValue().equals("{")));
operation_try.then((validator) -> validator.validate((token) -> token.getValue().equals("("))) operation_try.then((validator) -> validator.validate((token) -> token.getValue().equals("(")))
.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null))) .then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null)))
.then((validator) -> validator.validate((token) -> token.getValue().equals(")"))) .then((validator) -> validator.validate((token) -> token.getValue().equals(")")))
.then(operation_try_base); .then(operation_try_base);
operation_try_base.end((a,b) -> a) StateTree<JavaElement> operation_try_end = operation_try_base.end((a,b) -> a)
.multiple(function_container) .multiple(function_container)
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))) .unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
.end((a,b) -> a); .end((a,b) -> a);
// StateTree<JavaElement> operation_finally = operation_try_end.then((validator) -> validator.validate((token) -> token.getValue().equals("finally")))
// .then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
// .end((a,b) -> a)
// .multiple(function_container)
// .unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
// .end((a,b) -> a);
//
StateTree<JavaElement> operation_catch_named = operation_try_end.then((validator) -> validator.validate((token) -> token.getValue().equals("catch")))
.then((validator) -> validator.validate((token) -> token.getValue().equals("(")))
.then(new RedirectStateTree<>(type, (global, local) -> global.set(null)));
operation_catch_named.then((validator) -> validator.validate((token) -> token.getValue().equals("|")))
.then(operation_catch_named);
StateTree<JavaElement> operation_catch = operation_catch_named.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME)))
.then((validator) -> validator.validate((token) -> token.getValue().equals(")")))
.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.end((a,b) -> a)
.multiple(function_container)
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
.end((a,b) -> a);
// operation_catch.then(operation_finally);
// operation_catch.then(operation_catch);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("continue"))) operation.then((validator) -> validator.validate((token) -> token.getValue().equals("continue")))
.then((validator) -> validator.validate((token) -> token.getValue().equals(";"))) .then((validator) -> validator.validate((token) -> token.getValue().equals(";")))

View file

@ -26,11 +26,12 @@ class GlobalCover {
boolean hasWriten = false; boolean hasWriten = false;
Parser<JavaElement> parser = new JavaParser();
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
for(File file : files){ for(File file : files){
if(file.getName().endsWith(".java")){ if(file.getName().endsWith(".java")){
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = new BufferedReader(new FileReader(file));
Parser<JavaElement> parser = new JavaParser();
parser.parse(reader, new JavaFile()); parser.parse(reader, new JavaFile());
reader.close(); reader.close();