# Expression Parser

In some scenarios, you do not need the graphical user interface of the RadExpressionEditor, but would rather just require its parsing logic for a given expression.

As of **R3 2021**, you can use the new **RadExpressionParser** class to achieve this requirement.

It exposes the following methods which you can use to parse your expressions:

* **LambdaExpression Parse(string expression)**
* **Task<LambdaExpression> ParseAsync(string expression)**
* **Task<LambdaExpression> ParseAsync(string expression, CancellationToken cancellationToken)**
* **bool TryParse(string expression, out LambdaExpression lambda)**

> Check out the [ExpressionParser](https://demos.telerik.com/wpf) online demo to see the RadExpressionParser in action.

## Parse

You can use the **Parse** method to try to directly parse the expression and return a LambdaExpression if successfully parsed. Please note, however, that an **ExpressionParserException** will be thrown if the expression cannot be parsed.

**Example 1: Using the Parse method**

```C#
try
{
    var lambdaExpression = expressionParser.Parse(expression);
    var result = lambdaExpression.Compile().DynamicInvoke().ToString();
}
catch (ExpressionParserException ex)
{
    var errorMessages = string.Join("\n", ex.Errors.Select(er => er.Message));
}
```

## Function Calls

`Parse`, `TryParse`, and both `ParseAsync` overloads use `ExpressionFunctionContext.Context.FunctionRegistry` to resolve function calls. The default context includes Telerik's built-in functions. Register a custom function's exact method signature in the active context before parsing or evaluating an expression.

Expressions can call only built-in functions and explicitly registered custom functions. The allowlist does not remove access to expression members or properties. For registration and migration details, see [Custom Categories and Functions](https://www.telerik.com/products/wpf/documentation/controls/radexpressioneditor/expressions/custom-functions-and-categories) and [Function Invocation](https://www.telerik.com/products/wpf/documentation/controls/radexpressioneditor/expressions/function-invocation).

## ParseAsync

The two overloads of the **ParseAsync** method operate in the same manner as the Parse method, with the difference being that they are performed **asynchronously**. In the second overload, aside from the expression to be parsed, you can pass a cancellation token that will be assigned to the new task.

## TryParse

The **TryParse** method tries to convert the string representation of an expression to its LambdaExpression equivalent. The returned value indicates whether the conversion succeeded or failed. The **out** parameter will contain **null** if the parsing was unsuccessful.

**Example 2: Using the TryParse method**

```C#
LambdaExpression lambdaExpression;
var isExpressionValid = expressionParser.TryParse(expression, out lambdaExpression);
if (isExpressionValid)
{
    var result = lambdaExpression.Compile().DynamicInvoke().ToString();
}
else
{
    // display an error message
}
```

## Item

You can use the **Item** property to be able to access the public members of the passed object.

**Example 3: Setting the Item property**

```C#
this.expressionParser.Item = this.ProductsGridView.SelectedItem;
```

Note that when using the generated expression, you would then need to pass the same item as the parameter as shown in **Example 4**.

**Example 4: Passing the Item as a parameter of the expression**

```C#
var lambdaExpression = this.expressionParser.Parse(expression);
string result;
if (lambdaExpression.Parameters.Count == 1)
{
    result = lambdaExpression.Compile().DynamicInvoke(this.expressionParser.Item).ToString();
}
else
{
    result = lambdaExpression.Compile().DynamicInvoke().ToString();
}
```

## See Also

* [Function Invocation](https://www.telerik.com/products/wpf/documentation/controls/radexpressioneditor/expressions/function-invocation)
* [Custom Categories and Functions](https://www.telerik.com/products/wpf/documentation/controls/radexpressioneditor/expressions/custom-functions-and-categories)
* [Function Invocation](https://www.telerik.com/products/wpf/documentation/controls/radexpressioneditor/expressions/function-invocation)
