Skip to content

Java regex simple lookaheads#1646

Open
lmasroca wants to merge 27 commits into
masterfrom
java-regex-simple-assertions
Open

Java regex simple lookaheads#1646
lmasroca wants to merge 27 commits into
masterfrom
java-regex-simple-assertions

Conversation

@lmasroca

Copy link
Copy Markdown
Collaborator

Added support for basic java regex lookaheads.

This PR introduces a check to RegexGenes for which the RegexType is JVM, in which we check its randomized value after each randomize() for a match against its compiled java pattern. If the sampled value does not match we trigger repairs, which aim at repairing the assertions in the regex. These are repaired by sampling a value from the lookahead's internal DisjunctionListRxGene and attempting to force that value into the following genes. This is safely done as we check if the genes can actually take that value or not before forcing them. We attempt these repairs a couple of times before giving up and throwing.

We still do not support nested lookaheads (nested inside a group or nested within another lookahead) as the repair mechanism can not handle them yet. The current repairs this PR introduces can only repair regex genes that are on the same level as the lookahead itself (the terms of a DisjunctionRxGene). This also does not yet properly handle multiple concatenated lookaheads as the current repair logic steps over the previous repairs if a new repair is triggered.

Note: this PR adds a new gene AssertionRxGene as well as a new interface RxAbsorbable which is implemented by regex genes.

lmasroca added 27 commits July 13, 2026 19:17
…regex-simple-assertions

# Conflicts:
#	core/src/main/antlr4/org/evomaster/core/parser/RegexJava.g4
#	core/src/main/kotlin/org/evomaster/core/parser/GeneRegexJavaVisitor.kt
@lmasroca
lmasroca changed the base branch from master to java-regex-quote-bug July 20, 2026 18:55
Base automatically changed from java-regex-quote-bug to master July 20, 2026 18:57

internal class RegexHandlerTest{

@Disabled("Needs to hande lookahead in regex")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test needed (?iu), which is not actually lookahead but embedded flags (in particular Unicode case insensitive), which we support already.

@lmasroca
lmasroca requested a review from jgaleotti July 20, 2026 18:58
* finds one of the currently-unsupported ways an assertion's ancestry can appear (nested).
* Returns normally if it reaches the top-level pattern without hitting either.
*/
private fun rejectIfNestedAssertion(ctx: RegexJavaParser.AssertionContext) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this method return a boolen and then took action if nested assertions are found?

if (assertionCtx.CARET() != null || assertionCtx.DOLLAR() != null) {
res.data = ctx.assertion().text
} else {
rejectIfNestedAssertion(ctx.assertion())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the exception be thrown here? Is that an illegalArgumentException since ctx is a parameter?

return true
}

override fun absorbableCount(value: String): Int {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add javadoc for these methods

* the source pattern and found not to match.
*/
class AssertionRxGene(
val innerGene: DisjunctionListRxGene?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the innerGene nullable ? Add some javadoc to explain it.

override val canBeZeroWidth: Boolean = false

override fun tryForce(value: String): Int {
require(value.isNotEmpty())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this require always used? does it throw illegalArgumentException if not satisfied?

@lmasroca lmasroca Jul 21, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require(value: Boolean) throws IllegalArgumentException when value is false, no-op if value is true. Here it should always be true unless we have a bug, as tryForce's call sites should not allow this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants