Skip to content

Commit

Permalink
Fixes for TransitionRegex with effects (dotnet#65333)
Browse files Browse the repository at this point in the history
Effects on nothing leaves are now simplified away.
Enumerating TransitionsWithEffects will now ignore all transitions after
the first unconditionally nullable one.
  • Loading branch information
olsaarik authored Feb 15, 2022
1 parent 9cd7927 commit 193aeef
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,13 @@ public static TransitionRegex<S> Conditional(S test, TransitionRegex<S> thencase
public static TransitionRegex<S> Lookaround(SymbolicRegexNode<S> nullabilityTest, TransitionRegex<S> thencase, TransitionRegex<S> elsecase) =>
(thencase == elsecase) ? thencase : Create(thencase._builder, TransitionRegexKind.Lookaround, default(S), thencase, elsecase, nullabilityTest);

public static TransitionRegex<S> Effect(TransitionRegex<S> child, DerivativeEffect effect) =>
Create(child._builder, TransitionRegexKind.Effect, default(S), child, null, null, effect);
public static TransitionRegex<S> Effect(TransitionRegex<S> child, DerivativeEffect effect)
{
if (child.IsNothing)
return child;

return Create(child._builder, TransitionRegexKind.Effect, default(S), child, null, null, effect);
}

/// <summary>Intersection of transition regexes</summary>
public static TransitionRegex<S> operator &(TransitionRegex<S> one, TransitionRegex<S> two) => Intersect(one, two);
Expand Down Expand Up @@ -568,6 +573,8 @@ public SymbolicRegexNode<S> TransitionOrdered(S minterm, uint context)

/// <summary>
/// Enumerate the leaves reachable with a given minterm and context, and collect the effects on the path to each leaf.
/// Any transitions after the first unconditionally nullable one are ignored, as the backtracking engines would never
/// take a path corresponding to those transitions.
/// </summary>
/// <param name="minterm">the minterm of the next character</param>
/// <param name="context">the current context</param>
Expand All @@ -585,6 +592,9 @@ public SymbolicRegexNode<S> TransitionOrdered(S minterm, uint context)
case TransitionRegexKind.Leaf:
Debug.Assert(top._node is not null);
yield return (top._node, effects);
// If the leaf is nullable lower priority transitions would never get used anyway, so stop here
if (top._node.IsNullable)
yield break;
break;

case TransitionRegexKind.Conditional:
Expand Down

0 comments on commit 193aeef

Please sign in to comment.