MCPcopy Create free account
hub / github.com/Koihik/LuaFormatter / visitString

Method visitString

src/FormatVisitor.cpp:1094–1147  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1092}
1093
1094antlrcpp::Any FormatVisitor::visitString(LuaParser::StringContext* ctx) {
1095 if ((ctx->NORMALSTRING() == nullptr) && (ctx->CHARSTRING() == nullptr)) {
1096 cur_writer() << ctx->getText();
1097 return nullptr;
1098 }
1099 char quote = ctx->NORMALSTRING() != nullptr ? '\'' : '\"';
1100 tree::TerminalNode* tn = nullptr;
1101
1102 switch (quote) {
1103 case '\"':
1104 if (config_.get<bool>("single_quote_to_double_quote")) {
1105 tn = ctx->CHARSTRING();
1106 break;
1107 }
1108 cur_writer() << ctx->getText();
1109 return nullptr;
1110 case '\'':
1111 if (config_.get<bool>("double_quote_to_single_quote")) {
1112 tn = ctx->NORMALSTRING();
1113 break;
1114 }
1115 cur_writer() << ctx->getText();
1116 return nullptr;
1117 default:
1118 assert(0);
1119 }
1120
1121 std::string newstr = tn->getSymbol()->getText();
1122
1123 std::regex re_single("'", std::regex_constants::extended);
1124 std::regex re_double("\"", std::regex_constants::extended);
1125 std::regex re_escapedsingle("\\\\'", std::regex_constants::extended);
1126 std::regex re_escapeddouble("\\\\\"", std::regex_constants::extended);
1127
1128 if (quote == '\"') {
1129 newstr = regex_replace(newstr, re_escapedsingle, "'");
1130 newstr = regex_replace(newstr, re_double, "\\\"");
1131 } else {
1132 newstr = regex_replace(newstr, re_single, "\\'");
1133 newstr = regex_replace(newstr, re_escapeddouble, "\"");
1134 }
1135
1136 // switch the beginning and end to the new format
1137 *newstr.begin() = quote;
1138 *newstr.rbegin() = quote;
1139
1140 // undo a transformation that invalidates strings in certain conditions
1141 if (newstr.at(newstr.size() - 2) == '\\' && newstr.at(newstr.size() - 3) != '\\') {
1142 newstr.insert(newstr.size() - 2, "\\");
1143 }
1144
1145 cur_writer() << newstr;
1146 return nullptr;
1147}
1148
1149// varOrExp nameAndArgs*;
1150antlrcpp::Any FormatVisitor::visitPrefixexp(LuaParser::PrefixexpContext* ctx) {

Callers

nothing calls this directly

Calls 2

NORMALSTRINGMethod · 0.80
CHARSTRINGMethod · 0.80

Tested by

no test coverage detected