From 2e54af945792904d664a17f99359591e48f3fe18 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 21 Jan 2009 10:57:26 +0100 Subject: [PATCH] handling of string escape sequences in the lexer: not being fooled by double-backslash-then-digits, and produce a clearer message when an ASCII code beyond 255 is requested --- src/compiler/lexer.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/compiler/lexer.lua b/src/compiler/lexer.lua index f92ed14..698e5dd 100644 --- a/src/compiler/lexer.lua +++ b/src/compiler/lexer.lua @@ -67,8 +67,13 @@ local function unescape_string (s) -- Turn the digits of an escape sequence into the corresponding -- character, e.g. [unesc_digits("123") == string.char(123)]. local function unesc_digits (x) + if x:sub(1,1)=="\\" then return x end -- Hack to parse correctly "\\123" local k, j, i = x:reverse():byte(1, 3) local z = _G.string.byte "0" + local code = (k or z) + 10*(j or z) + 100*(i or z) - 111*z + if code > 255 then + error ("Illegal escape sequence '\\"..x.."' in string: ASCII codes must be in [0..255]") + end return _G.string.char ((k or z) + 10*(j or z) + 100*(i or z) - 111*z) end @@ -84,7 +89,7 @@ local function unescape_string (s) return s :gsub ("\\(%D)",unesc_letter) - :gsub ("\\([0-9][0-9]?[0-9]?)", unesc_digits) + :gsub ("\\(\\?[0-9][0-9]?[0-9]?)", unesc_digits) end lexer.extractors = { -- 2.44.0