print unmappable character in escaped form in errormessage

This commit is contained in:
Irmen de Jong 2021-05-11 18:09:09 +02:00
parent f4d83075be
commit a20efa56eb
3 changed files with 29 additions and 24 deletions

View File

@ -1,5 +1,6 @@
package prog8.compiler.target.cbm
import prog8.ast.antlr.escape
import java.io.CharConversionException
object Petscii {
@ -1073,7 +1074,7 @@ object Petscii {
}
else -> {
val case = if (lowercase) "lower" else "upper"
throw CharConversionException("no ${case}Petscii character for '$chr' (${chr.code})")
throw CharConversionException("no ${case}Petscii character for '${escape(chr.toString())}' (${chr.code})")
}
}
}
@ -1110,7 +1111,7 @@ object Petscii {
}
else -> {
val case = if (lowercase) "lower" else "upper"
throw CharConversionException("no ${case}Screencode character for '$chr' (${chr.code})")
throw CharConversionException("no ${case}Screencode character for '${escape(chr.toString())}' (${chr.code})")
}
}
}

View File

@ -10,9 +10,9 @@ fun escape(str: String): String {
'\n' -> "\\n"
'\r' -> "\\r"
'"' -> "\\\""
in '\u8000'..'\u80ff' -> "\\x" + (it.toInt() - 0x8000).toString(16).padStart(2, '0')
in '\u8000'..'\u80ff' -> "\\x" + (it.code - 0x8000).toString(16).padStart(2, '0')
in '\u0000'..'\u00ff' -> it.toString()
else -> "\\u" + it.toInt().toString(16).padStart(4, '0')
else -> "\\u" + it.code.toString(16).padStart(4, '0')
}
}
return es.joinToString("")

View File

@ -1,23 +1,27 @@
%import textio
%zeropage basicsafe
%option no_sysinit
%import textio ; txt.*
main {
sub start() {
; ATTENTION: uncomment only one problematic line at a time!
main {
; Normal string literals, i.e. PETSCII encoding
; ---------------------------------------------
txt.print("\"") ; fine
txt.print("\n") ; fine
txt.print("\r") ; fine
; txt.print("\\") ; yields CharConversionException
; txt.print("xyz\\") ; yields prog8.compiler.AssemblyError
byte[] xs1 = "foo1" ; <<<<<<<<<<<<
str xs2 = "foo2" ; <<<<<<<<<<<<
sub start() {
txt.print(xs1)
stringopt()
}
; @-strings, i.e. translated into
; the alternate character encoding (Screencodes/pokes)
; ----------------------------------------------------
txt.print(@"\"") ; fine
txt.print(@"\n") ; yields CharConversionException
; txt.print(@"xyz\n") ; yields prog8.compiler.AssemblyError
; txt.print(@"\r") ; yields CharConversionException
; txt.print(@"xyz\r") ; yields prog8.compiler.AssemblyError
; txt.print(@"\\") ; yields CharConversionException
; txt.print(@"\\") ; yields prog8.compiler.AssemblyError
sub stringopt() {
str message = "a"
txt.print(message)
txt.nl()
message[0] = '@'
txt.print(message)
txt.nl()
}
}
; there may be more...
}
}