Skip to content

Commit

Permalink
Merge pull request #275 from doug1234/EscapedQuotes
Browse files Browse the repository at this point in the history
Fix incorrect escape character handling for issue #274
  • Loading branch information
Koihik authored Feb 21, 2023
2 parents 274d786 + d7264fb commit 29afe10
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/visitor/FormatVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,38 @@ antlrcpp::Any FormatVisitor::visitString(LuaParser::StringContext* ctx) {
assert(0);
}

std::string oldstr = ctx->getText();
std::string newstr;

std::regex re_single("'", std::regex_constants::extended);
std::regex re_double("\"", std::regex_constants::extended);
std::regex re_escapedsingle("\\\\'", std::regex_constants::extended);
std::regex re_escapeddouble("\\\\\"", std::regex_constants::extended);

if (quote == '\"') {
regex_replace(std::back_inserter(newstr), oldstr.begin() + 1, oldstr.end() - 1, re_escapedsingle, "'");
newstr = regex_replace(newstr, re_double, "\\\"");
} else {
regex_replace(std::back_inserter(newstr), oldstr.begin() + 1, oldstr.end() - 1, re_single, "\\'");
newstr = regex_replace(newstr, re_escapeddouble, "\"");
//Switch the type of quote
std::string newstr = ctx->getText();
newstr.front() = quote;
newstr.back() = quote;
bool escaped = false; //Is the current character in the sequence escaped
const char oldChar = quote == '\'' ? '"' : '\'';
auto itr = ++newstr.begin();
auto itrStop = --newstr.end();
while (itr != itrStop) {
if (*itr == '\\') {
escaped = !escaped; //If the character isn't currently escaped, the next one will be escaped
} else {
if (*itr == quote) {
// Add the escape character before the non escaped old character
if (!escaped) {
itr = newstr.insert(itr, '\\');
++itr; // Extra incriment to get back to the previous character
itrStop = --newstr.end();
}
} else if (*itr == oldChar) {
// Remove the escape before the previously escaped old character
if (escaped) {
itr = newstr.erase(--itr);
itrStop = --newstr.end();
}
}
escaped = false;
}
++itr;
}

cur_writer() << quote + newstr + quote;
cur_writer() << newstr;
return nullptr;
}

Expand Down
5 changes: 5 additions & 0 deletions test/lua/literals/_doublequote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ local bar = '"'
local foobar = '\\\\"'
local kek = '\\'
local topkek = 'a\\'
local nothing = ''
local dir = 'C:\\\'Some Dir\'\\'
local escapedQuote = '\''
local manyEscape1 = '\\\\\\\\\''
local manyEscape2 = '\\\\\\\\\''
5 changes: 5 additions & 0 deletions test/lua/literals/_singlequote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ local foo = "'"
local foobar = "\\\\'"
local kek = "\\"
local topkek = "a\\"
local nothing = ""
local dir = "C:\\\"Some Dir\"\\"
local escapedQuote = "\""
local manyEscape1 = "\\\\\\\\\""
local manyEscape2 = "\\\\\\\\\""
5 changes: 5 additions & 0 deletions test/lua/literals/doublequote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ local bar = "\""
local foobar = "\\\\\""
local kek = "\\"
local topkek = "a\\"
local nothing = ""
local dir = "C:\\'Some Dir'\\"
local escapedQuote = "\'"
local manyEscape1 = "\\\\\\\\'"
local manyEscape2 = "\\\\\\\\\'"
5 changes: 5 additions & 0 deletions test/lua/literals/singlequote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ local foo = '\''
local foobar = '\\\\\''
local kek = '\\'
local topkek = 'a\\'
local nothing = ''
local dir = 'C:\\"Some Dir"\\'
local escapedQuote = '\"'
local manyEscape1 = '\\\\\\\\"'
local manyEscape2 = '\\\\\\\\\"'

0 comments on commit 29afe10

Please sign in to comment.