-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathOperandDefinition.cs
More file actions
74 lines (67 loc) · 3.11 KB
/
Copy pathOperandDefinition.cs
File metadata and controls
74 lines (67 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using StringToExpression.Parser;
using StringToExpression.Tokenizer;
using StringToExpression.Exceptions;
namespace StringToExpression.GrammerDefinitions
{
/// <summary>
/// Represents a piece of grammer that defines an operand.
/// </summary>
/// <seealso cref="StringToExpression.GrammerDefinitions.GrammerDefinition" />
public class OperandDefinition : GrammerDefinition
{
/// <summary>
/// A function to generate the operator's Expression.
/// </summary>
public readonly Func<string, Accessor[], Expression> ExpressionBuilder;
/// <summary>
/// Initializes a new instance of the <see cref="OperandDefinition"/> class.
/// </summary>
/// <param name="name">The name of the definition.</param>
/// <param name="regex">The regex to match tokens.</param>
/// <param name="expressionBuilder">The function to generate the operator's Expression given the token's value</param>
/// <exception cref="System.ArgumentNullException">expressionBuilder</exception>
public OperandDefinition(string name, string regex, Func<string, Expression> expressionBuilder)
: this(name, regex, (v,a)=>expressionBuilder(v))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="OperandDefinition"/> class.
/// </summary>
/// <param name="name">The name of the definition.</param>
/// <param name="regex">The regex to match tokens.</param>
/// <param name="expressionBuilder">The function to generate the operator's Expression given the token's value and parsing state parameters.</param>
/// <exception cref="System.ArgumentNullException">expressionBuilder</exception>
public OperandDefinition(string name, string regex, Func<string, Accessor[], Expression> expressionBuilder)
: base(name, regex)
{
if (expressionBuilder == null)
throw new ArgumentNullException(nameof(expressionBuilder));
ExpressionBuilder = expressionBuilder;
}
/// <summary>
/// Applies the token to the parsing state. Adds the result of the expression builder to the state.
/// </summary>
/// <param name="token">The token to apply.</param>
/// <param name="state">The state to apply the token to.</param>
/// <exception cref="StringToExpression.Exceptions.OperationInvalidException">When an error is encounted while running the expressionBuilder</exception>
public override void Apply(Token token, ParseState state)
{
Expression expression;
try
{
expression = ExpressionBuilder(token.Value, state.Parameters.ToArray());
}
catch (Exception ex)
{
throw new OperationInvalidException(token.SourceMap, ex);
}
state.Operands.Push(new Operand(expression, token.SourceMap));
}
}
}