98 lines
4.2 KiB
Java
98 lines
4.2 KiB
Java
package be.jeffcheasey88.peeratcode.parser.java;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import be.jeffcheasey88.peeratcode.parser.Parser;
|
|
import be.jeffcheasey88.peeratcode.parser.Token;
|
|
import be.jeffcheasey88.peeratcode.parser.TokenValidator;
|
|
import be.jeffcheasey88.peeratcode.parser.java.Annotation.AnnotableBuffer;
|
|
import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree;
|
|
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
|
|
|
|
public class ClassTests{
|
|
|
|
public static StateTree<JavaElement> get(){
|
|
StateTree<JavaElement> clazz_ = new StateTree<>();
|
|
StateTree<JavaElement> clazz = new StateTree<>();
|
|
|
|
// clazz_container.then(clazz_);
|
|
|
|
StateTree<JavaElement> clazz_base = clazz.then((validator) -> validator.validate((token) -> token.getValue().equals("class")))
|
|
.then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> global.set("name", local.get())));
|
|
clazz_base.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
|
|
.<JavaElement>end((parent, bag) -> {
|
|
Class current = new Class((((AnnotableBuffer)parent).getAnnotationBuffer()), bag.get("name"));
|
|
if(parent instanceof ClassContainer) ((ClassContainer)parent).addClass(current);
|
|
return current;
|
|
})
|
|
// .multiple(clazz_container)
|
|
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
|
|
.end((a,b) -> a);
|
|
StateTree<JavaElement> clazz_implement = clazz_base.then((validator) -> validator.validate((token) -> token.getValue().equals("implements")));
|
|
StateTree<JavaElement> clazz_implement_name = clazz_implement.then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> {
|
|
Token token = global.get("implement");
|
|
if(token == null) global.set("implement", local.get());
|
|
else global.set("implement", token.concat(local.get()));
|
|
}));
|
|
clazz_implement_name.then((validator) -> validator.validate(
|
|
(token) -> token.getValue().equals(","),
|
|
(bag, token) -> bag.set("implement", bag.<Token>get("implement").concat(token)))).then(clazz_implement_name);
|
|
clazz_implement_name.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
|
|
.<JavaElement>end((parent, bag) -> {
|
|
Class current = new Class((((AnnotableBuffer)parent).getAnnotationBuffer()),bag.get("name"), bag.get("extend"), bag.get("implements"));
|
|
if(parent instanceof ClassContainer) ((ClassContainer)parent).addClass(current);
|
|
return current;
|
|
})
|
|
// .multiple(clazz_container)
|
|
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
|
|
.end((a,b) -> a);
|
|
StateTree<JavaElement> clazz_extend = clazz_base.then((validator) -> validator.validate((token) -> token.getValue().equals("extends")))
|
|
.then(new RedirectStateTree<>(TypeTests.get(), (global, local) -> global.set("extend", local.get())));
|
|
clazz_extend.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
|
|
.<JavaElement>end((parent, bag) -> {
|
|
Class current = new Class((((AnnotableBuffer)parent).getAnnotationBuffer()), bag.get("name"), bag.get("extend"), bag.get("implements"));
|
|
if(parent instanceof ClassContainer) ((ClassContainer)parent).addClass(current);
|
|
return current;
|
|
})
|
|
// .multiple(clazz_container)
|
|
.unique((validator) -> validator.validate((token) -> token.getValue().equals("}")))
|
|
.end((a,b) -> a);
|
|
clazz_extend.then(clazz_implement);
|
|
|
|
|
|
StateTree<JavaElement> clazz_mod = clazz_.then(new RedirectStateTree<>(ModifierTests.get(), (global, local) -> global.set("modifier", local)));
|
|
clazz_.then(clazz);
|
|
clazz_mod.then(clazz);
|
|
|
|
return clazz_;
|
|
}
|
|
|
|
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 = null;
|
|
|
|
parser.parse(value, result);
|
|
|
|
assertEquals(TokenValidator.TOKENS, TokenValidator.MAX_VALIDATE);
|
|
|
|
return result;
|
|
}
|
|
|
|
@Test
|
|
void main() throws Exception{
|
|
JavaElement result = testCase("public static class Test extends MyTest<List<String>> implements Ahbon{}");
|
|
}
|
|
|
|
}
|