antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

Swift ANTLR Runtime: Terminals not working / crash when calling ctx.getText()

Open vinaykumar0339 opened this issue 3 months ago • 2 comments

Description

  • I am building an expression language using ANTLR 4.13.2.

  • It works fine in JavaScript and Java.

  • In Swift, I’m encountering an issue when accessing terminals.

Issue

The crash occurs at this method:

// This method is from TerminalNodeImpl.swift file
public func getText() -> String {
    return (symbol.getText())!   // ⚠️ crash here
}

Example input expression:

1+1

And in the visitor:

public override func visitNumberLiteral(_ ctx: JexLangParser.NumberLiteralContext) -> Any? {
    ctx.getText() // ⚠️ crash occurs here
}
  • When the visitor tries to access ctx.getText(), the application crashes.

  • It seems related to terminals not being recognized properly in runtime Swift Parser.

Notes

  • The grammar works in JS and Java, so the issue may be specific to the Swift ANTLR runtime.

  • Terminals like NUMBER or + might not be producing valid symbols when parsed in Swift.

Image
In an above Image for three children
0 -> NumberLiteral
1 -> Operator
2 -> NumberLiteral

vinaykumar0339 avatar Oct 04 '25 06:10 vinaykumar0339

Hi @aphyr, can you help me out here, please?

vinaykumar0339 avatar Oct 08 '25 15:10 vinaykumar0339

I am not a maintainer of Antlr, and I do not appreciate being tagged in to LLM slop issues.

aphyr avatar Oct 08 '25 20:10 aphyr