Cannot evaluate expression "0.25" - Unexpected token "25" <INTEGER>; expected one of <IDENTIFIER>

Jun 1, 2010 at 9:18 AM
Hello,
I'm using the Flee library in one of my projects and I'm excited about all the possibilities!
However, I have one problem, using decimals as literals, the following unittest demonstrates exactly what goes wrong...
I'm using version 0.9.26.0 and I'm working with VS 2010 in a Test project with .NET Framework 4.0
 
        /// <summary>
        /// Test parsing a double
        /// </summary>
        [TestMethod]
        public void SimpleDoubleExpressionParseTest()
        {
            // Allow the expression to use all static public methods of System.Math
            var OurExpressionContext = new ExpressionContext();
            OurExpressionContext.Imports.AddType(typeof(Math));
            OurExpressionContext.ParserOptions.DecimalSeparator = '.';
            OurExpressionContext.ParserOptions.RequireDigitsBeforeDecimalPoint = true;
            OurExpressionContext.Options.IntegersAsDoubles = true;
            OurExpressionContext.Options.RealLiteralDataType = RealLiteralDataType.Double;
            OurExpressionContext.Options.ResultType = typeof(double);

            var compiledExpression = OurExpressionContext.CompileGeneric<double>("0.25");

            double actual = compiledExpression.Evaluate();
            double expected = 0.25;
            Assert.AreEqual(expected, actual);
        }
If I run this test, I get the following exception:
Errormessage:
Test method TDABC.Test.Modules.FormulaExpressionTest.SimpleDoubleExpressionParseTest threw exception: 
Ciloci.Flee.ExpressionCompileException: SyntaxError: Unexpected token "25" <INTEGER>; expected one of <IDENTIFIER>
Line: 1, Column: 3
 ---> Ciloci.Flee.PerCederberg.Grammatica.Runtime.ParserLogException: Unexpected token "25" <INTEGER>; expected one of <IDENTIFIER>
Line: 1, Column: 3
Exception:
Ciloci.Flee.PerCederberg.Grammatica.Runtime.Parser.Parse()
Ciloci.Flee.ExpressionContext.DoParse()
Ciloci.Flee.ExpressionContext.DoParse()
Ciloci.Flee.ExpressionContext.Parse(String expression, IServiceProvider services)
Ciloci.Flee.Expression`1.Compile(String expression, ExpressionOptions options)
Ciloci.Flee.Expression`1..ctor(String expression, ExpressionContext context, Boolean isGeneric)
TDABC.Test.Modules.FormulaExpressionTest.SimpleDoubleExpressionParseTest() in K:\MyProjects\Test.cs: line 103
 
Can anyone please help me out? I can't imagine that this type of expressions would not be possible to evaluate.
Thanks in advance!
Best regards,
Jeroen
 

Jun 1, 2010 at 9:28 AM

Further investigation shows that it looks like the decimal separator is the problem here.
I'm working in The Netherlands on a Dutch decimal separator (comma) and thousand separator (dot).
The following unittest works well, using all default settings:

        /// <summary>
        /// Test parsing a double
        /// </summary>
        [TestMethod]
        public void SimpleDoubleExpressionParseTest()
        {
            // Allow the expression to use all static public methods of System.Math
            var OurExpressionContext = new ExpressionContext();
            var compiledExpression = OurExpressionContext.CompileGeneric<double>("0,25");

            var actual = compiledExpression.Evaluate();
            double expected = 0.25;
            Assert.AreEqual(expected, actual);
        }
When I add the line to set the decimal separator to a dot, it starts failing.
However, I would like to set a fixed separator instead of depending on the local settings.
Looks like a bug that needs fixing but I don't know where to look for it...
Jun 1, 2010 at 9:50 AM

You must set the culture.

I.e. OurExpressionContext.Options.ParseCulture = Globalization.CultureInfo.InvariantCulture

 

Jun 1, 2010 at 10:27 AM

 

Thanks! That fixed it indeed.

Best regards, Jeroen