Implement C++ pretty-printer

This commit is contained in:
2021-09-08 01:32:08 -04:00
parent 67b309a0b7
commit bac1a7b876
9 changed files with 322 additions and 454 deletions

View File

@@ -35,6 +35,38 @@ bool is_identifier(const std::string &str) {
return true;
}
void quote_string(const std::string &s, std::ostream *os) {
bool usesinglequote = false;
for (char c : s) {
if (c == '"') {
usesinglequote = true;
break;
}
}
(*os) << (usesinglequote ? '\'' : '"');
for (char c : s) {
if (c >= 32) {
if (c == '"') {
(*os) << (usesinglequote ? "\"" : "\\\"");
} else if (c == '\'') {
(*os) << (usesinglequote ? "\\'" : "'");
} else {
(*os) << c;
}
} else {
switch (c) {
case '\n': (*os) << "\\n"; break;
case '\t': (*os) << "\\t"; break;
case '\r': (*os) << "\\r"; break;
default:
(*os) << "\\" << std::setw(3) << int(c);
break;
}
}
}
(*os) << (usesinglequote ? '\'' : '"');
}
IdVector id_vector_create(int64_t id1, int64_t id2, int64_t id3, int64_t id4) {
IdVector result;
if (id1 >= 0) result.push_back(id1);