Numeric Values¶
Universal Expression Parser parses expression items that have numeric format to expression items of type UniversalExpressionParser.ExpressionItems.INumericExpressionItem. The format of expression items that will be parsed to UniversalExpressionParser.ExpressionItems.INumericExpressionItem is determined by property IReadOnlyList<NumericTypeDescriptor> NumericTypeDescriptors { get; } in interface UniversalExpressionParser.IExpressionLanguageProvider, an instance of which is passed to the parser.
Click here to see the definition of UniversalExpressionParser.NumericTypeDescriptor
Click here to see the definition of UniversalExpressionParser.ExpressionItems.INumericExpressionItem
The parser scans the regular expressions in list in property IReadOnlyList<string> RegularExpressions { get; } in type NumericTypeDescriptor for each provided instance of UniversalExpressionParser.NumericTypeDescriptor to try to parse the expression to numeric expression item of type UniversalExpressionParser.ExpressionItems.INumericExpressionItem.
The abstract class UniversalExpressionParser.ExpressionLanguageProviderBase that can be used as a base class for implementations of UniversalExpressionParser.IExpressionLanguageProvider in most cases, implements the property NumericTypeDescriptors as a virtual property. The implementation of property NumericTypeDescriptors in UniversalExpressionParser.ExpressionLanguageProviderBase is demonstrated below, and it can be overridden to provide different format for numeric values:
Note
The regular expressions used in implementation of property NumericTypeDescriptors should always start with ‘^’ and should never end with ‘$’.
1public virtual IReadOnlyList<NumericTypeDescriptor> NumericTypeDescriptors { get; } = new List<NumericTypeDescriptor>
2{
3 new NumericTypeDescriptor(KnownNumericTypeDescriptorIds.ExponentFormatValueId,
4 new[] { @"^(\d+\.\d+|\d+\.|\.\d+|\d+)(EXP|exp|E|e)[+|-]?(\d+\.\d+|\d+\.|\.\d+|\d+)"}),
5
6 new NumericTypeDescriptor(KnownNumericTypeDescriptorIds.FloatingPointValueId,
7 new[] { @"^(\d+\.\d+|\d+\.|\.\d+)"}),
8
9 new NumericTypeDescriptor(KnownNumericTypeDescriptorIds.IntegerValueId, new[] { @"^\d+" })
10}
The first regular expression that matches the expression, is stored in properties SucceededNumericTypeDescriptor of type UniversalExpressionParser.NumericTypeDescriptor and IndexOfSucceededRegularExpression in parsed expression item of type UniversalExpressionParser.ExpressionItems.INumericExpressionItem.
The numeric value is stored as text in property INameExpressionItem Value in text format. Therefore, there is no limit on numeric value digits.
The expression evaluator that uses the Universal Expression Parser can convert the textual value in property Value of type INameExpressionItem in UniversalExpressionParser.ExpressionItems.INumericExpressionItem to a value of numeric type (int, long, double, etc.).
Examples of numeric value expression items are demonstrated below:
1// By default exponent notation can be used.
2println(-0.5e-3+.2exp3.4+3.E2.7+2.1EXP.3);
3println(.5e15*x);
4
5// Numeric values can have no limitations on the number of digits. The value is stored as text in
6// UniversalExpressionParser.ExpressionItems.INumericExpressionItem.
7// The text can be validated farther and converted to numeric values by the expression evaluator that
8// uses the parser.
9var x = 2.3*x+123456789123456789123456789123456789;
Click here to see the visualized instance of UniversalExpressionParser.IParseExpressionResult