Skip to content

Commit

Permalink
#973: Support new lines in script options values
Browse files Browse the repository at this point in the history
  • Loading branch information
tomuben committed Oct 9, 2024
1 parent b37f762 commit fd2fa86
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,36 @@ auto&& add_option(Option&& e, options_type&& ob)
return std::move(ob);
}

const auto convert_escape_seq(std::string_view escape_seq) {
std::string retVal;
if (escape_seq == R"_(\;)_") {
retVal = ";";
} else if (escape_seq == R"_(\n)_") {
retVal = "\n";
} else if (escape_seq == R"_(\r)_") {
retVal = "\r";
} else {
throw std::runtime_error(std::string("Internal parser error: Unexpected escape sequence " + std::string(escape_seq)));
}

return retVal;
}



constexpr char alpha_numeric_pattern[] = R"_([0-9a-zA-Z_]+)_";
constexpr char not_semicolon_pattern[] = R"_([^;])_";
constexpr char whitespaces_pattern[] = R"_([ \x09\x0c\x0b]+)_";
constexpr char escape_pattern[] = R"_(\\;|\\n|\\r)_";



constexpr char_term start_option_token('%');
constexpr char_term end_option_token(';');
constexpr regex_term<alpha_numeric_pattern> alpha_numeric("alpha_numeric");
constexpr regex_term<not_semicolon_pattern> not_semicolon("not_semicolon");
constexpr regex_term<whitespaces_pattern> whitespaces("whitespace");
constexpr string_term semicolon_escape(R"_(\;)_");
constexpr regex_term<escape_pattern> escape_seq("escape_seq");

constexpr nterm<options_type> text("text");
constexpr nterm<options_type> options("options");
Expand All @@ -75,7 +92,7 @@ constexpr nterm<std::string> option_value("option_value");

constexpr parser option_parser(
text,
terms(start_option_token, semicolon_escape, whitespaces, end_option_token, alpha_numeric, not_semicolon),
terms(start_option_token, escape_seq, whitespaces, end_option_token, alpha_numeric, not_semicolon),
nterms(text, option_value, options, option_element, rest),
rules(
text(rest)
Expand All @@ -98,12 +115,12 @@ constexpr parser option_parser(
>= [](auto o) { return std::string(o.get_value()); },
option_value(whitespaces)
>= [](auto o) { return std::string(o.get_value()); },
option_value(semicolon_escape)
>= [](auto o) { return std::string(";"); },
option_value(escape_seq)
>= [](auto es) { return convert_escape_seq(es.get_value()); },
option_value(option_value, not_semicolon)
>= [](auto&& ov, auto v) { return std::move(ov.append(v.get_value())); },
option_value(option_value, semicolon_escape)
>= [](auto&& ov, auto v) { return std::move(ov.append(";")); },
option_value(option_value, escape_seq)
>= [](auto&& ov, auto es) { return std::move(ov.append(convert_escape_seq(es.get_value()))); },
option_value(option_value, start_option_token)
>= [](auto&& ov, auto v) { return std::move(ov.append("%")); },
option_value(option_value, alpha_numeric)
Expand All @@ -114,7 +131,7 @@ constexpr parser option_parser(
>= [](auto r) { return 0;},
rest(whitespaces)
>= [](auto r) { return 0;},
rest(semicolon_escape)
rest(escape_seq)
>= [](auto r) { return 0;},
rest(end_option_token)
>= [](auto r) { return 0;},
Expand All @@ -129,6 +146,8 @@ constexpr parser option_parser(
rest(rest, end_option_token)
>= [](auto r, skip) { return 0;},
rest(rest, start_option_token)
>= [](auto r, skip) { return 0;},
rest(rest, escape_seq)
>= [](auto r, skip) { return 0;}
)
);
Expand Down
Loading

0 comments on commit fd2fa86

Please sign in to comment.