@@ -903,7 +903,7 @@ protected virtual BindingFlags StaticBindingFlag
903
903
904
904
#endregion
905
905
906
- #region Custom and on the fly variables and methods
906
+ #region Custom and on the fly evaluation
907
907
908
908
/// <summary>
909
909
/// If set, this object is used to use it's fields, properties and methods as global variables and functions
@@ -959,12 +959,6 @@ public IDictionary<string, object> Variables
959
959
/// </summary>
960
960
public event EventHandler < FunctionEvaluationEventArg > EvaluateFunction ;
961
961
962
- /// <summary>
963
- /// Is fired if no indexing were found.
964
- /// Allow to define an indexing and the corresponding value on the fly.
965
- /// </summary>
966
- public event EventHandler < IndexingEvaluationEventArg > EvaluateIndexing ;
967
-
968
962
/// <summary>
969
963
/// Is fired when a parameter is not of the correct type for the function.
970
964
/// Allow to define a custom parameter cast to make the function call work on the fly.
@@ -1554,7 +1548,7 @@ public T Evaluate<T>(string expression)
1554
1548
EvaluateOperators ,
1555
1549
EvaluateChar ,
1556
1550
EvaluateParenthis ,
1557
- EvaluateIndexingOperator ,
1551
+ EvaluateIndexing ,
1558
1552
EvaluateString ,
1559
1553
EvaluateTernaryConditionalOperator ,
1560
1554
} ) ;
@@ -2644,7 +2638,7 @@ protected virtual void CorrectStackWithUnaryPlusOrMinusBeforeParenthisIfNecessar
2644
2638
}
2645
2639
}
2646
2640
2647
- protected virtual bool EvaluateIndexingOperator ( string expression , Stack < object > stack , ref int i )
2641
+ protected virtual bool EvaluateIndexing ( string expression , Stack < object > stack , ref int i )
2648
2642
{
2649
2643
if ( ! OptionIndexingActive )
2650
2644
return false ;
@@ -2700,58 +2694,69 @@ protected virtual bool EvaluateIndexingOperator(string expression, Stack<object>
2700
2694
return true ;
2701
2695
}
2702
2696
2703
- // IndexingPreEvaluationEventArg indexingPreEvaluationEventArg = new IndexingPreEvaluationEventArg(innerExp.ToString(), this, left);
2697
+ IndexingPreEvaluationEventArg indexingPreEvaluationEventArg = new IndexingPreEvaluationEventArg ( innerExp . ToString ( ) , this , left ) ;
2704
2698
2705
- // PreEvaluateIndexing?.Invoke(this, indexingPreEvaluationEventArg);
2699
+ PreEvaluateIndexing ? . Invoke ( this , indexingPreEvaluationEventArg ) ;
2706
2700
2707
- dynamic right = Evaluate ( innerExp . ToString ( ) ) ;
2708
- ExpressionOperator op = indexingBeginningMatch . Length == 2 ? ExpressionOperator . IndexingWithNullConditional : ExpressionOperator . Indexing ;
2701
+ if ( indexingPreEvaluationEventArg . CancelEvaluation )
2702
+ {
2703
+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "[{ left . GetType ( ) } ] can not be indexed.") ;
2704
+ }
2705
+ else if ( indexingPreEvaluationEventArg . HasValue )
2706
+ {
2707
+ stack . Push ( indexingPreEvaluationEventArg . Value ) ;
2708
+ }
2709
+ else
2710
+ {
2711
+ dynamic right = Evaluate ( innerExp . ToString ( ) ) ;
2712
+ ExpressionOperator op = indexingBeginningMatch . Length == 2 ? ExpressionOperator . IndexingWithNullConditional : ExpressionOperator . Indexing ;
2709
2713
2710
- if ( OptionForceIntegerNumbersEvaluationsAsDoubleByDefault && right is double && Regex . IsMatch ( innerExp . ToString ( ) , @"^\d+$" ) )
2711
- right = ( int ) right ;
2714
+ if ( OptionForceIntegerNumbersEvaluationsAsDoubleByDefault && right is double && Regex . IsMatch ( innerExp . ToString ( ) , @"^\d+$" ) )
2715
+ right = ( int ) right ;
2712
2716
2713
- Match assignationOrPostFixOperatorMatch = null ;
2717
+ Match assignationOrPostFixOperatorMatch = null ;
2714
2718
2715
- dynamic valueToPush = null ;
2719
+ dynamic valueToPush = null ;
2716
2720
2717
- if ( OptionIndexingAssignationActive && ( assignationOrPostFixOperatorMatch = assignationOrPostFixOperatorRegex . Match ( expression . Substring ( i + 1 ) ) ) . Success )
2718
- {
2719
- i += assignationOrPostFixOperatorMatch . Length + 1 ;
2721
+ if ( OptionIndexingAssignationActive && ( assignationOrPostFixOperatorMatch = assignationOrPostFixOperatorRegex . Match ( expression . Substring ( i + 1 ) ) ) . Success )
2722
+ {
2723
+ i += assignationOrPostFixOperatorMatch . Length + 1 ;
2720
2724
2721
- bool postFixOperator = assignationOrPostFixOperatorMatch . Groups [ "postfixOperator" ] . Success ;
2722
- string exceptionContext = postFixOperator ? "++ or -- operator" : "an assignation" ;
2725
+ bool postFixOperator = assignationOrPostFixOperatorMatch . Groups [ "postfixOperator" ] . Success ;
2726
+ string exceptionContext = postFixOperator ? "++ or -- operator" : "an assignation" ;
2723
2727
2724
- if ( stack . Count > 1 )
2725
- throw new ExpressionEvaluatorSyntaxErrorException ( $ "The left part of { exceptionContext } must be a variable, a property or an indexer.") ;
2728
+ if ( stack . Count > 1 )
2729
+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "The left part of { exceptionContext } must be a variable, a property or an indexer.") ;
2726
2730
2727
- if ( op == ExpressionOperator . IndexingWithNullConditional )
2728
- throw new ExpressionEvaluatorSyntaxErrorException ( $ "Null conditional is not usable left to { exceptionContext } ") ;
2731
+ if ( op == ExpressionOperator . IndexingWithNullConditional )
2732
+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "Null conditional is not usable left to { exceptionContext } ") ;
2729
2733
2730
- if ( postFixOperator )
2731
- {
2732
- if ( left is IDictionary < string , object > dictionaryLeft )
2733
- valueToPush = assignationOrPostFixOperatorMatch . Groups [ "postfixOperator" ] . Value . Equals ( "++" ) ? dictionaryLeft [ right ] ++ : dictionaryLeft [ right ] -- ;
2734
+ if ( postFixOperator )
2735
+ {
2736
+ if ( left is IDictionary < string , object > dictionaryLeft )
2737
+ valueToPush = assignationOrPostFixOperatorMatch . Groups [ "postfixOperator" ] . Value . Equals ( "++" ) ? dictionaryLeft [ right ] ++ : dictionaryLeft [ right ] -- ;
2738
+ else
2739
+ valueToPush = assignationOrPostFixOperatorMatch . Groups [ "postfixOperator" ] . Value . Equals ( "++" ) ? left [ right ] ++ : left [ right ] -- ;
2740
+ }
2734
2741
else
2735
- valueToPush = assignationOrPostFixOperatorMatch . Groups [ "postfixOperator" ] . Value . Equals ( "++" ) ? left [ right ] ++ : left [ right ] -- ;
2742
+ {
2743
+ valueToPush = ManageKindOfAssignation ( expression , ref i , assignationOrPostFixOperatorMatch , ( ) => OperatorsEvaluations [ 0 ] [ op ] ( left , right ) ) ;
2744
+
2745
+ if ( left is IDictionary < string , object > dictionaryLeft )
2746
+ dictionaryLeft [ right ] = valueToPush ;
2747
+ else
2748
+ left [ right ] = valueToPush ;
2749
+
2750
+ stack . Clear ( ) ;
2751
+ }
2736
2752
}
2737
2753
else
2738
2754
{
2739
- valueToPush = ManageKindOfAssignation ( expression , ref i , assignationOrPostFixOperatorMatch , ( ) => OperatorsEvaluations [ 0 ] [ op ] ( left , right ) ) ;
2740
-
2741
- if ( left is IDictionary < string , object > dictionaryLeft )
2742
- dictionaryLeft [ right ] = valueToPush ;
2743
- else
2744
- left [ right ] = valueToPush ;
2745
-
2746
- stack . Clear ( ) ;
2755
+ valueToPush = OperatorsEvaluations [ 0 ] [ op ] ( left , right ) ;
2747
2756
}
2748
- }
2749
- else
2750
- {
2751
- valueToPush = OperatorsEvaluations [ 0 ] [ op ] ( left , right ) ;
2752
- }
2753
2757
2754
- stack . Push ( valueToPush ) ;
2758
+ stack . Push ( valueToPush ) ;
2759
+ }
2755
2760
2756
2761
return true ;
2757
2762
}
@@ -4428,12 +4433,13 @@ public FunctionPreEvaluationEventArg(string name, List<string> args = null, Expr
4428
4433
public bool CancelEvaluation { get ; set ; }
4429
4434
}
4430
4435
4436
+
4431
4437
/// <summary>
4432
4438
/// Infos about the indexing that is currently evaluate
4433
4439
/// </summary>
4434
- public partial class IndexingEvaluationEventArg : EventArgs
4440
+ public partial class IndexingPreEvaluationEventArg : EventArgs
4435
4441
{
4436
- public IndexingEvaluationEventArg ( string arg , ExpressionEvaluator evaluator , object onInstance )
4442
+ public IndexingPreEvaluationEventArg ( string arg , ExpressionEvaluator evaluator , object onInstance )
4437
4443
{
4438
4444
Arg = arg ;
4439
4445
This = onInstance ;
@@ -4492,16 +4498,6 @@ public T EvaluateArg<T>()
4492
4498
{
4493
4499
return Evaluator . Evaluate < T > ( Arg ) ;
4494
4500
}
4495
- }
4496
-
4497
- /// <summary>
4498
- /// Infos about the indexing that is currently evaluate
4499
- /// </summary>
4500
- public partial class IndexingPreEvaluationEventArg : IndexingEvaluationEventArg
4501
- {
4502
- public IndexingPreEvaluationEventArg ( string arg , ExpressionEvaluator evaluator , object onInstance )
4503
- : base ( arg , evaluator , onInstance )
4504
- { }
4505
4501
4506
4502
/// <summary>
4507
4503
/// If set to true cancel the evaluation of the current function or method and throw an exception that the function does not exists
0 commit comments