Exemple #1
0
        public void verify_methods_ambiguity()
        {
            // since arguments types are not taken under consideration for methods overloading, following logic should fail
            var parser = new Parser();

            parser.AddFunction <int, string>("Whoami", i => string.Format("utility method {0}", i));
            parser.AddFunction <string, string>("Whoami", s => string.Format("utility method {0}", s));

            parser.AddFunction <string, string, string>("Glue", (s1, s2) => string.Concat(s1, s2));
            parser.AddFunction <int, int, string>("Glue", (i1, i2) => string.Concat(i1, i2));

            try
            {
                Assert.IsTrue(parser.Parse <object>("Whoami(0) == 'utility method 0'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    "Parse error: Function Whoami accepting 1 parameter is ambiguous.",
                    e.Message);
            }

            try
            {
                Assert.IsTrue(parser.Parse <object>("Glue('a', 'b') == 'ab'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    "Parse error: Function Glue accepting 2 parameters is ambiguous.",
                    e.Message);
            }

            // not only built-in, but also context extracted methods are subjected to the same rules
            parser = new Parser();
            var model = new ModelWithAmbiguousMethods();

            try
            {
                Assert.IsTrue(parser.Parse <ModelWithAmbiguousMethods>("Whoami(0) == 'model method 0'").Invoke(model));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.AreEqual(
                    "Parse error: Function Whoami accepting 1 parameter is ambiguous.",
                    e.Message);
            }
        }
Exemple #2
0
        public void verify_methods_overloading() // overloading concept exists, when there are two methods of the same name, but different signature
        {
            // methods overloading is based on the number of arguments
            var parser = new Parser();

            parser.AddFunction("Whoami", () => "utility method");
            parser.AddFunction <int, string>("Whoami", i => string.Format("utility method {0}", i));
            parser.AddFunction <int, string, string>("Whoami", (i, s) => string.Format("utility method {0} - {1}", i, s));

            Assert.IsTrue(parser.Parse <object>("Whoami() == 'utility method'").Invoke(null));
            Assert.IsTrue(parser.Parse <object>("Whoami(1) == 'utility method 1'").Invoke(null));
            Assert.IsTrue(parser.Parse <object>("Whoami(2, 'final') == 'utility method 2 - final'").Invoke(null));
        }
        public void verify_methods_overloading()
        {
            // methods overloading is based on the number of arguments
            var parser = new Parser();

            parser.AddFunction("Whoami", () => "utility method");
            parser.AddFunction <int, string>("Whoami", i => string.Format("utility method {0}", i));
            parser.AddFunction <int, string, string>("Whoami", (i, s) => string.Format("utility method {0} - {1}", i, s));

            Assert.IsTrue(parser.Parse <object>("Whoami() == 'utility method'").Invoke(null));
            Assert.IsTrue(parser.Parse <object>("Whoami(1) == 'utility method 1'").Invoke(null));
            Assert.IsTrue(parser.Parse <object>("Whoami(2, 'final') == 'utility method 2 - final'").Invoke(null));
        }
        public void verify_methods_overriding()
        {
            var parser = new Parser();

            // register utility methods
            parser.AddFunction("Whoami", () => "utility method");
            parser.AddFunction <int, string>("Whoami", i => string.Format("utility method {0}", i));

            var model = new ModelWithMethods();

            // redefined model methods take precedence
            Assert.IsTrue(parser.Parse <ModelWithMethods>("Whoami() == 'model method'").Invoke(model));
            Assert.IsTrue(parser.Parse <ModelWithMethods>("Whoami(1) == 'model method 1'").Invoke(model));
        }
Exemple #5
0
        public void verify_methods_overriding() // overriding concept exists, when there are two methods of the same name and signature, but different implementation
        {
            var parser = new Parser();

            // register utility methods
            parser.AddFunction("Whoami", () => "utility method");
            parser.AddFunction <int, string>("Whoami", i => string.Format("utility method {0}", i));

            var model = new ModelWithMethods();

            // redefined model methods take precedence
            Assert.IsTrue(parser.Parse <ModelWithMethods>("Whoami() == 'model method'").Invoke(model));
            Assert.IsTrue(parser.Parse <ModelWithMethods>("Whoami(1) == 'model method 1'").Invoke(model));
        }
        public void verify_invalid_arguments_conversion()
        {
            var parser = new Parser();

            parser.AddFunction <object, string>("Whoami", o => string.Format("utility method {0}", o));
            parser.AddFunction <int, string, string>("Whoami", (i, s) => string.Format("utility method {0} - {1}", i, s));

            Assert.IsTrue(parser.Parse <object>("Whoami('0') == 'utility method 0'").Invoke(null));        // successful conversion from String to Object
            Assert.IsTrue(parser.Parse <object>("Whoami(1, '2') == 'utility method 1 - 2'").Invoke(null)); // types matched, no conversion needed

            try
            {
                Assert.IsTrue(parser.Parse <object>("Whoami('1', '2') == 'utility method 1 - 2'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    "Parsing failed. Invalid expression: Whoami('1', '2') == 'utility method 1 - 2'",
                    e.Message);

                Assert.IsNotNull(e.InnerException);
                Assert.IsTrue(e.InnerException is InvalidOperationException);
                Assert.AreEqual(
                    "Argument 0 type conversion from String to needed Int32 failed.",
                    e.InnerException.Message);
            }

            try
            {
                Assert.IsTrue(parser.Parse <object>("Whoami(1, 2) == 'utility method 1 - 2'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    "Parsing failed. Invalid expression: Whoami(1, 2) == 'utility method 1 - 2'",
                    e.Message);

                Assert.IsNotNull(e.InnerException);
                Assert.IsTrue(e.InnerException is InvalidOperationException);
                Assert.AreEqual(
                    "Argument 1 type conversion from Int32 to needed String failed.",
                    e.InnerException.Message);
            }
        }
Exemple #7
0
        public void CustomFunctions_Test()
        {
            var parser = new Parser();

            parser.AddFunction("sqr", calc);
            parser.AddFunction("add", (a, b) => a + b);
            parser.AddFunction("av", (a, b, c) => (a + b + c) / 3);
            parser.AddFunction("sumv", calc2);
            parser.AddFunction("circ", "pi*r^2", "r");
            Assert.IsTrue(Array.IndexOf(parser.CustomFunctions, "add") > -1);
            Assert.AreEqual(64516, parser.Parse("sqr254").Evaluate());
            Assert.ThrowsException <ParserFunctionArgumentException>(() => parser.Parse("add(1, 2, 3)"));
            Assert.AreEqual(20, parser.Parse("av(32, 22, 6)").Evaluate());
            Assert.AreEqual(12.9, parser.Parse("sumv(7, -4, -2.5, 12.4)").Evaluate());
            Assert.AreEqual(Math.PI * 295.84, parser.Parse("circ(17.2)").Evaluate());
        }
Exemple #8
0
        public void ArgumentException_InvalidName(string name)
        {
            var parser = new Parser();

            Assert.ThrowsException <ArgumentException>(() => parser.AddFunction(name, "2*x", "x"));
            Assert.ThrowsException <ArgumentException>(() => parser.AddVariable(name));
        }
Exemple #9
0
        public static string Iterative(string sfx, string sfdx, double a, double b, out int count)
        {
            Parser fx = new Parser(sfx);
            fx.AddFunction<Algorithms>("log");
            fx.Compile();
            Parser fdx = new Parser(sfdx);
            fdx.AddFunction<Algorithms>("log");
            fdx.Compile();

            double x0 = (a + b)/2;
            if (x0 == 0)
            {
                x0 = (b + x0)/2;
            }
            fdx.Variables.Add("x", x0);

            double lambda = -1 / fdx.Calculate();

            count = 0;
            fx.Variables.Add("x", x0);
            double xn = x0 + lambda*fx.Calculate();

            while ((xn - x0) > eps)
            {
                x0 = xn;
                fx.Variables["x"] = xn;
                xn = xn + lambda * fx.Calculate();
                ++count;
            }

            return xn.ToString();
        }
Exemple #10
0
        public void ArgumentException_AlreadyExists()
        {
            var parser = new Parser("x");

            Assert.ThrowsException <ArgumentException>(() => parser.AddVariable("x"));
            Assert.ThrowsException <ArgumentException>(() => parser.AddVariable("sin"));
            Assert.ThrowsException <ArgumentException>(() => parser.AddFunction("pi", "2*r", "r"));
        }
Exemple #11
0
        public void SeparatorStyle_Test()
        {
            Parser parser = new Parser();

            parser.AddFunction("add", calc2);
            Assert.AreEqual(1018.65, parser.Parse("add(5.5, 1000.5, 12.65)").Evaluate());
            parser.SeparatorStyle = SeparatorStyle.Comma;
            Assert.AreEqual(1018.65, parser.Parse("add(5,5; 1000,5; 12,65)").Evaluate());
        }
Exemple #12
0
        public void IsDefined_Test()
        {
            var parser = new Parser("x");

            parser.AddFunction("sqr", "x^2", "x");
            Assert.IsTrue(parser.IsDefined("x"));
            Assert.IsTrue(parser.IsDefined("sqr"));
            Assert.IsTrue(parser.IsDefined("sin"));
            Assert.IsFalse(parser.IsDefined("y"));
        }
Exemple #13
0
 public static double Parse(string input)
 {
     Parser OMG = new Parser(input);
      OMG.AddFunction<Agent>("F");
      OMG.Compile();
      OMG.Variables.Add("x",2);
      OMG.Variables.Add("y", 2);
      OMG.Variables.Add("z", 2);
      return OMG.Calculate();
 }
 private static void ConfigureParser()
 {
     Parser.AddFunction("pi", new PiFunction());
     Parser.AddFunction("sin", new SinFunction());
     Parser.AddFunction("cos", new CosFunction());
     Parser.AddFunction("tg", new TangentFunction());
     Parser.AddFunction("abs", new AbsFunction());
     Parser.AddFunction("pow", new PowFunction());
     Parser.AddFunction("sqrt", new SqrtFunction());
     Parser.AddFunction("exp", new ExpFunction());
 }
Exemple #15
0
        public void VariablesFunctionsSeparation_Test()
        {
            var parser = new Parser("x", "y", "z", "a");

            Assert.IsTrue(Array.IndexOf(parser.CustomFunctions, "z") == -1);
            Assert.ThrowsException <ArgumentException>(() => parser.RemoveFunction("x"));
            Assert.IsTrue(Array.IndexOf(parser.CustomVariables, "x") > -1);
            parser.AddFunction("fun", "2*x", "x");
            Assert.IsNull(parser.GetVariable("fun"));
            parser.RemoveAllVariables();
            Assert.IsTrue(Array.IndexOf(parser.CustomFunctions, "fun") > -1);
        }
Exemple #16
0
        public void RemoveFunctions_Test()
        {
            string[] vars   = new string[] { "a", "b", "c", "d", "xyz", "alpha" };
            var      parser = new Parser();

            foreach (var s in vars)
            {
                parser.AddFunction(s, calc);
            }
            parser.RemoveFunction("xyz");
            Assert.IsFalse(Array.IndexOf(parser.CustomFunctions, "xyz") > -1);
            Assert.ThrowsException <ArgumentException>(() => parser.RemoveFunction("xyz"));
            parser.RemoveAllFunctions();
            foreach (string var in vars)
            {
                Assert.IsFalse(Array.IndexOf(parser.CustomFunctions, var) > -1);
            }
        }
Exemple #17
0
        public static string Chord(string sfx, string sfdx, double a, double b, out int count)
        {
            Parser fx = new Parser(sfx);
            fx.AddFunction<Algorithms>("log");
            fx.Compile();
            Parser fdx = new Parser(sfdx);
            fdx.AddFunction<Algorithms>("log");
            fdx.Compile();

            fx.Variables["x"] = a;
            fdx.Variables["x"] = a;
            double a_fpt = fx.Calculate()*fdx.Calculate();

            fx.Variables["x"] = b;
            fdx.Variables["x"] = b;
            double b_fpt = fx.Calculate()*fdx.Calculate();

            double fixedPoint = a_fpt > 0 ? a : b;

            double xnm1 = (a + b) / 2;
            fx.Variables["x"] = xnm1;
            double fx_r = fx.Calculate();
            fx.Variables["x"] = fixedPoint;
            double fx0_r = fx.Calculate();
            double xn = xnm1 - (xnm1 - fixedPoint)/(fx_r - fx0_r)*fx_r;

            count = 0;

            while ((xn - xnm1) > eps)
            {
                xnm1 = xn;

                fx.Variables["x"] = xn;
                fx_r = fx.Calculate();

                xn = xn - (xn - fixedPoint) / (fx_r - fx0_r) * fx_r;
                ++count;
            }

            return xn.ToString();
        }
Exemple #18
0
        public void verify_short_circuit_evaluation()
        {
            var parser = new Parser();

            parser.AddFunction <object, bool>("CastToBool", obj => (bool)obj);

            try
            {
                parser.Parse <object>("CastToBool(null)").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is NullReferenceException);
            }

            // below, the exception should not be thrown as above
            // reason? - first argument is suffient to determine the value of the expression so the second one is not going to be evaluated
            Assert.IsFalse(parser.Parse <object>("false && CastToBool(null)").Invoke(null));
            Assert.IsTrue(parser.Parse <object>("true || CastToBool(null)").Invoke(null));
        }
        public void verify_short_circuit_evaluation()
        {
            var parser = new Parser();
            parser.AddFunction<object, bool>("CastToBool", obj => (bool) obj);

            try
            {
                parser.Parse<object>("CastToBool(null)").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is NullReferenceException);
            }

            // below, the exception should not be thrown as above
            // reason? - first argument is suffient to determine the value of the expression so the second one is not going to be evaluated
            Assert.IsFalse(parser.Parse<object>("false && CastToBool(null)").Invoke(null));
            Assert.IsTrue(parser.Parse<object>("true || CastToBool(null)").Invoke(null));
        }
        public void verify_methods_overriding()
        {
            var parser = new Parser();

            // register utility methods
            parser.AddFunction("Whoami", () => "utility method");
            parser.AddFunction<int, string>("Whoami", i => string.Format("utility method {0}", i));

            var model = new ModelWithMethods();

            // redefined model methods take precedence
            Assert.IsTrue(parser.Parse<ModelWithMethods>("Whoami() == 'model method'").Invoke(model));
            Assert.IsTrue(parser.Parse<ModelWithMethods>("Whoami(1) == 'model method 1'").Invoke(model));
        }
        public void verify_methods_overloading()
        {
            // methods overloading is based on the number of arguments
            var parser = new Parser();
            parser.AddFunction("Whoami", () => "utility method");
            parser.AddFunction<int, string>("Whoami", i => string.Format("utility method {0}", i));
            parser.AddFunction<int, string, string>("Whoami", (i, s) => string.Format("utility method {0} - {1}", i, s));

            Assert.IsTrue(parser.Parse<object>("Whoami() == 'utility method'").Invoke(null));
            Assert.IsTrue(parser.Parse<object>("Whoami(1) == 'utility method 1'").Invoke(null));
            Assert.IsTrue(parser.Parse<object>("Whoami(2, 'final') == 'utility method 2 - final'").Invoke(null));
        }
        public void verify_methods_ambiguity()
        {
            // since arguments types are not taken under consideration for methods overloading, following logic should fail
            var parser = new Parser();
            parser.AddFunction<int, string>("Whoami", i => string.Format("utility method {0}", i));
            parser.AddFunction<string, string>("Whoami", s => string.Format("utility method {0}", s));

            parser.AddFunction<string, string, string>("Glue", (s1, s2) => string.Concat(s1, s2));
            parser.AddFunction<int, int, string>("Glue", (i1, i2) => string.Concat(i1, i2));

            try
            {
                Assert.IsTrue(parser.Parse<object>("Whoami(0) == 'utility method 0'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... Whoami(0) == 'utility method 0' ...
            ^--- Function 'Whoami' accepting 1 argument is ambiguous.",
                    e.Message);
            }

            try
            {
                Assert.IsTrue(parser.Parse<object>("Glue('a', 'b') == 'ab'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... Glue('a', 'b') == 'ab' ...
            ^--- Function 'Glue' accepting 2 arguments is ambiguous.",
                    e.Message);
            }

            // not only built-in, but also context extracted methods are subjected to the same rules
            parser = new Parser();
            var model = new ModelWithAmbiguousMethods();

            try
            {
                Assert.IsTrue(parser.Parse<ModelWithAmbiguousMethods>("Whoami(0) == 'model method 0'").Invoke(model));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... Whoami(0) == 'model method 0' ...
            ^--- Function 'Whoami' accepting 1 argument is ambiguous.",
                    e.Message);
            }
        }
        public void verify_implicit_type_conversion()
        {
            var parser = new Parser();
            parser.AddFunction<object, string>("Whoami", o => string.Format("utility method {0}", o));
            parser.AddFunction<int, string, string>("Whoami", (i, s) => string.Format("utility method {0} - {1}", i, s));

            Assert.IsTrue(parser.Parse<object>("Whoami('0') == 'utility method 0'").Invoke(null)); // successful conversion from String to Object
            Assert.IsTrue(parser.Parse<object>("Whoami(1, '2') == 'utility method 1 - 2'").Invoke(null)); // types matched, no conversion needed

            try
            {
                Assert.IsTrue(parser.Parse<object>("Whoami('1', '2') == 'utility method 1 - 2'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 8:
            ... '1', '2') == 'utility method 1 - 2' ...
            ^--- Function 'Whoami' 1st argument implicit conversion from 'System.String' to expected 'System.Int32' failed.",
                    e.Message);
            }

            try
            {
                Assert.IsTrue(parser.Parse<object>("Whoami(1, 2) == 'utility method 1 - 2'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 11:
            ... 2) == 'utility method 1 - 2' ...
            ^--- Function 'Whoami' 2nd argument implicit conversion from 'System.Int32' to expected 'System.String' failed.",
                    e.Message);
            }
        }
Exemple #24
0
 static void command_func(string[] args)
 {
     string[] funcArgs = args[2].Split(',');
     parser.AddFunction(args[0], args[1], funcArgs);
 }
        public void verify_various_parsing_errors()
        {
            var parser = new Parser();
            parser.RegisterMethods();
            parser.AddFunction<int, int, int>("Max", (x, y) => Math.Max(x, y));

            try
            {
                parser.Parse<object>("1++ +1==2").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 2:
            ... ++ +1==2 ...
            ^--- Unexpected token: '++'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("true # false").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... # false ...
            ^--- Invalid token.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("'abc' - 'abc'").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... - 'abc' ...
            ^--- Operator '-' cannot be applied to operands of type 'System.String' and 'System.String'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("0 + null").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 3:
            ... + null ...
            ^--- Operator '+' cannot be applied to operands of type 'System.Int32' and 'null'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("0 / null").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 3:
            ... / null ...
            ^--- Operator '/' cannot be applied to operands of type 'System.Int32' and 'null'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("true && null").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... && null ...
            ^--- Operator '&&' cannot be applied to operands of type 'System.Boolean' and 'null'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("true || null").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... || null ...
            ^--- Operator '||' cannot be applied to operands of type 'System.Boolean' and 'null'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("!null").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... !null ...
            ^--- Operator '!' cannot be applied to operand of type 'null'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("'abc' * 'abc'").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... * 'abc' ...
            ^--- Operator '*' cannot be applied to operands of type 'System.String' and 'System.String'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("1 + 2 + 'abc' - 'abc' > 0").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 15:
            ... - 'abc' > 0 ...
            ^--- Operator '-' cannot be applied to operands of type 'System.String' and 'System.String'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>(
                    @"1 - 2
            - (6 / ((2*'1.5' - 1) + 1)) * -2
            + 1/2/1 == 3.50").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 2, column 15:
            ... *'1.5' - 1) + 1)) * -2 ...
            ^--- Operator '*' cannot be applied to operands of type 'System.Int32' and 'System.String'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>(
                    @"1 - 2
            - 6
            + 1/x/1 == 3.50").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 3, column 9:
            ... x/1 == 3.50 ...
            ^--- Only public properties, constants and enums are accepted. Identifier 'x' not known.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("WriteLine('hello')").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... WriteLine('hello') ...
            ^--- Function 'WriteLine' not known.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("Max(1.1)").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... Max(1.1) ...
            ^--- Function 'Max' accepting 1 argument not found.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("Max(1.1, 1.2, 'a')").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... Max(1.1, 1.2, 'a') ...
            ^--- Function 'Max' accepting 3 arguments not found.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>(
                    @"Max(1,
              Max(1, 'a')) == 1.1").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 2, column 14:
            ... 'a')) == 1.1 ...
            ^--- Function 'Max' 2nd argument implicit conversion from 'System.String' to expected 'System.Int32' failed.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("Now() && Today()").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... && Today() ...
            ^--- Operator '&&' cannot be applied to operands of type 'System.DateTime' and 'System.DateTime'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("'a' >= 'b'").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 5:
            ... >= 'b' ...
            ^--- Operator '>=' cannot be applied to operands of type 'System.String' and 'System.String'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("!'a'").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... !'a' ...
            ^--- Operator '!' cannot be applied to operand of type 'System.String'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("!! Today()").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 2:
            ... ! Today() ...
            ^--- Operator '!' cannot be applied to operand of type 'System.DateTime'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("0 == '0'").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 3:
            ... == '0' ...
            ^--- Operator '==' cannot be applied to operands of type 'System.Int32' and 'System.String'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("0.1 != '0'").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 5:
            ... != '0' ...
            ^--- Operator '!=' cannot be applied to operands of type 'System.Double' and 'System.String'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("'asd' > null").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... > null ...
            ^--- Operator '>' cannot be applied to operands of type 'System.String' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("Date + Date").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... + Date ...
            ^--- Operator '+' cannot be applied to operands of type 'System.DateTime' and 'System.DateTime'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("NDate + NDate").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... + NDate ...
            ^--- Operator '+' cannot be applied to operands of type 'System.Nullable`1[System.DateTime]' and 'System.Nullable`1[System.DateTime]'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("Date + NDate").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... + NDate ...
            ^--- Operator '+' cannot be applied to operands of type 'System.DateTime' and 'System.Nullable`1[System.DateTime]'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("Date == 0").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... == 0 ...
            ^--- Operator '==' cannot be applied to operands of type 'System.DateTime' and 'System.Int32'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("Date != 'asd'").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... != 'asd' ...
            ^--- Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'System.String'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("Date > 0").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... > 0 ...
            ^--- Operator '>' cannot be applied to operands of type 'System.DateTime' and 'System.Int32'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("Date > null").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... > null ...
            ^--- Operator '>' cannot be applied to operands of type 'System.DateTime' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("Date > SubModelObject").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... > SubModelObject ...
            ^--- Operator '>' cannot be applied to operands of type 'System.DateTime' and 'System.Object'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("NDate > SubModelObject").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... > SubModelObject ...
            ^--- Operator '>' cannot be applied to operands of type 'System.Nullable`1[System.DateTime]' and 'System.Object'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("NDate > null").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... > null ...
            ^--- Operator '>' cannot be applied to operands of type 'System.Nullable`1[System.DateTime]' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("NDate != 'asd'").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... != 'asd' ...
            ^--- Operator '!=' cannot be applied to operands of type 'System.Nullable`1[System.DateTime]' and 'System.String'.",
                    e.Message);
            }

            try
            {
                var bag = new Bag {Lexer = new Lexer(), Parser = new Parser()};
                parser.Parse<Bag>("Lexer != Parser").Invoke(bag);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... != Parser ...
            ^--- Operator '!=' cannot be applied to operands of type 'ExpressiveAnnotations.Analysis.Lexer' and 'ExpressiveAnnotations.Analysis.Parser'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("SubModelObject > SubModelObject").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 16:
            ... > SubModelObject ...
            ^--- Operator '>' cannot be applied to operands of type 'System.Object' and 'System.Object'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("null > null").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... > null ...
            ^--- Operator '>' cannot be applied to operands of type 'null' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("null + null").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... + null ...
            ^--- Operator '+' cannot be applied to operands of type 'null' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("null - null").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... - null ...
            ^--- Operator '-' cannot be applied to operands of type 'null' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("'asd' - null").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... - null ...
            ^--- Operator '-' cannot be applied to operands of type 'System.String' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("null / null").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... / null ...
            ^--- Operator '/' cannot be applied to operands of type 'null' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("null && null").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... && null ...
            ^--- Operator '&&' cannot be applied to operands of type 'null' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("null || null").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... || null ...
            ^--- Operator '||' cannot be applied to operands of type 'null' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("null == 0").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... == 0 ...
            ^--- Operator '==' cannot be applied to operands of type 'null' and 'System.Int32'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("null < 0").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... < 0 ...
            ^--- Operator '<' cannot be applied to operands of type 'null' and 'System.Int32'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("Cash == null").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... == null ...
            ^--- Operator '==' cannot be applied to operands of type 'System.Decimal' and 'null'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("Date + 0").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... + 0 ...
            ^--- Operator '+' cannot be applied to operands of type 'System.DateTime' and 'System.Int32'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("NDate - 0").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... - 0 ...
            ^--- Operator '-' cannot be applied to operands of type 'System.Nullable`1[System.DateTime]' and 'System.Int32'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("Span + 0").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... + 0 ...
            ^--- Operator '+' cannot be applied to operands of type 'System.TimeSpan' and 'System.Int32'.",
                    e.Message);
            }

            try
            {
                var model = new Model();
                parser.Parse<Model>("NSpan - 0").Invoke(model);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... - 0 ...
            ^--- Operator '-' cannot be applied to operands of type 'System.Nullable`1[System.TimeSpan]' and 'System.Int32'.",
                    e.Message);
            }
        }
Exemple #26
0
 /// <summary>
 ///     Registers built-in methods for expressions parser.
 /// </summary>
 /// <param name="parser">The parser instance.</param>
 public static void RegisterMethods(this Parser parser)
 {
     parser.AddFunction("Now", () => DateTime.Now);
     parser.AddFunction("Today", () => DateTime.Today);
     parser.AddFunction <int, int, int, DateTime>("Date", (year, month, day) => new DateTime(year, month, day));
     parser.AddFunction <int, int, int, int, int, int, DateTime>("Date", (year, month, day, hour, minute, second) => new DateTime(year, month, day, hour, minute, second));
     parser.AddFunction <int, int, int, int, TimeSpan>("TimeSpan", (days, hours, minutes, seconds) => new TimeSpan(days, hours, minutes, seconds));
     parser.AddFunction <string, int>("Length", str => str != null ? str.Length : 0);
     parser.AddFunction <string, string>("Trim", str => str != null ? str.Trim() : null);
     parser.AddFunction <string, string, string>("Concat", (strA, strB) => string.Concat(strA, strB));
     parser.AddFunction <string, string, string, string>("Concat", (strA, strB, strC) => string.Concat(strA, strB, strC));
     parser.AddFunction <string, string, int>("CompareOrdinal", (strA, strB) => string.Compare(strA, strB, StringComparison.Ordinal) > 0 ? 1 : string.Compare(strA, strB, StringComparison.Ordinal) < 0 ? -1 : 0);
     parser.AddFunction <string, string, int>("CompareOrdinalIgnoreCase", (strA, strB) => string.Compare(strA, strB, StringComparison.OrdinalIgnoreCase) > 0 ? 1 : string.Compare(strA, strB, StringComparison.OrdinalIgnoreCase) < 0 ? -1 : 0);
     parser.AddFunction <string, string, bool>("StartsWith", (str, prefix) => str != null && prefix != null && str.StartsWith(prefix));
     parser.AddFunction <string, string, bool>("StartsWithIgnoreCase", (str, prefix) => str != null && prefix != null && str.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
     parser.AddFunction <string, string, bool>("EndsWith", (str, suffix) => str != null && suffix != null && str.EndsWith(suffix));
     parser.AddFunction <string, string, bool>("EndsWithIgnoreCase", (str, suffix) => str != null && suffix != null && str.EndsWith(suffix, StringComparison.OrdinalIgnoreCase));
     parser.AddFunction <string, string, bool>("Contains", (str, substr) => str != null && substr != null && str.Contains(substr));
     parser.AddFunction <string, string, bool>("ContainsIgnoreCase", (str, substr) => str != null && substr != null && str.ToLower().Contains(substr.ToLower()));
     parser.AddFunction <string, bool>("IsNullOrWhiteSpace", str => string.IsNullOrWhiteSpace(str));
     parser.AddFunction <string, bool>("IsDigitChain", str => str != null && Regex.IsMatch(str, @"^[0-9]+$"));
     parser.AddFunction <string, bool>("IsNumber", str => str != null && Regex.IsMatch(str, @"^[+-]?(?:(?:[0-9]+)|(?:[0-9]+[eE][+-]?[0-9]+)|(?:[0-9]*\.[0-9]+(?:[eE][+-]?[0-9]+)?))$"));                                                                                                                                                                                                                                                                                                                                                                                              // +/- lexer float
     parser.AddFunction <string, bool>("IsEmail", str => str != null && Regex.IsMatch(str, @"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"));                                                                                                                                                                                                                                                                                                                                                 // taken from HTML5 specification: http://www.w3.org/TR/html5/forms.html#e-mail-state-(type=email)
     parser.AddFunction <string, bool>("IsUrl", str => str != null && Regex.IsMatch(str, @"^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$", RegexOptions.IgnoreCase)); // contributed by Diego Perini: https://gist.github.com/dperini/729294 (https://mathiasbynens.be/demo/url-regex)
     parser.AddFunction <string, string, bool>("IsRegexMatch", (str, regex) => str != null && regex != null && Regex.IsMatch(str, regex));
     parser.AddFunction <string, Guid>("Guid", str => new Guid(str));
 }
Exemple #27
0
        public static string Newton(string sfx, string sfdx, string sfddx, double a, double b, out int count)
        {
            Parser fx = new Parser(sfx);
            fx.AddFunction<Algorithms>("log");
            fx.Compile();
            Parser fdx = new Parser(sfdx);
            fdx.AddFunction<Algorithms>("log");
            fdx.Compile();
            Parser fddx = new Parser(sfddx);
            fddx.AddFunction<Algorithms>("log");
            fdx.Compile();

            fx.Variables["x"] = a;
            fddx.Variables["x"] = a;
            double a_fpt = fx.Calculate() * fddx.Calculate();

            fx.Variables["x"] = b;
            fddx.Variables["x"] = b;
            fx.Compile();
            double b_fpt = fx.Calculate() * fddx.Calculate();

            double fixedPoint = a_fpt > 0 ? a : b;

            fx.Variables["x"] = fixedPoint;
            fdx.Variables["x"] = fixedPoint;
            double xn = fixedPoint - fx.Calculate()/fdx.Calculate();
            count = 0;
            while ((xn - fixedPoint) > eps)
            {
                fixedPoint = xn;

                fx.Variables["x"] = xn;
                fdx.Variables["x"] = xn;

                xn = fixedPoint - fx.Calculate() / fdx.Calculate();
                ++count;
            }

            return xn.ToString();
        }