|
|
|
@ -1,18 +1,11 @@
|
|
|
|
|
package parser;
|
|
|
|
|
|
|
|
|
|
import ast.BlockNode;
|
|
|
|
|
import ast.ClassNode;
|
|
|
|
|
import ast.LiteralNode;
|
|
|
|
|
import ast.ProgramNode;
|
|
|
|
|
import ast.expression.ExpressionNode;
|
|
|
|
|
import ast.member.FieldNode;
|
|
|
|
|
import ast.member.MemberNode;
|
|
|
|
|
import ast.member.MethodNode;
|
|
|
|
|
import ast.parameter.ParameterListNode;
|
|
|
|
|
import ast.parameter.ParameterNode;
|
|
|
|
|
import ast.statement.ReturnStatementNode;
|
|
|
|
|
import ast.statement.StatementNode;
|
|
|
|
|
import ast.type.*;
|
|
|
|
|
import org.antlr.v4.runtime.*;
|
|
|
|
|
import org.antlr.v4.runtime.tree.ParseTree;
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
@ -49,9 +42,9 @@ public class ParserTest {
|
|
|
|
|
tokenStream.fill();
|
|
|
|
|
|
|
|
|
|
// Prepare the expected results
|
|
|
|
|
List<Token> actualTokens = tokenStream.getTokens();
|
|
|
|
|
List<String> expectedTokens = Arrays.asList("public", "class", "Name", "{", "}", "<EOF>");
|
|
|
|
|
List<String> expectedTokenTypes = Arrays.asList(null, null, "IDENTIFIER", null, null, "EOF");
|
|
|
|
|
List<String> expectedTokenTypes = Arrays.asList("AccessModifier", "Class", "Identifier", "OpenCurlyBracket", "ClosedCurlyBracket", "EOF");
|
|
|
|
|
List<Token> actualTokens = tokenStream.getTokens();
|
|
|
|
|
|
|
|
|
|
// Compare the actual tokens and their types to the expected tokens and their types
|
|
|
|
|
assertEquals(expectedTokens.size(), actualTokens.size());
|
|
|
|
@ -76,33 +69,32 @@ public class ParserTest {
|
|
|
|
|
ParseTree parseTree = parser.program(); // parse the input
|
|
|
|
|
|
|
|
|
|
//Variante 1 (geht)
|
|
|
|
|
String expectedParseTreeAsString = "(program (classDeclaration public class Name { }))";
|
|
|
|
|
String actualParseTreeAsString = parseTree.toStringTree(parser);
|
|
|
|
|
String expectedParseTreeAsString = "(program (classDeclaration (accessType public) class Name { }))";
|
|
|
|
|
|
|
|
|
|
assertEquals(actualParseTreeAsString, expectedParseTreeAsString);
|
|
|
|
|
assertEquals(expectedParseTreeAsString, actualParseTreeAsString);
|
|
|
|
|
|
|
|
|
|
// Variante 2 (geht nicht)
|
|
|
|
|
// - Sollte es gehen und es liegt am Parser? (keine Ahnung) -> Bitte Fehler (actual und expected) durchlesen
|
|
|
|
|
Map<String, Object> actualTreeStructure = buildTreeStructure(parseTree, parser);
|
|
|
|
|
// ist die Methode parseStringToTree() korrekt? -> (glaub nicht)
|
|
|
|
|
Map<String, Object> expectedTreeStructure = parseStringToTree(expectedParseTreeAsString);
|
|
|
|
|
Map<String, Object> actualTreeStructure = buildTreeStructure(parseTree, parser);
|
|
|
|
|
|
|
|
|
|
assertEquals(actualTreeStructure, expectedTreeStructure);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// assertEquals(expectedTreeStructure, actualTreeStructure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void astBuilderTest() {
|
|
|
|
|
// TODO: Implement this test method
|
|
|
|
|
|
|
|
|
|
// ---------------- Aktuellen CompilerInput nachbauen ----------------
|
|
|
|
|
ProgramNode startNode = new ProgramNode();
|
|
|
|
|
// ---------------- Alter CompilerInput nachgebaut ----------------
|
|
|
|
|
// ProgramNode startNode = new ProgramNode();
|
|
|
|
|
// public class CompilerInput {}
|
|
|
|
|
ClassNode compilerInputClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "CompilerInput");
|
|
|
|
|
// ClassNode compilerInputClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "CompilerInput");
|
|
|
|
|
// public int a;
|
|
|
|
|
compilerInputClass.addMember(new FieldNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), new BaseTypeNode(EnumTypeNode.INT), "a"));
|
|
|
|
|
// compilerInputClass.addMember(new FieldNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), new BaseTypeNode(EnumTypeNode.INT), "a"));
|
|
|
|
|
// public static int testMethod(char x) { return 0; }
|
|
|
|
|
compilerInputClass.addMember(
|
|
|
|
|
/* compilerInputClass.addMember(
|
|
|
|
|
new MethodNode(
|
|
|
|
|
new AccessTypeNode(EnumAccessTypeNode.PUBLIC),
|
|
|
|
|
new BaseTypeNode(EnumTypeNode.INT),
|
|
|
|
@ -122,28 +114,48 @@ public class ParserTest {
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//compilerInputClass.addClass(testClass);
|
|
|
|
|
|
|
|
|
|
startNode.addClass(compilerInputClass);
|
|
|
|
|
startNode.addClass(testClass);
|
|
|
|
|
|
|
|
|
|
// ---------------- Aktuellen CompilerInput nachbauen ----------------
|
|
|
|
|
|
|
|
|
|
// startNode.addClass(compilerInputClass);
|
|
|
|
|
// startNode.addClass(testClass);
|
|
|
|
|
|
|
|
|
|
// ---------------- Leere Klasse nachgebaut ----------------
|
|
|
|
|
|
|
|
|
|
ProgramNode expectedASTEmptyClass = new ProgramNode();
|
|
|
|
|
|
|
|
|
|
// public class Name {}
|
|
|
|
|
ClassNode nameClass = new ClassNode("public", "Name");
|
|
|
|
|
|
|
|
|
|
expectedASTEmptyClass.addClass(nameClass);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------- Leere Klasse erzeugt ----------------
|
|
|
|
|
|
|
|
|
|
// init
|
|
|
|
|
CharStream inputCharStream = CharStreams.fromString("public class Name {}");
|
|
|
|
|
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
|
|
|
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
|
|
|
|
tokenStream.fill();
|
|
|
|
|
|
|
|
|
|
/* Parser -> Parsetree */
|
|
|
|
|
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
|
|
|
|
ParseTree parseTreeEmptyClass = parser.program(); // parse the input
|
|
|
|
|
|
|
|
|
|
/* AST builder -> AST */
|
|
|
|
|
ASTBuilder astBuilder = new ASTBuilder();
|
|
|
|
|
// ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
|
|
|
|
ProgramNode actualASTEmptyClass = (ProgramNode) new ASTBuilder().visit(parseTreeEmptyClass);
|
|
|
|
|
|
|
|
|
|
//String actualASTasString = new ASTBuilder().visit(parseTree).toString();
|
|
|
|
|
|
|
|
|
|
// ProgramNode actualAST = new ASTBuilder().visit(parseTree);
|
|
|
|
|
// ProgramNode expectedAST = new ProgramNode();
|
|
|
|
|
// expectedAST.add(new ProgramNode.ClassNode("Name", new ProgramNode()));
|
|
|
|
|
// ---------------- Vergleichen ----------------
|
|
|
|
|
|
|
|
|
|
String expectedASTasString = expectedASTEmptyClass.toString();
|
|
|
|
|
String actualASTasString = new ASTBuilder().visit(parseTreeEmptyClass).toString();
|
|
|
|
|
|
|
|
|
|
// Wie vergleiche ich das?
|
|
|
|
|
assertEquals(expectedASTasString, actualASTasString);
|
|
|
|
|
assertEquals(expectedASTEmptyClass, actualASTEmptyClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -196,7 +208,7 @@ public class ParserTest {
|
|
|
|
|
for (char ch : input.toCharArray()) {
|
|
|
|
|
if (ch == '(') {
|
|
|
|
|
if (depth == 0) {
|
|
|
|
|
if (currentToken.length() > 0) {
|
|
|
|
|
if (!currentToken.isEmpty()) {
|
|
|
|
|
node.put("node", currentToken.toString().trim());
|
|
|
|
|
currentToken.setLength(0);
|
|
|
|
|
}
|
|
|
|
@ -213,7 +225,7 @@ public class ParserTest {
|
|
|
|
|
currentToken.append(ch);
|
|
|
|
|
}
|
|
|
|
|
} else if (Character.isWhitespace(ch) && depth == 0) {
|
|
|
|
|
if (currentToken.length() > 0) {
|
|
|
|
|
if (!currentToken.isEmpty()) {
|
|
|
|
|
node.put("node", currentToken.toString().trim());
|
|
|
|
|
currentToken.setLength(0);
|
|
|
|
|
}
|
|
|
|
@ -222,7 +234,7 @@ public class ParserTest {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentToken.length() > 0) {
|
|
|
|
|
if (!currentToken.isEmpty()) {
|
|
|
|
|
node.put("node", currentToken.toString().trim());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|