added \xHH escape character to strings, allow strings of length zero.

This commit is contained in:
Irmen de Jong 2020-10-04 13:05:43 +02:00
parent a6427e0949
commit 956b0c3fa7
3 changed files with 19 additions and 5 deletions

View File

@ -647,7 +647,21 @@ private fun prog8Parser.VardeclContext.toAst(): VarDecl {
)
}
internal fun escape(str: String) = str.replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r")
internal fun escape(str: String): String {
val es2 = str.replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r")
val es = str.map {
when(it) {
'\t' -> "\\t"
'\n' -> "\\n"
'\r' -> "\\r"
'"' -> "\\\""
in '\u8000'..'\u80ff' -> "\\x" + (it.toInt() - 0x8000).toString(16).padStart(2, '0')
in '\u0000'..'\u00ff' -> it.toString()
else -> "\\u" + it.toInt().toString(16).padStart(4, '0')
}
}
return es.joinToString("")
}
internal fun unescape(str: String, position: Position): String {
val result = mutableListOf<Char>()
@ -664,7 +678,7 @@ internal fun unescape(str: String, position: Position): String {
'u' -> {
"${iter.nextChar()}${iter.nextChar()}${iter.nextChar()}${iter.nextChar()}".toInt(16).toChar()
}
'$' -> {
'x' -> {
// special hack 0x8000..0x80ff will be outputted verbatim without encoding
val hex = ("" + iter.nextChar() + iter.nextChar()).toInt(16)
(0x8000 + hex).toChar()

View File

@ -143,8 +143,8 @@ internal class AstIdentifiersChecker(private val program: Program, private val e
}
override fun visit(string: StringLiteralValue) {
if (string.value.length !in 1..255)
errors.err("string literal length must be between 1 and 255", string.position)
if (string.value.length > 255)
errors.err("string literal length max is 255", string.position)
super.visit(string)
}

View File

@ -416,7 +416,7 @@ There are several escape sequences available to put special characters into your
- ``\r`` - carriage return character (more or less the same as newline if printing to the screen)
- ``\"`` - quote character (otherwise it would terminate the string)
- ``\uHHHH`` - a unicode codepoint \u0000 - \uffff (16-bit hexadecimal)
- ``\$HH`` - 8-bit hex value that will be copied verbatim *without encoding*
- ``\xHH`` - 8-bit hex value that will be copied verbatim *without encoding*
Operators