dynamicexpresso / DynamicExpresso

C# expressions interpreter
http://dynamic-expresso.azurewebsites.net/
MIT License
1.99k stars 374 forks source link

Parsing exception while using 'as' operator #287

Closed pawel1708hp closed 1 year ago

pawel1708hp commented 1 year ago

I'm trying to use following expression:

string.IsNullOrEmpty(variable as string)

but it results with exception: DynamicExpresso.Exceptions.ParseException: ')' or ',' expected (at index 40).

Adding extra parenthesis as message suggests solves the issue, but the expression is obviously wrong then string.IsNullOrEmpty(variable as string))

Seems there is something wrong with parser.

metoule commented 1 year ago

I think we don't support dynamic casts yet (the expr as Type expression). PRs welcome :)

pawel1708hp commented 1 year ago

Well, according to documentation it is supported ;) image

IAMJDA commented 1 year ago

We have the exact issue since upgrading to a newer version. Expressions which worked before, now break.

davideicardi commented 1 year ago

It could be a regression. could you help us by telling us from which version it stopped working?

IAMJDA commented 1 year ago

Before: 2.8.1.0 After: 2.10.0.0

Test for As_Operator: Assert.AreEqual(string.IsNullOrEmpty(a as string), target.Eval("string.IsNullOrEmpty(a as string)"));

IAMJDA commented 1 year ago

Manual bisect:

178 seems to be to blame

metoule commented 1 year ago

@IAMJDA thanks for narrowing it down! It's indeed a regression in the way the types are parsed: we don't reset the parser position properly if the type has no modifier. I'll need to ensure it's working for all the possible type modifiers. For example:

string?
string?[]
string?[][]
IEnumerable<string>
IEnumerable<string?[]>?[]

but it shouldn't take too long 🤞