got rid of SourceCode.pathString() and the 'need' to strip < and >

This commit is contained in:
Irmen de Jong 2021-10-16 17:15:22 +02:00
parent 6ef438ce50
commit cf0e395921
9 changed files with 24 additions and 26 deletions

View File

@ -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): Result<St
}.mapError { NoSuchFileException(File(filename)) }
} else {
// first try in the isSameAs folder as where the containing file was imported from
val sib = Path(source.pathString()).resolveSibling(filename)
val sib = Path(source.origin).resolveSibling(filename)
if (sib.toFile().isFile)
Ok(sib.toFile().readText())

View File

@ -728,10 +728,10 @@ internal class AstChecker(private val program: Program,
return
val definingModule = directive.definingModule
if (definingModule.isLibrary || definingModule.source is SourceCode.Generated)
if (definingModule.isLibrary || !definingModule.source.isFromFilesystem)
return
val s = definingModule.source.pathString()
val s = definingModule.source.origin
val sourceFileCandidate = Path(s).resolveSibling(filename).toFile()
if (sourceFileCandidate.isFile)
return

View File

@ -1339,7 +1339,7 @@ $repeatLabel lda $counterVar
val length = if(stmt.args.size>2) ", ${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()

View File

@ -354,12 +354,10 @@ open class Module(final override var statements: MutableList<Statement>,
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

View File

@ -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)

View File

@ -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)
{
/**

View File

@ -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
}

View File

@ -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("^<String@[0-9a-f]+>$"))
}
@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("^<String@[0-9a-f]+>$"), 1, 0) // TODO: endCol wrong
}
@Test

View File

@ -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