Skip to content

Rebasing test/token label type on master #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2025
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
15 changes: 15 additions & 0 deletions antlr-kotlin-tests/antlr/BracketsParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
parser grammar BracketsParser;

tokens {
LEFT, RIGHT
}

options {
TokenLabelType=BracketToken;
}

@header {
import com.strumenta.antlrkotlin.test.BracketToken
}

matched: left=LEFT matched* left=RIGHT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.strumenta.antlrkotlin.test

import org.antlr.v4.kotlinruntime.CommonToken
import org.antlr.v4.kotlinruntime.Token

public sealed interface BracketToken : Token {
public class Left(text: String) : CommonToken(1, text), BracketToken
public class Right(text: String) : CommonToken(2, text), BracketToken
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.strumenta.antlrkotlin.test.grammars

import com.strumenta.antlrkotlin.test.BracketToken.Left
import com.strumenta.antlrkotlin.test.BracketToken.Right
import com.strumenta.antlrkotlin.test.ThrowingErrorListener
import com.strumenta.antlrkotlin.test.generated.BracketsParser
import com.strumenta.antlrkotlin.util.loadResourceText
import org.antlr.v4.kotlinruntime.ListTokenSource
import org.antlr.v4.kotlinruntime.UnbufferedTokenStream
import org.antlr.v4.kotlinruntime.tree.Trees
import kotlin.test.Test
import kotlin.test.assertEquals

@Suppress("unused")
class BracketsTest {

@Test
fun parsesMatched() {
// Create the parser
val tokenStream = UnbufferedTokenStream(
ListTokenSource(
listOf(
Left("("), Left("["), Right("]"),
Left("{"), Right("}"), Right(")")
)
)
)
val parser = BracketsParser(tokenStream)
parser.addErrorListener(ThrowingErrorListener)
val tree = parser.matched()
val lispTree = Trees.toStringTree(tree, parser)

val expectedLispTreeText = loadResourceText("src/commonTest/resources/brackets/matched.tree")

assertEquals(
expected = expectedLispTreeText,
actual = lispTree,
message = "Outputted LISP tree does not match the expected tree",
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(matched ( (matched [ ]) (matched { }) ))
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dokka = "1.9.20"
researchgate-release = "3.1.0"
maven-publish = "0.32.0"
gradle-plugin-publish = "1.3.0"
antlr-kotlin = "1.0.4"
antlr-kotlin = "1.0.5"

[libraries]
kotlinx-io-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-io-core", version.ref = "kotlinx-io" }
Expand Down
Loading