2021-06-13 12:59:57 +00:00
|
|
|
package prog8tests
|
|
|
|
|
2021-07-11 17:04:53 +00:00
|
|
|
import org.junit.jupiter.api.Disabled
|
2021-08-02 12:52:46 +00:00
|
|
|
import org.junit.jupiter.api.Nested
|
2021-10-10 22:22:04 +00:00
|
|
|
import org.junit.jupiter.api.Test
|
|
|
|
import org.junit.jupiter.api.TestInstance
|
|
|
|
import prog8.ast.IFunctionCall
|
|
|
|
import prog8.ast.Node
|
|
|
|
import prog8.ast.base.Position
|
|
|
|
import prog8.ast.expressions.CharLiteral
|
|
|
|
import prog8.ast.expressions.NumericLiteralValue
|
|
|
|
import prog8.ast.expressions.RangeExpr
|
|
|
|
import prog8.ast.expressions.StringLiteralValue
|
|
|
|
import prog8.ast.statements.*
|
2021-06-19 16:10:26 +00:00
|
|
|
import prog8.parser.ParseError
|
|
|
|
import prog8.parser.Prog8Parser.parseModule
|
2021-06-21 10:02:36 +00:00
|
|
|
import prog8.parser.SourceCode
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8tests.helpers.assumeNotExists
|
|
|
|
import prog8tests.helpers.assumeReadableFile
|
|
|
|
import prog8tests.helpers.fixturesDir
|
|
|
|
import kotlin.io.path.absolutePathString
|
|
|
|
import kotlin.io.path.name
|
|
|
|
import kotlin.io.path.nameWithoutExtension
|
|
|
|
import kotlin.test.assertContains
|
|
|
|
import kotlin.test.assertEquals
|
|
|
|
import kotlin.test.assertFailsWith
|
|
|
|
import kotlin.test.assertIs
|
2021-06-21 10:02:36 +00:00
|
|
|
|
2021-06-13 12:59:57 +00:00
|
|
|
|
2021-07-11 15:32:29 +00:00
|
|
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
2021-06-19 16:10:26 +00:00
|
|
|
class TestProg8Parser {
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Nested
|
|
|
|
inner class Newline {
|
2021-06-13 18:08:50 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Nested
|
|
|
|
inner class AtEnd {
|
2021-06-13 18:08:50 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `is not required - #40, fixed by #45`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val nl = "\n" // say, Unix-style (different flavours tested elsewhere)
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Text("foo {" + nl + "}") // source ends with '}' (= NO newline, issue #40)
|
2021-06-13 12:59:57 +00:00
|
|
|
|
2021-08-02 13:36:08 +00:00
|
|
|
// #40: Prog8ANTLRParser would report (throw) "missing <EOL> at '<EOF>'"
|
2021-08-02 12:52:46 +00:00
|
|
|
val module = parseModule(src)
|
|
|
|
assertEquals(1, module.statements.size)
|
|
|
|
}
|
2021-06-13 18:47:14 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `is still accepted - #40, fixed by #45`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val nl = "\n" // say, Unix-style (different flavours tested elsewhere)
|
|
|
|
val srcText = "foo {" + nl + "}" + nl // source does end with a newline (issue #40)
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text(srcText))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertEquals(1, module.statements.size)
|
|
|
|
}
|
|
|
|
}
|
2021-06-13 18:47:14 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `is required after each block except the last`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val nl = "\n" // say, Unix-style (different flavours tested elsewhere)
|
2021-06-13 18:47:14 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
// BAD: 2nd block `bar` does NOT start on new line; however, there's is a nl at the very end
|
|
|
|
val srcBad = "foo {" + nl + "}" + " bar {" + nl + "}" + nl
|
2021-06-13 20:49:54 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
// GOOD: 2nd block `bar` does start on a new line; however, a nl at the very end ain't needed
|
|
|
|
val srcGood = "foo {" + nl + "}" + nl + "bar {" + nl + "}"
|
2021-06-13 20:49:54 +00:00
|
|
|
|
2021-10-13 16:55:56 +00:00
|
|
|
assertFailsWith<ParseError> { parseModule(SourceCode.Text(srcBad)) }
|
|
|
|
val module = parseModule(SourceCode.Text(srcGood))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertEquals(2, module.statements.size)
|
|
|
|
}
|
2021-06-18 19:55:03 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `is required between two Blocks or Directives - #47`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
// block and block
|
2021-10-13 16:55:56 +00:00
|
|
|
assertFailsWith<ParseError>{ parseModule(SourceCode.Text("""
|
2021-08-02 12:52:46 +00:00
|
|
|
blockA {
|
|
|
|
} blockB {
|
|
|
|
}
|
|
|
|
""")) }
|
|
|
|
|
|
|
|
// block and directive
|
2021-10-13 16:55:56 +00:00
|
|
|
assertFailsWith<ParseError>{ parseModule(SourceCode.Text("""
|
2021-08-02 12:52:46 +00:00
|
|
|
blockB {
|
|
|
|
} %import textio
|
|
|
|
""")) }
|
|
|
|
|
|
|
|
// The following two are bogus due to directive *args* expected to follow the directive name.
|
|
|
|
// Leaving them in anyways.
|
|
|
|
|
|
|
|
// dir and block
|
2021-10-13 16:55:56 +00:00
|
|
|
assertFailsWith<ParseError>{ parseModule(SourceCode.Text("""
|
2021-08-02 12:52:46 +00:00
|
|
|
%import textio blockB {
|
|
|
|
}
|
|
|
|
""")) }
|
|
|
|
|
2021-10-13 16:55:56 +00:00
|
|
|
assertFailsWith<ParseError>{ parseModule(SourceCode.Text("""
|
2021-08-02 12:52:46 +00:00
|
|
|
%import textio %import syslib
|
|
|
|
""")) }
|
|
|
|
}
|
2021-06-18 19:55:03 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `can be Win, Unix or mixed, even mixed`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val nlWin = "\r\n"
|
|
|
|
val nlUnix = "\n"
|
|
|
|
val nlMac = "\r"
|
|
|
|
|
|
|
|
//parseModule(Paths.get("test", "fixtures", "mac_newlines.p8").toAbsolutePath())
|
|
|
|
|
|
|
|
// a good mix of all kinds of newlines:
|
|
|
|
val srcText =
|
|
|
|
"foo {" +
|
|
|
|
nlMac +
|
|
|
|
nlWin +
|
|
|
|
"}" +
|
|
|
|
nlMac + // <-- do test a single \r (!) where an EOL is expected
|
|
|
|
"bar {" +
|
|
|
|
nlUnix +
|
|
|
|
"}" +
|
|
|
|
nlUnix + nlMac // both should be "eaten up" by just one EOL token
|
|
|
|
"combi {" +
|
|
|
|
nlMac + nlWin + nlUnix // all three should be "eaten up" by just one EOL token
|
|
|
|
"}" +
|
|
|
|
nlUnix // end with newline (see testModuleSourceNeedNotEndWithNewline)
|
|
|
|
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text(srcText))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertEquals(2, module.statements.size)
|
|
|
|
}
|
2021-06-18 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Nested
|
|
|
|
inner class EOLsInterleavedWithComments {
|
|
|
|
|
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `are ok before first block - #47`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
// issue: #47
|
|
|
|
val srcText = """
|
|
|
|
; comment
|
|
|
|
|
|
|
|
; comment
|
|
|
|
|
|
|
|
blockA {
|
|
|
|
}
|
|
|
|
"""
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text(srcText))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertEquals(1, module.statements.size)
|
|
|
|
}
|
2021-06-19 18:27:04 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `are ok between blocks - #47`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
// issue: #47
|
|
|
|
val srcText = """
|
|
|
|
blockA {
|
|
|
|
}
|
|
|
|
; comment
|
|
|
|
|
|
|
|
; comment
|
|
|
|
|
|
|
|
blockB {
|
|
|
|
}
|
|
|
|
"""
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text(srcText))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertEquals(2, module.statements.size)
|
|
|
|
}
|
2021-07-11 15:32:29 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `are ok after last block - #47`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
// issue: #47
|
|
|
|
val srcText = """
|
|
|
|
blockA {
|
|
|
|
}
|
|
|
|
; comment
|
|
|
|
|
|
|
|
; comment
|
|
|
|
|
|
|
|
"""
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text(srcText))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertEquals(1, module.statements.size)
|
|
|
|
}
|
2021-06-19 18:27:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Nested
|
|
|
|
inner class ImportDirectives {
|
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `should not be looked into by the parser`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val importedNoExt = assumeNotExists(fixturesDir, "i_do_not_exist")
|
|
|
|
assumeNotExists(fixturesDir, "i_do_not_exist.p8")
|
|
|
|
val text = "%import ${importedNoExt.name}"
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text(text))
|
2021-07-10 19:03:14 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
assertEquals(1, module.statements.size)
|
|
|
|
}
|
2021-07-10 19:03:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Nested
|
|
|
|
inner class EmptySourcecode {
|
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `from an empty string should result in empty Module`() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text(""))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertEquals(0, module.statements.size)
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `from an empty file should result in empty Module`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val path = assumeReadableFile(fixturesDir, "empty.p8")
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.File(path))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertEquals(0, module.statements.size)
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Nested
|
|
|
|
inner class NameOfModule {
|
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `parsed from a string`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val srcText = """
|
|
|
|
main {
|
|
|
|
}
|
|
|
|
""".trimIndent()
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text(srcText))
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
// Note: assertContains has *actual* as first param
|
|
|
|
assertContains(module.name, Regex("^anonymous_[0-9a-f]+$"))
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `parsed from a file`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val path = assumeReadableFile(fixturesDir, "simple_main.p8")
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.File(path))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertEquals(path.nameWithoutExtension, module.name)
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Nested
|
|
|
|
inner class PositionOfAstNodesAndParseErrors {
|
|
|
|
|
2021-08-02 13:36:08 +00:00
|
|
|
private fun assertPosition(
|
2021-08-02 12:52:46 +00:00
|
|
|
actual: Position,
|
|
|
|
expFile: String? = null,
|
|
|
|
expLine: Int? = null,
|
|
|
|
expStartCol: Int? = null,
|
|
|
|
expEndCol: Int? = null
|
|
|
|
) {
|
|
|
|
require(!listOf(expLine, expStartCol, expEndCol).all { it == null })
|
|
|
|
if (expLine != null) assertEquals(expLine, actual.line, ".position.line (1-based)")
|
|
|
|
if (expStartCol != null) assertEquals(expStartCol, actual.startCol, ".position.startCol (0-based)")
|
|
|
|
if (expEndCol != null) assertEquals(expEndCol, actual.endCol, ".position.endCol (0-based)")
|
|
|
|
if (expFile != null) assertEquals(expFile, actual.file, ".position.file")
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 13:36:08 +00:00
|
|
|
private fun assertPosition(
|
2021-08-02 12:52:46 +00:00
|
|
|
actual: Position,
|
|
|
|
expFile: Regex? = null,
|
|
|
|
expLine: Int? = null,
|
|
|
|
expStartCol: Int? = null,
|
|
|
|
expEndCol: Int? = null
|
|
|
|
) {
|
|
|
|
require(!listOf(expLine, expStartCol, expEndCol).all { it == null })
|
|
|
|
if (expLine != null) assertEquals(expLine, actual.line, ".position.line (1-based)")
|
|
|
|
if (expStartCol != null) assertEquals(expStartCol, actual.startCol, ".position.startCol (0-based)")
|
|
|
|
if (expEndCol != null) assertEquals(expEndCol, actual.endCol, ".position.endCol (0-based)")
|
|
|
|
// Note: assertContains expects *actual* value first
|
|
|
|
if (expFile != null) assertContains(actual.file, expFile, ".position.file")
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 13:36:08 +00:00
|
|
|
private fun assertPositionOf(
|
2021-08-02 12:52:46 +00:00
|
|
|
actual: Node,
|
|
|
|
expFile: String? = null,
|
|
|
|
expLine: Int? = null,
|
|
|
|
expStartCol: Int? = null,
|
|
|
|
expEndCol: Int? = null
|
|
|
|
) =
|
|
|
|
assertPosition(actual.position, expFile, expLine, expStartCol, expEndCol)
|
|
|
|
|
2021-08-02 13:36:08 +00:00
|
|
|
private fun assertPositionOf(
|
2021-08-02 12:52:46 +00:00
|
|
|
actual: Node,
|
|
|
|
expFile: Regex? = null,
|
|
|
|
expLine: Int? = null,
|
|
|
|
expStartCol: Int? = null,
|
|
|
|
expEndCol: Int? = null
|
|
|
|
) =
|
|
|
|
assertPosition(actual.position, expFile, expLine, expStartCol, expEndCol)
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `in ParseError from bad string source code`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val srcText = "bad * { }\n"
|
|
|
|
|
2021-10-13 16:55:56 +00:00
|
|
|
assertFailsWith<ParseError> { parseModule(SourceCode.Text(srcText)) }
|
2021-08-02 12:52:46 +00:00
|
|
|
try {
|
2021-10-13 16:55:56 +00:00
|
|
|
parseModule(SourceCode.Text(srcText))
|
2021-08-02 12:52:46 +00:00
|
|
|
} catch (e: ParseError) {
|
|
|
|
assertPosition(e.position, Regex("^<String@[0-9a-f]+>$"), 1, 4, 4)
|
|
|
|
}
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `in ParseError from bad file source code`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val path = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8")
|
2021-06-18 19:55:03 +00:00
|
|
|
|
2021-10-13 16:55:56 +00:00
|
|
|
assertFailsWith<ParseError> { parseModule(SourceCode.File(path)) }
|
2021-08-02 12:52:46 +00:00
|
|
|
try {
|
2021-10-13 16:55:56 +00:00
|
|
|
parseModule(SourceCode.File(path))
|
2021-08-02 12:52:46 +00:00
|
|
|
} catch (e: ParseError) {
|
|
|
|
assertPosition(e.position, path.absolutePathString(), 2, 6) // TODO: endCol wrong
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
}
|
2021-06-19 16:10:26 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `of Module parsed from a string`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val srcText = """
|
|
|
|
main {
|
|
|
|
}
|
|
|
|
""".trimIndent()
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text(srcText))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertPositionOf(module, Regex("^<String@[0-9a-f]+>$"), 1, 0) // TODO: endCol wrong
|
2021-06-19 16:10:26 +00:00
|
|
|
}
|
2021-06-18 19:55:03 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `of Module parsed from a file`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val path = assumeReadableFile(fixturesDir, "simple_main.p8")
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.File(path))
|
2021-08-02 12:52:46 +00:00
|
|
|
assertPositionOf(module, path.absolutePathString(), 1, 0) // TODO: endCol wrong
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `of non-root Nodes parsed from file`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val path = assumeReadableFile(fixturesDir, "simple_main.p8")
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.File(path))
|
2021-08-02 12:52:46 +00:00
|
|
|
val mpf = module.position.file
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
assertPositionOf(module, path.absolutePathString(), 1, 0) // TODO: endCol wrong
|
|
|
|
val mainBlock = module.statements.filterIsInstance<Block>()[0]
|
|
|
|
assertPositionOf(mainBlock, mpf, 1, 0) // TODO: endCol wrong!
|
|
|
|
val startSub = mainBlock.statements.filterIsInstance<Subroutine>()[0]
|
|
|
|
assertPositionOf(startSub, mpf, 2, 4) // TODO: endCol wrong!
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-07-04 16:09:16 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
/**
|
|
|
|
* TODO: this test is testing way too much at once
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
@Disabled("TODO: fix .position of nodes below Module - step 8, 'refactor AST gen'")
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `of non-root Nodes parsed from a string`() {
|
2021-08-02 12:52:46 +00:00
|
|
|
val srcText = """
|
|
|
|
%target 16, "abc" ; DirectiveArg directly inherits from Node - neither an Expression nor a Statement..?
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte foo = 42
|
|
|
|
ubyte bar
|
|
|
|
when (foo) {
|
|
|
|
23 -> bar = 'x' ; WhenChoice, also directly inheriting Node
|
|
|
|
42 -> bar = 'y'
|
|
|
|
else -> bar = 'z'
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-02 12:52:46 +00:00
|
|
|
""".trimIndent()
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text(srcText))
|
2021-08-02 12:52:46 +00:00
|
|
|
val mpf = module.position.file
|
|
|
|
|
|
|
|
val targetDirective = module.statements.filterIsInstance<Directive>()[0]
|
|
|
|
assertPositionOf(targetDirective, mpf, 1, 0) // TODO: endCol wrong!
|
|
|
|
val mainBlock = module.statements.filterIsInstance<Block>()[0]
|
|
|
|
assertPositionOf(mainBlock, mpf, 2, 0) // TODO: endCol wrong!
|
|
|
|
val startSub = mainBlock.statements.filterIsInstance<Subroutine>()[0]
|
|
|
|
assertPositionOf(startSub, mpf, 3, 4) // TODO: endCol wrong!
|
|
|
|
val declFoo = startSub.statements.filterIsInstance<VarDecl>()[0]
|
|
|
|
assertPositionOf(declFoo, mpf, 4, 8) // TODO: endCol wrong!
|
|
|
|
val rhsFoo = declFoo.value!!
|
|
|
|
assertPositionOf(rhsFoo, mpf, 4, 20) // TODO: endCol wrong!
|
|
|
|
val declBar = startSub.statements.filterIsInstance<VarDecl>()[1]
|
|
|
|
assertPositionOf(declBar, mpf, 5, 8) // TODO: endCol wrong!
|
|
|
|
val whenStmt = startSub.statements.filterIsInstance<WhenStatement>()[0]
|
|
|
|
assertPositionOf(whenStmt, mpf, 6, 8) // TODO: endCol wrong!
|
|
|
|
assertPositionOf(whenStmt.choices[0], mpf, 7, 12) // TODO: endCol wrong!
|
|
|
|
assertPositionOf(whenStmt.choices[1], mpf, 8, 12) // TODO: endCol wrong!
|
|
|
|
assertPositionOf(whenStmt.choices[2], mpf, 9, 12) // TODO: endCol wrong!
|
|
|
|
}
|
2021-07-04 16:09:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Nested
|
|
|
|
inner class CharLiterals {
|
2021-06-21 18:16:50 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `in argument position, no altEnc`() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Text("""
|
2021-08-02 12:52:46 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
chrout('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""")
|
|
|
|
val module = parseModule(src)
|
2021-06-21 18:16:50 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
val startSub = module
|
|
|
|
.statements.filterIsInstance<Block>()[0]
|
|
|
|
.statements.filterIsInstance<Subroutine>()[0]
|
|
|
|
val funCall = startSub.statements.filterIsInstance<IFunctionCall>().first()
|
2021-06-21 18:16:50 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
assertIs<CharLiteral>(funCall.args[0])
|
|
|
|
val char = funCall.args[0] as CharLiteral
|
|
|
|
assertEquals('\n', char.value)
|
|
|
|
}
|
2021-06-21 18:16:50 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `on rhs of block-level var decl, no AltEnc`() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Text("""
|
2021-08-02 12:52:46 +00:00
|
|
|
main {
|
2021-06-21 18:16:50 +00:00
|
|
|
ubyte c = 'x'
|
|
|
|
}
|
2021-08-02 12:52:46 +00:00
|
|
|
""")
|
|
|
|
val module = parseModule(src)
|
|
|
|
val decl = module
|
|
|
|
.statements.filterIsInstance<Block>()[0]
|
|
|
|
.statements.filterIsInstance<VarDecl>()[0]
|
|
|
|
|
|
|
|
val rhs = decl.value as CharLiteral
|
|
|
|
assertEquals('x', rhs.value, "char literal's .value")
|
|
|
|
assertEquals(false, rhs.altEncoding, "char literal's .altEncoding")
|
|
|
|
}
|
2021-06-21 18:16:50 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `on rhs of block-level const decl, with AltEnc`() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Text("""
|
2021-08-02 12:52:46 +00:00
|
|
|
main {
|
2021-06-21 18:16:50 +00:00
|
|
|
const ubyte c = @'x'
|
|
|
|
}
|
2021-08-02 12:52:46 +00:00
|
|
|
""")
|
|
|
|
val module = parseModule(src)
|
|
|
|
val decl = module
|
|
|
|
.statements.filterIsInstance<Block>()[0]
|
|
|
|
.statements.filterIsInstance<VarDecl>()[0]
|
|
|
|
|
|
|
|
val rhs = decl.value as CharLiteral
|
|
|
|
assertEquals('x', rhs.value, "char literal's .value")
|
|
|
|
assertEquals(true, rhs.altEncoding, "char literal's .altEncoding")
|
|
|
|
}
|
2021-07-13 07:47:31 +00:00
|
|
|
|
2021-08-02 12:52:46 +00:00
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `on rhs of subroutine-level var decl, no AltEnc`() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Text("""
|
2021-08-02 12:52:46 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte c = 'x'
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
2021-08-02 12:52:46 +00:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
val module = parseModule(src)
|
|
|
|
val decl = module
|
|
|
|
.statements.filterIsInstance<Block>()[0]
|
|
|
|
.statements.filterIsInstance<Subroutine>()[0]
|
|
|
|
.statements.filterIsInstance<VarDecl>()[0]
|
|
|
|
|
|
|
|
val rhs = decl.value as CharLiteral
|
|
|
|
assertEquals('x', rhs.value, "char literal's .value")
|
|
|
|
assertEquals(false, rhs.altEncoding, "char literal's .altEncoding")
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `on rhs of subroutine-level const decl, with AltEnc`() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Text("""
|
2021-08-02 12:52:46 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
const ubyte c = @'x'
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
2021-08-02 12:52:46 +00:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
val module = parseModule(src)
|
|
|
|
val decl = module
|
|
|
|
.statements.filterIsInstance<Block>()[0]
|
|
|
|
.statements.filterIsInstance<Subroutine>()[0]
|
|
|
|
.statements.filterIsInstance<VarDecl>()[0]
|
|
|
|
|
|
|
|
val rhs = decl.value as CharLiteral
|
|
|
|
assertEquals('x', rhs.value, "char literal's .value")
|
|
|
|
assertEquals(true, rhs.altEncoding, "char literal's .altEncoding")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
inner class Ranges {
|
|
|
|
|
|
|
|
@Test
|
2021-08-02 13:36:08 +00:00
|
|
|
fun `in for-loops`() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val module = parseModule(SourceCode.Text("""
|
2021-08-02 12:52:46 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte ub
|
|
|
|
for ub in "start" downto "end" { ; #0
|
|
|
|
}
|
|
|
|
for ub in "something" { ; #1
|
|
|
|
}
|
|
|
|
for ub in @'a' to 'f' { ; #2
|
|
|
|
}
|
|
|
|
for ub in false to true { ; #3
|
|
|
|
}
|
|
|
|
for ub in 9 to 1 { ; #4 - yes, *parser* should NOT check!
|
|
|
|
}
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-02 12:52:46 +00:00
|
|
|
"""))
|
|
|
|
val iterables = module
|
|
|
|
.statements.filterIsInstance<Block>()[0]
|
|
|
|
.statements.filterIsInstance<Subroutine>()[0]
|
|
|
|
.statements.filterIsInstance<ForLoop>()
|
|
|
|
.map { it.iterable }
|
|
|
|
|
|
|
|
assertEquals(5, iterables.size)
|
|
|
|
|
|
|
|
val it0 = iterables[0] as RangeExpr
|
|
|
|
assertIs<StringLiteralValue>(it0.from, "parser should leave it as is")
|
|
|
|
assertIs<StringLiteralValue>(it0.to, "parser should leave it as is")
|
|
|
|
|
|
|
|
val it1 = iterables[1] as StringLiteralValue
|
|
|
|
assertEquals("something", it1.value, "parser should leave it as is")
|
|
|
|
|
|
|
|
val it2 = iterables[2] as RangeExpr
|
|
|
|
assertIs<CharLiteral>(it2.from, "parser should leave it as is")
|
|
|
|
assertIs<CharLiteral>(it2.to, "parser should leave it as is")
|
|
|
|
|
|
|
|
val it3 = iterables[3] as RangeExpr
|
|
|
|
// TODO: intro BoolLiteral
|
|
|
|
assertIs<NumericLiteralValue>(it3.from, "parser should leave it as is")
|
|
|
|
assertIs<NumericLiteralValue>(it3.to, "parser should leave it as is")
|
|
|
|
|
|
|
|
val it4 = iterables[4] as RangeExpr
|
|
|
|
assertIs<NumericLiteralValue>(it4.from, "parser should leave it as is")
|
|
|
|
assertIs<NumericLiteralValue>(it4.to, "parser should leave it as is")
|
|
|
|
}
|
2021-07-13 07:47:31 +00:00
|
|
|
}
|
2021-08-02 12:52:46 +00:00
|
|
|
|
2021-06-13 12:59:57 +00:00
|
|
|
}
|