diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java b/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java index 5f46c92..a35d5f2 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Annotation.java @@ -1,10 +1,11 @@ package be.jeffcheasey88.peeratcode.parser.java; import java.util.Map; +import java.util.function.Function; import be.jeffcheasey88.peeratcode.parser.Token; -public class Annotation{ +public class Annotation extends JavaElement{ public static interface Annotable{ @@ -28,4 +29,12 @@ public class Annotation{ return name; } + @Override + public E find(Function finder){ + for(Value value : this.values.values()){ + if(finder.apply(value)) return (E)value; + } + return null; + } + } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index dd7a040..c9354df 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -56,6 +56,17 @@ public class Class extends JavaElement implements Annotable, ClassContainer, Fun public void addClass(Class clazz){ this.elements.add(clazz); } + + @Override + public E find(java.util.function.Function finder){ + for(Annotation annotation : this.annotations){ + if(finder.apply(annotation)) return (E) annotation; + } + for(JavaElement element : this.elements){ + if(finder.apply(element)) return (E) element; + } + return null; + } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java index ca97732..9a84030 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -65,4 +65,18 @@ public class Function extends JavaElement implements Annotable, VariableContaine this.elements.add(operation); } + @Override + public E find(java.util.function.Function finder) { + for(Annotation annotation : this.annotations){ + if(finder.apply(annotation)) return (E) annotation; + } + for(Variable param : this.parameters){ + if(finder.apply(param)) return (E) param; + } + for(JavaElement content : this.elements){ + if(finder.apply(content)) return (E) content; + } + return null; + } + } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java index f406ec3..c76e151 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaElement.java @@ -1,9 +1,14 @@ package be.jeffcheasey88.peeratcode.parser.java; import be.jeffcheasey88.peeratcode.parser.Bag; +import java.util.function.Function; -public class JavaElement { +public abstract class JavaElement{ + + Bag bag; //to remove + + + public abstract E find(Function finder); + - Bag bag; - } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaFile.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaFile.java index e2ed472..219fb60 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaFile.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaFile.java @@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java; import java.util.ArrayList; import java.util.List; +import java.util.function.Function; import be.jeffcheasey88.peeratcode.parser.Bag; import be.jeffcheasey88.peeratcode.parser.Token; @@ -41,4 +42,13 @@ public class JavaFile extends JavaElement implements ClassContainer{ if(mainClazz == null) this.mainClazz = clazz; else subClazz.add(clazz); } + + @Override + public E find(Function finder){ + if(finder.apply(mainClazz)) return (E) mainClazz; + for(Class clazz : subClazz){ + if(finder.apply(clazz)) return (E) clazz; + } + return null; + } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index 223fb55..9b19948 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -71,8 +71,8 @@ public class JavaParser extends Parser { }else if(Character.isWhitespace(c)) continue; else{ if(c == '"'){ - String value = ""; - int j = i; + String value = "\""; + int j = i+1; for(; j < line.length(); j++){ c = line.charAt(j); if(c == '"'){ @@ -87,13 +87,15 @@ public class JavaParser extends Parser { } token = new Token(lineNumber, i+1, value, TokenType.STRING); i = j-1; + System.out.println(token); + }else{ + if(c == '/' && (i < line.length()-1 && line.charAt(i+1) == '/')) break; + if(c == '/' && (i < line.length()-1 && line.charAt(i+1) == '*')){ + longCommentary = true; + continue; + } + token = new Token(lineNumber, i+1, ""+c, TokenType.DELIMITER); } - if(c == '/' && (i < line.length()-1 && line.charAt(i+1) == '/')) break; - if(c == '/' && (i < line.length()-1 && line.charAt(i+1) == '*')){ - longCommentary = true; - continue; - } - token = new Token(lineNumber, i+1, ""+c, TokenType.DELIMITER); } getTokens().add(token); } @@ -232,6 +234,27 @@ public class JavaParser extends Parser { value_list_element.then((validator) -> validator.validate((token) -> token.getValue().equals(","))) .then(value_list_element); + value.then((validator) -> { + if(validator.validate((token) -> token.getValue().equals("'"))){ + validator.validate((token) -> token.getValue().equals("\\")); + + return + validator.validate((token) -> token.getValue().equals("'")) + || + (validator.validate((token) -> !token.getValue().equals("'")) + && validator.validate((token) -> token.getValue().equals("'"))); + } + return false; + }).end(); + + value.then((validator) -> validator.validate((token) -> { + System.out.println("string? "+token); + return token.getType().equals(TokenType.STRING); + })).end((parent,bag) -> { + bag.set(new Value(bag.get())); + return null; + }); + StateTree value_instance = value.then((validator) -> validator.validate((token) -> token.getValue().equals("new"))); StateTree value_name = new StateTree(); value.then(value_name); @@ -286,6 +309,7 @@ public class JavaParser extends Parser { if(count == null) count = 0; global.set("arg"+count, local); global.set("args", (count+1)); + System.out.println("ah "+local); })); value_arg.then((validator) -> validator.validate((token) -> token.getValue().equals(","))) .then(value_arg); @@ -320,7 +344,10 @@ public class JavaParser extends Parser { value_container.then((validator) -> validator.validate((token) -> token.getValue().equals("-"))).then(value_container); value_container.then((validator) -> validator.validate((token) -> token.getValue().equals("+"))).then(value_container); value_container.then((validator) -> validator.validate((token) -> token.getValue().equals("~"))).then(value_container); - StateTree value_inside = value_container.then(new RedirectStateTree<>(value, (global, local) -> global.set("left", local.get()))); + StateTree value_inside = value_container.then(new RedirectStateTree<>(value, (global, local) -> { + System.out.println("captured "+local); + global.set("left", local.get()); + })); value_inside.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("]"))) @@ -378,23 +405,6 @@ public class JavaParser extends Parser { value_instanceof.end(value_builder); value_instanceof.then(value_left); - value.then((validator) -> { - if(validator.validate((token) -> token.getValue().equals("'"))){ - validator.validate((token) -> token.getValue().equals("\\")); - - return - validator.validate((token) -> token.getValue().equals("'")) - || - (validator.validate((token) -> !token.getValue().equals("'")) - && validator.validate((token) -> token.getValue().equals("'"))); - } - return false; - }).end(); - value.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.STRING))).end((parent,bag) -> { - bag.set(new Value(bag.get())); - return null; - }); - braces_container.then(clazz_container); StateTree braces_array = braces_container.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null))); braces_array.then((validator) -> validator.validate((token) -> token.getValue().equals(","))) @@ -1012,7 +1022,7 @@ public class JavaParser extends Parser { } public static void main(String[] args) throws Exception{ - File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\dev\\peerat\\backend\\Configuration.java"); + File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\dev\\peerat\\backend\\bonus\\extract\\RouteExtracter.java"); BufferedReader reader = new BufferedReader(new FileReader(file)); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Operation.java b/src/be/jeffcheasey88/peeratcode/parser/java/Operation.java index 4c3c35e..6b41135 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Operation.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Operation.java @@ -1,5 +1,7 @@ package be.jeffcheasey88.peeratcode.parser.java; +import java.util.function.Function; + public class Operation extends JavaElement{ public static interface OperationContainer{ @@ -10,4 +12,9 @@ public static interface OperationContainer{ public Operation(){} + @Override + public E find(Function finder) { + return null; + } + } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Value.java b/src/be/jeffcheasey88/peeratcode/parser/java/Value.java index cf8546a..5c10a91 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Value.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Value.java @@ -1,5 +1,7 @@ package be.jeffcheasey88.peeratcode.parser.java; +import java.util.function.Function; + import be.jeffcheasey88.peeratcode.parser.Token; public class Value extends JavaElement{ @@ -30,6 +32,11 @@ public class Value extends JavaElement{ return "Value[token="+token+", content="+content+"]"; } + @Override + public E find(Function finder){ + return content != null ? finder.apply(content) ? (E) content : null : null; + } + public static class BiValue extends Value{ private Value left; @@ -58,6 +65,11 @@ public class Value extends JavaElement{ public String toString(){ return left+" "+action+" "+right; } + + @Override + public E find(Function finder){ + return finder.apply(left) ? (E) left : finder.apply(right) ? (E) right : null; + } } public static class TriValue extends Value{ @@ -80,6 +92,10 @@ public class Value extends JavaElement{ return fail; } + @Override + public E find(Function finder){ + return finder.apply(check) ? (E) check : finder.apply(success) ? (E) success : finder.apply(fail) ? (E) fail : null; + } } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java index 48e48a4..b85d25c 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java; import java.util.ArrayList; import java.util.List; +import java.util.function.Function; import be.jeffcheasey88.peeratcode.parser.Token; import be.jeffcheasey88.peeratcode.parser.java.Annotation.Annotable; @@ -48,4 +49,12 @@ public class Variable extends JavaElement implements Annotable{ public void addAnnotation(Annotation annotation){ this.annotations.add(annotation); } + + @Override + public E find(Function finder){ + for(Annotation annotation : this.annotations){ + if(finder.apply(annotation)) return (E) annotation; + } + return value != null ? finder.apply(value) ? (E)value : null : null; + } } diff --git a/test/ForCase.java b/test/ForCase.java index a95e26e..62ac39d 100644 --- a/test/ForCase.java +++ b/test/ForCase.java @@ -60,7 +60,7 @@ public class ForCase { TokenValidator.TOKENS = 0; TokenValidator.MAX_VALIDATE = 0; - parser.parse(value, new JavaElement()); + parser.parse(value, null); assertEquals(TokenValidator.TOKENS, TokenValidator.MAX_VALIDATE); } diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/AnnotationTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/AnnotationTests.java index 0647b17..0f0d7dc 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/AnnotationTests.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/AnnotationTests.java @@ -90,7 +90,7 @@ public class AnnotationTests { TokenValidator.TOKENS = 0; TokenValidator.MAX_VALIDATE = 0; - JavaElement result = new JavaElement(); + JavaElement result = null; parser.parse(value, result); diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/ClassTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/ClassTests.java index c359432..0608208 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/ClassTests.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/ClassTests.java @@ -80,7 +80,7 @@ public class ClassTests{ TokenValidator.TOKENS = 0; TokenValidator.MAX_VALIDATE = 0; - JavaElement result = new JavaElement(); + JavaElement result = null; parser.parse(value, result); diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java index d7ed762..c373015 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java @@ -136,7 +136,7 @@ public class FunctionTests { TokenValidator.TOKENS = 0; TokenValidator.MAX_VALIDATE = 0; - JavaElement result = new JavaElement(); + JavaElement result = null; parser.parse(value, result); diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/ModifierTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/ModifierTests.java index 5cb20be..b72dfea 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/ModifierTests.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/ModifierTests.java @@ -113,7 +113,7 @@ public class ModifierTests{ TokenValidator.TOKENS = 0; TokenValidator.MAX_VALIDATE = 0; - JavaElement result = new JavaElement(); + JavaElement result = null; parser.parse(value, result); diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/OperationTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/OperationTests.java index 5f69a1b..cbf25ba 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/OperationTests.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/OperationTests.java @@ -340,7 +340,7 @@ public class OperationTests { TokenValidator.TOKENS = 0; TokenValidator.MAX_VALIDATE = 0; - JavaElement result = new JavaElement(); + JavaElement result = null; parser.parse(value, result); diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/TypeTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/TypeTests.java index ce6a4ee..4dc67c8 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/TypeTests.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/TypeTests.java @@ -92,7 +92,7 @@ public class TypeTests{ TokenValidator.TOKENS = 0; TokenValidator.MAX_VALIDATE = 0; - JavaElement result = new JavaElement(); + JavaElement result = null; parser.parse(value, result); diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/ValueTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/ValueTests.java index 5aadc04..09fb844 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/ValueTests.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/ValueTests.java @@ -254,7 +254,7 @@ public class ValueTests { TokenValidator.TOKENS = 0; TokenValidator.MAX_VALIDATE = 0; - JavaElement result = new JavaElement(); + JavaElement result = null; parser.parse(value, result); diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/VariableTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/VariableTests.java index 42221d2..a4d85e2 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/VariableTests.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/VariableTests.java @@ -79,7 +79,7 @@ public class VariableTests{ TokenValidator.TOKENS = 0; TokenValidator.MAX_VALIDATE = 0; - JavaElement result = new JavaElement(); + JavaElement result = new Class(new Token(0, 0, "Test", TokenType.NAME)); parser.parse(value, result);