From b97909101bfb82d68541cc2776b11c0e53cc0970 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Fri, 11 Aug 2023 12:19:33 +0200 Subject: [PATCH] Only one instance of Parser for multiple parsing --- .../peeratcode/parser/Parser.java | 1 + .../peeratcode/parser/Tokenizer.java | 4 +++ .../peeratcode/parser/java/JavaParser.java | 35 +++++++++++++------ test/GlobalCover.java | 3 +- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/parser/Parser.java b/src/be/jeffcheasey88/peeratcode/parser/Parser.java index abf183c..a72fc2c 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/Parser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/Parser.java @@ -21,6 +21,7 @@ public class Parser{ } public final void parse(BufferedReader reader, E container) throws Exception{ + this.tokenizer.reset(); this.tokenizer.parse(reader); this.state.seed(this.tokenizer, container); diff --git a/src/be/jeffcheasey88/peeratcode/parser/Tokenizer.java b/src/be/jeffcheasey88/peeratcode/parser/Tokenizer.java index 6a6724f..f634694 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/Tokenizer.java +++ b/src/be/jeffcheasey88/peeratcode/parser/Tokenizer.java @@ -29,6 +29,10 @@ public class Tokenizer { return this.tokens; } + public void reset(){ + this.tokens.clear(); + } + public void parse(BufferedReader reader) throws Exception{ int lineNumber = 0; String line; diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index 5f22fa8..59296a3 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -31,13 +31,6 @@ public class JavaParser extends Parser { public JavaParser(){ Tokenizer tokenizer = new Tokenizer(){ - private List tokens = new ArrayList<>(); - - @Override - public List getTokens(){ - return this.tokens; - } - @Override public void parse(BufferedReader reader) throws Exception{ int lineNumber = 0; @@ -63,7 +56,7 @@ public class JavaParser extends Parser { if(c == '/' && (i == line.length()-1 || line.charAt(i+1) == '/')) break; 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 { if(current == null) current = token; else current = current.concat(token); bag.set(current); - System.out.println("set bag "+bag); })); value_name.end((a,b) -> a); @@ -349,16 +341,39 @@ public class JavaParser extends Parser { .end((a,b) -> a); operation.then((validator) -> validator.validate((token) -> token.getValue().equals("else"))) .end((a,b) -> a); + StateTree operation_try = operation.then((validator) -> validator.validate((token) -> token.getValue().equals("try"))); StateTree operation_try_base = 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((validator) -> validator.validate((token) -> token.getValue().equals(")"))) .then(operation_try_base); - operation_try_base.end((a,b) -> a) + StateTree operation_try_end = operation_try_base.end((a,b) -> a) .multiple(function_container) .unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))) .end((a,b) -> a); + +// StateTree 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 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 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"))) diff --git a/test/GlobalCover.java b/test/GlobalCover.java index fb48cc8..f6bddaa 100644 --- a/test/GlobalCover.java +++ b/test/GlobalCover.java @@ -26,11 +26,12 @@ class GlobalCover { boolean hasWriten = false; + Parser parser = new JavaParser(); + long time = System.currentTimeMillis(); for(File file : files){ if(file.getName().endsWith(".java")){ BufferedReader reader = new BufferedReader(new FileReader(file)); - Parser parser = new JavaParser(); parser.parse(reader, new JavaFile()); reader.close();