Regex: minor cleanup of the regex parsing code

This commit is contained in:
Maxime Coste 2017-10-23 11:22:07 +08:00
parent 6e0275e550
commit aea2de885d

View File

@ -151,7 +151,7 @@ private:
return nullptr; return nullptr;
} }
bool peek(StringView expected) const bool accept(StringView expected)
{ {
auto it = m_pos; auto it = m_pos;
for (Iterator expected_it{expected.begin(), expected}; expected_it != expected.end(); ++expected_it) for (Iterator expected_it{expected.begin(), expected}; expected_it != expected.end(); ++expected_it)
@ -159,21 +159,20 @@ private:
if (it == m_regex.end() or *it++ != *expected_it) if (it == m_regex.end() or *it++ != *expected_it)
return false; return false;
} }
m_pos = it;
return true; return true;
} }
bool modifiers() bool modifiers()
{ {
if (peek("(?i)")) if (accept("(?i)"))
{ {
m_ignore_case = true; m_ignore_case = true;
m_pos += 4;
return true; return true;
} }
if (peek("(?I)")) if (accept("(?I)"))
{ {
m_ignore_case = false; m_ignore_case = false;
m_pos += 4;
return true; return true;
} }
return false; return false;
@ -211,10 +210,9 @@ private:
}; };
for (auto& lookaround : lookarounds) for (auto& lookaround : lookarounds)
{ {
if (peek(lookaround.prefix)) if (accept(lookaround.prefix))
{ {
lookaround_op = lookaround.op; lookaround_op = lookaround.op;
m_pos += (int)lookaround.prefix.char_length();
break; break;
} }
} }
@ -244,13 +242,7 @@ private:
case '(': case '(':
{ {
++m_pos; ++m_pos;
bool capture = true; const bool capture = not accept("?:");
if (peek("?:"))
{
capture = false;
m_pos += 2;
}
AstNodePtr content = disjunction(capture ? m_parsed_regex.capture_count++ : -1); AstNodePtr content = disjunction(capture ? m_parsed_regex.capture_count++ : -1);
if (at_end() or *m_pos++ != ')') if (at_end() or *m_pos++ != ')')
parse_error("unclosed parenthesis"); parse_error("unclosed parenthesis");