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 get(){ BiFunction function_builder = (parent, bag) -> { buildVariable(bag); Integer mod = bag.get("mod"); Function function = new Function(mod == null ? 0 : mod, bag.get("generic"), bag.get("type"), bag.get("name"), bag.get("param"), bag.get("throws")); return function; }; InitialStateTree function = new InitialStateTree<>(); // function.multiple(annotation); // BuilderStateTree 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 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.get()); })); StateTree function_generic = function.then(new RedirectStateTree<>(TypeTests.getGeneric(), (global, local) -> global.set("generic", local.get()))); StateTree 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 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 function_begin = function_name.then((validator) -> validator.validate((token) -> token.getValue().equals("("))); StateTree 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 function_throws = function_end.then((validator) -> validator.validate((token) -> token.getValue().equals("throws"))) .then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> { List throwables = global.get("throws"); if(throwables == null){ throwables = new ArrayList<>(); global.set("throws", throwables); } throwables.add(local.get()); })); BuilderStateTree 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(); 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 function_arg_mod = function_begin.then((validator) -> validator.validate((token) -> token.getValue().equals("final"), (bag, token) -> bag.set("arg_mod", true))); StateTree 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 function_arg_type_array_begin = function_arg_type.then((validator) -> validator.validate((token) -> token.getValue().equals("["), (bag, token) -> bag.set("arg_type", bag.get("arg_type").concat(token)))); StateTree 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.get("arg_type").concat(token)))); function_arg_type_array_end.then(function_arg_type_array_begin); StateTree 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 function_arg_split = function_arg_name.then((validator) -> validator.validate((token) -> token.getValue().equals(","), (bag, token) -> { buildVariable(bag); })); function_arg_split.then(function_arg_mod); function_arg_split.then(function_arg_type); function_arg_name.then(function_end); return function; } private static void buildVariable(Bag bag){ if(!bag.has("arg_name")) return; List parameters = bag.get("param"); if(parameters == null){ parameters = new ArrayList<>(); bag.set("param", parameters); } Integer mod = bag.get("arg_mod"); Variable variable = new Variable(mod == null ? 0 : mod, bag.get("arg_type"), bag.get("arg_name"), bag.get("arg_elips") == Boolean.TRUE); bag.remove("arg_name"); bag.remove("arg_mod"); bag.remove("arg_type"); bag.remove("arg_name"); bag.remove("arg_elips"); parameters.add(variable); } private static Parser parser = new Parser(){ { 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){}"); } }