137 lines
3.8 KiB
Java
137 lines
3.8 KiB
Java
package be.jeffcheasey88.peeratcode.parser.java;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.lang.reflect.Modifier;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
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.Tokenizer;
|
|
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
|
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
|
|
|
|
public class ModifierTests{
|
|
|
|
public static Tokenizer TOKENIZER = new Tokenizer(){
|
|
@Override
|
|
public void parse(BufferedReader reader) throws Exception{
|
|
int lineNumber = 0;
|
|
String line;
|
|
boolean longCommentary = false;
|
|
while((line = reader.readLine()) != null){
|
|
lineNumber++;
|
|
|
|
for(int i = 0; i < line.length(); i++){
|
|
char c = line.charAt(i);
|
|
if(longCommentary){
|
|
if(c == '*' && (i < line.length()-1 && line.charAt(i+1) == '/')){
|
|
longCommentary = false;
|
|
i++;
|
|
}
|
|
continue;
|
|
}
|
|
Token token;
|
|
if(isNameValid(c)){
|
|
String value = "";
|
|
int j = i;
|
|
for(; j < line.length(); j++){
|
|
c = line.charAt(j);
|
|
if(isNameValid(c)) value+=c;
|
|
else break;
|
|
}
|
|
token = new Token(lineNumber, i+1, value, TokenType.NAME);
|
|
i = j-1;
|
|
}else if(Character.isWhitespace(c)) continue;
|
|
else{
|
|
if(c == '/' && (i < line.length()-1 && line.charAt(i+1) == '/')) break;
|
|
if(c == '/' && (i < line.length()-1 && line.charAt(i+1) == '*')){
|
|
longCommentary = true;
|
|
continue;
|
|
}
|
|
token = new Token(lineNumber, i+1, ""+c, TokenType.DELIMITER);
|
|
}
|
|
getTokens().add(token);
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean isNameValid(char c) {
|
|
return Character.isAlphabetic(c) || Character.isDigit(c) || c == '_' || c == '$';
|
|
}
|
|
};
|
|
|
|
public static StateTree<JavaElement> get(){
|
|
StateTree<JavaElement> modifier = new StateTree<JavaElement>();
|
|
modifier.then((validator) -> {
|
|
boolean mod = false;
|
|
while(validator.validate(
|
|
(token) -> getModifier(token.getValue()) > 0,
|
|
(bag, token) -> {
|
|
int m = getModifier(token.getValue());
|
|
Integer current = bag.get();
|
|
bag.set(current == null ? m : current+m);
|
|
})) mod = true;
|
|
return mod;
|
|
}).end();
|
|
|
|
return modifier;
|
|
}
|
|
|
|
public static int getModifier(String modifier){
|
|
switch(modifier){
|
|
case "public": return Modifier.PUBLIC;
|
|
case "private": return Modifier.PRIVATE;
|
|
case "protected": return Modifier.PROTECTED;
|
|
case "static": return Modifier.STATIC;
|
|
case "final": return Modifier.FINAL;
|
|
case "synchronized": return Modifier.SYNCHRONIZED;
|
|
case "volatile": return Modifier.VOLATILE;
|
|
case "transient": return Modifier.TRANSIENT;
|
|
case "native": return Modifier.NATIVE;
|
|
case "abstract": return Modifier.ABSTRACT;
|
|
case "strictfp": return Modifier.STRICT;
|
|
case "default": return Modifier.STRICT;
|
|
default: break;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
private static Parser<JavaElement> parser = new Parser<JavaElement>(){
|
|
{
|
|
setTokenizer(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 oneModifier() throws Exception{
|
|
JavaElement element = testCase("public");
|
|
// assertEquals(Modifier.PUBLIC, element.bag.<Integer>get());
|
|
}
|
|
|
|
@Test
|
|
void multiple() throws Exception{
|
|
JavaElement element = testCase("public static final ");
|
|
// assertEquals(Modifier.PUBLIC + Modifier.STATIC + Modifier.FINAL, element.bag.<Integer>get());
|
|
}
|
|
|
|
}
|