Exemple #1
0
 internal static CodegenExpression GetRegexpCode(
     ExprRegexpNodeForge forge,
     CodegenExpression pattern,
     CodegenExpression stringExpr)
 {
     CodegenExpression eval = ExprDotMethodChain(pattern).Add("IsMatch", stringExpr);
     return !forge.ForgeRenderable.IsNot ? eval : Not(eval);
 }
Exemple #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;
        }