+/* #40: add tests; temporarily undo fix for EOL-after-block so we can see that tests actually fail

This commit is contained in:
meisl 2021-06-13 20:08:50 +02:00
parent 2350843d1d
commit 32bad5df15
2 changed files with 30 additions and 14 deletions

View File

@ -17,6 +17,15 @@ class TestAntlrParser {
} }
} }
private fun parseModule(srcText: String): prog8Parser.ModuleContext {
val lexer = prog8Lexer(CharStreams.fromString(srcText))
val tokens = CommonTokenStream(lexer)
val parser = prog8Parser(tokens)
//parser.errorHandler = BailErrorStrategy()
parser.addErrorListener(MyErrorListener())
return parser.module()
}
object TestStringEncoding: IStringEncoding { object TestStringEncoding: IStringEncoding {
override fun encodeString(str: String, altEncoding: Boolean): List<Short> { override fun encodeString(str: String, altEncoding: Boolean): List<Short> {
TODO("Not yet implemented") TODO("Not yet implemented")
@ -28,24 +37,30 @@ class TestAntlrParser {
} }
@Test @Test
fun testAntlrTree() { fun testModuleFileNeedNotEndWithNewline() {
// can create charstreams from many other sources as well; val srcText = """
val charstream = CharStreams.fromString(""" main {
sub start() {
return
}
}""" // file ends with '}' (= NO newline, issue #40)
// before the fix, prog8Parser would have reported (thrown) "missing <EOL> at '<EOF>'"
val parseTree = parseModule(srcText)
assertEquals(parseTree.block().size, 1)
}
@Test
fun testModuleFileMayEndWithNewline() {
val srcText = """
main { main {
sub start() { sub start() {
return return
} }
} }
""") """ // file does end with a newline (issue #40)
val lexer = prog8Lexer(charstream) val parseTree = parseModule(srcText)
val tokens = CommonTokenStream(lexer) assertEquals(parseTree.block().size, 1)
val parser = prog8Parser(tokens)
parser.errorHandler = BailErrorStrategy()
// parser.removeErrorListeners()
// parser.addErrorListener(MyErrorListener())
val nodes = parser.module()
val blockName = nodes.block(0).identifier().NAME().text
assertEquals(blockName, "main")
} }
@Test @Test

View File

@ -74,7 +74,8 @@ register: 'A' | 'X' | 'Y' | 'AX' | 'AY' | 'XY' | 'Pc' | 'Pz' | 'Pn' | 'Pv' | 'R0
module : (directive | block | EOL)* EOF ; module : (directive | block | EOL)* EOF ;
block: identifier integerliteral? '{' EOL (block_statement | EOL) * '}' (EOL | EOF) ; //block: identifier integerliteral? '{' EOL (block_statement | EOL) * '}' (EOL | EOF) ;
block: identifier integerliteral? '{' EOL (block_statement | EOL) * '}' EOL ;
block_statement: block_statement: