diff --git a/README.md b/README.md index 26a7951..5bc2566 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,16 @@ public bool IsEnabled(LogEvent logEvent) This makes it easy to create dynamic, human-readable expressions for filtering or evaluating objects at runtime. +### Type conversions + +Use the `:>` operator to convert a value to a type, for example `count :> long` or `Math.PI :> float`. +A conversion binds more tightly than prefix and binary operators, so `-x :> int` means `-(x :> int)`, +and `x :> long + 1` means `(x :> long) + 1`. + +Earlier versions wrote conversions as `x:int`. That syntax conflicted with the conditional operator, +so expressions such as `flag ? 0 : count` failed to parse. It is no longer supported; +replace `x:int` with `x :> int`. + ### Limits An expression tree can be at most 256 levels deep. Each operator, member access, call, indexer, diff --git a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs index 46bc185..b2698f3 100644 --- a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs +++ b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs @@ -173,11 +173,15 @@ static Expr HijackMemberExpression(Expr result, Expr value) } }); + // + // The conversion operator is ':>' rather than ':', which would conflict with the conditional + // operator: in 'flag ? 0 : count', ': count' would be taken for a conversion to the type 'count'. + // var cast_expression = Seq( member_expression, identifier.Between( - Seq(L(':'), S), + Seq(L(":>"), S), S).Many() ).Do((expr, ids) => { diff --git a/src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs b/src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs index b779d61..7403406 100644 --- a/src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs +++ b/src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs @@ -75,11 +75,11 @@ public override T Accept(ExprVisitor visitor) => /// /// - /// A string representation of the unary expression, e.g., -x for operators or x:int for conversions. + /// A string representation of the unary expression, e.g., -x for operators or x :> int for conversions. /// public override string ToString() => UnaryType switch { - UnaryType.Convert => $"{Operand}:{Operator}", + UnaryType.Convert => $"{Operand} :> {Operator}", _ => $"{Operator}({Operand})" }; } diff --git a/src/Ramstack.ExpressionParser/Expressions/UnaryType.cs b/src/Ramstack.ExpressionParser/Expressions/UnaryType.cs index 4da0230..08d3ca1 100644 --- a/src/Ramstack.ExpressionParser/Expressions/UnaryType.cs +++ b/src/Ramstack.ExpressionParser/Expressions/UnaryType.cs @@ -9,7 +9,7 @@ public enum UnaryType /// Represents a type cast or conversion operation, such as converting a value to a specific type. /// /// - /// In the expression x:int, this indicates a conversion of x to an integer. + /// In the expression x :> int, this indicates a conversion of x to an integer. /// Convert, diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index f6a5764..1613a8b 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -10,24 +10,30 @@ Guid Guid.Value Cannot resolve symbol 'Value'. -string.Empty:text +string.Empty:>text Cannot resolve symbol 'text'. -single.10:text +single.10:>text Identifier expected (expression: 10). -float."10":text +float."10":>text Identifier expected (expression: "10"). -int.true:text -(1:4) Expected '(', '[', ':', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input +int.true:>text +(1:4) Expected '(', '[', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input -double.false:text -(1:7) Expected '(', '[', ':', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input +double.false:>text +(1:7) Expected '(', '[', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input -Math.int:text +Math.int:>text Cannot resolve symbol 'int'. +1 :> 2 +(1:6) Expected identifier + +1:int +(1:2) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input + byte(20) Non-invocable member 'byte' cannot be used like a method. @@ -35,7 +41,7 @@ short.maxvalue(10) Non-invocable member 'maxvalue' cannot be used like a method. 1 + (2 * 3 -(1:11) Expected '(', '[', '.', ':', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or ')' +(1:11) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or ')' 1 + 2 * 3( (1:11) Expected ')' @@ -50,7 +56,7 @@ Cannot apply indexing with [] to an expression of type 'System.Int32'. (1:11) Expected ']' 1 + 2 * 3] -(1:10) Expected '(', '[', '.', ':', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input +(1:10) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input 1 // 2 * 3 (1:4) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' @@ -77,7 +83,7 @@ Cannot apply indexing with [] to an expression of type 'System.Int32'. (1:4) Expected [!+\-~], double-quoted string, quoted-character, 'true', 'false', 'null', identifier, or '(' 0xp -(1:2) Expected '(', '[', '.', ':', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input +(1:2) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input "test"[] Ambiguous match found:\n Char Chars [Int32] (in System.String) @@ -112,16 +118,16 @@ Operator '+' cannot be applied to operands of type 'System.Decimal' and 'System. # If either operand is of type ulong, a binding-time error occurs # if the other operand is of type sbyte, short, int, or long. -1ul + 2:sbyte +1ul + 2:>sbyte Operator '+' cannot be applied to operands of type 'System.UInt64' and 'System.SByte'. -1ul + 2:short +1ul + 2:>short Operator '+' cannot be applied to operands of type 'System.UInt64' and 'System.Int16'. -1ul + 2:int +1ul + 2:>int Operator '+' cannot be applied to operands of type 'System.UInt64' and 'System.Int32'. -1ul + 2:long +1ul + 2:>long Operator '+' cannot be applied to operands of type 'System.UInt64' and 'System.Int64'. ####################################### diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt index d7c7b91..5618939 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt @@ -1,13 +1,13 @@ -1:byte + 2:byte +1:>byte + 2:>byte (Convert(Convert(1, Byte), Int32) + Convert(Convert(2, Byte), Int32)) -1:short + 2 +1:>short + 2 (Convert(Convert(1, Int16), Int32) + 2) -1 + 2:short +1 + 2:>short (1 + Convert(Convert(2, Int16), Int32)) --1:sbyte +-1:>sbyte -Convert(Convert(1, SByte), Int32) -2147483648 @@ -37,7 +37,7 @@ Convert((Convert(-9223372036854775808, UInt64) >> 4), Int64) 5000 >> 4 (5000 >> 4) -1:byte << 4 +1:>byte << 4 (Convert(Convert(1, Byte), Int32) << 4) byte.MaxValue + 1 @@ -46,7 +46,7 @@ byte.MaxValue + 1 byte.MaxValue + byte.MinValue (Convert(Byte.MaxValue, Int32) + Convert(Byte.MinValue, Int32)) -~1:short +~1:>short ~(Convert(Convert(1, Int16), Int32)) -100u @@ -73,7 +73,7 @@ IIF((1 == 1), IIF((2 == 2), IIF((3 == 3), (4 == 4), (5 == 5)), (6 == 6)), (7 == Math.PI * 2 + 1 ((Math.PI * Convert(2, Double)) + Convert(1, Double)) -Math.PI:float * 2 + 1 +Math.PI:>float * 2 + 1 ((Convert(Math.PI, Single) * Convert(2, Single)) + Convert(1, Single)) 2.93E5f + 2.17d @@ -205,6 +205,32 @@ true | false true & false (True And False) +####################################### +# Conditional operator branches that start with an identifier or contain a conversion +true ? Math.PI : Math.E +IIF(True, Math.PI, Math.E) + +1 > 0 ? int.MaxValue : int.MinValue +IIF((1 > 0), Int32.MaxValue, Int32.MinValue) + +true ? "a" : string.Empty +IIF(True, "a", String.Empty) + +true ? null : null +IIF(True, null, null) + +1 > 0 ? 2 > 0 ? int.MaxValue : int.MinValue : 0 +IIF((1 > 0), IIF((2 > 0), Int32.MaxValue, Int32.MinValue), 0) + +1 > 0 ? 0 : 2 > 0 ? int.MaxValue : int.MinValue +IIF((1 > 0), 0, IIF((2 > 0), Int32.MaxValue, Int32.MinValue)) + +true ? 1 :> long : 2 :> long +IIF(True, Convert(1, Int64), Convert(2, Int64)) + +true ? 1:>long:>int : 2 +IIF(True, Convert(Convert(1, Int64), Int32), 2) + ####################################### # Precedence test 1 + 2 - 4 * 5 / 4 << 1 & -3 ^ 17 | ~5 >> 2 >>> 1 % 5 @@ -222,28 +248,28 @@ true & false 1 + 2m (Convert(1, Decimal) + 2) -1m + 2:byte +1m + 2:>byte (1 + Convert(Convert(2, Byte), Decimal)) -1m + 2:sbyte +1m + 2:>sbyte (1 + Convert(Convert(2, SByte), Decimal)) -1m + 2:short +1m + 2:>short (1 + Convert(Convert(2, Int16), Decimal)) -1m + 2:ushort +1m + 2:>ushort (1 + Convert(Convert(2, UInt16), Decimal)) -1m + 2:int +1m + 2:>int (1 + Convert(2, Decimal)) -1m + 2:uint +1m + 2:>uint (1 + Convert(Convert(2, UInt32), Decimal)) -1m + 2:long +1m + 2:>long (1 + Convert(Convert(2, Int64), Decimal)) -1m + 2:ulong +1m + 2:>ulong (1 + Convert(Convert(2, UInt64), Decimal)) @@ -256,28 +282,28 @@ true & false 1 + 2d (Convert(1, Double) + 2) -1.0 + 2:byte +1.0 + 2:>byte (1 + Convert(Convert(2, Byte), Double)) -1.0 + 2:sbyte +1.0 + 2:>sbyte (1 + Convert(Convert(2, SByte), Double)) -1.0 + 2:short +1.0 + 2:>short (1 + Convert(Convert(2, Int16), Double)) -1.0 + 2:ushort +1.0 + 2:>ushort (1 + Convert(Convert(2, UInt16), Double)) -1.0 + 2:int +1.0 + 2:>int (1 + Convert(2, Double)) -1.0 + 2:uint +1.0 + 2:>uint (1 + Convert(Convert(2, UInt32), Double)) -1.0 + 2:long +1.0 + 2:>long (1 + Convert(Convert(2, Int64), Double)) -1.0 + 2:ulong +1.0 + 2:>ulong (1 + Convert(Convert(2, UInt64), Double)) ####################################### @@ -289,28 +315,28 @@ true & false 1 + 2f (Convert(1, Single) + 2) -1.0f + 2:byte +1.0f + 2:>byte (1 + Convert(Convert(2, Byte), Single)) -1.0f + 2:sbyte +1.0f + 2:>sbyte (1 + Convert(Convert(2, SByte), Single)) -1.0f + 2:short +1.0f + 2:>short (1 + Convert(Convert(2, Int16), Single)) -1.0f + 2:ushort +1.0f + 2:>ushort (1 + Convert(Convert(2, UInt16), Single)) -1.0f + 2:int +1.0f + 2:>int (1 + Convert(2, Single)) -1.0f + 2:uint +1.0f + 2:>uint (1 + Convert(Convert(2, UInt32), Single)) -1.0f + 2:long +1.0f + 2:>long (1 + Convert(Convert(2, Int64), Single)) -1.0f + 2:ulong +1.0f + 2:>ulong (1 + Convert(Convert(2, UInt64), Single)) ####################################### @@ -322,16 +348,16 @@ true & false 1u + 2ul (Convert(1, UInt64) + 2) -1ul + 2:byte +1ul + 2:>byte (1 + Convert(Convert(2, Byte), UInt64)) -1ul + 2:ushort +1ul + 2:>ushort (1 + Convert(Convert(2, UInt16), UInt64)) -1ul + 2:uint +1ul + 2:>uint (1 + Convert(Convert(2, UInt32), UInt64)) -1ul + 2:ulong +1ul + 2:>ulong (1 + Convert(2, UInt64)) ####################################### @@ -343,25 +369,25 @@ true & false 1 + 2L (Convert(1, Int64) + 2) -1L + 2:byte +1L + 2:>byte (1 + Convert(Convert(2, Byte), Int64)) -1L + 2:sbyte +1L + 2:>sbyte (1 + Convert(Convert(2, SByte), Int64)) -1L + 2:short +1L + 2:>short (1 + Convert(Convert(2, Int16), Int64)) -1L + 2:ushort +1L + 2:>ushort (1 + Convert(Convert(2, UInt16), Int64)) -1L + 2:int +1L + 2:>int (1 + Convert(2, Int64)) -1L + 2:uint +1L + 2:>uint (1 + Convert(Convert(2, UInt32), Int64)) -1L + 2:long +1L + 2:>long (1 + Convert(2, Int64)) ####################################### @@ -374,40 +400,40 @@ true & false 1 + 2u (Convert(1, Int64) + Convert(2, Int64)) -1u + 2:byte +1u + 2:>byte (1 + Convert(Convert(2, Byte), UInt32)) -1u + 2:sbyte +1u + 2:>sbyte (Convert(1, Int64) + Convert(Convert(2, SByte), Int64)) -1u + 2:short +1u + 2:>short (Convert(1, Int64) + Convert(Convert(2, Int16), Int64)) -1u + 2:ushort +1u + 2:>ushort (1 + Convert(Convert(2, UInt16), UInt32)) -1u + 2:int +1u + 2:>int (Convert(1, Int64) + Convert(2, Int64)) -1u + 2:uint +1u + 2:>uint (1 + Convert(2, UInt32)) ####################################### # Otherwise, both operands are converted to type int. -1 + 2:byte +1 + 2:>byte (1 + Convert(Convert(2, Byte), Int32)) -1 + 2:sbyte +1 + 2:>sbyte (1 + Convert(Convert(2, SByte), Int32)) -1 + 2:short +1 + 2:>short (1 + Convert(Convert(2, Int16), Int32)) -1 + 2:ushort +1 + 2:>ushort (1 + Convert(Convert(2, UInt16), Int32)) -1 + 2:int +1 + 2:>int (1 + 2) @@ -418,19 +444,19 @@ true & false ####################################### # + operator -+1:byte ++1:>byte +Convert(Convert(1, Byte), Int32) -+1:sbyte ++1:>sbyte +Convert(Convert(1, SByte), Int32) -+1:short ++1:>short +Convert(Convert(1, Int16), Int32) -+1:ushort ++1:>ushort +Convert(Convert(1, UInt16), Int32) -+1:int ++1:>int +1 +1u @@ -445,19 +471,19 @@ true & false ####################################### # - operator --1:byte +-1:>byte -Convert(Convert(1, Byte), Int32) --1:sbyte +-1:>sbyte -Convert(Convert(1, SByte), Int32) --1:short +-1:>short -Convert(Convert(1, Int16), Int32) --1:ushort +-1:>ushort -Convert(Convert(1, UInt16), Int32) --1:int +-1:>int -1 -1u diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 3b93928..f959a58 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -44,7 +44,7 @@ private static IEnumerable DeepExpressionSource const int Count = 200_000; yield return Case("prefix operators", new string('-', Count) + "1"); - yield return Case("casts", "1" + Repeat(":int", Count)); + yield return Case("casts", "1" + Repeat(":>int", Count)); yield return Case("binary operators", string.Join(" + ", Enumerable.Repeat("1", Count))); yield return Case("null-coalescing operators", string.Join(" ?? ", Enumerable.Repeat("\"a\"", Count))); yield return Case("member access", "\"a\"" + Repeat(".Length.ToString()", Count)); @@ -88,7 +88,7 @@ private static IEnumerable StackBudgetSource get { yield return Case("prefix operators", new string('-', 255) + "1"); - yield return Case("casts", "1" + Repeat(":int", 255)); + yield return Case("casts", "1" + Repeat(":>int", 255)); yield return Case("binary operators", string.Join(" + ", Enumerable.Repeat("1", 256))); yield return Case("null-coalescing operators", string.Join(" ?? ", Enumerable.Repeat("\"a\"", 256))); yield return Case("indexers and calls", "\"abc\"" + Repeat("[0].ToString()", 85));