public override XSolverResult Resolve(IXSolverContext context, XTreeNodeBase na, XTreeNodeBase nb, XTreeNodeBase nc)
        {
            XSolverResult sra = na.Resolve(context);

            if (sra.Error != null)
            {
                return(sra);
            }

            XSolverResult srb = nb.Resolve(context);

            if (srb.Error != null)
            {
                return(srb);
            }

            var s = sra.Data as string;

            if (s == null)
            {
                return(XSolverResult.FromData(false));
            }

            var re = srb.Data as Regex;

            if (re == null)
            {
                return(XSolverResult.FromData(false));
            }

            Match match = re.Match(s);

            return(XSolverResult.FromData(match.Success));
        }
Exemple #2
0
        public static Func <TIn, TOut> GetLambda <TIn, TOut>(
            this XTreeNodeBase resolver,
            XReferenceSolverBase <TIn> refres
            )
        {
            if (resolver == null)
            {
                return(null);
            }

            var rc = new MyResolverContext()
            {
                ReferenceSolver = refres
            };

            Func <TIn, TOut> func = input =>
            {
                refres.Context = input;
                XSolverResult result = resolver.Resolve(rc);
                if (result.Error == null)
                {
                    return((TOut)result.Data);
                }
                else
                {
                    throw result.Error;
                }
            };

            return(func);
        }
Exemple #3
0
 public override XSolverResult Resolve(IXSolverContext context, XTreeNodeBase na, XTreeNodeBase nb, XTreeNodeBase nc)
 {
     if (context.ReferenceSolver == null)
     {
         return(XSolverResult.FromData(null));
     }
     return(context.ReferenceSolver.GetValue(this));
 }
        public override XSolverResult Resolve(IXSolverContext context, XTreeNodeBase na, XTreeNodeBase nb, XTreeNodeBase nc)
        {
            XSolverResult result = na.Resolve(context);

            if (result.Error != null)
            {
                return(result);
            }
            bool value = XSolverHelpers.AsBool(result.Data);

            return(XSolverResult.FromData(value == false));
        }
Exemple #5
0
        public XSolverResult GetValue(XTokenRefId token)
        {
            var refId = token.Data as string;

            if (string.IsNullOrEmpty(refId))
            {
                return(XSolverResult.FromData(null));
            }

            object value;

            this._bag.TryGetValue(refId, out value);
            return(XSolverResult.FromData(value));
        }
        public override XSolverResult Resolve(IXSolverContext context, XTreeNodeBase na, XTreeNodeBase nb, XTreeNodeBase nc)
        {
            XSolverResult sra = na.Resolve(context);

            if (sra.Error != null)
            {
                return(sra);
            }

            XSolverResult srb = nb.Resolve(context);

            if (srb.Error != null)
            {
                return(srb);
            }

            bool value = XSolverHelpers.Match(sra.Data, srb.Data);

            return(XSolverResult.FromData(value));
        }
Exemple #7
0
        public override XSolverResult Resolve(IXSolverContext context, XTreeNodeBase na, XTreeNodeBase nb, XTreeNodeBase nc)
        {
            var pattern = this.Data as string;

            if (pattern == null)
            {
                return(XSolverResult.FromData(null));
            }

            var          flags   = this.Flags ?? string.Empty;
            RegexOptions options = RegexOptions.None;

            if (flags.Contains('i'))
            {
                options |= RegexOptions.IgnoreCase;
            }

            var re = new Regex(pattern, options);

            return(XSolverResult.FromData(re));
        }
        private static void Test(
            string text
            )
        {
#if DOC
            _sb.AppendLine("Expression:");
            _sb.AppendFormat("`{0}`", text);
            _sb.AppendLine();
            _sb.AppendLine();
            _sb.AppendLine("Result:");
            _sb.AppendLine("```");
            try
            {
                TreeNodeBase node  = Parser.Parse(text);
                XElement     xelem = ToXml(node);
                _sb.Append(xelem);
            }
            catch (Exception ex)
            {
                _sb.Append(ex.Message);
            }
            _sb.AppendLine();
            _sb.AppendLine("```");
            _sb.AppendLine("***");
            _sb.AppendLine();
            _sb.AppendLine();
#else
            Console.WriteLine($"Source expression: {text}");
            try
            {
                //parsing
                XTreeNodeBase xtree = XTreeNodeBase.Parse(text);

                //compact serialization
                var cser = new XTreeCompactSerializer();
                cser.ShouldPad = true;
                string xstr = cser.Serialize(xtree);
                Console.WriteLine($"Serialized: {xstr}");

                //xml serialization
                var      xser  = new XTreeXmlSerializer();
                XElement xelem = xser.Serialize(xtree);
                Console.WriteLine(xelem);

                //evaluation (against the sample context)
                XSolverResult sr = xtree.Resolve(_ctx);
                Console.WriteLine($"Result: {sr.Error ?? sr.Data}");

                //verify the compact serialization
                try
                {
                    XTreeNodeBase xtreeAlt = XTreeNodeBase.Parse(xstr);
                    XElement      xelemAlt = xser.Serialize(xtreeAlt);
                    if (xelem.ToString() != xelemAlt.ToString())
                    {
                        //fail!
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Verify error: " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Test error: " + ex.Message);
            }
            Console.WriteLine();
            Console.WriteLine();
#endif
        }
Exemple #9
0
 public override XSolverResult Resolve(IXSolverContext context, XTreeNodeBase na, XTreeNodeBase nb, XTreeNodeBase nc)
 {
     return(XSolverResult.FromData(this.Data));
 }