forked from hsutter/cppfront
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edf0b74
commit 7fa74b1
Showing
101 changed files
with
101,618 additions
and
9,954 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
create_result: (resultExpr: std::string, r) -> std::string = { | ||
result: std::string = ""; | ||
|
||
get_next := :(iter) -> _ = { | ||
start := std::distance(resultExpr&$*.cbegin(), iter); | ||
firstDollar := resultExpr&$*.find("$", start); | ||
firstAt := resultExpr&$*.find("@", start); | ||
|
||
end := std::min(firstDollar, firstAt); | ||
if end != std::string::npos { | ||
return resultExpr&$*.cbegin() + end; | ||
} | ||
else { | ||
return resultExpr&$*.cend(); | ||
} | ||
}; | ||
extract_group_and_advance := :(inout iter) -> _ = { | ||
start := iter; | ||
|
||
while std::isdigit(iter*) next iter++ {} | ||
|
||
return std::stoi(std::string(start, iter)); | ||
}; | ||
extract_until := :(inout iter, to: char) -> _ = { | ||
start := iter; | ||
|
||
while (to != iter*) next iter++ {} // TODO: Without bracket: error: postfix unary * (dereference) cannot be immediately followed by a (, identifier, or literal - add whitespace before * here if you meant binary * (multiplication) | ||
|
||
return std::string(start, iter); | ||
}; | ||
|
||
iter := resultExpr.begin(); | ||
|
||
while iter != resultExpr.end() { | ||
next := get_next(iter); | ||
|
||
if next != iter { | ||
result += std::string(iter, next); | ||
} | ||
if next != resultExpr.end() { | ||
if next* == '$' { | ||
next++; | ||
|
||
if next* == '&' { | ||
next++; | ||
result += r.group(0); | ||
} | ||
else if next* == '-' || next* == '+' { | ||
is_start := next* == '-'; | ||
next++; | ||
if next* == '{' { | ||
next++; // Skip { | ||
group := extract_until(next, '}'); | ||
next++; // Skip } | ||
result += r.group(group); | ||
} | ||
else if next* == '[' { | ||
next++; // Skip [ | ||
group := extract_group_and_advance(next); | ||
next++; // Skip ] | ||
|
||
if is_start { | ||
result += std::to_string(r.group_start(group)); | ||
} | ||
else { | ||
result += std::to_string(r.group_end(group)); | ||
} | ||
} | ||
else { | ||
// Return max group | ||
result += r.group(r.group_number() - 1); | ||
} | ||
} | ||
else if std::isdigit(next*) { | ||
group := extract_group_and_advance(next); | ||
result += r.group(group); | ||
} | ||
else { | ||
std::cerr << "Not implemented"; | ||
} | ||
} | ||
else if next* == '@' { | ||
next++; | ||
|
||
if next* == '-' || next* == '+' { | ||
i := 0; | ||
while i < cpp2::unsafe_narrow<int>(r.group_number()) next i++ { | ||
pos := 0; | ||
if next* == '-' { | ||
pos = r.group_start(i); | ||
} | ||
else { | ||
pos = r.group_end(i); | ||
} | ||
result += std::to_string(pos); | ||
} | ||
} | ||
else { | ||
std::cerr << "Not implemented"; | ||
} | ||
} | ||
else { | ||
std::cerr << "Not implemented."; | ||
} | ||
} | ||
iter = next; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
test: <M> (regex: M, id: std::string, regex_str: std::string, str: std::string, kind: std::string, resultExpr: std::string, | ||
resultExpected: std::string) = { | ||
|
||
warning: std::string = ""; | ||
if regex.to_string() != regex_str { | ||
warning = "Warning: Parsed regex does not match."; | ||
} | ||
|
||
status: std::string = "OK"; | ||
|
||
r := regex.search(str); | ||
|
||
if "y" == kind || "yM" == kind || "yS" == kind || "yB" == kind { | ||
if !r.matched { | ||
status = "Failure: Regex should apply."; | ||
} | ||
else { | ||
// Have a match check the result | ||
|
||
result := create_result(resultExpr, r); | ||
|
||
if result != resultExpected { | ||
status = "Failure: Result is wrong. (is: (result)$)"; | ||
} | ||
} | ||
} | ||
else if "n" == kind { | ||
if r.matched { | ||
status = "Failure: Regex should not apply. Result is '(r.group(0))$'"; | ||
} | ||
} else { | ||
status = "Unknown kind '(kind)$'"; | ||
} | ||
|
||
if !warning.empty() { | ||
warning += " "; | ||
} | ||
std::cout << "(id)$_(kind)$: (status)$ (warning)$regex: (regex_str)$ parsed_regex: (regex.to_string())$ str: (str)$ result_expr: (resultExpr)$ expected_results (resultExpected)$" << std::endl; | ||
} | ||
|
||
|
||
test_tests_01_char_matcher: @regex type = { | ||
regex_01 := R"(abc)"; | ||
regex_02 := R"(abc)"; | ||
regex_03 := R"(abc)"; | ||
regex_04 := R"(abc)"; | ||
regex_05 := R"(abc)"; | ||
regex_06 := R"(abc)"; | ||
regex_07 := R"(abc)"; | ||
regex_08 := R"(abc)"; | ||
regex_09 := R"(abc)"; | ||
regex_10 := R"(abc)"; | ||
regex_11 := R"(abc)"; | ||
regex_12 := R"(abc)"; | ||
run: (this) = { | ||
std::cout << "Running tests_01_char_matcher:"<< std::endl; | ||
test(regex_01, "01", R"(abc)", "abc", "y", R"($&)", "abc"); | ||
test(regex_02, "02", R"(abc)", "abc", "y", R"($-[0])", "0"); | ||
test(regex_03, "03", R"(abc)", "abc", "y", R"($+[0])", "3"); | ||
test(regex_04, "04", R"(abc)", "xbc", "n", R"(-)", "-"); | ||
test(regex_05, "05", R"(abc)", "axc", "n", R"(-)", "-"); | ||
test(regex_06, "06", R"(abc)", "abx", "n", R"(-)", "-"); | ||
test(regex_07, "07", R"(abc)", "xabcy", "y", R"($&)", "abc"); | ||
test(regex_08, "08", R"(abc)", "xabcy", "y", R"($-[0])", "1"); | ||
test(regex_09, "09", R"(abc)", "xabcy", "y", R"($+[0])", "4"); | ||
test(regex_10, "10", R"(abc)", "ababc", "y", R"($&)", "abc"); | ||
test(regex_11, "11", R"(abc)", "ababc", "y", R"($-[0])", "2"); | ||
test(regex_12, "12", R"(abc)", "ababc", "y", R"($+[0])", "5"); | ||
std::cout << std::endl; | ||
} | ||
} | ||
main: () = { | ||
test_tests_01_char_matcher().run(); | ||
} |
Oops, something went wrong.