2021-06-13 12:59:57 +00:00
|
|
|
package prog8tests
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test
|
2021-07-04 16:09:16 +00:00
|
|
|
import prog8.ast.Node
|
|
|
|
import prog8.ast.base.Position
|
2021-06-21 10:02:36 +00:00
|
|
|
import kotlin.test.*
|
|
|
|
import java.nio.file.Path // TODO: use kotlin.io.path.Path instead
|
|
|
|
import kotlin.io.path.*
|
2021-06-13 12:59:57 +00:00
|
|
|
import prog8.ast.statements.Block
|
2021-07-04 16:09:16 +00:00
|
|
|
import prog8.ast.statements.Directive
|
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-06-13 12:59:57 +00:00
|
|
|
|
2021-06-19 16:10:26 +00:00
|
|
|
class TestProg8Parser {
|
2021-07-04 16:09:16 +00:00
|
|
|
val workingDir = Path("").absolute() // Note: Path(".") does NOT work..!
|
|
|
|
val fixturesDir = workingDir.resolve("test/fixtures")
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testDirectoriesSanityCheck() {
|
|
|
|
assertEquals("compilerAst", workingDir.fileName.toString())
|
|
|
|
assertTrue(fixturesDir.isDirectory(), "sanity check; should be directory: $fixturesDir")
|
|
|
|
}
|
2021-06-18 19:55:03 +00:00
|
|
|
|
2021-06-13 12:59:57 +00:00
|
|
|
@Test
|
2021-06-13 18:28:01 +00:00
|
|
|
fun testModuleSourceNeedNotEndWithNewline() {
|
|
|
|
val nl = "\n" // say, Unix-style (different flavours tested elsewhere)
|
2021-06-21 10:02:36 +00:00
|
|
|
val src = SourceCode.of("foo {" + nl + "}") // source ends with '}' (= NO newline, issue #40)
|
2021-06-13 18:08:50 +00:00
|
|
|
|
2021-06-19 16:10:26 +00:00
|
|
|
// #45: Prog8ANTLRParser would report (throw) "missing <EOL> at '<EOF>'"
|
2021-06-21 10:02:36 +00:00
|
|
|
val module = parseModule(src)
|
2021-06-19 16:10:26 +00:00
|
|
|
assertEquals(1, module.statements.size)
|
2021-06-13 18:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2021-06-13 18:28:01 +00:00
|
|
|
fun testModuleSourceMayEndWithNewline() {
|
|
|
|
val nl = "\n" // say, Unix-style (different flavours tested elsewhere)
|
|
|
|
val srcText = "foo {" + nl + "}" + nl // source does end with a newline (issue #40)
|
2021-06-21 10:02:36 +00:00
|
|
|
val module = parseModule(SourceCode.of(srcText))
|
2021-06-19 16:10:26 +00:00
|
|
|
assertEquals(1, module.statements.size)
|
2021-06-13 12:59:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 18:47:14 +00:00
|
|
|
@Test
|
|
|
|
fun testAllBlocksButLastMustEndWithNewline() {
|
|
|
|
val nl = "\n" // say, Unix-style (different flavours tested elsewhere)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// 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-21 10:02:36 +00:00
|
|
|
assertFailsWith<ParseError> { parseModule(SourceCode.of(srcBad)) }
|
|
|
|
val module = parseModule(SourceCode.of(srcGood))
|
2021-06-19 16:10:26 +00:00
|
|
|
assertEquals(2, module.statements.size)
|
2021-06-13 18:47:14 +00:00
|
|
|
}
|
2021-06-13 20:49:54 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testWindowsAndMacNewlinesAreAlsoFine() {
|
|
|
|
val nlWin = "\r\n"
|
|
|
|
val nlUnix = "\n"
|
|
|
|
val nlMac = "\r"
|
|
|
|
|
2021-06-14 19:58:03 +00:00
|
|
|
//parseModule(Paths.get("test", "fixtures", "mac_newlines.p8").toAbsolutePath())
|
|
|
|
|
2021-06-13 20:49:54 +00:00
|
|
|
// a good mix of all kinds of newlines:
|
|
|
|
val srcText =
|
|
|
|
"foo {" +
|
2021-06-14 19:58:03 +00:00
|
|
|
nlMac +
|
2021-06-13 20:49:54 +00:00
|
|
|
nlWin +
|
|
|
|
"}" +
|
2021-06-14 19:58:03 +00:00
|
|
|
nlMac + // <-- do test a single \r (!) where an EOL is expected
|
2021-06-13 20:49:54 +00:00
|
|
|
"bar {" +
|
2021-06-14 19:58:03 +00:00
|
|
|
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-06-13 20:49:54 +00:00
|
|
|
|
2021-06-21 10:02:36 +00:00
|
|
|
val module = parseModule(SourceCode.of(srcText))
|
2021-06-19 16:10:26 +00:00
|
|
|
assertEquals(2, module.statements.size)
|
2021-06-13 20:49:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 19:55:03 +00:00
|
|
|
@Test
|
|
|
|
fun testInterleavedEolAndCommentBeforeFirstBlock() {
|
|
|
|
// issue: #47
|
|
|
|
val srcText = """
|
|
|
|
; comment
|
|
|
|
|
|
|
|
; comment
|
|
|
|
|
|
|
|
blockA {
|
|
|
|
}
|
|
|
|
"""
|
2021-06-21 10:02:36 +00:00
|
|
|
val module = parseModule(SourceCode.of(srcText))
|
2021-06-19 16:10:26 +00:00
|
|
|
assertEquals(1, module.statements.size)
|
2021-06-18 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testInterleavedEolAndCommentBetweenBlocks() {
|
|
|
|
// issue: #47
|
|
|
|
val srcText = """
|
|
|
|
blockA {
|
|
|
|
}
|
|
|
|
; comment
|
|
|
|
|
|
|
|
; comment
|
|
|
|
|
|
|
|
blockB {
|
|
|
|
}
|
|
|
|
"""
|
2021-06-21 10:02:36 +00:00
|
|
|
val module = parseModule(SourceCode.of(srcText))
|
2021-06-19 16:10:26 +00:00
|
|
|
assertEquals(2, module.statements.size)
|
2021-06-18 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testInterleavedEolAndCommentAfterLastBlock() {
|
|
|
|
// issue: #47
|
|
|
|
val srcText = """
|
|
|
|
blockA {
|
|
|
|
}
|
|
|
|
; comment
|
|
|
|
|
|
|
|
; comment
|
|
|
|
|
|
|
|
"""
|
2021-06-21 10:02:36 +00:00
|
|
|
val module = parseModule(SourceCode.of(srcText))
|
2021-06-19 16:10:26 +00:00
|
|
|
assertEquals(1, module.statements.size)
|
2021-06-18 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testNewlineBetweenTwoBlocksOrDirectivesStillRequired() {
|
|
|
|
// issue: #47
|
|
|
|
|
|
|
|
// block and block
|
2021-06-21 10:02:36 +00:00
|
|
|
assertFailsWith<ParseError>{ parseModule(SourceCode.of("""
|
2021-06-18 19:55:03 +00:00
|
|
|
blockA {
|
|
|
|
} blockB {
|
|
|
|
}
|
2021-06-21 10:02:36 +00:00
|
|
|
""")) }
|
2021-06-18 19:55:03 +00:00
|
|
|
|
|
|
|
// block and directive
|
2021-06-21 10:02:36 +00:00
|
|
|
assertFailsWith<ParseError>{ parseModule(SourceCode.of("""
|
2021-06-18 19:55:03 +00:00
|
|
|
blockB {
|
|
|
|
} %import textio
|
2021-06-21 10:02:36 +00:00
|
|
|
""")) }
|
2021-06-18 19:55:03 +00:00
|
|
|
|
|
|
|
// The following two are bogus due to directive *args* expected to follow the directive name.
|
|
|
|
// Leaving them in anyways.
|
|
|
|
|
|
|
|
// dir and block
|
2021-06-21 10:02:36 +00:00
|
|
|
assertFailsWith<ParseError>{ parseModule(SourceCode.of("""
|
2021-06-18 19:55:03 +00:00
|
|
|
%import textio blockB {
|
|
|
|
}
|
2021-06-21 10:02:36 +00:00
|
|
|
""")) }
|
2021-06-18 19:55:03 +00:00
|
|
|
|
2021-06-21 10:02:36 +00:00
|
|
|
assertFailsWith<ParseError>{ parseModule(SourceCode.of("""
|
2021-06-18 19:55:03 +00:00
|
|
|
%import textio %import syslib
|
2021-06-21 10:02:36 +00:00
|
|
|
""")) }
|
2021-06-19 18:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2021-06-21 10:02:36 +00:00
|
|
|
fun parseModuleShouldNotLookAtImports() {
|
|
|
|
val imported = "i_do_not_exist"
|
|
|
|
val pathNoExt = Path.of(imported).absolute()
|
|
|
|
val pathWithExt = Path.of("${pathNoExt}.p8")
|
|
|
|
val text = "%import $imported"
|
2021-06-19 18:27:04 +00:00
|
|
|
|
2021-06-21 10:02:36 +00:00
|
|
|
assertFalse(pathNoExt.exists(), "sanity check: file should not exist: $pathNoExt")
|
|
|
|
assertFalse(pathWithExt.exists(), "sanity check: file should not exist: $pathWithExt")
|
2021-06-19 18:27:04 +00:00
|
|
|
|
2021-06-21 10:02:36 +00:00
|
|
|
val module = parseModule(SourceCode.of(text))
|
2021-06-19 18:27:04 +00:00
|
|
|
assertEquals(1, module.statements.size)
|
|
|
|
}
|
|
|
|
|
2021-07-04 16:09:16 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testModuleNameForSourceFromString() {
|
|
|
|
val srcText = """
|
|
|
|
main {
|
|
|
|
}
|
|
|
|
""".trimIndent()
|
|
|
|
val module = parseModule(SourceCode.of(srcText))
|
|
|
|
|
|
|
|
// Note: assertContains has *actual* as first param
|
|
|
|
assertContains(module.name, Regex("^anonymous_[0-9a-f]+$"))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testModuleNameForSourceFromPath() {
|
|
|
|
val path = fixturesDir.resolve("simple_main.p8")
|
|
|
|
|
|
|
|
val module = parseModule(SourceCode.fromPath(path))
|
|
|
|
|
|
|
|
assertEquals(path.nameWithoutExtension, module.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun assertPosition(actual: Position, expFile: String, expLine: Int, expStartCol: Int, expEndCol: Int) {
|
|
|
|
assertEquals(expLine, actual.line, ".position.line (1-based)")
|
|
|
|
assertEquals(expStartCol, actual.startCol, ".position.startCol (0-based)" )
|
|
|
|
assertEquals(expEndCol, actual.endCol, ".position.endCol (0-based)")
|
|
|
|
assertEquals(expFile, actual.file, ".position.file")
|
|
|
|
}
|
|
|
|
|
|
|
|
fun assertPosition(actual: Position, expFile: Regex, expLine: Int, expStartCol: Int, expEndCol: Int) {
|
|
|
|
assertEquals(expLine, actual.line, ".position.line (1-based)")
|
|
|
|
assertEquals(expStartCol, actual.startCol, ".position.startCol (0-based)" )
|
|
|
|
assertEquals(expEndCol, actual.endCol, ".position.endCol (0-based)")
|
|
|
|
// Note: assertContains expects *actual* value first
|
|
|
|
assertContains(actual.file, expFile, ".position.file")
|
|
|
|
}
|
|
|
|
|
|
|
|
fun assertPositionOf(actual: Node, expFile: String, expLine: Int, expStartCol: Int, expEndCol: Int) =
|
|
|
|
assertPosition(actual.position, expFile, expLine, expStartCol, expEndCol)
|
|
|
|
|
|
|
|
fun assertPositionOf(actual: Node, expFile: Regex, expLine: Int, expStartCol: Int, expEndCol: Int) =
|
|
|
|
assertPosition(actual.position, expFile, expLine, expStartCol, expEndCol)
|
|
|
|
|
|
|
|
|
2021-06-18 19:55:03 +00:00
|
|
|
@Test
|
2021-06-19 16:10:26 +00:00
|
|
|
fun testErrorLocationForSourceFromString() {
|
|
|
|
val srcText = "bad * { }\n"
|
2021-06-18 19:55:03 +00:00
|
|
|
|
2021-06-21 10:02:36 +00:00
|
|
|
assertFailsWith<ParseError> { parseModule(SourceCode.of(srcText)) }
|
2021-06-19 16:10:26 +00:00
|
|
|
try {
|
2021-06-21 10:02:36 +00:00
|
|
|
parseModule(SourceCode.of(srcText))
|
2021-06-19 16:10:26 +00:00
|
|
|
} catch (e: ParseError) {
|
2021-07-04 16:09:16 +00:00
|
|
|
assertPosition(e.position, Regex("^<String@[0-9a-f]+>$"), 1, 4, 4)
|
2021-06-19 16:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testErrorLocationForSourceFromPath() {
|
2021-07-04 16:09:16 +00:00
|
|
|
val path = fixturesDir.resolve("file_with_syntax_error.p8")
|
2021-06-19 16:10:26 +00:00
|
|
|
|
2021-06-21 10:02:36 +00:00
|
|
|
assertFailsWith<ParseError> { parseModule(SourceCode.fromPath(path)) }
|
2021-06-19 16:10:26 +00:00
|
|
|
try {
|
2021-06-21 10:02:36 +00:00
|
|
|
parseModule(SourceCode.fromPath(path))
|
2021-06-19 16:10:26 +00:00
|
|
|
} catch (e: ParseError) {
|
2021-07-04 16:09:16 +00:00
|
|
|
assertPosition(e.position, path.absolutePathString(), 2, 6, 6)
|
2021-06-19 16:10:26 +00:00
|
|
|
}
|
2021-06-18 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:09:16 +00:00
|
|
|
@Test
|
|
|
|
fun testModulePositionForSourceFromString() {
|
|
|
|
val srcText = """
|
|
|
|
main {
|
|
|
|
}
|
|
|
|
""".trimIndent()
|
|
|
|
val module = parseModule(SourceCode.of(srcText))
|
|
|
|
assertPositionOf(module, Regex("^<String@[0-9a-f]+>$"), 1, 0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testModulePositionForSourceFromPath() {
|
|
|
|
val path = fixturesDir.resolve("simple_main.p8")
|
|
|
|
|
|
|
|
val module = parseModule(SourceCode.fromPath(path))
|
|
|
|
assertPositionOf(module, path.absolutePathString(), 1, 0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-13 12:59:57 +00:00
|
|
|
@Test
|
|
|
|
fun testProg8Ast() {
|
2021-06-21 10:02:36 +00:00
|
|
|
val module = parseModule(SourceCode.of("""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""))
|
2021-06-19 16:10:26 +00:00
|
|
|
assertIs<Block>(module.statements.first())
|
|
|
|
assertEquals((module.statements.first() as Block).name, "main")
|
2021-06-13 12:59:57 +00:00
|
|
|
}
|
|
|
|
}
|