Пример #1
0
        /// <summary>
        /// Evaluates the operator on scalar operands.
        /// </summary>
        /// <param name="Left">Left value.</param>
        /// <param name="Right">Right value.</param>
        /// <param name="Variables">Variables collection.</param>
        /// <returns>Result</returns>
        public override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
        {
            if (!(Left is StringValue L))
            {
                throw new ScriptRuntimeException("String values expected.", this);
            }

            if (!(Right is StringValue R))
            {
                throw new ScriptRuntimeException("String values expected.", this);
            }

            string sl = L.Value;
            string sr = R.Value;
            Match  M;

            ExpressionTransform h = this.TransformExpression;

            if (!(h is null))
            {
                sr = h(sr);
            }

            M = this.Matches(sl, sr, out string[] GroupNames);

            if (M.Success)
            {
                if (!(GroupNames is null))
                {
                    foreach (string GroupName in GroupNames)
                    {
                        Group G = M.Groups[GroupName];
                        if (G.Success)
                        {
                            string Value = G.Value;

                            if (Expression.TryParse(Value, out double d))
                            {
                                Variables[GroupName] = d;
                            }
                            else
                            {
                                Variables[GroupName] = Value;
                            }

                            Variables[GroupName + "_STR"] = Value;
                            Variables[GroupName + "_POS"] = G.Index;
                            Variables[GroupName + "_LEN"] = G.Length;
                        }
                    }
                }

                if (M.Index == 0 && M.Length == sl.Length)
                {
                    return(BooleanValue.True);
                }
            }

            return(BooleanValue.False);
        }
Пример #2
0
        /// <summary>
        /// Performs a pattern match operation.
        /// </summary>
        /// <param name="CheckAgainst">Value to check against.</param>
        /// <param name="AlreadyFound">Variables already identified.</param>
        /// <returns>Pattern match result</returns>
        public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary <string, IElement> AlreadyFound)
        {
            if (!(CheckAgainst is StringValue SL))
            {
                return(PatternMatchResult.NoMatch);
            }

            if (this.right is ConstantElement RightConstant &&
                RightConstant.Constant is StringValue SR)
            {
                string sr = SR.Value;

                ExpressionTransform h = this.TransformExpression;
                if (!(h is null))
                {
                    sr = h(sr);
                }

                Match M = this.Matches(SL.Value, sr, out string[] GroupNames);

                if (M.Success)
                {
                    if (!(GroupNames is null))
                    {
                        foreach (string GroupName in GroupNames)
                        {
                            Group G = M.Groups[GroupName];
                            if (G.Success)
                            {
                                string Value = G.Value;
                                object ObjValue;

                                if (Expression.TryParse(Value, out double d))
                                {
                                    ObjValue = d;
                                }
                                else
                                {
                                    ObjValue = Value;
                                }

                                if (AlreadyFound.TryGetValue(GroupName, out IElement E) &&
                                    !E.AssociatedObjectValue.Equals(ObjValue))
                                {
                                    return(PatternMatchResult.NoMatch);
                                }

                                AlreadyFound[GroupName] = Expression.Encapsulate(ObjValue);
                            }
                        }
                    }

                    return(this.left.PatternMatch(CheckAgainst, AlreadyFound));
                }
            }

            return(PatternMatchResult.NoMatch);
        }
Пример #3
0
        /// <summary>
        /// Evaluates the operator on scalar operands.
        /// </summary>
        /// <param name="Left">Left value.</param>
        /// <param name="Right">Right value.</param>
        /// <param name="Variables">Variables collection.</param>
        /// <returns>Result</returns>
        public override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
        {
            if (!(Left is StringValue L))
            {
                throw new ScriptRuntimeException("String values expected.", this);
            }

            if (!(Right is StringValue R))
            {
                throw new ScriptRuntimeException("String values expected.", this);
            }

            string sl = L.Value;
            string sr = R.Value;

            string[] GroupNames;
            Match    M;

            ExpressionTransform h = this.TransformExpression;

            if (!(h is null))
            {
                sr = h(sr);
            }

            lock (this.synchObject)
            {
                if (this.lastExpression is null || sr != this.lastExpression)
                {
                    this.lastExpression = sr;
                    this.regex          = new Regex(sr, RegexOptions.Singleline);

                    List <string> Names = null;

                    foreach (string s in this.regex.GetGroupNames())
                    {
                        if (!int.TryParse(s, out int i))
                        {
                            if (Names is null)
                            {
                                Names = new List <string>();
                            }

                            Names.Add(s);
                        }
                    }

                    if (Names is null)
                    {
                        this.groupNames = null;
                    }
                    else
                    {
                        this.groupNames = Names.ToArray();
                    }
                }

                M          = this.regex.Match(sl);
                GroupNames = this.groupNames;
            }

            if (M.Success)
            {
                if (!(GroupNames is null))
                {
                    foreach (string GroupName in GroupNames)
                    {
                        Group G = M.Groups[GroupName];
                        if (G.Success)
                        {
                            string Value = G.Value;

                            if (double.TryParse(Value.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator), out double d))
                            {
                                Variables[GroupName] = d;
                            }
                            else
                            {
                                Variables[GroupName] = Value;
                            }
                        }
                    }
                }

                if (M.Index == 0 && M.Length == sl.Length)
                {
                    return(BooleanValue.True);
                }
            }

            return(BooleanValue.False);
        }