[Build] Base Function

This commit is contained in:
jeffcheasey88 2023-09-12 17:09:17 +02:00
parent dccdcaf022
commit e0b498fa93
3 changed files with 179 additions and 5 deletions

View file

@ -0,0 +1,126 @@
package be.jeffcheasey88.peeratcode.parser.java;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
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.BuilderStateTree;
import be.jeffcheasey88.peeratcode.parser.state.InitialStateTree;
import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree;
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
public class FunctionTests {
public static StateTree<JavaElement> get(){
BiFunction<JavaElement, Bag, JavaElement> function_builder = (parent, bag) -> {
System.out.println(bag);
return null;
};
InitialStateTree<JavaElement> function = new InitialStateTree<>();
// function.multiple(annotation);
// BuilderStateTree<JavaElement,JavaElement> function_static = function.then((validator) -> validator.validate((token) -> token.getValue().equals("static")) && validator.validate((token) -> token.getValue().equals("{"))).end((a,b) -> a);
// function_static.multiple(function_container);
// function_static.unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))).end((a,b) -> a);
StateTree<JavaElement> function_mod = function.then(new RedirectStateTree<>(ModifierTests.get(), (global, local) -> {
Integer mod = global.get("mod");
if(mod == null) global.set("mod", local.get());
else global.set("mod", mod+local.<Integer>get());
}));
StateTree<JavaElement> function_generic = function.then(new RedirectStateTree<>(TypeTests.getGeneric(), (global, local) -> global.set("generic", local.get())));
StateTree<JavaElement> function_type = function.then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> global.set("type", local.get())));
function_mod.then(function_mod);
function_mod.then(function_generic);
function_mod.then(function_type);
function_generic.then(function_type);
StateTree<JavaElement> function_name = function.then((validator) -> validator.validate(
(token) -> token.getType().equals(TokenType.NAME),
(bag, token) -> bag.set("name", token)));
function_type.then(function_name);
StateTree<JavaElement> function_begin = function_name.then((validator) -> validator.validate((token) -> token.getValue().equals("(")));
StateTree<JavaElement> function_end = function_begin.then((validator) -> validator.validate((token) -> token.getValue().equals(")")));
function_end.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.end(function_builder)
// .multiple(function_container)
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))).end();
function_end.then((validator) -> validator.validate((token) -> token.getValue().equals(";"))).end();
function_end.then((validator) -> validator.validate((token) -> token.getValue().equals("default")))
// .then(new RedirectStateTree<>(value_container, (global, local) -> global.set(null)))
.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
.end(function_builder);
StateTree<JavaElement> function_throws = function_end.then((validator) -> validator.validate((token) -> token.getValue().equals("throws")))
.then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> {
List<Token> throwables = global.get("throws");
if(throwables == null){
throwables = new ArrayList<>();
global.set("throws", throwables);
}
throwables.add(local.get());
}));
BuilderStateTree<JavaElement,JavaElement> function_start_throws = function_throws.then((validator) -> validator.validate((token) -> token.getValue().equals("{"))).end(function_builder);
// function_start_throws.multiple(function_container);
function_start_throws.unique((validator) -> validator.validate((token) -> token.getValue().equals("}"))).end((a,b) -> a);
function_throws.then((validator) -> validator.validate((token) -> token.getValue().equals(","))).then(function_throws);
function_throws.then((validator) -> validator.validate((token) -> token.getValue().equals(";"))).end(function_builder);
StateTree<JavaElement> function_arg_mod = function_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("final"), (bag, token) -> bag.set("arg_mod", true)));
StateTree<JavaElement> function_arg_type = function_begin.then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> global.set("arg_type", local.get())));
function_arg_mod.then(function_arg_type);
StateTree<JavaElement> function_arg_type_array_begin = function_arg_type.then((validator) -> validator.validate((token) -> token.getValue().equals("["), (bag, token) -> bag.set("arg_type", bag.<Token>get("arg_type").concat(token))));
StateTree<JavaElement> function_arg_type_array_end = function_arg_type_array_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("]"), (bag, token) -> bag.set("arg_type", bag.<Token>get("arg_type").concat(token))));
function_arg_type_array_end.then(function_arg_type_array_begin);
StateTree<JavaElement> function_arg_name = function_arg_type.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME), (bag, token) -> bag.set("arg_name", token)));
function_arg_type_array_end.then(function_arg_name);
function_arg_type.then((validator) ->
validator.validate((token) -> token.getValue().equals(".")) &&
validator.validate((token) -> token.getValue().equals(".")) &&
validator.validate((token) -> token.getValue().equals("."), (bag, token) -> bag.set("arg_elips", true)))
.then(function_arg_name);
StateTree<JavaElement> function_arg_split = function_arg_name.then((validator) -> validator.validate((token) -> token.getValue().equals(",")));
function_arg_split.then(function_arg_mod);
function_arg_split.then(function_arg_type);
function_arg_name.then(function_end);
return function;
}
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 main() throws Exception{
JavaElement result = testCase("public static void main(String[] args){}");
}
}

View file

@ -16,19 +16,21 @@ import be.jeffcheasey88.peeratcode.parser.state.StateTree;
public class TypeTests{
public static StateTree<JavaElement> get(){
private static StateTree<JavaElement> type;
private static StateTree<JavaElement> gen;
static{
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>();
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<>();
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()))));
@ -60,10 +62,17 @@ public class TypeTests{
type_.end();
type_generic_end.end();
}
public static StateTree<JavaElement> get(){
return type;
}
public static StateTree<JavaElement> getGeneric(){
return gen;
}
private static Parser<JavaElement> parser = new Parser<JavaElement>(){
{
setTokenizer(ModifierTests.TOKENIZER);

View file

@ -0,0 +1,39 @@
package be.jeffcheasey88.peeratcode.parser.java;
import static org.junit.jupiter.api.Assertions.assertEquals;
import be.jeffcheasey88.peeratcode.parser.Parser;
import be.jeffcheasey88.peeratcode.parser.TokenValidator;
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
public class ValueTests {
public static StateTree<JavaElement> get(){
StateTree<JavaElement> value_container = new StateTree<>();
return value_container;
}
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;
}
}