[Complete tests] Annotation on class & add parse(String) in Tokenizer
This commit is contained in:
parent
ad22bc03c5
commit
90ec4ae478
3 changed files with 246 additions and 30 deletions
|
@ -54,6 +54,7 @@ public class JavaParser extends Parser<JavaElement> {
|
||||||
Tokenizer tokenizer = new Tokenizer(){
|
Tokenizer tokenizer = new Tokenizer(){
|
||||||
@Override
|
@Override
|
||||||
public void parse(BufferedReader reader) throws Exception{
|
public void parse(BufferedReader reader) throws Exception{
|
||||||
|
System.out.println("tokenizer");
|
||||||
int lineNumber = 0;
|
int lineNumber = 0;
|
||||||
String line;
|
String line;
|
||||||
boolean longCommentary = false;
|
boolean longCommentary = false;
|
||||||
|
@ -133,6 +134,83 @@ public class JavaParser extends Parser<JavaElement> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void parse(String line){
|
||||||
|
System.out.println("tokenizer");
|
||||||
|
int lineNumber = 1;
|
||||||
|
boolean longCommentary = false;
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
// System.out.print(c);
|
||||||
|
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 == '"'){
|
||||||
|
String value = "\"";
|
||||||
|
int j = i+1;
|
||||||
|
for(; j < line.length(); j++){
|
||||||
|
c = line.charAt(j);
|
||||||
|
if(c == '"'){
|
||||||
|
value+=c;
|
||||||
|
j++;
|
||||||
|
break;
|
||||||
|
}else if(c == '\\'){
|
||||||
|
value+=c+line.charAt(++j);
|
||||||
|
}else{
|
||||||
|
value+=c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
token = new Token(lineNumber, i+1, value, TokenType.STRING);
|
||||||
|
i = j-1;
|
||||||
|
System.out.println(token);
|
||||||
|
}else if(c == '\''){
|
||||||
|
String value = "'";
|
||||||
|
int j = i+1;
|
||||||
|
for(; j < line.length(); j++){
|
||||||
|
c = line.charAt(j);
|
||||||
|
if(c == '\''){
|
||||||
|
value+=c;
|
||||||
|
j++;
|
||||||
|
break;
|
||||||
|
}else if(c == '\\'){
|
||||||
|
value+=c+line.charAt(++j);
|
||||||
|
}else{
|
||||||
|
value+=c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
token = new Token(lineNumber, i+1, value, TokenType.CHAR);
|
||||||
|
i = j-1;
|
||||||
|
System.out.println(token);
|
||||||
|
}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) {
|
private boolean isNameValid(char c) {
|
||||||
return Character.isAlphabetic(c) || Character.isDigit(c) || c == '_' || c == '$';
|
return Character.isAlphabetic(c) || Character.isDigit(c) || c == '_' || c == '$';
|
||||||
}
|
}
|
||||||
|
@ -1181,30 +1259,30 @@ public class JavaParser extends Parser<JavaElement> {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception{
|
// public static void main(String[] args) throws Exception{
|
||||||
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\dev\\peerat\\backend\\routes\\users\\ChangePassword.java");
|
// File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\dev\\peerat\\backend\\routes\\users\\ChangePassword.java");
|
||||||
|
//
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
// BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||||
|
//
|
||||||
time = System.currentTimeMillis();
|
// time = System.currentTimeMillis();
|
||||||
|
//
|
||||||
Parser<JavaElement> parser = new JavaParser();
|
// Parser<JavaElement> parser = new JavaParser();
|
||||||
JavaFile jFile = new JavaFile();
|
// JavaFile jFile = new JavaFile();
|
||||||
parser.parse(reader, jFile);
|
// parser.parse(reader, jFile);
|
||||||
|
//
|
||||||
List<Value> values = new ArrayList<>();
|
// List<Value> values = new ArrayList<>();
|
||||||
jFile.findAll((e) -> e instanceof Value, values);
|
// jFile.findAll((e) -> e instanceof Value, values);
|
||||||
System.out.println("Find "+values.size()+" values");
|
// System.out.println("Find "+values.size()+" values");
|
||||||
for(Value v : values) System.out.println(v.getClass().getSimpleName()+" ->"+v);
|
// for(Value v : values) System.out.println(v.getClass().getSimpleName()+" ->"+v);
|
||||||
|
//
|
||||||
|
//
|
||||||
System.out.println((System.currentTimeMillis()-time)+"ms");
|
// System.out.println((System.currentTimeMillis()-time)+"ms");
|
||||||
|
//
|
||||||
System.out.println(TokenValidator.MAX_VALIDATE+" / "+TokenValidator.TOKENS);
|
// System.out.println(TokenValidator.MAX_VALIDATE+" / "+TokenValidator.TOKENS);
|
||||||
|
//
|
||||||
// parser.build(new BufferedWriter(new FileWriter(new File("/home/ParserV2.txt"))));
|
//// parser.build(new BufferedWriter(new FileWriter(new File("/home/ParserV2.txt"))));
|
||||||
Builder builder = new Builder();
|
// Builder builder = new Builder();
|
||||||
jFile.build(builder);
|
// jFile.build(builder);
|
||||||
builder.build(new BufferedWriter(new FileWriter(new File("/home/ParserV2.txt"))));
|
// builder.build(new BufferedWriter(new FileWriter(new File("/home/ParserV2.txt"))));
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,15 +7,11 @@ import org.junit.jupiter.api.Test;
|
||||||
import dev.peerat.parser.Parser;
|
import dev.peerat.parser.Parser;
|
||||||
import dev.peerat.parser.Token;
|
import dev.peerat.parser.Token;
|
||||||
import dev.peerat.parser.TokenValidator;
|
import dev.peerat.parser.TokenValidator;
|
||||||
import dev.peerat.parser.java.Class;
|
|
||||||
import dev.peerat.parser.java.ClassContainer;
|
|
||||||
import dev.peerat.parser.java.JavaElement;
|
|
||||||
import dev.peerat.parser.java.Annotation.AnnotableBuffer;
|
import dev.peerat.parser.java.Annotation.AnnotableBuffer;
|
||||||
import dev.peerat.parser.state.RedirectStateTree;
|
import dev.peerat.parser.state.RedirectStateTree;
|
||||||
import dev.peerat.parser.state.StateTree;
|
import dev.peerat.parser.state.StateTree;
|
||||||
|
|
||||||
public class ClassTests{
|
public class ClassTests{
|
||||||
|
|
||||||
public static StateTree<JavaElement> get(){
|
public static StateTree<JavaElement> get(){
|
||||||
StateTree<JavaElement> clazz_ = new StateTree<>();
|
StateTree<JavaElement> clazz_ = new StateTree<>();
|
||||||
StateTree<JavaElement> clazz = new StateTree<>();
|
StateTree<JavaElement> clazz = new StateTree<>();
|
||||||
|
|
|
@ -0,0 +1,142 @@
|
||||||
|
package dev.peerat.parser.java.element.annotation;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
import dev.peerat.parser.Token;
|
||||||
|
import dev.peerat.parser.TokenType;
|
||||||
|
import dev.peerat.parser.java.Annotation;
|
||||||
|
import dev.peerat.parser.java.Class;
|
||||||
|
import dev.peerat.parser.java.ClassBase;
|
||||||
|
import dev.peerat.parser.java.Value;
|
||||||
|
import dev.peerat.parser.java.element.BaseElementTests;
|
||||||
|
|
||||||
|
public class ClazzAnnotation extends BaseElementTests{
|
||||||
|
|
||||||
|
{
|
||||||
|
register(
|
||||||
|
() -> "package be.jeffcheasey88;"
|
||||||
|
+ ""
|
||||||
|
+ "@Test "
|
||||||
|
+ "public static class Test{}",
|
||||||
|
(javafile) -> {
|
||||||
|
ClassBase clazzb = javafile.getMainClass();
|
||||||
|
assertNotNull(clazzb);
|
||||||
|
assertTrue(clazzb instanceof Class);
|
||||||
|
Class clazz = (Class)clazzb;
|
||||||
|
List<Annotation> annotations = clazz.getAnnotations();
|
||||||
|
assertNotNull(annotations);
|
||||||
|
assertEquals(1, annotations.size());
|
||||||
|
Annotation annotation = annotations.get(0);
|
||||||
|
assertEquals("Test", annotation.getName().getValue());
|
||||||
|
assertNull(annotation.getParameters());
|
||||||
|
});
|
||||||
|
|
||||||
|
register(
|
||||||
|
() -> "package be.jeffcheasey88;"
|
||||||
|
+ ""
|
||||||
|
+ "@be.jeffcheasey88.Test "
|
||||||
|
+ "public static class Test{}",
|
||||||
|
(javafile) -> {
|
||||||
|
ClassBase clazzb = javafile.getMainClass();
|
||||||
|
assertNotNull(clazzb);
|
||||||
|
assertTrue(clazzb instanceof Class);
|
||||||
|
Class clazz = (Class)clazzb;
|
||||||
|
List<Annotation> annotations = clazz.getAnnotations();
|
||||||
|
assertNotNull(annotations);
|
||||||
|
assertEquals(1, annotations.size());
|
||||||
|
Annotation annotation = annotations.get(0);
|
||||||
|
assertEquals("be.jeffcheasey88.Test", annotation.getName().getValue());
|
||||||
|
assertNull(annotation.getParameters());
|
||||||
|
});
|
||||||
|
|
||||||
|
register(
|
||||||
|
() -> "package be.jeffcheasey88;"
|
||||||
|
+ ""
|
||||||
|
+ "@Test() "
|
||||||
|
+ "public static class Test{}",
|
||||||
|
(javafile) -> {
|
||||||
|
ClassBase clazzb = javafile.getMainClass();
|
||||||
|
assertNotNull(clazzb);
|
||||||
|
assertTrue(clazzb instanceof Class);
|
||||||
|
Class clazz = (Class)clazzb;
|
||||||
|
List<Annotation> annotations = clazz.getAnnotations();
|
||||||
|
assertNotNull(annotations);
|
||||||
|
assertEquals(1, annotations.size());
|
||||||
|
Annotation annotation = annotations.get(0);
|
||||||
|
assertEquals("Test", annotation.getName().getValue());
|
||||||
|
assertNull(annotation.getParameters());
|
||||||
|
});
|
||||||
|
|
||||||
|
register(
|
||||||
|
() -> "package be.jeffcheasey88;"
|
||||||
|
+ ""
|
||||||
|
+ "@Test(\"Hello\") "
|
||||||
|
+ "public static class Test{}",
|
||||||
|
(javafile) -> {
|
||||||
|
ClassBase clazzb = javafile.getMainClass();
|
||||||
|
assertNotNull(clazzb);
|
||||||
|
assertTrue(clazzb instanceof Class);
|
||||||
|
Class clazz = (Class)clazzb;
|
||||||
|
List<Annotation> annotations = clazz.getAnnotations();
|
||||||
|
assertNotNull(annotations);
|
||||||
|
assertEquals(1, annotations.size());
|
||||||
|
Annotation annotation = annotations.get(0);
|
||||||
|
assertEquals("Test", annotation.getName().getValue());
|
||||||
|
assertNotNull(annotation.getParameters());
|
||||||
|
Value value = annotation.getParameters().get(null);
|
||||||
|
assertNotNull(value);
|
||||||
|
Token token = value.getToken();
|
||||||
|
assertNotNull(token);
|
||||||
|
assertEquals(TokenType.STRING, token.getType());
|
||||||
|
assertEquals("\"Hello\"", token.getValue());
|
||||||
|
});
|
||||||
|
|
||||||
|
register(
|
||||||
|
() -> "package be.jeffcheasey88;"
|
||||||
|
+ ""
|
||||||
|
+ "@Test(offset=8) "
|
||||||
|
+ "public static class Test{}",
|
||||||
|
(javafile) -> {
|
||||||
|
ClassBase clazzb = javafile.getMainClass();
|
||||||
|
assertNotNull(clazzb);
|
||||||
|
assertTrue(clazzb instanceof Class);
|
||||||
|
Class clazz = (Class)clazzb;
|
||||||
|
List<Annotation> annotations = clazz.getAnnotations();
|
||||||
|
assertNotNull(annotations);
|
||||||
|
assertEquals(1, annotations.size());
|
||||||
|
Annotation annotation = annotations.get(0);
|
||||||
|
assertEquals("Test", annotation.getName().getValue());
|
||||||
|
assertNotNull(annotation.getParameters());
|
||||||
|
assertEquals(1, annotation.getParameters().size());
|
||||||
|
Entry<Token, Value> entry = annotation.getParameters().entrySet().iterator().next();
|
||||||
|
assertEquals("offset", entry.getKey().getValue());
|
||||||
|
assertEquals("8", entry.getValue().getToken().getValue());
|
||||||
|
});
|
||||||
|
|
||||||
|
register(
|
||||||
|
() -> "package be.jeffcheasey88;"
|
||||||
|
+ ""
|
||||||
|
+ "@Test(offset=8,concat=\",\") "
|
||||||
|
+ "public static class Test{}",
|
||||||
|
(javafile) -> {
|
||||||
|
ClassBase clazzb = javafile.getMainClass();
|
||||||
|
assertNotNull(clazzb);
|
||||||
|
assertTrue(clazzb instanceof Class);
|
||||||
|
Class clazz = (Class)clazzb;
|
||||||
|
List<Annotation> annotations = clazz.getAnnotations();
|
||||||
|
assertNotNull(annotations);
|
||||||
|
assertEquals(1, annotations.size());
|
||||||
|
Annotation annotation = annotations.get(0);
|
||||||
|
assertEquals("Test", annotation.getName().getValue());
|
||||||
|
assertNotNull(annotation.getParameters());
|
||||||
|
assertEquals(2, annotation.getParameters().size());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue