Skip to content

Commit

Permalink
Added white space escape sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
tomuben committed Oct 10, 2024
1 parent f03dae4 commit 0268a87
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,30 @@ const auto convert_escape_seq(std::string_view escape_seq) {
}


const auto convert_whitespace_escape_seq(std::string_view escape_seq) {
std::string retVal;
if (escape_seq == R"_(\ )_") {
retVal = " ";
} else if (escape_seq == R"_(\t)_") {
retVal = "\t";
} else if (escape_seq == R"_(\f)_") {
retVal = "\f";
} else if (escape_seq == R"_(\v)_") {
retVal = "\v";
} else {
throw std::runtime_error(std::string("Internal parser error: Unexpected white space 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 whitespace_escape_pattern[] = R"_(\\ |\\t|\\f|\\v)_";



Expand All @@ -82,6 +101,7 @@ 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 regex_term<escape_pattern> escape_seq("escape_seq");
constexpr regex_term<whitespace_escape_pattern> whitespace_escape_seq("escape_seq");

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

constexpr parser option_parser(
text,
terms(start_option_token, escape_seq, whitespaces, end_option_token, alpha_numeric, not_semicolon),
terms(start_option_token, escape_seq, whitespace_escape_seq, whitespaces, end_option_token, alpha_numeric, not_semicolon),
nterms(text, option_value, options, option_element, rest),
rules(
text(rest)
Expand All @@ -113,12 +133,14 @@ constexpr parser option_parser(
>= [](auto o) { return std::string(o.get_value()); },
option_value(not_semicolon)
>= [](auto o) { return std::string(o.get_value()); },
option_value(whitespaces)
>= [](auto o) { return std::string(o.get_value()); },
option_value(whitespace_escape_seq)
>= [](auto o) { return std::string(convert_whitespace_escape_seq(o.get_value())); },
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, whitespace_escape_seq)
>= [](auto&& ov, auto es) { return std::move(ov.append(es.get_value())); },
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)
Expand All @@ -133,6 +155,8 @@ constexpr parser option_parser(
>= [](auto r) { return 0;},
rest(escape_seq)
>= [](auto r) { return 0;},
rest(whitespace_escape_seq)
>= [](auto r) { return 0;},
rest(end_option_token)
>= [](auto r) { return 0;},
rest(not_semicolon)
Expand All @@ -148,6 +172,8 @@ constexpr parser option_parser(
rest(rest, start_option_token)
>= [](auto r, skip) { return 0;},
rest(rest, escape_seq)
>= [](auto r, skip) { return 0;},
rest(rest, whitespace_escape_seq)
>= [](auto r, skip) { return 0;}
)
);
Expand Down
Loading

0 comments on commit 0268a87

Please sign in to comment.