Skip to content

Commit

Permalink
Merge pull request #4 from djluck/deep-clone
Browse files Browse the repository at this point in the history
Adding support for deep cloning of `Expr`. Also clarified documentati…
  • Loading branch information
djluck authored Apr 21, 2022
2 parents 5728f62 + 72033cf commit 7ec2f3d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ BinaryExpr {
VectorMatching = ...
}
```
All non-value AST types are mutable and can also be updated in place using a visitor such as `DepthFirstExpressionVisitor`.
Deep cloning of `Expr` is also supported via `Expr.DeepClone()`. Additionally all `Expr` AST types are mutable and can also be updated in place using a visitor such as `DepthFirstExpressionVisitor`.

### Emitting PromQL expressions
An Abstract Syntax Tree can be converted back to its PromQL string representation, e.g:
Expand Down
35 changes: 27 additions & 8 deletions src/PromQL.Parser/Ast.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using ExhaustiveMatching;
using Superpower.Model;
using Superpower.Parsers;

namespace PromQL.Parser.Ast
{
/// <summary>
/// Base of all PromQL syntactic components.
/// </summary>
public interface IPromQlNode
public interface IPromQlNode
{
void Accept(IVisitor visitor);
TextSpan? Span { get; }
Expand All @@ -35,6 +35,12 @@ public interface IPromQlNode
public interface Expr : IPromQlNode
{
ValueType Type { get; }

/// <summary>
/// Makes a deep clone of an expression.
/// </summary>
/// <returns></returns>
public Expr DeepClone();
}

/// <summary>
Expand Down Expand Up @@ -67,6 +73,7 @@ public AggregateExpr(AggregateOperator @operator, Expr expr, Expr param, bool wi
public ValueType Type => ValueType.Vector;

public void Accept(IVisitor visitor) => visitor.Visit(this);
public Expr DeepClone() => this with {Expr = Expr.DeepClone(), Param = Param?.DeepClone() };
}

/// <summary>
Expand Down Expand Up @@ -95,6 +102,8 @@ public ValueType Type
return ValueType.Vector;
}
}

public Expr DeepClone() => this with { LeftHandSide = LeftHandSide.DeepClone(), RightHandSide = RightHandSide.DeepClone() };
}

/// <summary>
Expand All @@ -119,10 +128,10 @@ public VectorMatching(bool returnBool) : this (DefaultMatchCardinality, Immutabl
{
}

public Operators.VectorMatchCardinality MatchCardinality { get; set; } = MatchCardinality;
public bool On { get; set; } = On;
public ImmutableArray<string> Include { get; set; } = Include;
public bool ReturnBool { get; set; } = ReturnBool;
public Operators.VectorMatchCardinality MatchCardinality { get; internal set; } = MatchCardinality;
public bool On { get; } = On;
public ImmutableArray<string> Include { get; internal set; } = Include;
public bool ReturnBool { get; } = ReturnBool;

public void Accept(IVisitor visitor) => visitor.Visit(this);
};
Expand All @@ -146,6 +155,8 @@ public FunctionCall(Function function, params Expr[] args)

public void Accept(IVisitor visitor) => visitor.Visit(this);

public Expr DeepClone() => this with { Args = Args.Select(a => a.DeepClone()).ToImmutableArray() };

protected virtual bool PrintMembers(StringBuilder builder)
{
builder.AppendLine($"{nameof(Function)} = {Function.Name}, ");
Expand All @@ -161,6 +172,7 @@ public record ParenExpression(Expr Expr, TextSpan? Span = null) : Expr
public Expr Expr { get; set; } = Expr;
public void Accept(IVisitor visitor) => visitor.Visit(this);
public ValueType Type => Expr.Type;
public Expr DeepClone() => this with { Expr = Expr.DeepClone() };
}

public record OffsetExpr(Expr Expr, Duration Duration, TextSpan? Span = null) : Expr
Expand All @@ -169,6 +181,7 @@ public record OffsetExpr(Expr Expr, Duration Duration, TextSpan? Span = null) :
public Duration Duration { get; set; } = Duration;
public void Accept(IVisitor visitor) => visitor.Visit(this);
public ValueType Type => Expr.Type;
public Expr DeepClone() => this with { Expr = Expr.DeepClone() };
}

public record MatrixSelector(VectorSelector Vector, Duration Duration, TextSpan? Span = null) : Expr
Expand All @@ -177,6 +190,7 @@ public record MatrixSelector(VectorSelector Vector, Duration Duration, TextSpan?
public Duration Duration { get; set; } = Duration;
public void Accept(IVisitor visitor) => visitor.Visit(this);
public ValueType Type => ValueType.Matrix;
public Expr DeepClone() => this with { };
}

public record UnaryExpr(Operators.Unary Operator, Expr Expr, TextSpan? Span = null) : Expr
Expand All @@ -186,6 +200,7 @@ public record UnaryExpr(Operators.Unary Operator, Expr Expr, TextSpan? Span = nu

public void Accept(IVisitor visitor) => visitor.Visit(this);
public ValueType Type => Expr.Type;
public Expr DeepClone() => this with { Expr = Expr.DeepClone() };
}

public record VectorSelector : Expr
Expand Down Expand Up @@ -216,11 +231,12 @@ public VectorSelector(MetricIdentifier metricIdentifier, LabelMatchers labelMatc
public ValueType Type => ValueType.Vector;

public void Accept(IVisitor visitor) => visitor.Visit(this);
public Expr DeepClone() => this with { };
}

public record LabelMatchers(ImmutableArray<LabelMatcher> Matchers, TextSpan? Span = null) : IPromQlNode
{
protected virtual bool PrintMembers(System.Text.StringBuilder builder)
protected virtual bool PrintMembers(StringBuilder builder)
{
builder.Append($"{nameof(Matchers)} = ");
Matchers.PrintArray(builder);
Expand All @@ -247,6 +263,7 @@ public record NumberLiteral(double Value, TextSpan? Span = null) : Expr
{
public void Accept(IVisitor visitor) => visitor.Visit(this);
public ValueType Type => ValueType.Scalar;
public Expr DeepClone() => this with { };
}

public record Duration(TimeSpan Value, TextSpan? Span = null) : IPromQlNode
Expand All @@ -258,6 +275,7 @@ public record StringLiteral(char Quote, string Value, TextSpan? Span = null) : E
{
public void Accept(IVisitor visitor) => visitor.Visit(this);
public ValueType Type => ValueType.String;
public Expr DeepClone() => this with { };
}

public record SubqueryExpr(Expr Expr, Duration Range, Duration? Step = null, TextSpan? Span = null) : Expr
Expand All @@ -268,6 +286,7 @@ public record SubqueryExpr(Expr Expr, Duration Range, Duration? Step = null, Tex
public ValueType Type => ValueType.Matrix;

public void Accept(IVisitor visitor) => visitor.Visit(this);
public Expr DeepClone() => this with { Expr = Expr.DeepClone() };
}

internal static class Extensions
Expand All @@ -278,7 +297,7 @@ internal static void PrintArray<T>(this ImmutableArray<T> arr, System.Text.Strin
sb.Append("[ ");
for (int i = 0; i < arr.Length; i++)
{
sb.Append(arr[i].ToString());
sb.Append(arr[i]);
if (i < arr.Length - 1)
sb.Append(", ");
}
Expand Down

0 comments on commit 7ec2f3d

Please sign in to comment.