Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
{
Expand Down
4 changes: 2 additions & 2 deletions src/Ramstack.ExpressionParser/Expressions/Expr.Unary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public override T Accept<T>(ExprVisitor<T> visitor) =>

/// <inheritdoc />
/// <returns>
/// A string representation of the unary expression, e.g., <c>-x</c> for operators or <c>x:int</c> for conversions.
/// A string representation of the unary expression, e.g., <c>-x</c> for operators or <c>x :> int</c> for conversions.
/// </returns>
public override string ToString() => UnaryType switch
{
UnaryType.Convert => $"{Operand}:{Operator}",
UnaryType.Convert => $"{Operand} :> {Operator}",
_ => $"{Operator}({Operand})"
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Ramstack.ExpressionParser/Expressions/UnaryType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public enum UnaryType
/// Represents a type cast or conversion operation, such as converting a value to a specific type.
/// </summary>
/// <example>
/// In the expression <c>x:int</c>, this indicates a conversion of <c>x</c> to an integer.
/// In the expression <c>x :> int</c>, this indicates a conversion of <c>x</c> to an integer.
/// </example>
Convert,

Expand Down
36 changes: 21 additions & 15 deletions tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,38 @@ 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.

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 ')'
Expand All @@ -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 '('
Expand All @@ -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)
Expand Down Expand Up @@ -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'.

#######################################
Expand Down
Loading
Loading