Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ public int getPriority() {
* @param ctx AST выражения
* @return дерево вычисления выражения
*/
public static @Nullable BslExpression buildExpressionTree(BSLParser.ExpressionContext ctx) {
public static @Nullable BslExpression buildExpressionTree(BSLParser.@Nullable ExpressionContext ctx) {
if (ctx == null) {
return null;
}
var instance = new ExpressionTreeBuildingVisitor();
instance.visitExpression(ctx);
return instance.getExpressionTree();
Expand Down Expand Up @@ -419,11 +422,19 @@ public ParseTree visitAccessCall(BSLParser.AccessCallContext ctx) {

@Override
public ParseTree visitTernaryOperator(BSLParser.TernaryOperatorContext ctx) {
var ternary = TernaryOperatorNode.create(
Objects.requireNonNull(makeSubexpression(ctx.expression(0))),
Objects.requireNonNull(makeSubexpression(ctx.expression(1))),
Objects.requireNonNull(makeSubexpression(ctx.expression(2)))
);
var condition = makeSubexpression(ctx.expression(0));
var truePart = makeSubexpression(ctx.expression(1));
var falsePart = makeSubexpression(ctx.expression(2));

// If any expression is null (which happens when the parser encounters syntax errors
// in incomplete ternary operators like "?" or "Возврат ?"), create an error node
// instead of attempting to create a TernaryOperatorNode to avoid NullPointerException
if (condition == null || truePart == null || falsePart == null) {
operands.push(new ErrorExpressionNode(ctx));
return ctx;
}

var ternary = TernaryOperatorNode.create(condition, truePart, falsePart);

ternary.setRepresentingAst(ctx);
operands.push(ternary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ static Stream<Arguments> errorHandlingTestCases() {
createIfStatementTestCase("New expression", "Новый"),
createIfStatementTestCase("New expression with parenthes", "Новый ("),
createIfStatementTestCase("New expression with two parantheses", "Новый ()"),
createIfStatementTestCase("New expression with NOT", "Новый Структура(\"\", Не)", ConstructorCallNode.class)
createIfStatementTestCase("New expression with NOT", "Новый Структура(\"\", Не)", ConstructorCallNode.class),
createIfStatementTestCase("Incomplete ternary operator - only question mark", "?")
);
}

Expand Down
Loading