diff --git a/compiler/test/ModuleImporterTests.kt b/compiler/test/ModuleImporterTests.kt index 9d724b57d..74a2a1fe6 100644 --- a/compiler/test/ModuleImporterTests.kt +++ b/compiler/test/ModuleImporterTests.kt @@ -15,6 +15,7 @@ import prog8.ast.Program import prog8.compiler.IErrorReporter import prog8.compiler.ModuleImporter import prog8.parser.ParseError +import prog8.parser.SourceCode import prog8tests.helpers.* import kotlin.io.path.* @@ -191,7 +192,7 @@ class TestModuleImporter { repeat(2) { n -> assertFailsWith(count[n] + " call") { act() }.let { - assertThat(it.position.file, equalTo(srcPath.absolutePathString())) + assertThat(it.position.file, equalTo(SourceCode.relative(srcPath).toString())) assertThat("line; should be 1-based", it.position.line, equalTo(2)) assertThat("startCol; should be 0-based", it.position.startCol, equalTo(6)) assertThat("endCol; should be 0-based", it.position.endCol, equalTo(6)) @@ -221,7 +222,7 @@ class TestModuleImporter { repeat(repetitions) { n -> assertFailsWith(count[n] + " call") { act() }.let { - assertThat(it.position.file, equalTo(imported.absolutePathString())) + assertThat(it.position.file, equalTo(SourceCode.relative(imported).toString())) assertThat("line; should be 1-based", it.position.line, equalTo(2)) assertThat("startCol; should be 0-based", it.position.startCol, equalTo(6)) assertThat("endCol; should be 0-based", it.position.endCol, equalTo(6)) @@ -277,7 +278,7 @@ class TestModuleImporter { repeat(2) { n -> assertFailsWith(count[n] + " call") { importer.importLibraryModule(srcPath.nameWithoutExtension) }.let { - assertThat(it.position.file, equalTo(srcPath.absolutePathString())) + assertThat(it.position.file, equalTo(SourceCode.relative(srcPath).toString())) assertThat("line; should be 1-based", it.position.line, equalTo(2)) assertThat("startCol; should be 0-based", it.position.startCol, equalTo(6)) assertThat("endCol; should be 0-based", it.position.endCol, equalTo(6)) @@ -297,7 +298,7 @@ class TestModuleImporter { repeat(repetitions) { n -> assertFailsWith(count[n] + " call") { act() }.let { - assertThat(it.position.file, equalTo(imported.normalize().absolutePathString())) + assertThat(it.position.file, equalTo(SourceCode.relative(imported).toString())) assertThat("line; should be 1-based", it.position.line, equalTo(2)) assertThat("startCol; should be 0-based", it.position.startCol, equalTo(6)) assertThat("endCol; should be 0-based", it.position.endCol, equalTo(6)) diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index 666268e25..a5ae77086 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -6,6 +6,9 @@ import prog8.ast.base.* import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.parser.Prog8ANTLRParser +import prog8.parser.SourceCode +import java.nio.file.Path +import kotlin.io.path.isRegularFile /***************** Antlr Extension methods to create AST ****************/ @@ -14,8 +17,12 @@ private data class NumericLiteral(val number: Number, val datatype: DataType) private fun ParserRuleContext.toPosition() : Position { - val filename = start.inputStream.sourceName - + val path = Path.of(start.inputStream.sourceName) + val filename = if(path.isRegularFile()) { + SourceCode.relative(Path.of(start.inputStream.sourceName)).toString() + } else { + path.toString().substringAfter("<").substringBeforeLast(">") + } // note: beware of TAB characters in the source text, they count as 1 column... return Position(filename, start.line, start.charPositionInLine, stop.charPositionInLine + stop.text.length) } diff --git a/compilerAst/src/prog8/parser/SourceCode.kt b/compilerAst/src/prog8/parser/SourceCode.kt index 605598f5e..9ab2adfef 100644 --- a/compilerAst/src/prog8/parser/SourceCode.kt +++ b/compilerAst/src/prog8/parser/SourceCode.kt @@ -8,7 +8,6 @@ import java.nio.channels.Channels import java.nio.charset.CodingErrorAction import java.nio.charset.StandardCharsets import java.nio.file.Path -import kotlin.io.path.absolutePathString import kotlin.io.path.exists import kotlin.io.path.isDirectory import kotlin.io.path.isReadable @@ -66,6 +65,7 @@ sealed class SourceCode { */ const val libraryFilePrefix = "library:" val curdir: Path = Path.of(".").toAbsolutePath() + fun relative(path: Path): Path = curdir.relativize(path.toAbsolutePath()) } /** @@ -103,12 +103,12 @@ sealed class SourceCode { } override val isFromResources = false - override val origin = curdir.relativize(normalized.toAbsolutePath()).normalize().toString() + override val origin = relative(normalized).toString() override fun getCharStream(): CharStream = CharStreams.fromPath(normalized) } /** - * [origin]: `` for a given `pathString` of "x/y/z.p8" + * [origin]: `library:/x/y/z.p8` for a given `pathString` of "x/y/z.p8" */ class Resource(pathString: String): SourceCode() { private val normalized = "/" + Path.of(pathString).normalize().toMutableList().joinToString("/") diff --git a/compilerAst/test/TestProg8Parser.kt b/compilerAst/test/TestProg8Parser.kt index 4fca5034c..8208eeed9 100644 --- a/compilerAst/test/TestProg8Parser.kt +++ b/compilerAst/test/TestProg8Parser.kt @@ -1,6 +1,5 @@ package prog8tests -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance @@ -18,7 +17,6 @@ import prog8.parser.SourceCode 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 @@ -303,7 +301,7 @@ class TestProg8Parser { try { parseModule(SourceCode.File(path)) } catch (e: ParseError) { - assertPosition(e.position, path.absolutePathString(), 2, 6) // TODO: endCol wrong + assertPosition(e.position, SourceCode.relative(path).toString(), 2, 6) // TODO: endCol wrong } } @@ -314,15 +312,14 @@ class TestProg8Parser { } """.trimIndent() val module = parseModule(SourceCode.Text(srcText)) - assertPositionOf(module, Regex("^$"), 1, 0) // TODO: endCol wrong + assertPositionOf(module, Regex("^String@[0-9a-f]+$"), 1, 0) // TODO: endCol wrong } @Test fun `of Module parsed from a file`() { val path = assumeReadableFile(fixturesDir, "simple_main.p8") - val module = parseModule(SourceCode.File(path)) - assertPositionOf(module, path.absolutePathString(), 1, 0) // TODO: endCol wrong + assertPositionOf(module, SourceCode.relative(path).toString(), 1, 0) // TODO: endCol wrong } @Test @@ -331,8 +328,7 @@ class TestProg8Parser { val module = parseModule(SourceCode.File(path)) val mpf = module.position.file - - assertPositionOf(module, path.absolutePathString(), 1, 0) // TODO: endCol wrong + assertPositionOf(module, SourceCode.relative(path).toString(), 1, 0) // TODO: endCol wrong val mainBlock = module.statements.filterIsInstance()[0] assertPositionOf(mainBlock, mpf, 1, 0) // TODO: endCol wrong! val startSub = mainBlock.statements.filterIsInstance()[0] diff --git a/compilerAst/test/TestSourceCode.kt b/compilerAst/test/TestSourceCode.kt index 395ed6395..04d8e5d99 100644 --- a/compilerAst/test/TestSourceCode.kt +++ b/compilerAst/test/TestSourceCode.kt @@ -1,13 +1,11 @@ package prog8tests -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import prog8.parser.SourceCode import prog8.parser.SourceCode.Companion.libraryFilePrefix import prog8tests.helpers.* import kotlin.io.path.Path -import kotlin.io.path.absolutePathString import kotlin.test.assertContains import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -53,8 +51,7 @@ class TestSourceCode { val filename = "simple_main.p8" val path = assumeReadableFile(fixturesDir, filename) val src = SourceCode.File(path) - - val expectedOrigin = path.normalize().absolutePathString() + val expectedOrigin = SourceCode.relative(path).toString() assertEquals(expectedOrigin, src.origin) assertEquals(path.toFile().readText(), src.asString()) } @@ -65,8 +62,7 @@ class TestSourceCode { val path = Path(".", "test", "..", "test", "fixtures", filename) val srcFile = assumeReadableFile(path).toFile() val src = SourceCode.File(path) - - val expectedOrigin = path.normalize().absolutePathString() + val expectedOrigin = SourceCode.relative(path).toString() assertEquals(expectedOrigin, src.origin) assertEquals(srcFile.readText(), src.asString()) }