Added OR operator support to regex parser

This commit is contained in:
Andrew S. Rightenburg 2023-08-23 22:28:24 -04:00
parent 8ace9f2ee3
commit 55869763c8
Signed by: rail5
GPG Key ID: A0CB570AB6629159
1 changed files with 24 additions and 2 deletions

View File

@ -18,7 +18,8 @@
#define CLEAR_ALL_MAIN_FLAGS escaped = false; \
caret = false; \
backslash_b = false; \
backslash_capital_b = false;
backslash_capital_b = false; \
or_operator = false;
std::vector<std::string> parse_regex(std::string expression) {
/***
@ -48,6 +49,9 @@ std::vector<std::string> parse_regex(std::string expression) {
// A flag to set when we hit backslash-B (\B), signifying "NOT start of word"
bool backslash_capital_b = false;
// A flag to set when we hit an OR operator (|)
bool or_operator = false;
// A flag to set when we're entering multiple characters into one element of the vector
bool multi_char_entry = false;
@ -68,7 +72,7 @@ std::vector<std::string> parse_regex(std::string expression) {
std::string part(1, c);
int current_index = parsed_expression.size()-1;
multi_char_entry = (square_brackets_open || parentheses_level > 0 || escaped || caret || backslash_b || backslash_capital_b);
multi_char_entry = (square_brackets_open || parentheses_level > 0 || escaped || caret || or_operator || backslash_b || backslash_capital_b);
if (parentheses_level < 0) {
parentheses_level = 0;
@ -88,6 +92,7 @@ std::vector<std::string> parse_regex(std::string expression) {
caret = false;
backslash_b = false;
backslash_capital_b = false;
or_operator = false;
break;
case '[':
if (multi_char_entry) {
@ -213,6 +218,7 @@ std::vector<std::string> parse_regex(std::string expression) {
escaped = false;
backslash_b = false;
backslash_capital_b = false;
or_operator = false;
break;
case '$':
@ -221,6 +227,20 @@ std::vector<std::string> parse_regex(std::string expression) {
CLEAR_ALL_MAIN_FLAGS
break;
case '|':
parsed_expression[current_index] += part;
if (!escaped) {
or_operator = true;
}
escaped = false;
caret = false;
backslash_b = false;
backslash_capital_b = false;
break;
case 'b':
if (escaped) {
backslash_b = true;
@ -237,6 +257,7 @@ std::vector<std::string> parse_regex(std::string expression) {
escaped = false;
caret = false;
backslash_capital_b = false;
or_operator = false;
break;
case 'B':
@ -254,6 +275,7 @@ std::vector<std::string> parse_regex(std::string expression) {
escaped = false;
caret = false;
or_operator = false;
break;
default: