Пример #1
0
        public void ParseLiterals()
        {
            var parser = new Core.Parser();

            var literalExpression = parser.Parse("true") as LiteralExpression;

            Assert.NotNull(literalExpression);
            Assert.True((bool)literalExpression.LiteralValue.Value);

            literalExpression = parser.Parse("false") as LiteralExpression;
            Assert.NotNull(literalExpression);
            Assert.False((bool)literalExpression.LiteralValue.Value);

            literalExpression = parser.Parse("null") as LiteralExpression;
            Assert.NotNull(literalExpression);
            Assert.Null(literalExpression.LiteralValue.Value);

            var option = ParseOption.CreateOption()
                         .AddLiteralValue("longValue1", 2147483649L)
                         .AddLiteralValue("longValue2", 2147483641L)
                         .AsReadOnly();

            literalExpression = parser.Parse("longValue1", option) as LiteralExpression;
            Assert.NotNull(literalExpression);
            Assert.Equal(2147483649L, literalExpression.LiteralValue.Value);

            literalExpression = parser.Parse("longValue2", option) as LiteralExpression;
            Assert.NotNull(literalExpression);
            Assert.Equal(2147483641L, literalExpression.LiteralValue.Value);
        }
Пример #2
0
        public void ParseInteger()
        {
            var parser = new Core.Parser();

            var constantExpression = parser.Parse("100") as ConstantExpression;

            Assert.NotNull(constantExpression);
            Assert.Equal("100", constantExpression.Raw);
            Assert.Equal(100, constantExpression.Value);

            constantExpression = parser.Parse("12312381") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("12312381", constantExpression.Raw);
            Assert.Equal(12312381, constantExpression.Value);

            constantExpression = parser.Parse("956869218") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("956869218", constantExpression.Raw);
            Assert.Equal(956869218, constantExpression.Value);

            var option = ParseOption.CreateOption()
                         .NotAllowedConvertUnsignedInteger()
                         .AsReadOnly();

            constantExpression = parser.Parse("956869218", option) as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("956869218", constantExpression.Raw);
            Assert.NotEqual(956869218, constantExpression.Value);
            Assert.Equal(956869218UL, constantExpression.Value);
        }
Пример #3
0
        public void ParseIdentifier()
        {
            var parser = new Core.Parser();

            var identifierExpression = parser.Parse("$aa") as IdentifierExpression;

            Assert.NotNull(identifierExpression);
            Assert.Equal("$aa", identifierExpression.Name);

            identifierExpression = parser.Parse("_11") as IdentifierExpression;
            Assert.NotNull(identifierExpression);
            Assert.Equal("_11", identifierExpression.Name);

            identifierExpression = parser.Parse("i") as IdentifierExpression;
            Assert.NotNull(identifierExpression);
            Assert.Equal("i", identifierExpression.Name);

            identifierExpression = parser.Parse("codemonk") as IdentifierExpression;
            Assert.NotNull(identifierExpression);
            Assert.Equal("codemonk", identifierExpression.Name);

            identifierExpression = parser.Parse("$a1a") as IdentifierExpression;
            Assert.NotNull(identifierExpression);
            Assert.Equal("$a1a", identifierExpression.Name);
        }
Пример #4
0
        public void ParseArray()
        {
            var parser = new Core.Parser();

            var arrayExpression = parser.Parse("[1, 2.0222 ,true,'das']") as ArrayExpression;

            Assert.NotNull(arrayExpression);
            Assert.Equal(4, arrayExpression.Elements.Length);

            Assert.Equal(0, arrayExpression.StartIndex);
            Assert.Equal(23, arrayExpression.EndIndex);

            Assert.Equal(1, ((ConstantExpression)arrayExpression.Elements[0]).Value);
            Assert.Equal(2.0222m, ((ConstantExpression)arrayExpression.Elements[1]).Value);
            Assert.Equal(true, ((LiteralExpression)arrayExpression.Elements[2]).LiteralValue.Value);
            Assert.Equal("das", ((ConstantExpression)arrayExpression.Elements[3]).Value);

            arrayExpression = parser.Parse("[]") as ArrayExpression;
            Assert.NotNull(arrayExpression);
            Assert.Empty(arrayExpression.Elements);

            arrayExpression = parser.Parse($"[{string.Join(",", Enumerable.Range(1, 1000))}]") as ArrayExpression;
            Assert.NotNull(arrayExpression);
            Assert.Equal(1000, arrayExpression.Elements.Length);
        }
Пример #5
0
        public void ParseNakedFunction()
        {
            var option = ParseOption.CreateOption()
                         .AddNakedFunction("abc", args => "abc")
                         .AddNakedFunction("hash", args => args.GetHashCode())
                         .AsReadOnly();

            var parser = new Core.Parser();

            var callExpression = parser.Parse("abc(1)", option) as NakedFunctionCallExpression;

            Assert.NotNull(callExpression);
            Assert.Equal("abc", callExpression.NakedFunction.Name);

            callExpression = parser.Parse("max(1,2,3)", option) as NakedFunctionCallExpression;
            Assert.NotNull(callExpression);
            Assert.Equal("max", callExpression.NakedFunction.Name);

            callExpression = parser.Parse("abc(hash(1,2,3))", option) as NakedFunctionCallExpression;
            Assert.NotNull(callExpression);
            Assert.Equal("abc", callExpression.NakedFunction.Name);
            callExpression = callExpression.Arguments[0] as NakedFunctionCallExpression;
            Assert.NotNull(callExpression);
            Assert.Equal("hash", callExpression.NakedFunction.Name);
        }
Пример #6
0
        public void ParseObjectExpression()
        {
            var parser = new Core.Parser();

            var callExpression = parser.Parse("x.func(a,b)") as ObjectsFunctionCallExpression;

            Assert.NotNull(callExpression);

            var objectMemberExpression = callExpression.Callee;

            Assert.NotNull(objectMemberExpression);
            Assert.False(objectMemberExpression.IsIndexer);
            Assert.Equal("func", (objectMemberExpression.Member as IdentifierExpression)?.Name);
            Assert.Equal("x", (objectMemberExpression.Object as IdentifierExpression)?.Name);

            Assert.Equal(2, callExpression.Arguments.Length);
            Assert.Equal("a", (callExpression.Arguments[0] as IdentifierExpression)?.Name);
            Assert.Equal("b", (callExpression.Arguments[1] as IdentifierExpression)?.Name);

            callExpression = parser.Parse("d['func'](a,b)") as ObjectsFunctionCallExpression;
            Assert.NotNull(callExpression);

            var objectIndexerExpression = callExpression.Callee;

            Assert.NotNull(objectIndexerExpression);
            Assert.True(objectIndexerExpression.IsIndexer);
            Assert.Equal("func", (objectIndexerExpression.Member as ConstantExpression)?.Value);
            Assert.Equal("d", (objectIndexerExpression.Object as IdentifierExpression)?.Name);
        }
Пример #7
0
        void HighlightCells(ExcelInterop.Range range, Core.Parser parser)
        {
            if (range.Cells.Count > 1)
            {
                foreach (var cell in range.Cells.Cast <ExcelInterop.Range>())
                {
                    HighlightCells(cell, parser);
                }
            }
            else
            {
                var text = (string)range.Text;
                if (string.IsNullOrEmpty(text))
                {
                    return;
                }
                // reset text color.
                range.Font.ColorIndex = ExcelInterop.XlColorIndex.xlColorIndexAutomatic;

                foreach (var f in parser.Parse(text))
                {
                    // NOTE: indices of Characters are 1-based
                    range.get_Characters(f.Start + 1, f.Length).Font.Color = f.ForegroundColor;
                }
            }
        }
Пример #8
0
        public void ParseDecimal()
        {
            var parser = new Core.Parser();

            var constantExpression = parser.Parse("3.155225") as ConstantExpression;

            Assert.NotNull(constantExpression);
            Assert.Equal("3.155225", constantExpression.Raw);
            Assert.Equal(3.155225M, constantExpression.Value);

            constantExpression = parser.Parse("10.0") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("10.0", constantExpression.Raw);
            Assert.Equal(10.0M, constantExpression.Value);

            constantExpression = parser.Parse(".01212") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal(".01212", constantExpression.Raw);
            Assert.Equal(.01212M, constantExpression.Value);

            constantExpression = parser.Parse("1.3e-5") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("1.3e-5", constantExpression.Raw);
            Assert.Equal(1.3e-05M, constantExpression.Value);

            constantExpression = parser.Parse("23.88e4") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("23.88e4", constantExpression.Raw);
            Assert.Equal(238800M, constantExpression.Value);

            constantExpression = parser.Parse("12.45E4") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("12.45E4", constantExpression.Raw);
            Assert.Equal(124500M, constantExpression.Value);

            constantExpression = parser.Parse("100e+3") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("100e+3", constantExpression.Raw);
            Assert.Equal(100000M, constantExpression.Value);

            constantExpression = parser.Parse("20e3") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("20e3", constantExpression.Raw);
            Assert.Equal(20000M, constantExpression.Value);

            constantExpression = parser.Parse("12E4") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("12E4", constantExpression.Raw);
            Assert.Equal(120_000M, constantExpression.Value);
        }
Пример #9
0
 void HighlightTextRange(Office.TextRange2 textRange, Core.Parser parser)
 {
     textRange.Font.Fill.ForeColor.RGB = System.Drawing.Color.Black.ToMsoColor();
     foreach (var f in parser.Parse(textRange.Text))
     {
         // NOTE: indices of Characters are 1-based
         textRange.get_Characters(1 + f.Start, f.Length).Font.Fill.ForeColor.RGB = f.ForegroundColor.ToMsoColor();
     }
 }
Пример #10
0
        public void ParseString()
        {
            var parser = new Core.Parser();

            var constantExpression = parser.Parse("'abasdkas'") as ConstantExpression;

            Assert.NotNull(constantExpression);
            Assert.Equal("abasdkas", constantExpression.Raw);
            Assert.Equal("abasdkas", constantExpression.Value);

            constantExpression = "'abasdkas'".ToEvaluableExpression() as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("abasdkas", constantExpression.Raw);
            Assert.Equal("abasdkas", constantExpression.Value);

            constantExpression = "'abasdkas'".ToEvaluableExpression(parser) as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("abasdkas", constantExpression.Raw);
            Assert.Equal("abasdkas", constantExpression.Value);

            constantExpression = parser.Parse("\"as1781uhj21\"") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("as1781uhj21", constantExpression.Raw);
            Assert.Equal("as1781uhj21", constantExpression.Value);

            constantExpression = parser.Parse("\"121asadas\t1uhj21\"") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("121asadas\t1uhj21", constantExpression.Raw);
            Assert.Equal("121asadas\t1uhj21", constantExpression.Value);

            constantExpression = parser.Parse("'twew\\tqqwkqekkk'") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("twew\tqqwkqekkk", constantExpression.Raw);
            Assert.Equal("twew\tqqwkqekkk", constantExpression.Value);


            constantExpression = parser.Parse("'aksja\\n6127\\t1makd\\bahjkla;+1212/1212\\r\\f\\v'") as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("aksja\n6127\t1makd\bahjkla;+1212/1212\r\f\v", constantExpression.Raw);
            Assert.Equal("aksja\n6127\t1makd\bahjkla;+1212/1212\r\f\v", constantExpression.Value);
        }
Пример #11
0
        public void ParseConditionalExpression()
        {
            var parser = new Core.Parser();

            var conditionalExpression = parser.Parse("x ?a:b") as ConditionalExpression;

            Assert.NotNull(conditionalExpression);

            Assert.Equal("x", (conditionalExpression.Condition as IdentifierExpression)?.Name);
            Assert.Equal("a", (conditionalExpression.ValueIfTrue as IdentifierExpression)?.Name);
            Assert.Equal("b", (conditionalExpression.ValueIfFalse as IdentifierExpression)?.Name);

            conditionalExpression = parser.Parse("12?true:max($a)") as ConditionalExpression;
            Assert.NotNull(conditionalExpression);
            Assert.Equal(12, ((conditionalExpression.Condition as ConstantExpression)?.Value));
            Assert.Equal(true, ((conditionalExpression.ValueIfTrue as LiteralExpression)?.LiteralValue.Value));
            var callExpression = conditionalExpression.ValueIfFalse as NakedFunctionCallExpression;

            Assert.Equal("max", callExpression?.NakedFunction.Name);
            Assert.Equal("$a", (callExpression?.Arguments.First() as IdentifierExpression)?.Name);
        }
Пример #12
0
        public void ParseInExpression()
        {
            var parser = new Core.Parser();

            var inExpression = parser.Parse("1 in (1,3)") as InExpression;

            Assert.NotNull(inExpression);
            Assert.Equal(1, (inExpression.Value as ConstantExpression)?.Value);
            Assert.Equal(2, inExpression.Values.Length);

            inExpression = parser.Parse("f.x in (a,b,c,true)") as InExpression;
            Assert.NotNull(inExpression);
            Assert.Equal("f", ((inExpression.Value as ObjectMemberExpression)?.Object as IdentifierExpression)?.Name);
            Assert.Equal("x", ((inExpression.Value as ObjectMemberExpression)?.Member as IdentifierExpression)?.Name);

            Assert.Equal(4, inExpression.Values.Length);
            Assert.Equal(true, (inExpression.Values[3] as LiteralExpression)?.LiteralValue.Value);

            inExpression = parser.Parse("(a+f.x) in (a,b,c,true)") as InExpression;
            Assert.NotNull(inExpression);
        }
Пример #13
0
        void HighlightRange(WordInterop.Range range, Core.Parser parser)
        {
            // reset text color.
            range.Font.ColorIndex = WordInterop.WdColorIndex.wdAuto;

            var chars = range.Characters;

            foreach (var f in parser.Parse(range.Text))
            {
                // NOTE: indices of Characters are 1-based
                var r = chars[1 + f.Start];
                r.SetRange(r.Start, r.Start + f.Length);
                r.Font.Fill.ForeColor.RGB = f.ForegroundColor.ToMsoColor();
            }
        }
Пример #14
0
        void HighlightTextRange(Office.TextRange2 textRange, Core.Parser parser)
        {
            var text = textRange.Text;

            if (string.IsNullOrEmpty(text))
            {
                return;
            }
            // reset text color.
            textRange.Font.Fill.ForeColor.RGB = System.Drawing.Color.Black.ToMsoColor();

            foreach (var f in parser.Parse(text))
            {
                // NOTE: indices of Characters are 1-based
                textRange.get_Characters(1 + f.Start, f.Length).Font.Fill.ForeColor.RGB = f.ForegroundColor.ToMsoColor();
            }
        }
Пример #15
0
        public void NotAllowedInExpression()
        {
            var option = ParseOption.CreateOption()
                         .NotAllowedInExpression()
                         .AsReadOnly();
            var parser = new Core.Parser();

            Assert.Equal("In expression is not allowed at character 5", Assert.Throws <ParseException>(() => parser.Parse("1 in (1,2,3)", option)).Message);
            Assert.Equal("In expression is not allowed at character 6", Assert.Throws <ParseException>(() => parser.Parse("xy in (m['u'],n,y)", option)).Message);

            Assert.NotNull(parser.Parse("[1,2,3]", option) as ArrayExpression);
            Assert.NotNull(parser.Parse("true?1:2", option) as ConditionalExpression);
            Assert.NotNull(parser.Parse("x.f", option) as ObjectMemberExpression);
            Assert.NotNull(parser.Parse("x['f']", option) as ObjectMemberExpression);
            var add = parser.Parse("add", option) as IdentifierExpression;

            Assert.NotNull(add);
            Assert.Equal("add", add.Name);
            Assert.NotNull(parser.Parse("12", option) as ConstantExpression);
        }
Пример #16
0
        public void ErrorExpression()
        {
            var parser = new Core.Parser();

            Assert.Throws <ArgumentException>(() => parser.Parse(""));
            Assert.Throws <ArgumentException>(() => parser.Parse(null));
            Assert.Throws <ArgumentException>(() => parser.Parse("    "));

            Assert.Equal("Unexpected \"#\" at character 0", Assert.Throws <ParseException>(() => parser.Parse("#aaa")).Message);
            Assert.Equal("Unexpected \"@\" at character 0", Assert.Throws <ParseException>(() => parser.Parse("@aaa")).Message);
            Assert.Equal("Unexpected \"@\" at character 4", Assert.Throws <ParseException>(() => parser.Parse("$aa @a)")).Message);
            Assert.Equal("Unexpected \0 at character 2", Assert.Throws <ParseException>(() => parser.Parse("x.")).Message);
            Assert.Equal("Unclosed ( at character 1", Assert.Throws <ParseException>(() => parser.Parse("(")).Message);
            Assert.Equal("Expected ] at character 1", Assert.Throws <ParseException>(() => parser.Parse("[")).Message);
            Assert.Equal("Expected ) at character 5", Assert.Throws <ParseException>(() => parser.Parse("x.f(a")).Message);
            Assert.Equal("Unclosed ( at character 1", Assert.Throws <ParseException>(() => parser.Parse("(@11)")).Message);
            Assert.Equal("Unclosed ( at character 1", Assert.Throws <ParseException>(() => parser.Parse("(#aa)")).Message);
            Assert.Equal("Unclosed ( at character 5", Assert.Throws <ParseException>(() => parser.Parse("($aa @a)")).Message);
            Assert.Equal("Unclosed [ at character 2", Assert.Throws <ParseException>(() => parser.Parse("x[")).Message);
            Assert.Equal("Unclosed quote after \"\" at character 3", Assert.Throws <ParseException>(() => parser.Parse("x['")).Message);
            Assert.Equal("Expected ] at character 6", Assert.Throws <ParseException>(() => parser.Parse("x.f([1")).Message);
            Assert.Equal("Expected , at character 5", Assert.Throws <ParseException>(() => parser.Parse("x.f([)")).Message);
            Assert.Equal("Unexpected token , at character 6", Assert.Throws <ParseException>(() => parser.Parse("max([,")).Message);
            Assert.Equal("Unexpected token , at character 2", Assert.Throws <ParseException>(() => parser.Parse("[,,,]")).Message);
            Assert.Equal("Unexpected token , at character 4", Assert.Throws <ParseException>(() => parser.Parse("[a,,]")).Message);
            Assert.Equal("Expected , at character 6", Assert.Throws <ParseException>(() => parser.Parse("avg((),aa)")).Message);
            Assert.Equal("Expected , at character 13", Assert.Throws <ParseException>(() => parser.Parse("avg([1,2,t,],#aa)")).Message);
            Assert.Equal("Unexpected \"a\" at character 5", Assert.Throws <ParseException>(() => parser.Parse("a in aa")).Message);

            Assert.Equal("Expected expression after % at character 2", Assert.Throws <ParseException>(() => parser.Parse("2%")).Message);
            Assert.Equal("Variable names cannot start with a number (10a) at character 2", Assert.Throws <ParseException>(() => parser.Parse("10a")).Message);
            Assert.Equal("Unexpected period at character 3", Assert.Throws <ParseException>(() => parser.Parse("10..")).Message);
            Assert.Equal("Expected exponent (10e) at character 3", Assert.Throws <ParseException>(() => parser.Parse("10er")).Message);

            Assert.Equal("Unclosed quote after \"abasdkas\" at character 9", Assert.Throws <ParseException>(() => parser.Parse("'abasdkas")).Message);
            Assert.Equal("Unclosed quote after \"rualkl\" at character 7", Assert.Throws <ParseException>(() => parser.Parse("\"rualkl")).Message);
            Assert.Equal("Unclosed quote after \"bm121\\u22a\" at character 11", Assert.Throws <ParseException>(() => parser.Parse("\"bm121\\u22a")).Message);
            Assert.Equal("Unclosed quote after \"bm121\" at character 7", Assert.Throws <ParseException>(() => parser.Parse("\"bm121\\")).Message);

            Assert.Equal("Expected expression at character 2", Assert.Throws <ParseException>(() => parser.Parse("x?")).Message);
            Assert.Equal("Expected expression at character 2", Assert.Throws <ParseException>(() => parser.Parse("x?:")).Message);
            Assert.Equal("Expected : at character 3", Assert.Throws <ParseException>(() => parser.Parse("x?p")).Message);
            Assert.Equal("Expected expression at character 4", Assert.Throws <ParseException>(() => parser.Parse("x?p:")).Message);
            Assert.Equal("Expected expression at character 2", Assert.Throws <ParseException>(() => parser.Parse("x?:p")).Message);
            Assert.Equal("Expected expression at character 4", Assert.Throws <ParseException>(() => parser.Parse("x?p:")).Message);

            Assert.Equal("Expected expression after + at character 2", Assert.Throws <ParseException>(() => parser.Parse("x+")).Message);
            Assert.Equal("Expected expression after + at character 4", Assert.Throws <ParseException>(() => parser.Parse("xYY+")).Message);
            Assert.Equal("Expected expression after * at character 4", Assert.Throws <ParseException>(() => parser.Parse("x+y*")).Message);

            Assert.Equal("Unexpected token , at character 5", Assert.Throws <ParseException>(() => parser.Parse("avg(,aa)")).Message);
            Assert.Equal("Unexpected token ) at character 9", Assert.Throws <ParseException>(() => parser.Parse("a[max(c,)]")).Message);

            Assert.Equal("Can not find naked function (f) at character 0", Assert.Throws <ParseException>(() => parser.Parse("f(1,2)")).Message);
            Assert.Equal("Can not find naked function (p) at character 2", Assert.Throws <ParseException>(() => parser.Parse("1+p(1,2)")).Message);

            Assert.Equal("Can not parse value(111111111111111111111111) to ulong at (start:0,end:24)", Assert.Throws <InvalidCastException>(() => parser.Parse("111111111111111111111111")).Message);
            Assert.Equal("Can not parse value(7792281625142643375935439503359228162514264337593543950335.11) to decimal at (start:0,end:61)", Assert.Throws <InvalidCastException>(() => parser.Parse("7792281625142643375935439503359228162514264337593543950335.11")).Message);


            var option = ParseOption.CreateOption()
                         .AddBinaryOperator("add", (a, b) => null, 8)
                         .AddBinaryOperator("a@", (a, b) => null, 8)
                         .AsReadOnly();

            Assert.Throws <ParseException>(() => parser.Parse("12 add ", option));
            Assert.Throws <ParseException>(() => parser.Parse("12 a@", option));
        }
Пример #17
0
        public void ParseBinaryExpression()
        {
            var parser = new Core.Parser();

            var binaryExpression = parser.Parse("12+34+true") as BinaryExpression;

            Assert.NotNull(binaryExpression);
            Assert.Equal("+", binaryExpression.BinaryOperator.Operator);
            Assert.Equal(true, (binaryExpression.Right as LiteralExpression)?.LiteralValue.Value);

            Assert.Equal(0, binaryExpression.StartIndex);
            Assert.Equal(10, binaryExpression.EndIndex);

            var left = binaryExpression.Left as BinaryExpression;

            Assert.NotNull(left);
            Assert.Equal("+", left.BinaryOperator.Operator);
            Assert.Equal(12, (left.Left as ConstantExpression)?.Value);
            Assert.Equal(34, (left.Right as ConstantExpression)?.Value);

            binaryExpression = parser.Parse("12*34+true") as BinaryExpression;
            Assert.NotNull(binaryExpression);
            Assert.Equal("+", binaryExpression.BinaryOperator.Operator);
            Assert.Equal(true, (binaryExpression.Right as LiteralExpression)?.LiteralValue.Value);

            left = binaryExpression.Left as BinaryExpression;
            Assert.NotNull(left);
            Assert.Equal("*", left.BinaryOperator.Operator);
            Assert.Equal(12, (left.Left as ConstantExpression)?.Value);
            Assert.Equal(34, (left.Right as ConstantExpression)?.Value);

            binaryExpression = parser.Parse("(12*34+true)||(false)") as BinaryExpression;
            Assert.NotNull(binaryExpression);
            Assert.Equal("||", binaryExpression.BinaryOperator.Operator);
            Assert.Equal(false, (binaryExpression.Right as LiteralExpression)?.LiteralValue.Value);

            left = binaryExpression.Left as BinaryExpression;
            Assert.NotNull(left);
            Assert.Equal("+", left.BinaryOperator.Operator);
            Assert.Equal(true, (left.Right as LiteralExpression)?.LiteralValue.Value);

            var right = left.Left as BinaryExpression;

            Assert.NotNull(right);
            Assert.Equal("*", right.BinaryOperator.Operator);
            Assert.Equal(12, (right.Left as ConstantExpression)?.Value);
            Assert.Equal(34, (right.Right as ConstantExpression)?.Value);


            binaryExpression = parser.Parse("12 + 19") as BinaryExpression;
            Assert.NotNull(binaryExpression);
            Assert.Equal("+", binaryExpression.BinaryOperator.Operator);
            Assert.Equal(12, (binaryExpression.Left as ConstantExpression)?.Value);
            Assert.Equal(19, (binaryExpression.Right as ConstantExpression)?.Value);

            var option = ParseOption.CreateOption()
                         .AddBinaryOperator("@", (a, b) => null, 8)
                         .AsReadOnly();

            binaryExpression = parser.Parse("12 @ true", option) as BinaryExpression;
            Assert.NotNull(binaryExpression);
            Assert.Equal("@", binaryExpression.BinaryOperator.Operator);
            Assert.Equal(12, (binaryExpression.Left as ConstantExpression)?.Value);
            Assert.Equal(true, (binaryExpression.Right as LiteralExpression)?.LiteralValue.Value);

            option = ParseOption.CreateOption()
                     .AddBinaryOperator("add", (a, b) => null, 8)
                     .AsReadOnly();
            var add = parser.Parse("12 add 34", option) as BinaryExpression;

            Assert.NotNull(add);
            Assert.Equal("add", add.BinaryOperator.Operator);
            Assert.Equal(12, (add.Left as ConstantExpression)?.Value);
            Assert.Equal(34, (add.Right as ConstantExpression)?.Value);
        }
Пример #18
0
        public void ParseUnaryExpression()
        {
            var parser = new Core.Parser();

            var unaryExpression = parser.Parse("+12") as UnaryExpression;

            Assert.NotNull(unaryExpression);
            Assert.Equal("+", unaryExpression.UnaryOperator.Operator);
            var constantExpression = unaryExpression.Argument as ConstantExpression;

            Assert.NotNull(constantExpression);
            Assert.Equal("12", constantExpression.Raw);
            Assert.Equal(12, constantExpression.Value);

            unaryExpression = parser.Parse("-19.0") as UnaryExpression;
            Assert.NotNull(unaryExpression);
            Assert.Equal("-", unaryExpression.UnaryOperator.Operator);
            constantExpression = unaryExpression.Argument as ConstantExpression;
            Assert.NotNull(constantExpression);
            Assert.Equal("19.0", constantExpression.Raw);
            Assert.Equal(19.0m, constantExpression.Value);

            unaryExpression = parser.Parse("!true") as UnaryExpression;
            Assert.NotNull(unaryExpression);
            Assert.Equal("!", unaryExpression.UnaryOperator.Operator);
            var literalExpression = unaryExpression.Argument as LiteralExpression;

            Assert.NotNull(literalExpression);
            Assert.True((bool)literalExpression.LiteralValue.Value);

            unaryExpression = parser.Parse("!$11") as UnaryExpression;
            Assert.NotNull(unaryExpression);
            Assert.Equal("!", unaryExpression.UnaryOperator.Operator);
            var identifier = unaryExpression.Argument as IdentifierExpression;

            Assert.NotNull(identifier);
            Assert.Equal("$11", identifier.Name);

            unaryExpression = parser.Parse("!_11") as UnaryExpression;
            Assert.NotNull(unaryExpression);
            Assert.Equal("!", unaryExpression.UnaryOperator.Operator);
            identifier = unaryExpression.Argument as IdentifierExpression;
            Assert.NotNull(identifier);
            Assert.Equal("_11", identifier.Name);

            unaryExpression = parser.Parse("!cm") as UnaryExpression;
            Assert.NotNull(unaryExpression);
            Assert.Equal("!", unaryExpression.UnaryOperator.Operator);
            identifier = unaryExpression.Argument as IdentifierExpression;
            Assert.NotNull(identifier);
            Assert.Equal("cm", identifier.Name);


            unaryExpression = parser.Parse("!(12+34)") as UnaryExpression;
            Assert.NotNull(unaryExpression);
            Assert.Equal("!", unaryExpression.UnaryOperator.Operator);
            var binaryExpression = unaryExpression.Argument as BinaryExpression;

            Assert.NotNull(binaryExpression);
            Assert.Equal("+", binaryExpression.BinaryOperator.Operator);
            var left = binaryExpression.Left as ConstantExpression;

            Assert.NotNull(left);
            Assert.Equal(12, left.Value);
            var right = binaryExpression.Right as ConstantExpression;

            Assert.NotNull(right);
            Assert.Equal(34, right.Value);

            unaryExpression = parser.Parse("+avg(a,abc)") as UnaryExpression;
            Assert.NotNull(unaryExpression);
            Assert.Equal("+", unaryExpression.UnaryOperator.Operator);
            var callExpression = unaryExpression.Argument as NakedFunctionCallExpression;

            Assert.NotNull(callExpression);
            Assert.Equal("avg", callExpression.NakedFunction.Name);
            var a = callExpression.Arguments[0] as IdentifierExpression;

            Assert.NotNull(a);
            Assert.Equal("a", a.Name);
            var b = callExpression.Arguments[1] as IdentifierExpression;

            Assert.NotNull(b);
            Assert.Equal("abc", b.Name);

            unaryExpression = parser.Parse("![1,'asa',true]") as UnaryExpression;
            Assert.NotNull(unaryExpression);
            Assert.Equal("!", unaryExpression.UnaryOperator.Operator);

            var arrayExpression = unaryExpression.Argument as ArrayExpression;

            Assert.NotNull(arrayExpression);
            Assert.Equal(3, arrayExpression.Elements.Length);
        }