Skip to content

Add cpp2 raw string literals support with interpolation #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions regression-tests/pure2-raw-string-literal-and-interpolation.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
main: () -> int = {
i := 42;
m : std::map<std::string, int> = ();
m["one"] = 1;
m["two"] = 2;

str : std::string = "this is a string";

raw_str : std::string = R"string(raw string without interpolation)string";

raw_str_multi : std::string = R"test(this is raw string literal

that can last for multiple

lines)test";

raw_str_inter : std::string = $R"test(this is raw string literal
that can last for multiple
lines
(i)$ R"(this can be added too)"
calculations like m["one"] + m["two"] = (m["one"] + m["two"])$ also works
("at the beginning of the line")$!!!)test";

raw_str_inter_multi : std::string = $R"(

)" + $R"((i)$)" + $R"((i)$)";

std::cout << str << std::endl;
std::cout << raw_str << std::endl;
std::cout << raw_str_multi << std::endl;
std::cout << raw_str_inter << std::endl;
std::cout << raw_str_inter_multi << std::endl;
std::cout << ($R"((m["one"])$.)" + $R"((m["two"])$.)" + $R"((m["three"])$.)" + $R"((i)$)") << std::endl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
this is a string
raw string without interpolation
this is raw string literal

that can last for multiple

lines
this is raw string literal
that can last for multiple
lines
42 R"(this can be added too)"
calculations like m["one"] + m["two"] = 3 also works
at the beginning of the line!!!


4242
1.2.0.42
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
this is a string
raw string without interpolation
this is raw string literal

that can last for multiple

lines
this is raw string literal
that can last for multiple
lines
42 R"(this can be added too)"
calculations like m["one"] + m["two"] = 3 also works
at the beginning of the line!!!


4242
1.2.0.42
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

#define CPP2_USE_MODULES Yes

#include "cpp2util.h"


#line 1 "pure2-raw-string-literal-and-interpolation.cpp2"
[[nodiscard]] auto main() -> int;

//=== Cpp2 definitions ==========================================================

#line 1 "pure2-raw-string-literal-and-interpolation.cpp2"
[[nodiscard]] auto main() -> int{
auto i {42};
std::map<std::string,int> m {};
cpp2::assert_in_bounds(m, "one") = 1;
cpp2::assert_in_bounds(m, "two") = 2;

std::string str {"this is a string"};

std::string raw_str {R"string(raw string without interpolation)string"};

std::string raw_str_multi {R"test(this is raw string literal

that can last for multiple

lines)test"};

std::string raw_str_inter {R"test(this is raw string literal
that can last for multiple
lines
)test" + cpp2::to_string(i) + R"test( R"(this can be added too)"
calculations like m["one"] + m["two"] = )test" + cpp2::to_string(cpp2::assert_in_bounds(m, "one") + cpp2::assert_in_bounds(m, "two")) + R"test( also works
)test" + cpp2::to_string("at the beginning of the line") + R"test(!!!)test"};

std::string raw_str_inter_multi {R"(

)" + cpp2::to_string(i) + cpp2::to_string(i)};

std::cout << std::move(str) << std::endl;
std::cout << std::move(raw_str) << std::endl;
std::cout << std::move(raw_str_multi) << std::endl;
std::cout << std::move(raw_str_inter) << std::endl;
std::cout << std::move(raw_str_inter_multi) << std::endl;
std::cout << (cpp2::to_string(cpp2::assert_in_bounds(m, "one")) + R"(.)" + cpp2::to_string(cpp2::assert_in_bounds(m, "two")) + R"(.)" + cpp2::to_string(cpp2::assert_in_bounds(std::move(m), "three")) + R"(.)" + cpp2::to_string(std::move(i))) << std::endl;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-raw-string-literal-and-interpolation.cpp2... ok (all Cpp2, passes safety checks)

125 changes: 125 additions & 0 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,131 @@ struct comment
std::string text;
};

struct string_parts {
struct cpp_code { std::string text; };
struct raw_string { std::string text; };
enum adds_sequences { no_ends = 0, on_the_begining = 1, on_the_end = 2, on_both_ends = 3 };

string_parts(const std::string& beginseq,
const std::string& endseq,
adds_sequences strateg)
: begin_seq{beginseq}
, end_seq{endseq}
, strategy{strateg}
{
if (!(strategy & on_the_begining)) {
parts.push_back(raw_string{""});
}
}

void add_code(const std::string& text) { parts.push_back(cpp_code{text});}
void add_string(const std::string& text) { parts.push_back(raw_string{text});}
void add_string(const std::string_view& text) { parts.push_back(raw_string{std::string(text)});}

void clear() { parts.clear(); }


auto generate() const -> std::string {

if (parts.empty()) {
return (strategy & on_the_begining ? begin_seq : std::string{})
+ (strategy & on_the_end ? end_seq : std::string{});
}

auto result = std::visit(begin_visit{begin_seq, strategy},
parts.front());

if (std::ssize(parts) > 1) {
auto it1 = parts.cbegin();
auto it2 = parts.cbegin()+1;
for(;it2 != parts.cend(); ++it1, ++it2) {
result += std::visit(generator_visit{begin_seq, end_seq}, *it1, *it2);
}
}

if (!(strategy & on_the_end)) {
result += std::visit([this](const auto& lhs) {
return generator_visit{begin_seq, end_seq}(lhs, raw_string{""});
}, parts.back());
}

result += std::visit(end_visit{end_seq, strategy}, parts.back());

return result;
}

auto is_expanded() const -> bool {
for (const auto& p : parts) {
if (std::holds_alternative<cpp_code>(p)) {
return true;
}
}
return false;
}

private:
std::string begin_seq;
std::string end_seq;
adds_sequences strategy;
std::vector<std::variant<raw_string, cpp_code>> parts;

struct begin_visit {
std::string begin_seq;
adds_sequences strategy;

auto operator()(const raw_string& part) const -> std::string {
return (strategy & on_the_begining ? begin_seq : "") + part.text;
}
auto operator()(const cpp_code& part) const -> std::string {
return part.text;
}
};

struct end_visit {
std::string end_seq;
adds_sequences strategy;
auto operator()(const raw_string&) const -> std::string {
return strategy & on_the_end ? end_seq : "";
}
auto operator()(const cpp_code&) const -> std::string {
return {};
}
};

struct generator_visit {
std::string begin_seq;
std::string end_seq;

auto operator()(const raw_string&, const cpp_code& part ) const -> std::string {
return end_seq + " + " + part.text;
}
auto operator()(const cpp_code&, const raw_string& part ) const -> std::string {
return " + " + begin_seq + part.text;
}
auto operator()(const raw_string&, const raw_string& part ) const -> std::string {
return part.text;
}
auto operator()(const cpp_code&, const cpp_code& part ) const -> std::string {
return " + " + part.text;
}
};
};

struct raw_string
{
source_position start;
std::string text;
std::string opening_seq;
std::string closing_seq;
bool should_interpolate = false;
};

struct multiline_raw_string
{
std::string text;
source_position end = {0, 0};
};

//-----------------------------------------------------------------------
//
// error: represents a user-readable error message
Expand Down
Loading