Skip to content

Commit

Permalink
fix(parser): handle null contexts in PythonAstBaseListener.kt #28
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Jul 9, 2024
1 parent 3956222 commit 7c3f1db
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 7c3f1db

Please # to comment.