|
1 | 1 | #include "FormatVisitor.h" |
2 | 2 |
|
3 | 3 | #include <iostream> |
| 4 | +#include <regex> |
4 | 5 |
|
5 | 6 | #include "LuaLexer.h" |
6 | 7 |
|
@@ -993,13 +994,61 @@ antlrcpp::Any FormatVisitor::visitExp(LuaParser::ExpContext* ctx) { |
993 | 994 | visitFunctiondef(ctx->functiondef()); |
994 | 995 | } else if (ctx->tableconstructor() != NULL) { |
995 | 996 | visitTableconstructor(ctx->tableconstructor()); |
| 997 | + } else if (ctx->string() != NULL) { |
| 998 | + visitString(ctx->string()); |
996 | 999 | } else { |
997 | 1000 | cur_writer() << ctx->getText(); |
998 | 1001 | } |
999 | 1002 | LOG_FUNCTION_END("visitExp"); |
1000 | 1003 | return nullptr; |
1001 | 1004 | } |
1002 | 1005 |
|
| 1006 | +antlrcpp::Any FormatVisitor::visitString(LuaParser::StringContext *ctx) { |
| 1007 | + if (ctx->NORMALSTRING() || ctx->CHARSTRING()) { |
| 1008 | + char quote = ctx->NORMALSTRING() ? '\'' : '\"'; |
| 1009 | + tree::TerminalNode *tn; |
| 1010 | + |
| 1011 | + switch (quote) { |
| 1012 | + case '\"': |
| 1013 | + if (!config_.get<bool>("single_quote_to_double_quote")) goto out; |
| 1014 | + tn = ctx->CHARSTRING(); |
| 1015 | + break; |
| 1016 | + case '\'': |
| 1017 | + if (!config_.get<bool>("double_quote_to_single_quote")) goto out; |
| 1018 | + tn = ctx->NORMALSTRING(); |
| 1019 | + break; |
| 1020 | + default: |
| 1021 | + goto out; |
| 1022 | + } |
| 1023 | + |
| 1024 | + string newstr = tn->getSymbol()->getText(); |
| 1025 | + |
| 1026 | + regex re_single("'", regex_constants::extended); |
| 1027 | + regex re_double("\"", regex_constants::extended); |
| 1028 | + regex re_escapedsingle("\\\\'", regex_constants::extended); |
| 1029 | + regex re_escapeddouble("\\\\\"", regex_constants::extended); |
| 1030 | + |
| 1031 | + if (quote == '\"') { |
| 1032 | + newstr = regex_replace(newstr, re_escapedsingle, "'"); |
| 1033 | + newstr = regex_replace(newstr, re_double, "\\\""); |
| 1034 | + } else { |
| 1035 | + newstr = regex_replace(newstr, re_single, "\\'"); |
| 1036 | + newstr = regex_replace(newstr, re_escapeddouble, "\""); |
| 1037 | + } |
| 1038 | + |
| 1039 | + // switch the beginning and end to the new format |
| 1040 | + *newstr.begin() = quote; |
| 1041 | + *newstr.rbegin() = quote; |
| 1042 | + |
| 1043 | + cur_writer() << newstr; |
| 1044 | + return nullptr; |
| 1045 | + } |
| 1046 | + |
| 1047 | +out: |
| 1048 | + cur_writer() << ctx->getText(); |
| 1049 | + return nullptr; |
| 1050 | +} |
| 1051 | + |
1003 | 1052 | // varOrExp nameAndArgs*; |
1004 | 1053 | antlrcpp::Any FormatVisitor::visitPrefixexp(LuaParser::PrefixexpContext* ctx) { |
1005 | 1054 | LOG_FUNCTION_BEGIN("visitPrefixexp"); |
|
0 commit comments