Compare commits
3 commits
111eecd929
...
035ff99edf
Author | SHA1 | Date | |
---|---|---|---|
|
035ff99edf | ||
|
4cd54b738f | ||
|
dd2854f34b |
18 changed files with 159 additions and 58 deletions
|
@ -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 extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
for(Value value : this.values.values()){
|
||||
if(finder.apply(value)) return (E)value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -57,5 +57,16 @@ public class Class extends JavaElement implements Annotable, ClassContainer, Fun
|
|||
this.elements.add(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -65,4 +65,18 @@ public class Function extends JavaElement implements Annotable, VariableContaine
|
|||
this.elements.add(operation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 extends JavaElement> E find(Function<JavaElement, Boolean> finder);
|
||||
|
||||
Bag bag;
|
||||
|
||||
}
|
||||
|
|
|
@ -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 extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
if(finder.apply(mainClazz)) return (E) mainClazz;
|
||||
for(Class clazz : subClazz){
|
||||
if(finder.apply(clazz)) return (E) clazz;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,6 @@ import be.jeffcheasey88.peeratcode.parser.TokenValidator;
|
|||
import be.jeffcheasey88.peeratcode.parser.Tokenizer;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.Annotation.Annotable;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.Function.FunctionContainer;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.Value.BiValue;
|
||||
import be.jeffcheasey88.peeratcode.parser.java.Variable.VariableContainer;
|
||||
import be.jeffcheasey88.peeratcode.parser.state.BuilderStateTree;
|
||||
import be.jeffcheasey88.peeratcode.parser.state.InitialStateTree;
|
||||
|
@ -71,12 +70,32 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
i = j-1;
|
||||
}else if(Character.isWhitespace(c)) continue;
|
||||
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;
|
||||
if(c == '"'){
|
||||
String value = "\"";
|
||||
int j = i+1;
|
||||
for(; j < line.length(); j++){
|
||||
c = line.charAt(j);
|
||||
if(c == '"'){
|
||||
value+=c;
|
||||
j++;
|
||||
break;
|
||||
}else if(c == '\\'){
|
||||
value+=c+line.charAt(++j);
|
||||
}else{
|
||||
value+=c;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
token = new Token(lineNumber, i+1, ""+c, TokenType.DELIMITER);
|
||||
}
|
||||
getTokens().add(token);
|
||||
}
|
||||
|
@ -162,6 +181,11 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
type_generic_named_super.then(type_generic_end);
|
||||
type_generic_named_super.then(type_generic_split);
|
||||
|
||||
StateTree<JavaElement> type_array_begin = type_.then((validator) -> validator.validate((token) -> token.getValue().equals("[")));
|
||||
StateTree<JavaElement> type_array_end = type_array_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("]")));
|
||||
type_array_end.then(type_array_begin);
|
||||
type_array_end.end();
|
||||
|
||||
type_.end();
|
||||
type_generic_end.end();
|
||||
|
||||
|
@ -210,6 +234,27 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
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.<Token>get()));
|
||||
return null;
|
||||
});
|
||||
|
||||
StateTree<JavaElement> value_instance = value.then((validator) -> validator.validate((token) -> token.getValue().equals("new")));
|
||||
StateTree<JavaElement> value_name = new StateTree<JavaElement>();
|
||||
value.then(value_name);
|
||||
|
@ -264,6 +309,7 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
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);
|
||||
|
@ -298,7 +344,10 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
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<JavaElement> value_inside = value_container.then(new RedirectStateTree<>(value, (global, local) -> global.set("left", local.get())));
|
||||
StateTree<JavaElement> 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("]")))
|
||||
|
@ -356,41 +405,6 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
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) -> {
|
||||
if(validator.validate((token) -> token.getValue().equals("\""))){
|
||||
while(validator.validate(
|
||||
(token) -> !token.getValue().equals("\""),
|
||||
(bag, token) -> {
|
||||
Token current = bag.get();
|
||||
if(current == null) current = token;
|
||||
else current = current.concat(token);
|
||||
bag.set(current);
|
||||
}));
|
||||
return validator.validate((token) -> token.getValue().equals("\""), (bag, token) -> {
|
||||
Token current = bag.get();
|
||||
if(current == null) current = token;
|
||||
else current = current.concat(token);
|
||||
bag.set(current);
|
||||
});
|
||||
}
|
||||
return false;
|
||||
}).end((parent,bag) -> {
|
||||
bag.set(new Value(bag.<Token>get()));
|
||||
return null;
|
||||
});
|
||||
|
||||
braces_container.then(clazz_container);
|
||||
StateTree<JavaElement> braces_array = braces_container.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null)));
|
||||
braces_array.then((validator) -> validator.validate((token) -> token.getValue().equals(",")))
|
||||
|
@ -1008,7 +1022,7 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
}
|
||||
|
||||
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));
|
||||
|
||||
|
|
|
@ -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 extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 extends JavaElement> E find(Function<JavaElement, Boolean> 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 extends JavaElement> E find(Function<JavaElement, Boolean> 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 extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
return finder.apply(check) ? (E) check : finder.apply(success) ? (E) success : finder.apply(fail) ? (E) fail : null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 extends JavaElement> E find(Function<JavaElement, Boolean> finder){
|
||||
for(Annotation annotation : this.annotations){
|
||||
if(finder.apply(annotation)) return (E) annotation;
|
||||
}
|
||||
return value != null ? finder.apply(value) ? (E)value : null : null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -62,6 +62,11 @@ public class TypeTests{
|
|||
type_generic_named_super.then(type_generic_end);
|
||||
type_generic_named_super.then(type_generic_split);
|
||||
|
||||
StateTree<JavaElement> type_array_begin = type_.then((validator) -> validator.validate((token) -> token.getValue().equals("[")));
|
||||
StateTree<JavaElement> type_array_end = type_array_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("]")));
|
||||
type_array_end.then(type_array_begin);
|
||||
type_array_end.end();
|
||||
|
||||
type_.end();
|
||||
type_generic_end.end();
|
||||
}
|
||||
|
@ -87,7 +92,7 @@ public class TypeTests{
|
|||
TokenValidator.TOKENS = 0;
|
||||
TokenValidator.MAX_VALIDATE = 0;
|
||||
|
||||
JavaElement result = new JavaElement();
|
||||
JavaElement result = null;
|
||||
|
||||
parser.parse(value, result);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
@ -265,6 +265,6 @@ public class ValueTests {
|
|||
|
||||
@Test
|
||||
void doubleInDouble() throws Exception{
|
||||
JavaElement result = testCase("(jeffcheasey88 == goefra) || (yuzzu == yugs)");
|
||||
JavaElement result = testCase("(jeffcheasey88 == goefra) || (yuzzu == yugs) || (test == \"Content-Disposition: form-data; name=\\\"?\\\"\")");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,6 +50,7 @@ public class VariableTests{
|
|||
bag.set("last", token);
|
||||
map.put(token, null);
|
||||
}));
|
||||
|
||||
variable_name.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
|
||||
.end(variable_builder);
|
||||
StateTree<JavaElement> variable_split = variable_name.then((validator) -> validator.validate((token) -> token.getValue().equals(",")));
|
||||
|
@ -78,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);
|
||||
|
||||
|
@ -89,6 +90,6 @@ public class VariableTests{
|
|||
|
||||
@Test
|
||||
void hashMap() throws Exception{
|
||||
JavaElement result = testCase("private static final Map<Map<List<String>>> map = new HashMap<>();");
|
||||
JavaElement result = testCase("private static final Map<Map<List<String[]>>> map = new HashMap<>();");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue