/// <summary>
 ///     NOTE: Code-generation-invoked method, method name and parameter order matters
 /// </summary>
 /// <param name="text">regex pattern</param>
 /// <returns>pattern</returns>
 public static Regex ExprRegexNodeCompilePattern(string text)
 {
     try {
         return RegexExtensions.Compile(text, out string patternText);
     }
     catch (ArgumentException ex) {
         throw new EPException("Failed to compile regex pattern '" + text + "': " + ex.Message, ex);
     }
 }
Beispiel #2
0
        public override ExprNode Validate(ExprValidationContext validationContext)
        {
            if (ChildNodes.Length != 2) {
                throw new ExprValidationException("The regexp operator requires 2 child expressions");
            }

            // check pattern child node
            var patternChildType = ChildNodes[1].Forge.EvaluationType;
            if (patternChildType != typeof(string)) {
                throw new ExprValidationException("The regexp operator requires a String-type pattern expression");
            }

            var constantPattern = ChildNodes[1].Forge.ForgeConstantType.IsCompileTimeConstant;

            // check eval child node - can be String or numeric
            var evalChildType = ChildNodes[0].Forge.EvaluationType;
            var isNumericValue = TypeHelper.IsNumeric(evalChildType);
            if ((evalChildType != typeof(string)) && (!isNumericValue)) {
                throw new ExprValidationException(
                    "The regexp operator requires a String or numeric type left-hand expression");
            }

            if (constantPattern) {
                var patternText = (string) ChildNodes[1].Forge.ExprEvaluator.Evaluate(null, true, null);

                Regex pattern;
                try {
                    pattern = RegexExtensions.Compile(patternText, out patternText);
                }
                catch (ArgumentException ex) {
                    throw new ExprValidationException(
                        "Failed to compile regex pattern '" + patternText + "': " + ex.Message,
                        ex);
                }

                var patternInit = NewInstance<Regex>(Constant(patternText));
                _forge = new ExprRegexpNodeForgeConst(this, isNumericValue, pattern, patternInit);
            }
            else {
                _forge = new ExprRegexpNodeForgeNonconst(this, isNumericValue);
            }

            return null;
        }