From b2ddc1d7687591b4cbc686d274cf3b1f67c3ce1e Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 3 Jan 2018 17:23:46 -0800 Subject: [PATCH] Added support for \000 and \x00 style byte literals. --- nomsu.lua | 13 ++++++++----- nomsu.moon | 7 +++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/nomsu.lua b/nomsu.lua index a0be840..d902f61 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -45,9 +45,14 @@ local STRING_ESCAPES = { f = "\f", r = "\r" } -local ESCAPE_CHAR = (P("\\") * S("ntbavfr")) / function(s) - return STRING_ESCAPES[s:sub(2, 2)] +local DIGIT, HEX = R('09'), R('09', 'af', 'AF') +local ESCAPE_CHAR = (P("\\") * S("xX") * C(HEX * HEX)) / function(self) + return string.char(tonumber(self, 16)) end +ESCAPE_CHAR = ESCAPE_CHAR + ((P("\\") * C(DIGIT * (DIGIT ^ -2))) / function(self) + return string.char(tonumber(self)) +end) +ESCAPE_CHAR = ESCAPE_CHAR + ((P("\\") * C(S("ntbavfr"))) / STRING_ESCAPES) local OPERATOR_CHAR = S("'~`!@$^&*-+=|<>?/") local UTF8_CHAR = (R("\194\223") * R("\128\191") + R("\224\239") * R("\128\191") * R("\128\191") + R("\240\244") * R("\128\191") * R("\128\191") * R("\128\191")) local IDENT_CHAR = R("az", "AZ", "09") + P("_") + UTF8_CHAR @@ -1395,9 +1400,7 @@ end)]]):format(concat(lua_bits, "\n")) local self = _class_0 self.def_number = 0 self.unescape_string = function(self, str) - return str:gsub("\\(.)", (function(c) - return STRING_ESCAPES[c] or c - end)) + return Cs(((P("\\\\") / "\\") + (P("\\\"") / '"') + ESCAPE_CHAR + P(1)) ^ 0):match(str) end self.comma_separated_items = function(self, open, items, close) local bits = { diff --git a/nomsu.moon b/nomsu.moon index 89b3971..fad764e 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -41,7 +41,10 @@ lpeg.setmaxstack 10000 -- whoa {:P,:R,:V,:S,:Cg,:C,:Cp,:B,:Cmt} = lpeg STRING_ESCAPES = n:"\n", t:"\t", b:"\b", a:"\a", v:"\v", f:"\f", r:"\r" -ESCAPE_CHAR = (P("\\")*S("ntbavfr")) / (s)->STRING_ESCAPES[s\sub(2,2)] +DIGIT, HEX = R('09'), R('09','af','AF') +ESCAPE_CHAR = (P("\\")*S("xX")*C(HEX*HEX)) / => string.char(tonumber(@, 16)) +ESCAPE_CHAR += (P("\\")*C(DIGIT*(DIGIT^-2))) / => string.char(tonumber @) +ESCAPE_CHAR += (P("\\")*C(S("ntbavfr"))) / STRING_ESCAPES OPERATOR_CHAR = S("'~`!@$^&*-+=|<>?/") UTF8_CHAR = ( R("\194\223")*R("\128\191") + @@ -698,7 +701,7 @@ end)]])\format(concat(lua_bits, "\n")) return concat(bits, "\n") @unescape_string: (str)=> - str\gsub("\\(.)", ((c)-> STRING_ESCAPES[c] or c)) + Cs(((P("\\\\")/"\\") + (P("\\\"")/'"') + ESCAPE_CHAR + P(1))^0)\match(str) @comma_separated_items: (open, items, close)=> bits = {open}