Please allow bracket in parameter name
Opened this issue · 3 comments
Thaina commented
I wish I could write parameter name with bracket like this
var array = new float[] { 1,2,3,4,5,6 };
var e = new Expression("1 + (2 * [array[0]])");
e.EvaluateParameter += delegate(string name, ParameterArgs args) {
// got "array[0]" as name
if (name.StartsWith("array[") && name.EndsWith("]"))
{
int i = int.Parse(name.Replace("array[","").Replace("]",""));
args.Result = array[i];
}
};
If possible I would like to have it allow arbitrary nested bracket
var keys = new string[] { key0,key1,key2 };
var dict = new Dictionary<string,float>();
var e = new Expression("1 + (2 * [dict[key[0]]])");
e.EvaluateParameter += delegate(string name, ParameterArgs args) {
// got "dict[key[0]]" as name for doing lookup
};
Thaina commented
Will changing this
Lines 265 to 268 in 19359d6
To this
NAME : '[' ( (options {greedy=false;} : ~('[' | ']')*) NAME* )* ']'
;
Did the job?
Thaina commented
Today I have clone your repo and change it to antlr4
This rule work for me
NAME : '[' (~('[' | ']') | NAME)*? ']'
;
If you interest https://github.com/Thaina/NCalc2
Bykiev commented
@Thaina, I believe it'll be better to use EvaluateOptions.IterateParameters
like this:
NCalc2/test/NCalc.Tests/Fixtures.cs
Lines 655 to 664 in f8e42df