diff --git a/antlr-kotlin-tests/antlr/SizedList.g4 b/antlr-kotlin-tests/antlr/SizedList.g4 new file mode 100644 index 00000000..2b1a880a --- /dev/null +++ b/antlr-kotlin-tests/antlr/SizedList.g4 @@ -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] locals [remaining: Int]: + { + $value = mutableListOf() + $remaining = $count + } + ( + {$remaining!! > 0}? STRING + { + $value!! += $STRING.text + $remaining = $remaining!! - 1 + } + )*; + +STRING: [a-z]+; +NUM: [0-9]+; +WHITESPACE: [ \t\r\n]+ -> skip; \ No newline at end of file diff --git a/antlr-kotlin-tests/src/commonTest/kotlin/com/strumenta/antlrkotlin/test/grammars/SizedListGrammarTest.kt b/antlr-kotlin-tests/src/commonTest/kotlin/com/strumenta/antlrkotlin/test/grammars/SizedListGrammarTest.kt new file mode 100644 index 00000000..b1c92953 --- /dev/null +++ b/antlr-kotlin-tests/src/commonTest/kotlin/com/strumenta/antlrkotlin/test/grammars/SizedListGrammarTest.kt @@ -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() { + override fun createLexer(input: CharStream) = SizedListLexer(input) + + override fun createParser(input: TokenStream) = SizedListParser(input) + + override fun setupTreeTestRuns(runs: MutableSet) { + runs += LispTestRun("sizedList/five.sl", "sizedList/five.sl.tree") + } + + override fun setupErrorsTestRuns(runs: MutableSet) { + runs += ErrorsTestRun("sizedList/oneTooMany.sl", "sizedList/oneTooMany.sl.errors") + } + + override fun getTree(parser: SizedListParser) = parser.stringList() +} diff --git a/antlr-kotlin-tests/src/commonTest/resources/sizedList/five.sl b/antlr-kotlin-tests/src/commonTest/resources/sizedList/five.sl new file mode 100644 index 00000000..824797db --- /dev/null +++ b/antlr-kotlin-tests/src/commonTest/resources/sizedList/five.sl @@ -0,0 +1,6 @@ +5 +one +two +three +four +five \ No newline at end of file diff --git a/antlr-kotlin-tests/src/commonTest/resources/sizedList/five.sl.tree b/antlr-kotlin-tests/src/commonTest/resources/sizedList/five.sl.tree new file mode 100644 index 00000000..8d01f616 --- /dev/null +++ b/antlr-kotlin-tests/src/commonTest/resources/sizedList/five.sl.tree @@ -0,0 +1 @@ +(stringList (int 5) (stringSequence one two three four five) ) \ No newline at end of file diff --git a/antlr-kotlin-tests/src/commonTest/resources/sizedList/oneTooMany.sl b/antlr-kotlin-tests/src/commonTest/resources/sizedList/oneTooMany.sl new file mode 100644 index 00000000..f9c0aa87 --- /dev/null +++ b/antlr-kotlin-tests/src/commonTest/resources/sizedList/oneTooMany.sl @@ -0,0 +1,7 @@ +5 +one +two +three +four +five +six \ No newline at end of file diff --git a/antlr-kotlin-tests/src/commonTest/resources/sizedList/oneTooMany.sl.errors b/antlr-kotlin-tests/src/commonTest/resources/sizedList/oneTooMany.sl.errors new file mode 100644 index 00000000..9982bf23 --- /dev/null +++ b/antlr-kotlin-tests/src/commonTest/resources/sizedList/oneTooMany.sl.errors @@ -0,0 +1 @@ +line 7:0 no viable alternative at input 'six' \ No newline at end of file