From b56322fcb5c3e63b27af4aef2914bb285fefecd6 Mon Sep 17 00:00:00 2001 From: Edoardo Luppi Date: Thu, 4 Jul 2024 18:53:31 +0200 Subject: [PATCH] refactor: minor cleanups --- .../kotlin/org/antlr/v4/kotlinruntime/Parser.kt | 6 ++++-- .../kotlin/org/antlr/v4/kotlinruntime/Token.kt | 10 ++++------ .../v4/kotlinruntime/tree/IterativeParseTreeWalker.kt | 6 +++++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/Parser.kt b/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/Parser.kt index 41737385..38d5f760 100644 --- a/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/Parser.kt +++ b/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/Parser.kt @@ -49,8 +49,10 @@ public abstract class Parser(input: TokenStream) : Recognizer) { - (ctx.children as ArrayList<*>).trimToSize() + val children = ctx.children + + if (children is ArrayList<*>) { + children.trimToSize() } } } diff --git a/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/Token.kt b/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/Token.kt index 3a554c73..5d1e2330 100644 --- a/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/Token.kt +++ b/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/Token.kt @@ -117,10 +117,8 @@ public interface Token { public fun startPoint(): Point = Point(line, charPositionInLine) - public fun endPoint(): Point? = - if (text == null) { - null - } else { - Point(line, charPositionInLine).advance(text!!) - } + public fun endPoint(): Point? { + val text = this.text ?: return null + return Point(line, charPositionInLine).advance(text) + } } diff --git a/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/tree/IterativeParseTreeWalker.kt b/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/tree/IterativeParseTreeWalker.kt index 28f23b52..6005ac91 100644 --- a/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/tree/IterativeParseTreeWalker.kt +++ b/antlr-kotlin-runtime/src/commonMain/kotlin/org/antlr/v4/kotlinruntime/tree/IterativeParseTreeWalker.kt @@ -57,7 +57,11 @@ public open class IterativeParseTreeWalker : ParseTreeWalker() { // No next, sibling, so move up currentNode = nodeStack.removeFirst() currentIndex = indexStack.pop() - } while (currentNode != null) + + // The original condition was 'currentNode != null', + // but since nodeStack.removeFirst() throws if there + // is no element, the condition is always true + } while (true) } } }