Java regex simple lookaheads#1646
Conversation
…regex-simple-assertions # Conflicts: # core/src/main/antlr4/org/evomaster/core/parser/RegexJava.g4 # core/src/main/kotlin/org/evomaster/core/parser/GeneRegexJavaVisitor.kt
|
|
||
| internal class RegexHandlerTest{ | ||
|
|
||
| @Disabled("Needs to hande lookahead in regex") |
There was a problem hiding this comment.
This test needed (?iu), which is not actually lookahead but embedded flags (in particular Unicode case insensitive), which we support already.
| * 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) { |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
Should the exception be thrown here? Is that an illegalArgumentException since ctx is a parameter?
| return true | ||
| } | ||
|
|
||
| override fun absorbableCount(value: String): Int { |
There was a problem hiding this comment.
Add javadoc for these methods
| * the source pattern and found not to match. | ||
| */ | ||
| class AssertionRxGene( | ||
| val innerGene: DisjunctionListRxGene? |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
is this require always used? does it throw illegalArgumentException if not satisfied?
There was a problem hiding this comment.
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.
Added support for basic java regex lookaheads.
This PR introduces a check to
RegexGenes for which theRegexTypeisJVM, in which we check its randomized value after eachrandomize()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 internalDisjunctionListRxGeneand 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
termsof aDisjunctionRxGene). 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
AssertionRxGeneas well as a new interfaceRxAbsorbablewhich is implemented by regex genes.