Esempio n. 1
0
        public void IntSimpleTest()
        {
            var expression = @"(123+";
            var target     = "123";
            var matchIndex = 1;

            var number = ExpressionRegex.GetNextNumber(expression);

            Assert.AreEqual(number.Item1, target);
            Assert.AreEqual(number.Item2, matchIndex);
        }
Esempio n. 2
0
        public void OperandTest2()
        {
            var expression = @"5-3";
            var target     = "-";
            var matchIndex = 1;

            var operand = ExpressionRegex.GetNextOperand(expression);

            Assert.AreEqual(operand.Item1, target);
            Assert.AreEqual(operand.Item2, matchIndex);
        }
Esempio n. 3
0
        public void ScientificNotationTest2()
        {
            var expression = @"(.23456E-10+";
            var target     = ".23456E-10";
            var matchIndex = 1;

            var number = ExpressionRegex.GetNextNumber(expression);

            Assert.AreEqual(number.Item1, target);
            Assert.AreEqual(number.Item2, matchIndex);
        }
Esempio n. 4
0
        public void OperandMathFunctionTest2()
        {
            var expression = @"abs(-1)";
            var target     = "abs";
            var matchIndex = 0;

            var operand = ExpressionRegex.GetNextMathFunction(expression);

            Assert.AreEqual(operand.Item1, target);
            Assert.AreEqual(operand.Item2, matchIndex);
        }
Esempio n. 5
0
        public void OperandMathFunctionTest1()
        {
            var expression = @"2+sin(45)";
            var target     = "sin";
            var matchIndex = 2;

            var operand = ExpressionRegex.GetNextMathFunction(expression);

            Assert.AreEqual(operand.Item1, target);
            Assert.AreEqual(operand.Item2, matchIndex);
        }
Esempio n. 6
0
            public SectionInfo(string name, bool inverted, bool expression)
            {
                this.name       = name;
                this.inverted   = inverted;
                this.parts      = new Collection <ITemplatePart>();
                this.properties = new Dictionary <string, object>();
                this.jsonKeys   = new Collection <string>();
                if (expression)
                {
                    Match match = ExpressionRegex.Match(name);

                    if (match.Success)
                    {
                        this.name = match.Groups[1].Value.Trim() + ":" + match.Groups[2].Value.Trim();
                        string values = match.Groups[3].Value.Trim();

                        IEnumerable <KeyValuePair <string, string> > attributesMatches =
                            ExpressionParamRegex1.Matches(values)
                            .Cast <Match>()
                            .Concat(ExpressionParamRegex2.Matches(values).Cast <Match>())
                            .Where(
                                x =>
                                x.Success && !string.IsNullOrWhiteSpace(x.Groups[1].Value) &&
                                !string.IsNullOrWhiteSpace(x.Groups[2].Value))
                            .Select(x => new KeyValuePair <string, string>(x.Groups[1].Value, x.Groups[2].Value));
                        IEnumerable <KeyValuePair <string, string> > jsonMatches =
                            ExpressionParamRegex3.Matches(values)
                            .Cast <Match>()
                            .Concat(ExpressionParamRegex4.Matches(values).Cast <Match>())
                            .Where(
                                x =>
                                x.Success && !string.IsNullOrWhiteSpace(x.Groups[1].Value) &&
                                !string.IsNullOrWhiteSpace(x.Groups[2].Value))
                            .Select(x => new KeyValuePair <string, string>(x.Groups[1].Value, x.Groups[2].Value));

                        foreach (var attributesMatch in attributesMatches)
                        {
                            if (!this.properties.ContainsKey(attributesMatch.Key))
                            {
                                this.properties.Add(attributesMatch.Key, attributesMatch.Value);
                            }
                        }

                        foreach (var jsonMatch in jsonMatches)
                        {
                            if (!this.jsonKeys.Contains(jsonMatch.Key))
                            {
                                this.jsonKeys.Add(jsonMatch.Key);
                                this.properties.Add(jsonMatch.Key, jsonMatch.Value);
                            }
                        }
                    }
                }
            }
Esempio n. 7
0
        public static IEnumerable <T> Parse <T>(string expression,
                                                Func <string, T> propertySelector,
                                                Func <object, T> indexSelector)
        {
            if (propertySelector == null)
            {
                throw new ArgumentNullException("propertySelector");
            }
            if (indexSelector == null)
            {
                throw new ArgumentNullException("indexSelector");
            }

            expression = (expression ?? string.Empty).Trim();
            if (expression.Length == 0)
            {
                yield break;
            }

            var match = ExpressionRegex.Match(expression);

            if (!match.Success)
            {
                throw new FormatException(null);
            }

            var accessors =
                from Capture c in match.Groups["a"].Captures
                select c.Value.TrimStart('.') into text
                let isIndexer = text[0] == '[' || text[0] == '('
                                select new { Text = text, IsIndexer = isIndexer };

            var indexes =
                from Capture c in match.Groups["i"].Captures
                select c.Value into text
                select text[0] == '\'' || text[0] == '\"'
                     ? (object)text.Substring(1, text.Length - 2)
                     : int.Parse(text, NumberStyles.Integer, CultureInfo.InvariantCulture);

            using (var i = indexes.GetEnumerator())
                foreach (var a in accessors)
                {
                    if (a.IsIndexer)
                    {
                        i.MoveNext();
                    }
                    yield return(a.IsIndexer ? indexSelector(i.Current) : propertySelector(a.Text));
                }
        }