[Build] Type
This commit is contained in:
parent
e2b62bb58e
commit
dccdcaf022
4 changed files with 137 additions and 40 deletions
|
@ -8,7 +8,9 @@ import java.io.FileWriter;
|
|||
import java.lang.reflect.Modifier;
|
||||
import java.util.LinkedList;
|
||||
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.Token;
|
||||
import be.jeffcheasey88.peeratcode.parser.TokenType;
|
||||
|
@ -106,54 +108,51 @@ public class JavaParser extends Parser<JavaElement> {
|
|||
bag.set(current == null ? m : current+m);
|
||||
})) mod = true;
|
||||
return mod;
|
||||
}).end((a,b) -> null);
|
||||
}).end();
|
||||
|
||||
//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_ = type.then((validator) ->
|
||||
validator.validate(
|
||||
(token) -> token.getType().equals(TokenType.NAME),
|
||||
(bag, token) -> bag.set(token)));
|
||||
type_.then((validator) -> validator.validate((token) -> token.getValue().equals(".")))
|
||||
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("<")));
|
||||
StateTree<JavaElement> type_generic = type_begin.then(new RedirectStateTree<>(type, (global, local) -> global.set(null)));
|
||||
StateTree<JavaElement> type_generic_end = 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(",")));
|
||||
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("?")));
|
||||
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")))
|
||||
.then(new RedirectStateTree<>(type, (global, local) -> global.set(null)));
|
||||
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")))
|
||||
.then(new RedirectStateTree<>(type, (global, local) -> global.set(null)));
|
||||
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")))
|
||||
.then(new RedirectStateTree<>(type, (global, local) -> global.set(null)));
|
||||
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")))
|
||||
.then(new RedirectStateTree<>(type, (global, local) -> global.set(null)));
|
||||
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((a,b) -> a);
|
||||
type_generic_end.end((a,b) -> a);
|
||||
|
||||
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);
|
||||
type_.end();
|
||||
type_generic_end.end();
|
||||
|
||||
|
||||
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_list = new StateTree<>();
|
||||
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.then((validator) -> validator.validate((token) -> token.getValue().equals(",")))
|
||||
.then(value_list_element);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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);
|
||||
|
|
|
@ -67,6 +67,10 @@ public class StateTree<E>{
|
|||
return this;
|
||||
}
|
||||
|
||||
public <B> BuilderStateTree<E, B> end(){
|
||||
return end((a,b) -> null);
|
||||
}
|
||||
|
||||
public <B> BuilderStateTree<E, B> end(BiFunction<E, Bag, B> builder){
|
||||
BuilderStateTree<E, B> builderState = new BuilderStateTree<>(builder);
|
||||
this.builder = builderState;
|
||||
|
|
|
@ -77,10 +77,7 @@ public class ModifierTests{
|
|||
bag.set(current == null ? m : current+m);
|
||||
})) mod = true;
|
||||
return mod;
|
||||
}).end((a,b) -> {
|
||||
// a.bag = b;
|
||||
return null;
|
||||
});
|
||||
}).end();
|
||||
|
||||
return modifier;
|
||||
}
|
||||
|
|
106
test/be/jeffcheasey88/peeratcode/parser/java/TypeTests.java
Normal file
106
test/be/jeffcheasey88/peeratcode/parser/java/TypeTests.java
Normal 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>>");
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue