Commit 328b0d8a authored by miloyip's avatar miloyip

Minor refactor regex

parent 994b0dfe
...@@ -152,8 +152,7 @@ private: ...@@ -152,8 +152,7 @@ private:
} }
void Patch(SizeType l, SizeType s) { void Patch(SizeType l, SizeType s) {
SizeType next; for (SizeType next; l != kRegexInvalidState; l = next) {
for (; l != kRegexInvalidState; l = next) {
next = GetState(l).out; next = GetState(l).out;
GetState(l).out = s; GetState(l).out = s;
} }
...@@ -173,7 +172,7 @@ private: ...@@ -173,7 +172,7 @@ private:
switch (codepoint) { switch (codepoint) {
case '|': case '|':
while (!operatorStack.Empty() && *operatorStack.template Top<Operator>() < kAlternation) while (!operatorStack.Empty() && *operatorStack.template Top<Operator>() < kAlternation)
if (!Eval(operandStack, operatorStack)) if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
return; return;
*operatorStack.template Push<Operator>() = kAlternation; *operatorStack.template Push<Operator>() = kAlternation;
*atomCountStack.template Top<unsigned>() = 0; *atomCountStack.template Top<unsigned>() = 0;
...@@ -186,7 +185,7 @@ private: ...@@ -186,7 +185,7 @@ private:
case ')': case ')':
while (!operatorStack.Empty() && *operatorStack.template Top<Operator>() != kLeftParenthesis) while (!operatorStack.Empty() && *operatorStack.template Top<Operator>() != kLeftParenthesis)
if (!Eval(operandStack, operatorStack)) if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
return; return;
if (operatorStack.Empty()) if (operatorStack.Empty())
return; return;
...@@ -196,20 +195,17 @@ private: ...@@ -196,20 +195,17 @@ private:
break; break;
case '?': case '?':
*operatorStack.template Push<Operator>() = kZeroOrOne; if (!Eval(operandStack, kZeroOrOne))
if (!Eval(operandStack, operatorStack))
return; return;
break; break;
case '*': case '*':
*operatorStack.template Push<Operator>() = kZeroOrMore; if (!Eval(operandStack, kZeroOrMore))
if (!Eval(operandStack, operatorStack))
return; return;
break; break;
case '+': case '+':
*operatorStack.template Push<Operator>() = kOneOrMore; if (!Eval(operandStack, kOneOrMore))
if (!Eval(operandStack, operatorStack))
return; return;
break; break;
...@@ -221,7 +217,7 @@ private: ...@@ -221,7 +217,7 @@ private:
} }
while (!operatorStack.Empty()) while (!operatorStack.Empty())
if (!Eval(operandStack, operatorStack)) if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
return; return;
// Link the operand to matching state. // Link the operand to matching state.
...@@ -229,6 +225,7 @@ private: ...@@ -229,6 +225,7 @@ private:
Frag* e = operandStack.template Pop<Frag>(1); Frag* e = operandStack.template Pop<Frag>(1);
Patch(e->out, NewState(kRegexInvalidState, kRegexInvalidState, 0)); Patch(e->out, NewState(kRegexInvalidState, kRegexInvalidState, 0));
root_ = e->start; root_ = e->start;
#if RAPIDJSON_REGEX_VERBOSE #if RAPIDJSON_REGEX_VERBOSE
printf("root: %d\n", root_); printf("root: %d\n", root_);
for (SizeType i = 0; i < stateCount_ ; i++) { for (SizeType i = 0; i < stateCount_ ; i++) {
...@@ -240,9 +237,8 @@ private: ...@@ -240,9 +237,8 @@ private:
} }
} }
bool Eval(Stack<Allocator>& operandStack, Stack<Allocator>& operatorStack) { bool Eval(Stack<Allocator>& operandStack, Operator op) {
// printf("Eval %c\n", "?*+.|("[*operatorStack.template Top<Operator>()]); switch (op) {
switch (*operatorStack.template Pop<Operator>(1)) {
case kConcatenation: case kConcatenation:
if (operandStack.GetSize() >= sizeof(Frag) * 2) { if (operandStack.GetSize() >= sizeof(Frag) * 2) {
Frag e2 = *operandStack.template Pop<Frag>(1); Frag e2 = *operandStack.template Pop<Frag>(1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment