* used "@embedded@" convention instead of "<res:...>", put it into SourceCode

This commit is contained in:
meisl 2021-07-09 16:28:04 +02:00
parent 137a89da15
commit b0073ac933
2 changed files with 22 additions and 13 deletions

View File

@ -41,19 +41,12 @@ object Prog8Parser {
parseTree.block().forEach { module.add(it.toAst(module.isLibrary(), PetsciiEncoding)) } parseTree.block().forEach { module.add(it.toAst(module.isLibrary(), PetsciiEncoding)) }
return module return module
} }
// FIXME: hacking together a path string:
private fun SourceCode.pathString() =
origin
.substringAfter("<res:")
.substringAfter("<")
.substringBeforeLast(">")
private class ParsedModule(src: SourceCode) : Module( private class ParsedModule(src: SourceCode) : Module(
// FIXME: hacking together a name for the module: // FIXME: hacking together a name for the module:
name = src.pathString() name = src.pathString()
.substringBeforeLast(".") .substringBeforeLast(".") // must also work with an origin = "<String@123beef>"
.substringAfterLast("/") .substringAfterLast("/")
.substringAfterLast("\\") .substringAfterLast("\\")
.replace("String@", "anonymous_"), .replace("String@", "anonymous_"),

View File

@ -10,16 +10,32 @@ import kotlin.io.path.*
* Encapsulates - and ties together - actual source code (=text) * Encapsulates - and ties together - actual source code (=text)
* and its [origin]. * and its [origin].
*/ */
abstract class SourceCode() { abstract class SourceCode {
/**
* To be used *only* by the parser (as input to a TokenStream).
* DO NOT mess around with!
*/
internal abstract fun getCharStream(): CharStream
/** /**
* Where this [SourceCode] instance came from. * Where this [SourceCode] instance came from.
* This can be one of the following: * This can be one of the following:
* * a normal string representation of a [java.nio.file.Path], if it originates from a file (see [fromPath]) * * a normal string representation of a [java.nio.file.Path], if it originates from a file (see [fromPath])
* * `<String@44c56085>` if was created via [of] * * `<String@44c56085>` if was created via [of]
* * `<res:/x/y/z.ext>` if it came from resources (see [fromResources]) * * `@embedded@/x/y/z.ext` if it came from resources (see [fromResources])
*/ */
abstract val origin: String abstract val origin: String
abstract fun getCharStream(): CharStream
/**
* FIXME: hacking together a [SourceCode]'s "path 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*)
*/
fun pathString() =
origin
.substringAfter("<").substringBeforeLast(">") // or from plain string?
/** /**
* The source code as plain string. * The source code as plain string.
@ -92,7 +108,7 @@ abstract class SourceCode() {
reason = "looked in resources rooted at $rscRoot") reason = "looked in resources rooted at $rscRoot")
} }
return object : SourceCode() { return object : SourceCode() {
override val origin = "<res:$normalized>" override val origin = "@embedded@$normalized"
override fun getCharStream(): CharStream { override fun getCharStream(): CharStream {
val inpStr = object{}.javaClass.getResourceAsStream(normalized) val inpStr = object{}.javaClass.getResourceAsStream(normalized)
val chars = CharStreams.fromStream(inpStr) val chars = CharStreams.fromStream(inpStr)