diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 0d296ce0b..6891e2c26 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -17,8 +17,8 @@ import prog8.compiler.target.ICompilationTarget import prog8.compiler.target.asmGeneratorFor import prog8.optimizer.* import prog8.parser.ParsingFailedError +import prog8.parser.SourceCode.Companion.libraryFilePrefix import java.io.File -import java.io.InputStream import java.nio.file.Path import kotlin.io.path.Path import kotlin.io.path.nameWithoutExtension @@ -352,38 +352,19 @@ fun printAst(programAst: Program) { } internal fun loadAsmIncludeFile(filename: String, sourcePath: Path): Result { - if (filename.startsWith("library:")) { - val resource = getEmbeddedResource(filename.substring(8)) - resource.fold( - onSuccess = { resource -> - val text = resource.bufferedReader().use { it.readText() } - return Result.success(text) - }, - onFailure = { - return Result.failure(IllegalArgumentException("library file '$filename' not found")) // TODO FileNotFoundException instead? - } - ) + return if (filename.startsWith(libraryFilePrefix)) { + runCatching { + val stream = object {}.javaClass.getResourceAsStream("/prog8lib/${filename.substring(libraryFilePrefix.length)}") // TODO handle via SourceCode + stream ?: throw NoSuchFileException(File(filename)) + }.mapCatching { + it.bufferedReader().use { r -> r.readText() } + } } else { // first try in the isSameAs folder as where the containing file was imported from val sib = sourcePath.resolveSibling(filename) - return if (sib.toFile().isFile) + if (sib.toFile().isFile) Result.success(sib.toFile().readText()) else Result.success(File(filename).readText()) } } - -/** - * Handle via SourceCode - */ -internal fun getEmbeddedResource(name: String): Result { - return try { - val stream = object {}.javaClass.getResourceAsStream("/prog8lib/$name") - if(stream!=null) - Result.success(stream) - else - Result.failure(NoSuchFileException(File(name))) - } catch(ex: Exception) { - Result.failure(ex) - } -} diff --git a/compiler/test/helpers/ErrorReporterForTests.kt b/compiler/test/helpers/ErrorReporterForTests.kt index bfb589af9..0d77cc766 100644 --- a/compiler/test/helpers/ErrorReporterForTests.kt +++ b/compiler/test/helpers/ErrorReporterForTests.kt @@ -5,6 +5,7 @@ import prog8.compiler.IErrorReporter class ErrorReporterForTests: IErrorReporter { + val errors = mutableListOf() val warnings = mutableListOf() diff --git a/compilerAst/src/prog8/parser/SourceCode.kt b/compilerAst/src/prog8/parser/SourceCode.kt index dbe0ccb52..44436b44a 100644 --- a/compilerAst/src/prog8/parser/SourceCode.kt +++ b/compilerAst/src/prog8/parser/SourceCode.kt @@ -61,6 +61,11 @@ abstract class SourceCode { // "static" factory methods companion object { + /** + * filename prefix to designate library files that will be retreived from internal resources rather than disk + */ + const val libraryFilePrefix = "library:" + /** * Turn a plain String into a [SourceCode] object. * [origin] will be something like ``. @@ -121,7 +126,7 @@ abstract class SourceCode { } return object : SourceCode() { override val isFromResources = true - override val origin = "library:$normalized" + override val origin = "$libraryFilePrefix$normalized" override fun getCharStream(): CharStream { val inpStr = object {}.javaClass.getResourceAsStream(normalized) return CharStreams.fromStream(inpStr) diff --git a/compilerAst/test/TestSourceCode.kt b/compilerAst/test/TestSourceCode.kt index 52b2e944f..1597b7ad7 100644 --- a/compilerAst/test/TestSourceCode.kt +++ b/compilerAst/test/TestSourceCode.kt @@ -4,6 +4,7 @@ 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 @@ -76,7 +77,7 @@ class TestSourceCode { val srcFile = assumeReadableFile(resourcesDir, pathString).toFile() val src = SourceCode.fromResources(pathString) - assertEquals("library:/$pathString", src.origin) + assertEquals("$libraryFilePrefix/$pathString", src.origin) assertEquals(srcFile.readText(), src.asString()) } @@ -86,7 +87,7 @@ class TestSourceCode { val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile() val src = SourceCode.fromResources(pathString) - assertEquals("library:$pathString", src.origin) + assertEquals("$libraryFilePrefix$pathString", src.origin) assertEquals(srcFile.readText(), src.asString()) } @@ -96,7 +97,7 @@ class TestSourceCode { val srcFile = assumeReadableFile(resourcesDir, pathString).toFile() val src = SourceCode.fromResources(pathString) - assertEquals("library:/$pathString", src.origin) + assertEquals("$libraryFilePrefix/$pathString", src.origin) assertEquals(srcFile.readText(), src.asString()) assertTrue(src.isFromResources, ".isFromResources") } @@ -107,7 +108,7 @@ class TestSourceCode { val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile() val src = SourceCode.fromResources(pathString) - assertEquals("library:$pathString", src.origin) + assertEquals("$libraryFilePrefix$pathString", src.origin) assertEquals(srcFile.readText(), src.asString()) } @@ -117,7 +118,7 @@ class TestSourceCode { val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile() val src = SourceCode.fromResources(pathString) - assertEquals("library:/prog8lib/math.p8", src.origin) + assertEquals("$libraryFilePrefix/prog8lib/math.p8", src.origin) assertEquals(srcFile.readText(), src.asString()) assertTrue(src.isFromResources, ".isFromResources") }