[Build] Type

This commit is contained in:
jeffcheasey88 2023-09-12 00:14:45 +02:00
parent e2b62bb58e
commit dccdcaf022
4 changed files with 137 additions and 40 deletions

View file

@ -8,7 +8,9 @@ import java.io.FileWriter;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.function.BiConsumer;
import be.jeffcheasey88.peeratcode.parser.Bag;
import be.jeffcheasey88.peeratcode.parser.Parser; import be.jeffcheasey88.peeratcode.parser.Parser;
import be.jeffcheasey88.peeratcode.parser.Token; import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.TokenType; import be.jeffcheasey88.peeratcode.parser.TokenType;
@ -106,54 +108,51 @@ public class JavaParser extends Parser<JavaElement> {
bag.set(current == null ? m : current+m); bag.set(current == null ? m : current+m);
})) mod = true; })) mod = true;
return mod; return mod;
}).end((a,b) -> null); }).end();
//TYPE //TYPE
BiConsumer<Bag, Token> concat = (bag, token) -> {
Token value = bag.get();
if(value == null) bag.set(token);
else bag.set(value.concat(token));
};
StateTree<JavaElement> type = new StateTree<JavaElement>(); StateTree<JavaElement> type = new StateTree<JavaElement>();
StateTree<JavaElement> type_ = type.then((validator) -> StateTree<JavaElement> type_ = type.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME),concat));
validator.validate( type_.then((validator) -> validator.validate((token) -> token.getValue().equals("."), concat))
(token) -> token.getType().equals(TokenType.NAME),
(bag, token) -> bag.set(token)));
type_.then((validator) -> validator.validate((token) -> token.getValue().equals(".")))
.then(type_); .then(type_);
StateTree<JavaElement> gen = new StateTree<>(); StateTree<JavaElement> gen = new StateTree<>();
type_.then(gen); type_.then(gen);
StateTree<JavaElement> type_begin = gen.then((validator) -> validator.validate((token) -> token.getValue().equals("<"))); StateTree<JavaElement> type_begin = gen.then((validator) -> validator.validate((token) -> token.getValue().equals("<"), concat));
StateTree<JavaElement> type_generic = type_begin.then(new RedirectStateTree<>(type, (global, local) -> global.set(null))); StateTree<JavaElement> type_generic = type_begin.then(new RedirectStateTree<>(type, (global, local) -> global.set(global.<Token>get().concat(local.get()))));
StateTree<JavaElement> type_generic_end = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals(">"))); StateTree<JavaElement> type_generic_end = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals(">"), concat));
StateTree<JavaElement> type_generic_split = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals(","))); StateTree<JavaElement> type_generic_split = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals(","), concat));
type_generic_split.then(type_generic); type_generic_split.then(type_generic);
StateTree<JavaElement> type_generic_unknow = type_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("?"))); StateTree<JavaElement> type_generic_unknow = type_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("?"), concat));
type_generic_unknow.then(type_generic_end); type_generic_unknow.then(type_generic_end);
type_generic_split.then(type_generic_unknow); type_generic_split.then(type_generic_unknow);
type_generic_unknow.then(type_generic_split); type_generic_unknow.then(type_generic_split);
StateTree<JavaElement> type_generic_unknow_extends = type_generic_unknow.then((validator) -> validator.validate((token) -> token.getValue().equals("extends"))) StateTree<JavaElement> type_generic_unknow_extends = type_generic_unknow.then((validator) -> validator.validate((token) -> token.getValue().equals("extends"), concat))
.then(new RedirectStateTree<>(type, (global, local) -> global.set(null))); .then(new RedirectStateTree<>(type, (global, local) -> global.set(global.<Token>get().concat(local.get()))));
type_generic_unknow_extends.then(type_generic_end); type_generic_unknow_extends.then(type_generic_end);
type_generic_unknow_extends.then(type_generic_split); type_generic_unknow_extends.then(type_generic_split);
StateTree<JavaElement> type_generic_unknow_super = type_generic_unknow.then((validator) -> validator.validate((token) -> token.getValue().equals("super"))) StateTree<JavaElement> type_generic_unknow_super = type_generic_unknow.then((validator) -> validator.validate((token) -> token.getValue().equals("super"), concat))
.then(new RedirectStateTree<>(type, (global, local) -> global.set(null))); .then(new RedirectStateTree<>(type, (global, local) -> global.set(global.<Token>get().concat(local.get()))));
type_generic_unknow_super.then(type_generic_end); type_generic_unknow_super.then(type_generic_end);
type_generic_unknow_super.then(type_generic_split); type_generic_unknow_super.then(type_generic_split);
StateTree<JavaElement> type_generic_named_extends = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals("extends"))) StateTree<JavaElement> type_generic_named_extends = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals("extends"), concat))
.then(new RedirectStateTree<>(type, (global, local) -> global.set(null))); .then(new RedirectStateTree<>(type, (global, local) -> global.set(global.<Token>get().concat(local.get()))));
type_generic_named_extends.then(type_generic_end); type_generic_named_extends.then(type_generic_end);
type_generic_named_extends.then(type_generic_split); type_generic_named_extends.then(type_generic_split);
StateTree<JavaElement> type_generic_named_super = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals("super"))) StateTree<JavaElement> type_generic_named_super = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals("super"), concat))
.then(new RedirectStateTree<>(type, (global, local) -> global.set(null))); .then(new RedirectStateTree<>(type, (global, local) -> global.set(global.<Token>get().concat(local.get()))));
type_generic_named_super.then(type_generic_end); type_generic_named_super.then(type_generic_end);
type_generic_named_super.then(type_generic_split); type_generic_named_super.then(type_generic_split);
type_.end((a,b) -> a); type_.end();
type_generic_end.end((a,b) -> a); type_generic_end.end();
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_generic_end.then(type_array_begin);
type_array_end.then(type_array_begin);
type_array_end.end((a,b) -> a);
StateTree<JavaElement> clazz_container = new StateTree<>(); StateTree<JavaElement> clazz_container = new StateTree<>();
@ -168,21 +167,12 @@ public class JavaParser extends Parser<JavaElement> {
StateTree<JavaElement> value_container = new StateTree<>(); StateTree<JavaElement> value_container = new StateTree<>();
StateTree<JavaElement> value_list = new StateTree<>(); StateTree<JavaElement> value_list = new StateTree<>();
StateTree<JavaElement> value_list_element = value_list.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null))); StateTree<JavaElement> value_list_element = value_list.then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null)));
value_list_element.end((a,b) -> a); value_list_element.end((a,b) -> a);
value_list_element.then((validator) -> validator.validate((token) -> token.getValue().equals(","))) value_list_element.then((validator) -> validator.validate((token) -> token.getValue().equals(",")))
.then(value_list_element); .then(value_list_element);
StateTree<JavaElement> value_instance = value.then((validator) -> validator.validate((token) -> token.getValue().equals("new"))); StateTree<JavaElement> value_instance = value.then((validator) -> validator.validate((token) -> token.getValue().equals("new")));
StateTree<JavaElement> value_name = new StateTree<JavaElement>(); StateTree<JavaElement> value_name = new StateTree<JavaElement>();
value.then(value_name); value.then(value_name);

View file

@ -67,6 +67,10 @@ public class StateTree<E>{
return this; return this;
} }
public <B> BuilderStateTree<E, B> end(){
return end((a,b) -> null);
}
public <B> BuilderStateTree<E, B> end(BiFunction<E, Bag, B> builder){ public <B> BuilderStateTree<E, B> end(BiFunction<E, Bag, B> builder){
BuilderStateTree<E, B> builderState = new BuilderStateTree<>(builder); BuilderStateTree<E, B> builderState = new BuilderStateTree<>(builder);
this.builder = builderState; this.builder = builderState;

View file

@ -77,10 +77,7 @@ public class ModifierTests{
bag.set(current == null ? m : current+m); bag.set(current == null ? m : current+m);
})) mod = true; })) mod = true;
return mod; return mod;
}).end((a,b) -> { }).end();
// a.bag = b;
return null;
});
return modifier; return modifier;
} }

View file

@ -0,0 +1,106 @@
package be.jeffcheasey88.peeratcode.parser.java;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.function.BiConsumer;
import org.junit.jupiter.api.Test;
import be.jeffcheasey88.peeratcode.parser.Bag;
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.state.RedirectStateTree;
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
public class TypeTests{
public static StateTree<JavaElement> get(){
BiConsumer<Bag, Token> concat = (bag, token) -> {
Token value = bag.get();
if(value == null) bag.set(token);
else bag.set(value.concat(token));
};
StateTree<JavaElement> type = new StateTree<JavaElement>();
StateTree<JavaElement> type_ = type.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME),concat));
type_.then((validator) -> validator.validate((token) -> token.getValue().equals("."), concat))
.then(type_);
StateTree<JavaElement> gen = new StateTree<>();
type_.then(gen);
StateTree<JavaElement> type_begin = gen.then((validator) -> validator.validate((token) -> token.getValue().equals("<"), concat));
StateTree<JavaElement> type_generic = type_begin.then(new RedirectStateTree<>(type, (global, local) -> global.set(global.<Token>get().concat(local.get()))));
StateTree<JavaElement> type_generic_end = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals(">"), concat));
StateTree<JavaElement> type_generic_split = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals(","), concat));
type_generic_split.then(type_generic);
StateTree<JavaElement> type_generic_unknow = type_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("?"), concat));
type_generic_unknow.then(type_generic_end);
type_generic_split.then(type_generic_unknow);
type_generic_unknow.then(type_generic_split);
StateTree<JavaElement> type_generic_unknow_extends = type_generic_unknow.then((validator) -> validator.validate((token) -> token.getValue().equals("extends"), concat))
.then(new RedirectStateTree<>(type, (global, local) -> global.set(global.<Token>get().concat(local.get()))));
type_generic_unknow_extends.then(type_generic_end);
type_generic_unknow_extends.then(type_generic_split);
StateTree<JavaElement> type_generic_unknow_super = type_generic_unknow.then((validator) -> validator.validate((token) -> token.getValue().equals("super"), concat))
.then(new RedirectStateTree<>(type, (global, local) -> global.set(global.<Token>get().concat(local.get()))));
type_generic_unknow_super.then(type_generic_end);
type_generic_unknow_super.then(type_generic_split);
StateTree<JavaElement> type_generic_named_extends = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals("extends"), concat))
.then(new RedirectStateTree<>(type, (global, local) -> global.set(global.<Token>get().concat(local.get()))));
type_generic_named_extends.then(type_generic_end);
type_generic_named_extends.then(type_generic_split);
StateTree<JavaElement> type_generic_named_super = type_generic.then((validator) -> validator.validate((token) -> token.getValue().equals("super"), concat))
.then(new RedirectStateTree<>(type, (global, local) -> global.set(global.<Token>get().concat(local.get()))));
type_generic_named_super.then(type_generic_end);
type_generic_named_super.then(type_generic_split);
type_.end();
type_generic_end.end();
return type;
}
private static Parser<JavaElement> parser = new Parser<JavaElement>(){
{
setTokenizer(ModifierTests.TOKENIZER);
setStateTree(get());
}
};
JavaElement testCase(String value) throws Exception{
TokenValidator.TOKENS = 0;
TokenValidator.MAX_VALIDATE = 0;
JavaElement result = new JavaElement();
parser.parse(value, result);
assertEquals(TokenValidator.TOKENS, TokenValidator.MAX_VALIDATE);
return result;
}
@Test
void baseType() throws Exception{
JavaElement element = testCase("TokenType");
}
@Test
void baseGenericType() throws Exception{
JavaElement element = testCase("TokenType<E>");
}
@Test
void genericType() throws Exception{
JavaElement element = testCase("TokenType<Map<String,E extends Test>>");
}
}