Skip to content

Commit a1df43b

Browse files
authored
Merge pull request #47 from rameel/parser/parenthesize-conversions
parser: parenthesize conversions and conditionals in Expr.ToString
2 parents ff929b6 + 6df3b3f commit a1df43b

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎src/Ramstack.ExpressionParser/Expressions/Expr.Conditional.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public override T Accept<T>(ExprVisitor<T> visitor) =>
4444

4545
/// <inheritdoc />
4646
public override string ToString() =>
47-
$"{Test} ? {IfTrue} : {IfFalse}";
47+
$"({Test} ? {IfTrue} : {IfFalse})";
4848
}
4949
}

‎src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ public override T Accept<T>(ExprVisitor<T> visitor) =>
7575

7676
/// <inheritdoc />
7777
/// <returns>
78-
/// A string representation of the unary expression, e.g., <c>-(x)</c> for operators or <c>x :> int</c> for conversions.
78+
/// A string representation of the unary expression, e.g., <c>-(x)</c> for operators or <c>(x :> int)</c> for conversions.
7979
/// </returns>
8080
public override string ToString() => UnaryType switch
8181
{
82-
UnaryType.Convert => $"{Operand} :> {Operator}",
82+
UnaryType.Convert => $"({Operand} :> {Operator})",
8383
_ => $"{Operator}({Operand})"
8484
};
8585
}

‎tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,48 @@ public void StringConcatTest(string source, string expected)
698698
Assert.That(result.ToString(), Is.EqualTo(expected));
699699
}
700700

701+
[TestCase("1 :> long + 1", "((1 :> long) + 1)")]
702+
[TestCase("1 + 2 :> long", "(1 + (2 :> long))")]
703+
[TestCase("a == b :> bool", "(a == (b :> bool))")]
704+
[TestCase("-x :> int", "-((x :> int))")]
705+
[TestCase("a ? b : c :> int", "(a ? b : (c :> int))")]
706+
public void ToString_ConversionOperand_ParenthesizesConversion(string source, string expected) =>
707+
Assert.That(ExpressionParser.Parser.Parse(source).Value!.ToString(), Is.EqualTo(expected));
708+
709+
[TestCaseSource(nameof(BuiltOperandSource))]
710+
public void ToString_BuiltTreeOperand_ParenthesizesOperand(Expr expr, string expected) =>
711+
Assert.That(expr.ToString(), Is.EqualTo(expected));
712+
713+
private static IEnumerable<TestCaseData> BuiltOperandSource
714+
{
715+
get
716+
{
717+
//
718+
// The parser wraps such operands in Expr.Parenthesized, but a tree can be built without it.
719+
//
720+
var conditional = new Expr.Conditional(Reference("a"), Reference("b"), Reference("c"));
721+
var conversion = new Expr.Unary(new Identifier("object"), UnaryType.Convert, Reference("x"));
722+
723+
yield return Case("conditional in binary",
724+
new Expr.Binary(new Identifier("+"), conditional, new Expr.Literal(1)),
725+
"((a ? b : c) + 1)");
726+
727+
yield return Case("conditional in conversion",
728+
new Expr.Unary(new Identifier("int"), UnaryType.Convert, conditional),
729+
"((a ? b : c) :> int)");
730+
731+
yield return Case("conversion in member access",
732+
new Expr.Call(new Expr.MemberAccess(conversion, Reference("ToString")), []),
733+
"(x :> object).ToString()");
734+
735+
static Expr Reference(string name) =>
736+
new Expr.Reference(new Identifier(name));
737+
738+
static TestCaseData Case(string name, Expr expr, string expected) =>
739+
new TestCaseData(expr, expected).SetArgDisplayNames(name);
740+
}
741+
}
742+
701743
private static void AssertNullableResult(string expression, Type type, object? expected) =>
702744
AssertResult(new NullableContext(), expression, type, expected);
703745

0 commit comments

Comments
 (0)