diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 125556019..363b776c7 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -197,8 +197,8 @@ fun parseImports(filepath: Path, errors.report() val importedFiles = programAst.modules.map { it.source } - .filter { it !is SourceCode.Generated && !it.isFromResources } // TODO: parseImports/importedFiles - maybe rather `source.isFromFilesystem`? - .map { Path(it.pathString()) } + .filter { it.isFromFilesystem } + .map { Path(it.origin) } val compilerOptions = determineCompilationOptions(programAst, compTarget) if (compilerOptions.launcher == LauncherType.BASIC && compilerOptions.output != OutputType.PRG) throw ParsingFailedError("${programAst.modules.first().position} BASIC launcher requires output type PRG.") @@ -383,7 +383,7 @@ internal fun loadAsmIncludeFile(filename: String, source: SourceCode): Result2) ", ${stmt.args[2].int}" else "" if(stmt.definingModule.source is SourceCode.Generated) TODO("%asmbinary inside non-library, non-filesystem module") - val sourcePath = Path(stmt.definingModule.source.pathString()) + val sourcePath = Path(stmt.definingModule.source.origin) val includedPath = sourcePath.resolveSibling(includedName) val pathForAssembler = outputDir // #54: 64tass needs the path *relative to the .asm file* .toAbsolutePath() diff --git a/compilerAst/src/prog8/ast/AstToplevel.kt b/compilerAst/src/prog8/ast/AstToplevel.kt index 13711f87f..b3e665848 100644 --- a/compilerAst/src/prog8/ast/AstToplevel.kt +++ b/compilerAst/src/prog8/ast/AstToplevel.kt @@ -354,12 +354,10 @@ open class Module(final override var statements: MutableList, override lateinit var parent: Node lateinit var program: Program - // the module name is derived back from the path of the source - override val name = source.pathString() + override val name = source.origin .substringBeforeLast(".") .substringAfterLast("/") .substringAfterLast("\\") - .replace("String@", "anonymous_") val loadAddress: Int by lazy { val address = (statements.singleOrNull { it is Directive && it.directive == "%address" } as? Directive)?.args?.single()?.int ?: 0 diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index 6e9e1129f..eadba560e 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -23,10 +23,10 @@ private fun ParserRuleContext.toPosition() : Position { if(path.isRegularFile()) { SourceCode.relative(path).toString() } else { - path.toString().substringAfter("<").substringBeforeLast(">") // TODO fix the need to get rid of the < and > + path.toString() } } else { - pathString.substringAfter("<").substringBeforeLast(">") // TODO fix the need to get rid of the < and > + pathString } // 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/Prog8Parser.kt b/compilerAst/src/prog8/parser/Prog8Parser.kt index 5ae045491..546615afa 100644 --- a/compilerAst/src/prog8/parser/Prog8Parser.kt +++ b/compilerAst/src/prog8/parser/Prog8Parser.kt @@ -43,7 +43,7 @@ object Prog8Parser { } private class ParsedModule(source: SourceCode) : - Module(mutableListOf(), Position(source.pathString(), 1, 0, 0), source) + Module(mutableListOf(), Position(source.origin, 1, 0, 0), source) { /** diff --git a/compilerAst/src/prog8/parser/SourceCode.kt b/compilerAst/src/prog8/parser/SourceCode.kt index 380eb3384..c41c676f2 100644 --- a/compilerAst/src/prog8/parser/SourceCode.kt +++ b/compilerAst/src/prog8/parser/SourceCode.kt @@ -25,11 +25,15 @@ sealed class SourceCode { internal abstract fun getCharStream(): CharStream /** - * Whether this [SourceCode] instance was created by - * factory method [Resource] + * Whether this [SourceCode] instance was created as a [Resource] */ abstract val isFromResources: Boolean + /** + * Whether this [SourceCode] instance was created as a [File] + */ + abstract val isFromFilesystem: Boolean + /** * Where this [SourceCode] instance came from. * This can be one of the following: @@ -39,14 +43,6 @@ sealed class SourceCode { */ abstract val origin: String - - /** - * This is really just [origin] with any stuff removed that would render it an invalid path name. - * (Note: a *valid* path name does NOT mean that the denoted file or folder *exists*) - * TODO this promise doesn't work.... a "library:/...." resource path is not valid on Windows for example - */ - fun pathString() = origin.substringAfter("<").substringBeforeLast(">") // or from plain string? - /** * The source code as plain string. * *Note: this is meant for testing and debugging, do NOT use in application code!* @@ -78,6 +74,7 @@ sealed class SourceCode { */ class Text(val text: String): SourceCode() { override val isFromResources = false + override val isFromFilesystem = false override val origin = "$stringSourcePrefix${System.identityHashCode(text).toString(16)}>" override fun getCharStream(): CharStream = CharStreams.fromString(text, origin) } @@ -107,6 +104,7 @@ sealed class SourceCode { } override val isFromResources = false + override val isFromFilesystem = true override val origin = relative(normalized).toString() override fun getCharStream(): CharStream = CharStreams.fromPath(normalized) } @@ -129,6 +127,7 @@ sealed class SourceCode { } override val isFromResources = true + override val isFromFilesystem = false override val origin = "$libraryFilePrefix$normalized" override fun getCharStream(): CharStream { val inpStr = object {}.javaClass.getResourceAsStream(normalized)!! @@ -142,8 +141,9 @@ sealed class SourceCode { * SourceCode for internally generated nodes (usually Modules) */ class Generated(name: String) : SourceCode() { - override fun getCharStream(): CharStream = throw IOException("generated code doesn't have a stream to read") + override fun getCharStream(): CharStream = throw IOException("generated code nodes doesn't have a stream to read") override val isFromResources: Boolean = false + override val isFromFilesystem: Boolean = false override val origin: String = name } diff --git a/compilerAst/test/TestProg8Parser.kt b/compilerAst/test/TestProg8Parser.kt index 8208eeed9..4910fc7e6 100644 --- a/compilerAst/test/TestProg8Parser.kt +++ b/compilerAst/test/TestProg8Parser.kt @@ -219,7 +219,7 @@ class TestProg8Parser { val module = parseModule(SourceCode.Text(srcText)) // Note: assertContains has *actual* as first param - assertContains(module.name, Regex("^anonymous_[0-9a-f]+$")) + assertContains(module.name, Regex("^$")) } @Test @@ -312,7 +312,7 @@ class TestProg8Parser { } """.trimIndent() val module = parseModule(SourceCode.Text(srcText)) - assertPositionOf(module, Regex("^String@[0-9a-f]+$"), 1, 0) // TODO: endCol wrong + assertPositionOf(module, Regex("^$"), 1, 0) // TODO: endCol wrong } @Test diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 263111537..2be82798f 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,7 @@ TODO For next compiler release ^^^^^^^^^^^^^^^^^^^^^^^^^ -get rid of SourceCode.pathString() and the 'need' to strip < and > ? +... Blocked by Commander-x16 v39 release