Sawmill

Interface IRewriter<T>

A rewriter is an object which knows how to access the immediate children of a value of type T.

Implementations should ensure that you always get the children you just set (rewriter.GetChildren(rewriter.SetChildren(children, expr)) == children), and that successive sets overwrite the earlier operation (rewriter.SetChildren(children2, rewriter.SetChildren(children1, expr)) == rewriter.SetChildren(children2, expr)).

IRewritable<T>.

Declaration
public interface IRewriter<T>
Type Parameters
Name Description

T

The type for which the rewriter can get the immediate children

Methods

CountChildren(T)

Count the immediate children of the value. CountChildren()

Declaration
int CountChildren(T value)
Parameters
Type Name Description

T

value

The value

Returns
Type Description

Int32

value's number of immediate children

Examples

Given a representation of the expression (1+2)+3,

Expr expr = new Add(
    new Add(
        new Lit(1),
        new Lit(2)
    ),
    new Lit(3)
);

CountChildren(T) counts the immediate children of the topmost (Add) node.

Assert.Equal(2, rewriter.CountChildren(expr));

GetChildren(Span<T>, T)

Copy the immediate children of the value into childrenReceiver. GetChildren(Span<T>)

Declaration
void GetChildren(Span<T> childrenReceiver, T value)
Parameters
Type Name Description

Span<T>

childrenReceiver

A Span<T> to copy value's immediate children into. The Span<T>'s Length will be equal to the number returned by CountChildren(T).

T

value

The value

Examples

Given a representation of the expression (1+2)+3,

Expr expr = new Add(
    new Add(
        new Lit(1),
        new Lit(2)
    ),
    new Lit(3)
);

GetChildren(Span<T>, T) copies the immediate children of the topmost node into the span.

Expr[] expected = new[]
    {
        new Add(
            new Lit(1),
            new Lit(2)
        ),
        new Lit(3)
    };
var array = new Expr[rewriter.CountChildren(expr)];
rewriter.GetChildren(array, expr);
Assert.Equal(expected, array);

SetChildren(ReadOnlySpan<T>, T)

Set the immediate children of the value.

Callers should ensure that newChildren contains the same number of children as was returned by GetChildren(Span<T>, T).

SetChildren(ReadOnlySpan<T>)

Declaration
T SetChildren(ReadOnlySpan<T> newChildren, T value)
Parameters
Type Name Description

ReadOnlySpan<T>

newChildren

The new children

T

value

The old value, whose immediate children should be replaced

Returns
Type Description

T

A copy of value with updated children.

Examples

Given a representation of the expression (1+2)+3,

Expr expr = new Add(
    new Add(
        new Lit(1),
        new Lit(2)
    ),
    new Lit(3)
);

SetChildren(ReadOnlySpan<T>, T) replaces the immediate children of the topmost node.

Expr expected = new Add(
    new Lit(4),
    new Lit(5)
);
Assert.Equal(expected, rewriter.SetChildren(Children.Two(new Lit(4), new Lit(5)), expr));

Extension Methods

Rewriter.ChildrenInContext<T>(IRewriter<T>, T)
Rewriter.Cursor<T>(IRewriter<T>, T)
Rewriter.DescendantAt<T>(IRewriter<T>, IEnumerable<Direction>, T)
Rewriter.ReplaceDescendantAt<T>(IRewriter<T>, IEnumerable<Direction>, T, T)
Rewriter.RewriteDescendantAt<T>(IRewriter<T>, IEnumerable<Direction>, Func<T, T>, T)
Rewriter.RewriteDescendantAt<T>(IRewriter<T>, IEnumerable<Direction>, Func<T, ValueTask<T>>, T)
Rewriter.DescendantsAndSelf<T>(IRewriter<T>, T)
Rewriter.DescendantsAndSelfInContext<T>(IRewriter<T>, T)
Rewriter.Fold<T, U>(IRewriter<T>, SpanFunc<U, T, U>, T)
Rewriter.Fold<T, U>(IRewriter<T>, Func<Memory<U>, T, ValueTask<U>>, T)
Rewriter.GetChildren<T>(IRewriter<T>, T)
Rewriter.Rewrite<T>(IRewriter<T>, Func<T, T>, T)
Rewriter.Rewrite<T>(IRewriter<T>, Func<T, ValueTask<T>>, T)
Rewriter.RewriteChildren<T>(IRewriter<T>, Func<T, T>, T)
Rewriter.RewriteChildren<T>(IRewriter<T>, Func<T, ValueTask<T>>, T)
Rewriter.SelfAndDescendants<T>(IRewriter<T>, T)
Rewriter.SelfAndDescendantsBreadthFirst<T>(IRewriter<T>, T)
Rewriter.SelfAndDescendantsInContext<T>(IRewriter<T>, T)
Rewriter.SelfAndDescendantsInContextBreadthFirst<T>(IRewriter<T>, T)
Rewriter.ZipFold<T, U>(IRewriter<T>, Func<T[], IEnumerable<U>, U>, T[])
Rewriter.ZipFold<T, U>(IRewriter<T>, Func<T[], IAsyncEnumerable<U>, ValueTask<U>>, T[])