Adding comments to ASTBuilder und ANTL Grammar
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Purplumbi504 2024-07-04 21:57:53 +02:00
parent 2b7e0d0d42
commit 3c0f7e857a
2 changed files with 392 additions and 90 deletions

View File

@ -30,7 +30,18 @@ import parser.generated.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* ASTBuilder is a visitor that converts the parse tree into an abstract syntax tree (AST).
*/
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> { public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
/**
* Visits a program node and creates a ProgramNode.
* It iterates through all class declarations in the context and adds them to the ProgramNode.
*
* @param ctx the program context
* @return the AST node for the program
*/
@Override @Override
public ASTNode visitProgram(SimpleJavaParser.ProgramContext ctx) { public ASTNode visitProgram(SimpleJavaParser.ProgramContext ctx) {
ProgramNode program = new ProgramNode(); ProgramNode program = new ProgramNode();
@ -40,6 +51,14 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return program; return program;
} }
/**
* Visits a class declaration and creates a ClassNode.
* It checks for an access modifier, processes member declarations, and ensures the class has a constructor.
*
* @param ctx the class declaration context
* @return the AST node for the class
*/
@Override @Override
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) { public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
ClassNode classNode; ClassNode classNode;
@ -71,6 +90,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return classNode; return classNode;
} }
/**
* Visits a constructor declaration and creates a ConstructorNode.
* It processes access modifiers, block statements, and parameters.
*
* @param ctx the constructor declaration context
* @return the AST node for the constructor
*/
@Override @Override
public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) { public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) {
ConstructorNode constructorNode; ConstructorNode constructorNode;
@ -88,6 +114,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return constructorNode; return constructorNode;
} }
/**
* Visits a method declaration and creates a MethodNode or MainMethodNode.
* It handles method types, access modifiers, block statements, and parameters.
*
* @param ctx the method declaration context
* @return the AST node for the method
*/
@Override @Override
public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) { public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) {
if(ctx.MainMethodDeclaration() != null) { if(ctx.MainMethodDeclaration() != null) {
@ -137,6 +170,14 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
} }
} }
/**
* Visits a field declaration and creates a FieldNode.
* It handles access modifiers and types.
*
* @param ctx the field declaration context
* @return the AST node for the field
*/
@Override @Override
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) { public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
if(ctx.AccessModifier() != null) { if(ctx.AccessModifier() != null) {
@ -146,11 +187,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
} }
} }
/**
* Visits a parameter and creates a ParameterNode.
* It handles the type and identifier of the parameter.
*
* @param ctx the parameter context
* @return the AST node for the parameter
*/
@Override @Override
public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) { public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) {
return new ParameterNode(createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); return new ParameterNode(createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
} }
/**
* Visits a statement and creates the corresponding AST node.
* It determines the specific type of statement and processes it accordingly.
*
* @param ctx the statement context
* @return the AST node for the statement
*/
@Override @Override
public ASTNode visitStatement(SimpleJavaParser.StatementContext ctx) { public ASTNode visitStatement(SimpleJavaParser.StatementContext ctx) {
if(ctx.returnStatement() != null) { if(ctx.returnStatement() != null) {
@ -175,11 +230,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a return statement and creates a ReturnNode.
* It processes the expression in the return statement.
*
* @param ctx the return statement context
* @return the AST node for the return statement
*/
@Override @Override
public ASTNode visitReturnStatement(SimpleJavaParser.ReturnStatementContext ctx) { public ASTNode visitReturnStatement(SimpleJavaParser.ReturnStatementContext ctx) {
return new ReturnNode((IExpressionNode) visit(ctx.expression())); return new ReturnNode((IExpressionNode) visit(ctx.expression()));
} }
/**
* Visits a return statement and creates a ReturnNode.
* It processes the expression in the return statement.
*
* @param ctx the return statement context
* @return the AST node for the return statement
*/
@Override @Override
public ASTNode visitLocalVariableDeclaration(SimpleJavaParser.LocalVariableDeclarationContext ctx) { public ASTNode visitLocalVariableDeclaration(SimpleJavaParser.LocalVariableDeclarationContext ctx) {
if(ctx.Assign() != null) { if(ctx.Assign() != null) {
@ -190,6 +259,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
} }
/**
* Visits a block statement and creates a BlockNode.
* It processes all statements within the block.
*
* @param ctx the block statement context
* @return the AST node for the block statement
*/
@Override @Override
public ASTNode visitBlockStatement(SimpleJavaParser.BlockStatementContext ctx) { public ASTNode visitBlockStatement(SimpleJavaParser.BlockStatementContext ctx) {
BlockNode blockNode = new BlockNode(); BlockNode blockNode = new BlockNode();
@ -199,11 +275,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return blockNode; return blockNode;
} }
/**
* Visits a while statement and creates a WhileNode.
* It processes the condition expression and the block statement.
*
* @param ctx the while statement context
* @return the AST node for the while statement
*/
@Override @Override
public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) { public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) {
return new WhileNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement())); return new WhileNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement()));
} }
/**
* Visits a do-while statement and creates a BlockNode containing a WhileNode.
* It processes the condition expression and the block statement.
*
* @param ctx the do-while statement context
* @return the AST node for the do-while statement
*/
@Override @Override
public ASTNode visitDoWhileStatement(SimpleJavaParser.DoWhileStatementContext ctx) { public ASTNode visitDoWhileStatement(SimpleJavaParser.DoWhileStatementContext ctx) {
IExpressionNode condition = (IExpressionNode) visit(ctx.expression()); IExpressionNode condition = (IExpressionNode) visit(ctx.expression());
@ -217,6 +307,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return resultBlock; return resultBlock;
} }
/**
* Visits a for statement and creates a BlockNode that contains a WhileNode.
* It processes the initialization, condition, and increment parts of the for loop.
*
* @param ctx the for statement context
* @return the AST node for the for statement
*/
@Override @Override
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) { public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
List<IStatementNode> statements = new ArrayList<>(); List<IStatementNode> statements = new ArrayList<>();
@ -279,6 +376,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return resultBlock; return resultBlock;
} }
/**
* Visits an if-else statement and creates an IfElseNode.
* It processes the if, else-if, and else parts of the statement.
*
* @param ctx the if-else statement context
* @return the AST node for the if-else statement
*/
@Override @Override
public ASTNode visitIfElseStatement(SimpleJavaParser.IfElseStatementContext ctx) { public ASTNode visitIfElseStatement(SimpleJavaParser.IfElseStatementContext ctx) {
IfElseNode ifElseStatementNode; IfElseNode ifElseStatementNode;
@ -296,21 +400,49 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return ifElseStatementNode; return ifElseStatementNode;
} }
/**
* Visits an if statement and creates an IfNode.
* It processes the condition expression and the block statement.
*
* @param ctx the if statement context
* @return the AST node for the if statement
*/
@Override @Override
public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) { public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) {
return new IfNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement())); return new IfNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement()));
} }
/**
* Visits an else-if statement and creates an IfNode.
* It processes the condition expression and the block statement.
*
* @param ctx the else-if statement context
* @return the AST node for the else-if statement
*/
@Override @Override
public ASTNode visitElseIfStatement(SimpleJavaParser.ElseIfStatementContext ctx) { public ASTNode visitElseIfStatement(SimpleJavaParser.ElseIfStatementContext ctx) {
return new IfNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement())); return new IfNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement()));
} }
/**
* Visits an else statement and creates an ElseNode.
* It processes the block statement of the else part.
*
* @param ctx the else statement context
* @return the AST node for the else statement
*/
@Override @Override
public ASTNode visitElseStatement(SimpleJavaParser.ElseStatementContext ctx) { public ASTNode visitElseStatement(SimpleJavaParser.ElseStatementContext ctx) {
return new ElseNode((BlockNode) visit(ctx.blockStatement())); return new ElseNode((BlockNode) visit(ctx.blockStatement()));
} }
/**
* Visits a statement expression and creates the corresponding AST node.
* It handles assignments, new declarations, method calls, and increment/decrement expressions.
*
* @param ctx the statement expression context
* @return the AST node for the statement expression
*/
@Override @Override
public ASTNode visitStatementExpression(SimpleJavaParser.StatementExpressionContext ctx) { public ASTNode visitStatementExpression(SimpleJavaParser.StatementExpressionContext ctx) {
if(ctx.assign() != null) { if(ctx.assign() != null) {
@ -325,6 +457,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a switch statement and creates an IfElseNode representing the switch cases.
* It processes the switch expression, case statements, and default statement.
*
* @param ctx the switch statement context
* @return the AST node for the switch statement
*/
@Override @Override
public ASTNode visitSwitchStatement(SimpleJavaParser.SwitchStatementContext ctx) { public ASTNode visitSwitchStatement(SimpleJavaParser.SwitchStatementContext ctx) {
UnaryNode switchExpression = (UnaryNode) visit(ctx.expression()); UnaryNode switchExpression = (UnaryNode) visit(ctx.expression());
@ -368,11 +507,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return ifElseNode; return ifElseNode;
} }
/**
* Visits an assignment expression and creates an AssignNode.
* It handles the assignable expression and the assigned expression.
*
* @param ctx the assignment context
* @return the AST node for the assignment
*/
@Override @Override
public ASTNode visitAssign(SimpleJavaParser.AssignContext ctx) { public ASTNode visitAssign(SimpleJavaParser.AssignContext ctx) {
return new AssignNode((AssignableNode) visit(ctx.assignableExpression()), (IExpressionNode) visit(ctx.expression())); return new AssignNode((AssignableNode) visit(ctx.assignableExpression()), (IExpressionNode) visit(ctx.expression()));
} }
/**
* Visits a new declaration and creates a NewDeclarationNode.
* It processes the identifier and the argument list.
*
* @param ctx the new declaration context
* @return the AST node for the new declaration
*/
@Override @Override
public ASTNode visitNewDeclaration(SimpleJavaParser.NewDeclarationContext ctx) { public ASTNode visitNewDeclaration(SimpleJavaParser.NewDeclarationContext ctx) {
NewDeclarationNode newDeclarationNode = new NewDeclarationNode(ctx.Identifier().getText()); NewDeclarationNode newDeclarationNode = new NewDeclarationNode(ctx.Identifier().getText());
@ -382,6 +535,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return newDeclarationNode; return newDeclarationNode;
} }
/**
* Visits a method call and creates a MethodCallNode.
* It processes the target, chained methods, and argument list.
*
* @param ctx the method call context
* @return the AST node for the method call
*/
@Override @Override
public ASTNode visitMethodCall(SimpleJavaParser.MethodCallContext ctx) { public ASTNode visitMethodCall(SimpleJavaParser.MethodCallContext ctx) {
MethodCallNode methodCallStatementExpressionNode; MethodCallNode methodCallStatementExpressionNode;
@ -399,6 +559,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return methodCallStatementExpressionNode; return methodCallStatementExpressionNode;
} }
/**
* Visits the target of a method call and creates a TargetNode.
* It handles various types of targets including 'this', member access, new declarations, and identifiers.
*
* @param ctx the target context
* @return the AST node for the target
*/
@Override @Override
public ASTNode visitTarget(SimpleJavaParser.TargetContext ctx) { public ASTNode visitTarget(SimpleJavaParser.TargetContext ctx) {
if(ctx.This() != null) { if(ctx.This() != null) {
@ -413,6 +580,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a chained method and creates a ChainedMethodNode.
* It processes the identifier and the argument list.
*
* @param ctx the chained method context
* @return the AST node for the chained method
*/
@Override @Override
public ASTNode visitChainedMethod(SimpleJavaParser.ChainedMethodContext ctx) { public ASTNode visitChainedMethod(SimpleJavaParser.ChainedMethodContext ctx) {
ChainedMethodNode chainedMethodNode = new ChainedMethodNode(ctx.Identifier().getText()); ChainedMethodNode chainedMethodNode = new ChainedMethodNode(ctx.Identifier().getText());
@ -422,6 +596,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return chainedMethodNode; return chainedMethodNode;
} }
/**
* Visits an increment or decrement expression and creates the corresponding AST node.
* It handles both prefix and suffix forms of increment and decrement.
*
* @param ctx the crement expression context
* @return the AST node for the crement expression
*/
@Override @Override
public ASTNode visitCrementExpression(SimpleJavaParser.CrementExpressionContext ctx) { public ASTNode visitCrementExpression(SimpleJavaParser.CrementExpressionContext ctx) {
if(ctx.incrementExpression() != null) { if(ctx.incrementExpression() != null) {
@ -432,6 +613,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits an increment expression and creates an IncrementNode.
* It handles both prefix and suffix forms.
*
* @param ctx the increment expression context
* @return the AST node for the increment expression
*/
@Override @Override
public ASTNode visitIncrementExpression(SimpleJavaParser.IncrementExpressionContext ctx) { public ASTNode visitIncrementExpression(SimpleJavaParser.IncrementExpressionContext ctx) {
if(ctx.prefixIncrementExpression() != null) { if(ctx.prefixIncrementExpression() != null) {
@ -442,16 +630,35 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a prefix increment expression and creates an IncrementNode.
*
* @param ctx the prefix increment expression context
* @return the AST node for the prefix increment expression
*/
@Override @Override
public ASTNode visitPrefixIncrementExpression(SimpleJavaParser.PrefixIncrementExpressionContext ctx) { public ASTNode visitPrefixIncrementExpression(SimpleJavaParser.PrefixIncrementExpressionContext ctx) {
return new IncrementNode(CrementType.PREFIX, (AssignableNode) visit(ctx.assignableExpression())); return new IncrementNode(CrementType.PREFIX, (AssignableNode) visit(ctx.assignableExpression()));
} }
/**
* Visits a suffix increment expression and creates an IncrementNode.
*
* @param ctx the suffix increment expression context
* @return the AST node for the suffix increment expression
*/
@Override @Override
public ASTNode visitSuffixIncrementExpression(SimpleJavaParser.SuffixIncrementExpressionContext ctx) { public ASTNode visitSuffixIncrementExpression(SimpleJavaParser.SuffixIncrementExpressionContext ctx) {
return new IncrementNode(CrementType.SUFFIX, (AssignableNode) visit(ctx.assignableExpression())); return new IncrementNode(CrementType.SUFFIX, (AssignableNode) visit(ctx.assignableExpression()));
} }
/**
* Visits a decrement expression and creates a DecrementNode.
* It handles both prefix and suffix forms.
*
* @param ctx the decrement expression context
* @return the AST node for the decrement expression
*/
@Override @Override
public ASTNode visitDecrementExpression(SimpleJavaParser.DecrementExpressionContext ctx) { public ASTNode visitDecrementExpression(SimpleJavaParser.DecrementExpressionContext ctx) {
if(ctx.prefixDecrementExpression() != null) { if(ctx.prefixDecrementExpression() != null) {
@ -462,16 +669,35 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a prefix decrement expression and creates a DecrementNode.
*
* @param ctx the prefix decrement expression context
* @return the AST node for the prefix decrement expression
*/
@Override @Override
public ASTNode visitPrefixDecrementExpression(SimpleJavaParser.PrefixDecrementExpressionContext ctx) { public ASTNode visitPrefixDecrementExpression(SimpleJavaParser.PrefixDecrementExpressionContext ctx) {
return new DecrementNode(CrementType.PREFIX, (AssignableNode) visit(ctx.assignableExpression())); return new DecrementNode(CrementType.PREFIX, (AssignableNode) visit(ctx.assignableExpression()));
} }
/**
* Visits a suffix decrement expression and creates a DecrementNode.
*
* @param ctx the suffix decrement expression context
* @return the AST node for the suffix decrement expression
*/
@Override @Override
public ASTNode visitSuffixDecrementExpression(SimpleJavaParser.SuffixDecrementExpressionContext ctx) { public ASTNode visitSuffixDecrementExpression(SimpleJavaParser.SuffixDecrementExpressionContext ctx) {
return new DecrementNode(CrementType.SUFFIX, (AssignableNode) visit(ctx.assignableExpression())); return new DecrementNode(CrementType.SUFFIX, (AssignableNode) visit(ctx.assignableExpression()));
} }
/**
* Visits an expression and creates the corresponding AST node.
* It handles both unary and binary expressions.
*
* @param ctx the expression context
* @return the AST node for the expression
*/
@Override @Override
public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) { public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) {
if(ctx.unaryExpression() != null) { if(ctx.unaryExpression() != null) {
@ -482,6 +708,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a unary expression and creates a UnaryNode.
* It handles various forms of unary expressions including 'this', identifiers, member access, values, and more.
*
* @param ctx the unary expression context
* @return the AST node for the unary expression
*/
@Override @Override
public ASTNode visitUnaryExpression(SimpleJavaParser.UnaryExpressionContext ctx) { public ASTNode visitUnaryExpression(SimpleJavaParser.UnaryExpressionContext ctx) {
if(ctx.This() != null) { if(ctx.This() != null) {
@ -502,6 +735,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a member access expression and creates a MemberAccessNode.
* It processes the 'this' keyword and identifiers for member access.
*
* @param ctx the member access context
* @return the AST node for the member access
*/
@Override @Override
public ASTNode visitMemberAccess(SimpleJavaParser.MemberAccessContext ctx) { public ASTNode visitMemberAccess(SimpleJavaParser.MemberAccessContext ctx) {
MemberAccessNode memberAccessNode; MemberAccessNode memberAccessNode;
@ -516,6 +756,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return memberAccessNode; return memberAccessNode;
} }
/**
* Visits a value and creates a ValueNode.
* It handles integer, boolean, char, and null values.
*
* @param ctx the value context
* @return the AST node for the value
*/
@Override @Override
public ASTNode visitValue(SimpleJavaParser.ValueContext ctx) { public ASTNode visitValue(SimpleJavaParser.ValueContext ctx) {
if(ctx.IntValue() != null) { if(ctx.IntValue() != null) {
@ -530,12 +777,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a not expression and creates a NotNode.
* It processes the expression to which the 'not' operator is applied.
*
* @param ctx the not expression context
* @return the AST node for the not expression
*/
@Override @Override
public ASTNode visitNotExpression(SimpleJavaParser.NotExpressionContext ctx) { public ASTNode visitNotExpression(SimpleJavaParser.NotExpressionContext ctx) {
return new NotNode((IExpressionNode) visitExpression(ctx.expression())); return new NotNode((IExpressionNode) visitExpression(ctx.expression()));
} }
/**
* Visits a binary expression and creates the corresponding AST node.
* It handles both calculation and non-calculation expressions.
*
* @param ctx the binary expression context
* @return the AST node for the binary expression
*/
@Override @Override
public ASTNode visitBinaryExpression(SimpleJavaParser.BinaryExpressionContext ctx) { public ASTNode visitBinaryExpression(SimpleJavaParser.BinaryExpressionContext ctx) {
if(ctx.calculationExpression() != null) { if(ctx.calculationExpression() != null) {
@ -546,6 +806,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a calculation expression and creates a CalculationNode.
* It handles line operators and dot expressions.
*
* @param ctx the calculation expression context
* @return the AST node for the calculation expression
*/
@Override @Override
public ASTNode visitCalculationExpression(SimpleJavaParser.CalculationExpressionContext ctx) { public ASTNode visitCalculationExpression(SimpleJavaParser.CalculationExpressionContext ctx) {
if(ctx.calculationExpression() != null) { if(ctx.calculationExpression() != null) {
@ -556,6 +823,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a dot expression and creates a DotNode.
* It handles dot operators and dot subtraction expressions.
*
* @param ctx the dot expression context
* @return the AST node for the dot expression
*/
@Override @Override
public ASTNode visitDotExpression(SimpleJavaParser.DotExpressionContext ctx) { public ASTNode visitDotExpression(SimpleJavaParser.DotExpressionContext ctx) {
if(ctx.dotExpression() != null) { if(ctx.dotExpression() != null) {
@ -566,6 +840,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a dot subtraction expression and creates a DotSubtractionNode.
* It handles integer values, identifiers, member access, and method calls.
*
* @param ctx the dot subtraction expression context
* @return the AST node for the dot subtraction expression
*/
@Override @Override
public ASTNode visitDotSubtractionExpression(SimpleJavaParser.DotSubtractionExpressionContext ctx) { public ASTNode visitDotSubtractionExpression(SimpleJavaParser.DotSubtractionExpressionContext ctx) {
if(ctx.IntValue() != null) { if(ctx.IntValue() != null) {
@ -580,11 +861,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a non-calculation expression and creates a NonCalculationNode.
* It handles unary expressions combined with non-calculation operators.
*
* @param ctx the non-calculation expression context
* @return the AST node for the non-calculation expression
*/
@Override @Override
public ASTNode visitNonCalculationExpression(SimpleJavaParser.NonCalculationExpressionContext ctx) { public ASTNode visitNonCalculationExpression(SimpleJavaParser.NonCalculationExpressionContext ctx) {
return new NonCalculationNode((UnaryNode) visit(ctx.unaryExpression()), ctx.nonCalculationOperator().getText(), (IExpressionNode) visit(ctx.expression())); return new NonCalculationNode((UnaryNode) visit(ctx.unaryExpression()), ctx.nonCalculationOperator().getText(), (IExpressionNode) visit(ctx.expression()));
} }
/**
* Visits an assignable expression and creates an AssignableNode.
* It handles identifiers and member access.
*
* @param ctx the assignable expression context
* @return the AST node for the assignable expression
*/
@Override @Override
public ASTNode visitAssignableExpression(SimpleJavaParser.AssignableExpressionContext ctx) { public ASTNode visitAssignableExpression(SimpleJavaParser.AssignableExpressionContext ctx) {
if(ctx.Identifier() != null) { if(ctx.Identifier() != null) {
@ -595,6 +890,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Creates an ITypeNode based on the type identifier.
* It handles basic types and reference types.
*
* @param identifier the type identifier
* @return the type node
*/
public ITypeNode createTypeNode(String identifier){ public ITypeNode createTypeNode(String identifier){
return switch (identifier) { return switch (identifier) {
case "int" -> new BaseType(TypeEnum.INT); case "int" -> new BaseType(TypeEnum.INT);

View File

@ -1,18 +1,18 @@
grammar SimpleJava; grammar SimpleJava;
// Programm-Konstrukte // Programm-Konstrukte
program: classDeclaration+; program: classDeclaration+; // Ein Programm besteht aus einer oder mehreren Klassendeklarationen
classDeclaration: AccessModifier? 'class' Identifier OpenCurlyBracket memberDeclaration* ClosedCurlyBracket; classDeclaration: AccessModifier? 'class' Identifier OpenCurlyBracket memberDeclaration* ClosedCurlyBracket; // Deklariert eine Klasse mit optionalem Zugriffsmodifikator und mehreren Mitgliedern
memberDeclaration: constructorDeclaration | fieldDeclaration | methodDeclaration; memberDeclaration: constructorDeclaration | fieldDeclaration | methodDeclaration; // Ein Mitglied kann ein Konstruktor, ein Feld oder eine Methode sein
constructorDeclaration: AccessModifier? Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement; constructorDeclaration: AccessModifier? Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement; // Deklariert einen Konstruktor
fieldDeclaration: AccessModifier? type Identifier (Assign expression)? Semicolon; fieldDeclaration: AccessModifier? type Identifier (Assign expression)? Semicolon; // Deklariert ein Feld mit optionaler Initialisierung
methodDeclaration: MainMethodDeclaration blockStatement | AccessModifier? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement; methodDeclaration: MainMethodDeclaration blockStatement | AccessModifier? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement; // Deklariert eine Methode
parameterList: parameter (Comma parameter)*; parameterList: parameter (Comma parameter)*; // Parameterliste für Methoden und Konstruktoren
parameter: type Identifier; parameter: type Identifier; // Ein einzelner Parameter mit Typ und Bezeichner
argumentList: (expression (Comma expression)*)?; argumentList: (expression (Comma expression)*)?; // Argumentliste für Methodenaufrufe
// Anweisungen // Anweisungen
statement: returnStatement Semicolon statement: returnStatement Semicolon
@ -23,32 +23,32 @@ statement: returnStatement Semicolon
| forStatement | forStatement
| ifElseStatement | ifElseStatement
| switchStatement | switchStatement
| statementExpression Semicolon; | statementExpression Semicolon; // Verschiedene Arten von Anweisungen
blockStatement: OpenCurlyBracket statement* ClosedCurlyBracket; blockStatement: OpenCurlyBracket statement* ClosedCurlyBracket; // Ein Block von Anweisungen
returnStatement: Return (expression)?; returnStatement: Return (expression)?; // Rückgabe-Anweisung
localVariableDeclaration: type Identifier (Assign expression)?; localVariableDeclaration: type Identifier (Assign expression)?; // Deklaration einer lokalen Variable
whileStatement: While OpenRoundBracket expression ClosedRoundBracket blockStatement; whileStatement: While OpenRoundBracket expression ClosedRoundBracket blockStatement; // While-Schleife
doWhileStatement: Do blockStatement While OpenRoundBracket expression ClosedRoundBracket Semicolon; doWhileStatement: Do blockStatement While OpenRoundBracket expression ClosedRoundBracket Semicolon; // Do-While-Schleife
forStatement: For OpenRoundBracket (statementExpression | localVariableDeclaration) Semicolon (expression)? Semicolon (statementExpression)? ClosedRoundBracket blockStatement; forStatement: For OpenRoundBracket (statementExpression | localVariableDeclaration) Semicolon (expression)? Semicolon (statementExpression)? ClosedRoundBracket blockStatement; // For-Schleife
ifElseStatement: ifStatement elseIfStatement* elseStatement?; ifElseStatement: ifStatement elseIfStatement* elseStatement?; // If-Else-Bedingung
ifStatement: If OpenRoundBracket expression ClosedRoundBracket blockStatement; ifStatement: If OpenRoundBracket expression ClosedRoundBracket blockStatement; // If-Teil der Bedingung
elseIfStatement: Else If OpenRoundBracket expression ClosedRoundBracket blockStatement; elseIfStatement: Else If OpenRoundBracket expression ClosedRoundBracket blockStatement; // Else-If-Teil der Bedingung
elseStatement: Else blockStatement; elseStatement: Else blockStatement; // Else-Teil der Bedingung
switchStatement: Switch OpenRoundBracket expression ClosedRoundBracket OpenCurlyBracket caseStatement+ defaultStatement? ClosedCurlyBracket; switchStatement: Switch OpenRoundBracket expression ClosedRoundBracket OpenCurlyBracket caseStatement+ defaultStatement? ClosedCurlyBracket; // Switch-Anweisung
caseStatement: Case value Colon statement*; caseStatement: Case value Colon statement*; // Ein Case-Teil einer Switch-Anweisung
defaultStatement: Default Colon statement*; defaultStatement: Default Colon statement*; // Default-Teil einer Switch-Anweisung
statementExpression: assign | newDeclaration | methodCall | crementExpression; statementExpression: assign | newDeclaration | methodCall | crementExpression; // Ausdruck innerhalb einer Anweisung
assign: assignableExpression Assign expression; assign: assignableExpression Assign expression; // Zuweisung
newDeclaration: New Identifier OpenRoundBracket argumentList ClosedRoundBracket; newDeclaration: New Identifier OpenRoundBracket argumentList ClosedRoundBracket; // Instanziierung eines neuen Objekts
// Ausdrücke // Ausdrücke
expression: unaryExpression | binaryExpression; expression: unaryExpression | binaryExpression; // Ein Ausdruck kann unär oder binär sein
unaryExpression: This unaryExpression: This
| Identifier | Identifier
@ -56,97 +56,97 @@ unaryExpression: This
| value | value
| notExpression | notExpression
| statementExpression | statementExpression
| OpenRoundBracket expression ClosedRoundBracket; | OpenRoundBracket expression ClosedRoundBracket; // Unäre Ausdrücke
notExpression: Not expression; notExpression: Not expression; // Nicht-Ausdruck
crementExpression: incrementExpression | decrementExpression; crementExpression: incrementExpression | decrementExpression; // Inkrement-/Dekrement-Ausdrücke
incrementExpression: prefixIncrementExpression | suffixIncrementExpression; incrementExpression: prefixIncrementExpression | suffixIncrementExpression;
prefixIncrementExpression: '++' assignableExpression; prefixIncrementExpression: '++' assignableExpression; // Präfix-Inkrement
suffixIncrementExpression: assignableExpression '++'; suffixIncrementExpression: assignableExpression '++'; // Suffix-Inkrement
decrementExpression: prefixDecrementExpression | suffixDecrementExpression; decrementExpression: prefixDecrementExpression | suffixDecrementExpression; // Dekrement-Ausdrücke
prefixDecrementExpression: '--' assignableExpression; prefixDecrementExpression: '--' assignableExpression; // Präfix-Dekrement
suffixDecrementExpression: assignableExpression '--'; suffixDecrementExpression: assignableExpression '--'; // Suffix-Dekrement
assignableExpression: Identifier | memberAccess; assignableExpression: Identifier | memberAccess; // Zuweisbarer Ausdruck
memberAccess: This Dot Identifier memberAccess: This Dot Identifier
| (This Dot)? (Identifier Dot)+ Identifier; | (This Dot)? (Identifier Dot)+ Identifier; // Mitgliedszugriff
binaryExpression: calculationExpression | nonCalculationExpression; binaryExpression: calculationExpression | nonCalculationExpression; // Binäre Ausdrücke
calculationExpression: calculationExpression LineOperator dotExpression calculationExpression: calculationExpression LineOperator dotExpression
| dotExpression; | dotExpression; // Berechnungsausdrücke
dotExpression: dotExpression DotOperator dotSubtractionExpression dotExpression: dotExpression DotOperator dotSubtractionExpression
| dotSubtractionExpression; | dotSubtractionExpression; // Punktoperationen
dotSubtractionExpression: IntValue dotSubtractionExpression: IntValue
| Identifier | Identifier
| memberAccess | memberAccess
| methodCall OpenRoundBracket calculationExpression ClosedRoundBracket; | methodCall OpenRoundBracket calculationExpression ClosedRoundBracket; // Punkt-Subtraktionsausdrücke
nonCalculationExpression: unaryExpression nonCalculationOperator expression; nonCalculationExpression: unaryExpression nonCalculationOperator expression; // Nicht-Berechnungsausdrücke
// Methodenaufrufe // Methodenaufrufe
methodCall: target? chainedMethod* Identifier OpenRoundBracket argumentList ClosedRoundBracket; methodCall: target? chainedMethod* Identifier OpenRoundBracket argumentList ClosedRoundBracket; // Methodenaufruf
target: (This | memberAccess | newDeclaration | Identifier) Dot; target: (This | memberAccess | newDeclaration | Identifier) Dot; // Ziel eines Methodenaufrufs
chainedMethod: Identifier OpenRoundBracket argumentList ClosedRoundBracket Dot; chainedMethod: Identifier OpenRoundBracket argumentList ClosedRoundBracket Dot; // Verkettete Methode
// Typen // Typen
type: Int type: Int
| Boolean | Boolean
| Char | Char
| Identifier; | Identifier; // Datentypen
Void: 'void'; Void: 'void'; // Void-Typ
Boolean: 'boolean'; Boolean: 'boolean'; // Boolean-Typ
Char: 'char'; Char: 'char'; // Char-Typ
Int: 'int'; Int: 'int'; // Integer-Typ
value: IntValue value: IntValue
| BooleanValue | BooleanValue
| CharValue | CharValue
| NullValue; | NullValue; // Werte
// Zugriffsmodifikatoren // Zugriffsmodifikatoren
AccessModifier: 'public' | 'private' | 'public static' | 'private static'; AccessModifier: 'public' | 'private' | 'public static' | 'private static'; // Zugriffsmodifikatoren
MainMethodDeclaration: 'public static void main(String[] args)'; MainMethodDeclaration: 'public static void main(String[] args)'; // Hauptmethoden-Deklaration
// Operatoren // Operatoren
nonCalculationOperator: LogicalOperator | ComparisonOperator; nonCalculationOperator: LogicalOperator | ComparisonOperator; // Nicht-Berechnungsoperatoren
DotOperator: Mult | Div | Modulo; DotOperator: Mult | Div | Modulo; // Punktoperatoren
LineOperator: Plus | Minus; LineOperator: Plus | Minus; // Linienoperatoren
ComparisonOperator: Greater | Less | GreaterEqual | LessEqual | Equal | NotEqual; ComparisonOperator: Greater | Less | GreaterEqual | LessEqual | Equal | NotEqual; // Vergleichsoperatoren
LogicalOperator: And | Or; LogicalOperator: And | Or; // Logische Operatoren
Assign: '='; Assign: '='; // Zuweisungsoperator
Plus: '+'; Plus: '+'; // Plus-Operator
Minus: '-'; Minus: '-'; // Minus-Operator
Mult: '*'; Mult: '*'; // Multiplikationsoperator
Modulo: '%'; Modulo: '%'; // Modulo-Operator
Div: '/'; Div: '/'; // Divisionsoperator
Greater: '>'; Greater: '>'; // Größer-Operator
Less: '<'; Less: '<'; // Kleiner-Operator
GreaterEqual: '>='; GreaterEqual: '>='; // Größer-Gleich-Operator
LessEqual: '<='; LessEqual: '<='; // Kleiner-Gleich-Operator
Equal: '=='; Equal: '=='; // Gleichheitsoperator
NotEqual: '!='; NotEqual: '!='; // Ungleichheitsoperator
Not: '!'; Not: '!'; // Nicht-Operator
And: '&&'; And: '&&'; // Und-Operator
Or: '||'; Or: '||'; // Oder-Operator
// Symbole // Symbole
Dot: '.'; Dot: '.'; // Punkt-Symbol
OpenRoundBracket: '('; OpenRoundBracket: '('; // Öffnende runde Klammer
ClosedRoundBracket: ')'; ClosedRoundBracket: ')'; // Schließende runde Klammer
OpenCurlyBracket: '{'; OpenCurlyBracket: '{'; // Öffnende geschweifte Klammer
ClosedCurlyBracket: '}'; ClosedCurlyBracket: '}'; // Schließende geschweifte Klammer
Semicolon: ';'; Semicolon: ';'; // Semikolon
Comma: ','; Comma: ','; // Komma
// Schlüsselwörter // Schlüsselwörter
Class: 'class'; Class: 'class';
@ -161,21 +161,21 @@ New: 'new';
Switch: 'switch'; Switch: 'switch';
Case: 'case'; Case: 'case';
Default: 'default'; Default: 'default';
Colon: ':'; Colon: ':'; // Doppelpunkt
// Werte // Werte
CharValue: '\'' ~[\r\n]* '\''; CharValue: '\'' ~[\r\n]* '\''; // Zeichenwert
IntValue: Minus? Numeric+; IntValue: Minus? Numeric+; // Ganzzahlwert
BooleanValue: 'true' | 'false'; BooleanValue: 'true' | 'false'; // Boolean-Wert
NullValue: 'null'; NullValue: 'null'; // Null-Wert
// Bezeichner // Bezeichner
fragment Alphabetic: [a-zA-Z]; fragment Alphabetic: [a-zA-Z];
fragment Numeric: [0-9]; fragment Numeric: [0-9];
fragment ValidIdentSymbols: Alphabetic | Numeric | '$' | '_'; fragment ValidIdentSymbols: Alphabetic | Numeric | '$' | '_';
Identifier: Alphabetic ValidIdentSymbols*; Identifier: Alphabetic ValidIdentSymbols*; // Bezeichner
// Whitespaces und Kommentare ignorieren // Whitespaces und Kommentare ignorieren
WS: [ \t\r\n]+ -> skip; WS: [ \t\r\n]+ -> skip; // Leerzeichen ignorieren
InlineComment: '//' ~[\r\n]* -> skip; InlineComment: '//' ~[\r\n]* -> skip; // Einzeilige Kommentare ignorieren
MultilineComment: '/*' .*? '*/' -> skip; MultilineComment: '/*' .*? '*/' -> skip; // Mehrzeilige Kommentare ignorieren