This project has moved and is read-only. For the latest updates, please go here.

"m_vars" is not a valid variable name?

Sep 28, 2013 at 6:17 PM
string parsestr = "(_vars / 4 * 3) + (_vars / (2 + 5))";
var p = new CompiledExpression(parsestr);
p.RegisterType("_vars", 365.00);
Console.WriteLine("Result: {0}", p.Eval());
Parser error at position 2: Unknown type or identifier 'm'
string parsestr = "(m_vars / 4 * 3) + (m_vars / (2 + 5))";
var p = new CompiledExpression(parsestr);
p.RegisterType("m_vars", 365.00);
Console.WriteLine("Result: {0}", p.Eval());
Is this a bug?
Nov 10, 2013 at 4:37 PM
Edited Nov 10, 2013 at 4:41 PM
Had a project that needed a quick parser for and don't have a lot of time to dig deeper to find out what the underscore is treated special for, but here is a quick work around if you are using source as I couldn't find anything at first glance that would give rise to the idea it is a special directive or anything in the current code.

I found a reference in Parcer.cs that appears to actually attempt to consider the underscore as just another character
In Parcer.cs find the line (at current it is line 96):
else if (HelperMethods.IsAlpha(_pstr[_ptr]) || (_pstr[_ptr] == '_')
At first I just removed the (_pstr[ptr] == '') but still ended up with the same message. This is because in the next loop we are not looking for the underscore.
while (IsInBounds() && (HelperMethods.IsAlpha(_pstr[_ptr]) || HelperMethods.IsNumeric(_pstr, _ptr)))
To fix the issue just make the IsAlpha in HelperMethods.cs return true for underscores like the below snip:
public static bool IsAlpha(char chr)
            return (chr >= 'A' & chr <= 'Z') || (chr >= 'a' & chr <= 'z') || chr == '_';
I am sure there is a better way to do this, but like I said needed a quick fix and have to move on so it is a hack. Not sure if the underscore was intended to be a parsing directive that could be placed in strings, but would be a bad idea since underscores are common in property names. Think that a more complete check should be done against valid characters that would include any valid object property value. Regular expression validation possibly that would check alphanumeric and special character cases in property names.
If the underscore would be used as a directive in the future I think that the ability to at least escape the underscore to ignore would be ideal at a minimum.

Just want to add a thanks to the developer... Nice work and very helpful...
Feb 1, 2014 at 9:56 AM
Hi, the _ in variable names has been fixed, thanks!