Skip to content

test: rule arguments #219

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 1 commit into from
May 23, 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
29 changes: 29 additions & 0 deletions antlr-kotlin-tests/antlr/SizedList.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
grammar SizedList;

stringList:
size=int
items=stringSequence[$size.value]
EOF;

int returns [value: Int]:
NUM
{
$value = $NUM.text.toInt()
};

stringSequence[count: Int] returns [value: MutableList<String>] locals [remaining: Int]:
{
$value = mutableListOf<String>()
$remaining = $count
}
(
{$remaining!! > 0}? STRING
{
$value!! += $STRING.text
$remaining = $remaining!! - 1
}
)*;

STRING: [a-z]+;
NUM: [0-9]+;
WHITESPACE: [ \t\r\n]+ -> skip;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.strumenta.antlrkotlin.test.grammars

import com.strumenta.antlrkotlin.test.GrammarTest
import com.strumenta.antlrkotlin.test.generated.SizedListLexer
import com.strumenta.antlrkotlin.test.generated.SizedListParser
import org.antlr.v4.kotlinruntime.CharStream
import org.antlr.v4.kotlinruntime.TokenStream

@Suppress("unused")
class SizedListGrammarTest : GrammarTest<SizedListLexer, SizedListParser>() {
override fun createLexer(input: CharStream) = SizedListLexer(input)

override fun createParser(input: TokenStream) = SizedListParser(input)

override fun setupTreeTestRuns(runs: MutableSet<LispTestRun>) {
runs += LispTestRun("sizedList/five.sl", "sizedList/five.sl.tree")
}

override fun setupErrorsTestRuns(runs: MutableSet<ErrorsTestRun>) {
runs += ErrorsTestRun("sizedList/oneTooMany.sl", "sizedList/oneTooMany.sl.errors")
}

override fun getTree(parser: SizedListParser) = parser.stringList()
}
6 changes: 6 additions & 0 deletions antlr-kotlin-tests/src/commonTest/resources/sizedList/five.sl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5
one
two
three
four
five
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(stringList (int 5) (stringSequence one two three four five) <EOF>)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
5
one
two
three
four
five
six
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
line 7:0 no viable alternative at input 'six'
Loading