@@ -886,6 +886,7 @@ std::optional<Regexp> NLexer::_regexp() {
886886 advance (-1 );
887887 // reset capture indices
888888 nested_index = 0 ;
889+ inside_index = 0 ;
889890 while (branch_reset_indices.size ())
890891 branch_reset_indices.pop ();
891892
@@ -1228,6 +1229,7 @@ std::optional<Regexp> NLexer::regexp_expression() {
12281229 RegexpType::SubExprCall, (char )backrefnum,
12291230 regexp_debug_info (this , " \\ g" , 2 )};
12301231 reg.subexprcall = backrefnum;
1232+ reg.inside_subexpr = inside_index;
12311233 return reg;
12321234 }
12331235 lexer_error (*this , Errors::InvalidRegexpSyntax, error_token (),
@@ -1463,6 +1465,7 @@ std::optional<Regexp> NLexer::regexp_expression() {
14631465 // parenthesised expression
14641466 if (c == ' (' ) {
14651467 nested_index++;
1468+ inside_index++;
14661469 std::optional<int > reset_branch{};
14671470 int branch = 0 ;
14681471 if (!branch_reset_indices.empty ()) {
@@ -1492,6 +1495,7 @@ std::optional<Regexp> NLexer::regexp_expression() {
14921495 }
14931496 }
14941497 advance (1 ); // consume ')'
1498+ inside_index--;
14951499 if (seen_newline) {
14961500 const Token &mtoken = error_token ();
14971501 lexer_error (*this , Errors::InvalidRegexpSyntax, mtoken,
@@ -1511,6 +1515,7 @@ std::optional<Regexp> NLexer::regexp_expression() {
15111515 // what _this_ index is
15121516 branch_reset_indices.push (nested_index);
15131517 auto reg = regexp ();
1518+ inside_index--;
15141519 if (reset_branch.has_value ()) {
15151520 nested_index = *reset_branch;
15161521 branch_reset_indices.push (branch);
@@ -1540,6 +1545,7 @@ std::optional<Regexp> NLexer::regexp_expression() {
15401545 nested_index = *reset_branch;
15411546 branch_reset_indices.push (branch);
15421547 }
1548+ inside_index--;
15431549 return {};
15441550 }
15451551 if (c == ' <' || c == ' =' ) {
@@ -1550,6 +1556,7 @@ std::optional<Regexp> NLexer::regexp_expression() {
15501556 nested_index = *reset_branch;
15511557 branch_reset_indices.push (branch);
15521558 }
1559+ inside_index--;
15531560 return {};
15541561 }
15551562 if (c == ' >' ) {
@@ -1560,6 +1567,7 @@ std::optional<Regexp> NLexer::regexp_expression() {
15601567 nested_index = *reset_branch;
15611568 branch_reset_indices.push (branch);
15621569 }
1570+ inside_index--;
15631571 return {};
15641572 }
15651573 if (c == ' :' ) {
@@ -1572,6 +1580,7 @@ std::optional<Regexp> NLexer::regexp_expression() {
15721580 if (!reg.has_value ())
15731581 return reg;
15741582 auto &rv = reg.value ();
1583+ inside_index--;
15751584 c = *source_p;
15761585 if (c != ' )' ) {
15771586 const Token &mtoken = error_token ();
@@ -1589,6 +1598,7 @@ std::optional<Regexp> NLexer::regexp_expression() {
15891598 }
15901599 auto my_index = nested_index;
15911600 auto reg = regexp ();
1601+ inside_index--;
15921602 if (reset_branch.has_value ()) {
15931603 nested_index = *reset_branch;
15941604 branch_reset_indices.push (branch);
@@ -2068,6 +2078,7 @@ Regexp::compile(std::multimap<const Regexp *, NFANode<std::string> *> &cache,
20682078 NFANode<std::string> *tl = new NFANode<std::string>{" R<>" + mangle ()};
20692079 parent->epsilon_transition_to (tl);
20702080 tl->subexpr_call = subexprcall;
2081+ tl->inside_subexpr = inside_subexpr;
20712082 tl->named_rule = namef;
20722083 result = tl;
20732084 result->debug_info = debug_info;
0 commit comments