From 7c3f1db60406d419fd3eba533be9fa2929309da9 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 9 Jul 2024 14:23:50 +0800 Subject: [PATCH] fix(parser): handle null contexts in PythonAstBaseListener.kt #28 Avoid crashes due to null contexts by using nullable types and safe calls in the PythonAstBaseListener. This ensures the parser can handle cases where child contexts may not be present. --- .../kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt b/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt index 06f8c2d8..c08162fc 100644 --- a/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt +++ b/chapi-ast-python/src/main/kotlin/chapi/ast/pythonast/PythonAstBaseListener.kt @@ -90,8 +90,8 @@ open class PythonAstBaseListener : PythonParserBaseListener() { fun buildExprStmt(exprCtx: PythonParser.Expr_stmtContext) { var leftPart = "" - val starExpr = exprCtx.getChild(0) as PythonParser.Testlist_star_exprContext - val childType = starExpr.getChild(0) + val starExpr = exprCtx.getChild(0) as? PythonParser.Testlist_star_exprContext + val childType = starExpr?.getChild(0) if (childType is PythonParser.TestlistContext) { for (testContext in starExpr.testlist().test()) { buildTestContext(testContext) @@ -109,7 +109,7 @@ open class PythonAstBaseListener : PythonParserBaseListener() { private fun buildAssignPart(assignPartCtx: PythonParser.Assign_partContext): String { var returnAtom = "" for (starExprCtx in assignPartCtx.testlist_star_expr()) { - when (val child = starExprCtx.getChild(0)) { + when (val child = starExprCtx?.getChild(0)) { is PythonParser.TestlistContext -> { child.test().forEach { testContext -> returnAtom = this.buildTestContext(testContext) @@ -123,7 +123,7 @@ open class PythonAstBaseListener : PythonParserBaseListener() { private fun buildTestContext(testContext: PythonParser.TestContext): String { var returnType = "" - when (val childCtx = testContext.getChild(0).getChild(0).getChild(0)) { + when (val childCtx = testContext.getChild(0)?.getChild(0)?.getChild(0)) { is PythonParser.ExprContext -> { val exprChild = childCtx.getChild(0) when (exprChild) {