[AST] Base search

This commit is contained in:
jeffcheasey88 2023-10-17 03:49:42 +02:00
parent 4cd54b738f
commit 035ff99edf
18 changed files with 131 additions and 40 deletions

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}

View file

@ -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;
}
}

View file

@ -71,8 +71,8 @@ public class JavaParser extends Parser<JavaElement> {
}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<JavaElement> {
}
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<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);
@ -286,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);
@ -320,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("]")))
@ -378,23 +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) -> validator.validate((token) -> token.getType().equals(TokenType.STRING))).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(",")))
@ -1012,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));

View 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;
}
}

View file

@ -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;
}
}
}

View file

@ -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;
}
}

View file

@ -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);
}

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);