diff --git a/src/be/jeffcheasey88/peeratcode/parser/Bag.java b/src/be/jeffcheasey88/peeratcode/parser/Bag.java index 229e869..4f5cd0f 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/Bag.java +++ b/src/be/jeffcheasey88/peeratcode/parser/Bag.java @@ -37,6 +37,10 @@ public class Bag{ return (E) this.map.get(key); } + public boolean has(String key){ + return this.map.containsKey(key); + } + public void set(String key, Object value){ this.map.put(key, value); } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index 5c82a17..387c730 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -17,9 +17,9 @@ public class Class extends JavaElement{ private List elements; public Class(Bag bag){ - this.name = bag.get("name"); - this.extend = bag.get("extend"); - this.implement = bag.get("implement"); + this.name = bag.get("name"); + if(bag.has("extend")) this.extend = bag.get("extend"); + if(bag.has("implement")) this.implement = bag.get("implement"); this.annotations = new ArrayList<>(); this.elements = new ArrayList<>(); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index d87bb65..704a014 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -12,6 +12,7 @@ import java.util.List; import be.jeffcheasey88.peeratcode.parser.Parser; import be.jeffcheasey88.peeratcode.parser.Token; import be.jeffcheasey88.peeratcode.parser.TokenType; +import be.jeffcheasey88.peeratcode.parser.TokenValidator; import be.jeffcheasey88.peeratcode.parser.Tokenizer; import be.jeffcheasey88.peeratcode.parser.state.BuilderStateTree; import be.jeffcheasey88.peeratcode.parser.state.InitialStateTree; @@ -38,6 +39,8 @@ public class JavaParser extends Parser { System.out.println((System.currentTimeMillis()-time)+"ms"); + System.out.println(TokenValidator.MAX_VALIDATE); + parser.build(new BufferedWriter(new FileWriter(new File("/home/ParserV2.txt")))); } @@ -167,6 +170,49 @@ public class JavaParser extends Parser { operation_value.then(operation_end); operation_value.then((validator) -> validator.validate((token) -> token.getValue().equals("."))).then(operation_value); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("return"))) + .then(new RedirectStateTree<>(value, (global, local) -> global.set(null))) + .then((validator) -> validator.validate((token) -> token.getValue().equals(";"))) + .end((a,b) -> a); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("throw"))) + .then(new RedirectStateTree<>(value, (global, local) -> global.set(null))) + .then((validator) -> validator.validate((token) -> token.getValue().equals(";"))) + .end((a,b) -> a); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("do"))) + .then(new RedirectStateTree<>(value, (global, local) -> global.set(null))) + .then((validator) -> validator.validate((token) -> token.getValue().equals(";"))) + .end((a,b) -> a); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("else"))) + .end((a,b) -> a); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("try"))) + .end((a,b) -> a); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("continue"))) + .then((validator) -> validator.validate((token) -> token.getValue().equals(";"))) + .end((a,b) -> a); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("break"))) + .then((validator) -> validator.validate((token) -> token.getValue().equals(";"))) + .end((a,b) -> a); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("if"))) + .then((validator) -> validator.validate((token) -> token.getValue().equals("("))) + .then(new RedirectStateTree<>(value, (global, local) -> global.set(null))) + .then((validator) -> validator.validate((token) -> token.getValue().equals(")"))) + .end((a,b) -> a); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("for"))) + .then((validator) -> validator.validate((token) -> token.getValue().equals("("))) + .then(new RedirectStateTree<>(value, (global, local) -> global.set(null))) + .then((validator) -> validator.validate((token) -> token.getValue().equals(")"))) + .end((a,b) -> a); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("while"))) + .then((validator) -> validator.validate((token) -> token.getValue().equals("("))) + .then(new RedirectStateTree<>(value, (global, local) -> global.set(null))) + .then((validator) -> validator.validate((token) -> token.getValue().equals(")"))) + .end((a,b) -> a); + operation.then((validator) -> validator.validate((token) -> token.getValue().equals("synchronized"))) + .then((validator) -> validator.validate((token) -> token.getValue().equals("("))) + .then(new RedirectStateTree<>(value, (global, local) -> global.set(null))) + .then((validator) -> validator.validate((token) -> token.getValue().equals(")"))) + .end((a,b) -> a); + //VARIABLE StateTree variable = new StateTree<>(); StateTree variable_mod = variable.then(new RedirectStateTree<>(modifier, (global, local) -> global.set("modifier", local))); @@ -244,7 +290,7 @@ public class JavaParser extends Parser { clazz_implement_name.then((validator) -> validator.validate((token) -> token.getValue().equals("{"))) .end((JavaElement, bag) -> new Class(bag)).multiple(clazz_container); StateTree clazz_extend = clazz_base.then((validator) -> validator.validate((token) -> token.getValue().equals("extends"))) - .then(new RedirectStateTree<>(type, (global, local) -> global.set("extend", local))); + .then(new RedirectStateTree<>(type, (global, local) -> global.set("extend", local.get()))); clazz_extend.then((validator) -> validator.validate((token) -> token.getValue().equals("{"))) .end((JavaElement, bag) -> new Class(bag)).multiple(clazz_container); clazz_extend.then(clazz_implement); diff --git a/test/GlobalCover.java b/test/GlobalCover.java index 67dacc5..2e03eb8 100644 --- a/test/GlobalCover.java +++ b/test/GlobalCover.java @@ -19,17 +19,18 @@ class GlobalCover { @Test void workspace() throws Exception{ - JavaParser parser = new JavaParser(); - List files = files(new File("C:\\Users\\jeffc\\eclipse-workspace")); - int validated = 0; - double tokens = 0; + long validated = 0; + long tokens = 0; int count = 0; long time = System.currentTimeMillis(); for(File file : files){ if(file.getName().endsWith(".java")){ - parser.parse(new BufferedReader(new FileReader(file)), new JavaFile()); + BufferedReader reader = new BufferedReader(new FileReader(file)); + new JavaParser().parse(reader, new JavaFile()); + reader.close(); + validated += TokenValidator.MAX_VALIDATE; tokens += TokenValidator.TOKENS; System.out.println(file+" "+validated+" / "+tokens); @@ -41,7 +42,7 @@ class GlobalCover { time = ((System.currentTimeMillis()-time)/1000); - String data = "{\"hash\": "+System.currentTimeMillis()+",\"time\": "+time+",\"percentage\": "+String.format("%.3f", ((validated/tokens)*100.0)).replace(",", ".")+",\"validated\": "+validated+",\"files\": "+count+"}"; + String data = "{\"hash\": "+System.currentTimeMillis()+",\"time\": "+time+",\"percentage\": "+String.format("%.3f", ((validated/((double)tokens))*100.0)).replace(",", ".")+",\"validated\": "+validated+",\"files\": "+count+"}"; File file = new File("/home/tmp_.txt"); BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(data+"\n");