diff --git a/src/be/jeffcheasey88/peeratcode/parser/Bag.java b/src/be/jeffcheasey88/peeratcode/parser/Bag.java index d600abf..c5df579 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/Bag.java +++ b/src/be/jeffcheasey88/peeratcode/parser/Bag.java @@ -42,6 +42,10 @@ public class Bag{ return this.map.containsKey(key); } + public void remove(String key){ + this.map.remove(key); + } + public void set(String key, Object value){ this.map.put(key, value); } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java index 52b6c32..0735cee 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java @@ -8,7 +8,7 @@ public class Function extends JavaElement{ private List annotations; - private int modifier; + private int mod; private Token generic; private Token type; private Token name; @@ -17,6 +17,25 @@ public class Function extends JavaElement{ private List elements; - public Function(){} + public Function(int mod, Token type, Token name){ + this.mod = mod; + this.type = type; + this.name = name; + } + + public Function(int mod, Token generic, Token type, Token name){ + this(mod, type, name); + this.generic = generic; + } + + public Function(int mod, Token generic, Token type, Token name, List parameters){ + this(mod, generic, type, name); + this.parameters = parameters; + } + + public Function(int mod, Token generic, Token type, Token name, List parameters, List throwables){ + this(mod, generic, type, name, parameters); + this.throwables = throwables; + } } diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java index c217713..6929e33 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java @@ -14,5 +14,24 @@ public class Variable extends JavaElement{ private boolean elips; private Value value; - public Variable(){} + public Variable(int mod, Token type, Token name){ + this.mod = mod; + this.type = type; + this.name = name; + } + + public Variable(int mod, Token type, Token name, boolean elips){ + this(mod, type, name); + this.elips = elips; + } + + public Variable(int mod, Token type, Token name, boolean elips, Value value){ + this(mod, type, name, elips); + this.value = value; + } + + @Override + public String toString(){ + return "Variable[mod="+mod+", type="+type+", name="+name+", elips="+elips+", value="+value+"]"; + } } diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java b/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java index a037c5a..e148796 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/FunctionTests.java @@ -23,8 +23,10 @@ public class FunctionTests { public static StateTree get(){ BiFunction function_builder = (parent, bag) -> { - System.out.println(bag); - return null; + 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<>(); @@ -71,7 +73,7 @@ public class FunctionTests { })); 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((a,b) -> a); + 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); @@ -89,7 +91,9 @@ public class FunctionTests { 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(","))); + 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); @@ -97,6 +101,27 @@ public class FunctionTests { 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);