TypeScriptToLua icon indicating copy to clipboard operation
TypeScriptToLua copied to clipboard

Escape non-printable unicode characters

Open ark120202 opened this issue 6 years ago • 3 comments

Playground

Input:

print("\u0001");

Current Result:

print("")

Expected Result:

print("\1")

Related:

ark120202 avatar Jul 12 '20 16:07 ark120202

FYI - found a case where this actually breaks.

Control character 0x1A (substitute) breaks lua parsing in windows. It fails in lua 5.1, 5.2, and 5.3, but not in luajit or any version on linux.

const c = "\x1A";
c = "" --unfinished string near '<eof>'

tomblind avatar Jul 19 '20 19:07 tomblind

@ark120202 @tomblind @Perryvw

Input:

print("\u0001");

I've researched the topic a bit as I also had issues translating strings like \x00\x01\x02 etc. The workaround in my case was to use Lua's escape characters, like that: \00\01\02 (note: they're integer, not hex). The TypeScript parser doesn't modify the node's text in this case.

However, the multi-byte strings are a huge discrepancy point between the ECMAScript and what Lua is capable of in terms of Unicode.

First of all, some UTF-8 functionality appeared only in Lua 5.3. Starting from this version, you can use the built-in utf8 module as well as the new escape character \u{} (but not \uHHHH!), which was specifically made for Unicode:

local str = "\u{1F639}" -- The "cat laughing" emoji: 😹

print(#str) -- Prints "4"
print(str:len()) -- Prints "4" (same as string.len(str))
print(utf8.len(str)) -- Prints "1"

The same code in ECMAScript:

const str = "\u{1F639}";
console.log(str.length); // Prints "2"

Second, the main discrepancy is the use of UTF-16 by ECMAScript. The functions like codePointAt() (which is not supported by TSTL yet) also conform to the UTF-16 encoding schema, meaning that pretty much like the .length property, it will count the surrogate characters. So, given the example above, one Unicode code point (the emoji) will be represented as two UTF-16 code points, so codePointAt(0) and codePointAt(1) will return the valid integer code point values.

What could be done now:

  1. For the luaTarget: 5.3+, detect the use of \uHHHH and emit a compile-time error saying that this form of the hexadecimal character escape is not supported
  2. For the luaTarget: 5.1, detect the use of \xHH, \uHHHH, and \u{HHHH} and emit a compile-time error saying that this is not supported, or at least emit a warning (because it won't be a runtime error in Lua 5.1)
  3. Separate the StringLiteral into single-byte and multi-byte types. Show a warning of some kind when the user is trying to access the .length property for the multi-byte strings. This will probably include other features
  4. Don't use node.text in the StringLiteral visitor (getFullText()? haven't checked yet)
  5. The same rules may or may not be applied to the TemplateLiteral (the backtick strings)
  6. Make a new section in the docs ("Caveats"?) that covers the differences and shows what is supported and what isn't

Apart from that, the mapping from UTF-16 to UTF-8 is a huge PITA and some decisions have to be made in regards to Unicode support in the TSTL project.

I haven't checked LuaJIT yet, but I guess it conforms with Lua5.1.

jacob1237 avatar May 23 '23 16:05 jacob1237

Okay so seems the biggest problem now is that instead of the escape sequence, the output Lua actually contains the unicode character (like the original issue states). This is something we should definitely resolve.

Secondly, I don't think it would be a bad idea to add a warning for cases 1 and 2 where the user is trying to use a unicode pattern that is probably going to work. I don't think we should fail compilation on this with an error.

The length discrepancy is something that I honestly don't really care about, and which sounds like a huge pain to actually resolve, so I would propose to indeed just add that to the docs as a caveat.

Keeping the caveats page in the docs up to date is always good :)

Perryvw avatar May 25 '23 21:05 Perryvw