fix SourceCode to properly set the sourceName of a resource or string as well

This commit is contained in:
Irmen de Jong 2021-10-13 21:46:38 +02:00
parent d790878af6
commit 855e18b31c
4 changed files with 10 additions and 16 deletions

View File

@ -14,15 +14,6 @@ private data class NumericLiteral(val number: Number, val datatype: DataType)
private fun ParserRuleContext.toPosition() : Position {
/*
val customTokensource = this.start.tokenSource as? CustomLexer
val filename =
when {
customTokensource!=null -> customTokensource.modulePath.toString()
start.tokenSource.sourceName == IntStream.UNKNOWN_SOURCE_NAME -> "@internal@"
else -> File(start.inputStream.sourceName).name
}
*/
val filename = start.inputStream.sourceName
// note: beware of TAB characters in the source text, they count as 1 column...

View File

@ -4,6 +4,9 @@ import org.antlr.v4.runtime.CharStream
import org.antlr.v4.runtime.CharStreams
import java.io.File
import java.io.IOException
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
@ -72,7 +75,7 @@ sealed class SourceCode {
class Text(val text: String): SourceCode() {
override val isFromResources = false
override val origin = "<String@${System.identityHashCode(text).toString(16)}>"
override fun getCharStream(): CharStream = CharStreams.fromString(text)
override fun getCharStream(): CharStream = CharStreams.fromString(text, origin)
}
/**
@ -124,8 +127,10 @@ sealed class SourceCode {
override val isFromResources = true
override val origin = "$libraryFilePrefix$normalized"
override fun getCharStream(): CharStream {
val inpStr = object {}.javaClass.getResourceAsStream(normalized)
return CharStreams.fromStream(inpStr)
val inpStr = object {}.javaClass.getResourceAsStream(normalized)!!
// CharStreams.fromStream() doesn't allow us to set the stream name properly, so we use a lower level api
val channel = Channels.newChannel(inpStr)
return CharStreams.fromChannel(channel, StandardCharsets.UTF_8, 4096, CodingErrorAction.REPLACE, origin, -1);
}
}

View File

@ -3,9 +3,7 @@ TODO
For next compiler release
^^^^^^^^^^^^^^^^^^^^^^^^^
- fix "unknown" reported in resulting .asm as source file of included library modules/resources
unittest: "of non-root Nodes parsed from a string"
- fix weird error when doing %import textio.p8
Blocked by Commander-x16 v39 release

View File

@ -4,7 +4,7 @@ main {
str myBar = "main.bar"
foo_bar:
%asminclude "compiler/test/fixtures/foo_bar.asm22" ; FIXME: should be accessible from inside start() but give assembler error
; %asminclude "compiler/test/fixtures/foo_bar.asm22" ; FIXME: should be accessible from inside start() but give assembler error
sub start() {
txt.print(myBar)