Skip to content

Commit

Permalink
Fix the calculation of enclosedCaptureGroups when wrapping groups
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkamarsik committed Feb 16, 2024
1 parent bf6c001 commit c4bdc53
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public TRegexBacktrackingNFAExecutorNode(RegexAST ast, PureNFA nfa, int numberOf
QuantifiableTerm quantifiable = zeroWidthQuantifiables.get(i);
if (quantifiable.isGroup()) {
Group group = quantifiable.asGroup();
this.zeroWidthTermEnclosedCGLow[i] = group.getEnclosedCaptureGroupsLow();
offset += 2 * (group.getEnclosedCaptureGroupsHigh() - group.getEnclosedCaptureGroupsLow());
this.zeroWidthTermEnclosedCGLow[i] = group.getCaptureGroupsLow();
offset += 2 * (group.getCaptureGroupsHigh() - group.getCaptureGroupsLow());
}
this.zeroWidthQuantifierCGOffsets[i + 1] = offset;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,8 @@ private void setQuantifier(QuantifiableTerm term, Token.Quantifier quantifier) {
private Group wrapTermInGroup(Term term) {
Group wrapperGroup = ast.createGroup();
if (term.isGroup()) {
wrapperGroup.setEnclosedCaptureGroupsLow(term.asGroup().getEnclosedCaptureGroupsLow());
wrapperGroup.setEnclosedCaptureGroupsHigh(term.asGroup().getEnclosedCaptureGroupsHigh());
wrapperGroup.setEnclosedCaptureGroupsLow(term.asGroup().getCaptureGroupsLow());
wrapperGroup.setEnclosedCaptureGroupsHigh(term.asGroup().getCaptureGroupsHigh());
} else if (term.isAtomicGroup()) {
wrapperGroup.setEnclosedCaptureGroupsLow(term.asAtomicGroup().getEnclosedCaptureGroupsLow());
wrapperGroup.setEnclosedCaptureGroupsHigh(term.asAtomicGroup().getEnclosedCaptureGroupsHigh());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ private void createOptional(QuantifiableTerm term, Token.Quantifier quantifier,
curGroup.setExpandedQuantifier(unroll);
curGroup.setQuantifier(quantifier);
if (term.isGroup()) {
curGroup.setEnclosedCaptureGroupsLow(term.asGroup().getEnclosedCaptureGroupsLow());
curGroup.setEnclosedCaptureGroupsHigh(term.asGroup().getEnclosedCaptureGroupsHigh());
curGroup.setEnclosedCaptureGroupsLow(term.asGroup().getCaptureGroupsLow());
curGroup.setEnclosedCaptureGroupsHigh(term.asGroup().getCaptureGroupsHigh());
}
if (quantifier.isGreedy()) {
createOptionalBranch(term, quantifier, copy, unroll, recurse, emptyGuard);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ public int getEnclosedCaptureGroupsLow() {
return enclosedCaptureGroupsLow;
}

/**
* Gets the (inclusive) lower bound of the range of capture groups in this term. In contrast to
* {@link #getEnclosedCaptureGroupsLow()}, this range contains the group itself if it is a
* capturing group.
*/
public int getCaptureGroupsLow() {
return isCapturing() ? getGroupNumber() : enclosedCaptureGroupsLow;
}

/**
* Sets the (inclusive) lower bound of the range of capture groups contained within this group.
*/
Expand All @@ -242,6 +251,13 @@ public int getEnclosedCaptureGroupsHigh() {
return enclosedCaptureGroupsHigh;
}

/**
* Gets the (exclusive) upper bound of the range of capture groups in this term.
*/
public int getCaptureGroupsHigh() {
return enclosedCaptureGroupsHigh;
}

/**
* Sets the (exclusive) upper bound of the range of capture groups contained within this group.
*/
Expand Down

0 comments on commit c4bdc53

Please sign in to comment.