Ejemplo n.º 1
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);
        }
        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));
        }
Ejemplo n.º 3
0
        public static XTreeNodeBase Associate <TOper>(
            this IEnumerable <XTreeNodeBase> source
            )
            where TOper : XToken, ITokenAssociative, new()
        {
            XTreeNodeBase result = null;
            var           iter   = source.GetEnumerator();

            while (iter.MoveNext())
            {
                if (result == null)
                {
                    result = iter.Current;
                }
                else
                {
                    result = new XTreeNodeBinary(
                        new TOper(),
                        result,
                        iter.Current
                        );
                }
            }
            return(result);
        }
Ejemplo n.º 4
0
        public override string Serialize(XTreeNodeBase xtree)
        {
            var ctx = new SerializationContext();

            ctx.AddSpaces = this.ShouldPad;
            this.Serialize(xtree, ctx, 0);
            return(ctx.Builder.ToString());
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 7
0
        private void Serialize(XTreeNodeBase xtree, SerializationContext ctx, int level)
        {
            var term   = xtree as XTreeNodeTerminal;
            var unary  = xtree as XTreeNodeUnary;
            var binary = xtree as XTreeNodeBinary;

            if (term != null)
            {
                this.VisitorMap[term.Token.GetType()](term.Token, ctx);
            }
            else if (unary != null)
            {
                this.VisitorMap[unary.Token.GetType()](unary.Token, ctx);
                this.Serialize(unary.Child, ctx, level + 1);
            }
            else if (binary != null)
            {
                if (level != 0)
                {
                    ctx.Builder.Append('(');
                }
                ctx.LastIsAlphaNum = false;

                this.Serialize(binary.LeftChild, ctx, level + 1);
                if (ctx.AddSpaces)
                {
                    ctx.Builder.Append(' ');
                    ctx.LastIsAlphaNum = false;
                }
                this.VisitorMap[binary.Token.GetType()](binary.Token, ctx);
                if (ctx.AddSpaces)
                {
                    ctx.Builder.Append(' ');
                    ctx.LastIsAlphaNum = false;
                }
                this.Serialize(binary.RightChild, ctx, level + 1);

                if (level != 0)
                {
                    ctx.Builder.Append(')');
                }
            }
            else
            {
                throw new NotSupportedException();
            }
        }
        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));
        }
        //TODO use the visitor pattern rather a fixed procedure
        public override XElement Serialize(XTreeNodeBase xtree)
        {
            var xelem = new XElement(
                xtree.Token.GetType().Name,
                xtree.GetChildren().Select(_ => this.Serialize(_))
                );

            if (xtree.Token.Data != null)
            {
                xelem.Add(new XAttribute("data", xtree.Token.Data));
            }

            var mp = xtree.Token as XTokenMatchParam;

            if (mp != null)
            {
                xelem.Add(new XAttribute("flags", mp.Flags));
            }
            return(xelem);
        }
Ejemplo n.º 10
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));
        }
 public override XSolverResult Resolve(IXSolverContext context, XTreeNodeBase na, XTreeNodeBase nb, XTreeNodeBase nc)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 12
0
        public static void Main(string[] args)
        {
            Test("");
            Test("    ");
            Test("false  ");
            Test("  true");
            Test("  null  ");
            Test(" 123 ");
            Test("-456  ");
            Test(" +7 ");
            Test(" 3.151927 ");
            Test(" +2.718 ");
            Test("1.6e-19");
            Test(" 6.022E23 ");
            Test(" 5E-1 ");
            Test(" .5 ");   //should throw
            Test(" -.5 ");  //should throw
            Test(" +5. ");  //should throw
            Test("myvar  ");
            Test("  w0rd");
            Test("_abc_def_");
            Test("my.multi.level.reference");
            Test("'single-quoted string'");
            Test("\"double-quoted string\"");
            //Test("'here is a \"nested string\"'");
            //Test(@"'here is a ""nested escaped string""'");

            Test("zero == zero  ");
            Test(" black != white");
            Test(" 12 < 45");
            Test("20 >4");
            Test("10<=100");
            Test("100   >=   1");

            Test("!false==!!true");
            Test("to_be || !to_be");
            Test(" maccheroni || spaghetti || rigatoni");
            Test(" sex && drug && rock && roll   ");
            Test("!me || you && !they ");
            Test("a==b && c!=d");
            Test("pname match/abc/");
            Test("pname match /xyz/i");
            Test("pname   match /(\\w+)\\s(\\w+)/");

            Test("(!me ||you)&&they");
            Test("!(a=='q') && (b!='x')");
            Test("(a || b) && (c || d) || (e && f)");
            Test("! (a && (b && c || d && e) || (g == h && j))");
            Test("!! (((a)==b) && ((((c && ((g)))))))");

#if DOC
            System.IO.File.WriteAllText(@"i:\temp\parser_output.txt", _sb.ToString());
#endif

            {
                XTreeNodeBase node = XTreeNodeBase.Parse("! (a && (b && c || d && e) || (g == h && j))");
                const int     N    = 1000;

                var sw = new Stopwatch();
                sw.Start();

                XSolverResult sr;
                for (int i = 0; i < N; i++)
                {
                    sr = node.Resolve(_ctx);
                    sr = node.Resolve(_ctx);
                    sr = node.Resolve(_ctx);
                    sr = node.Resolve(_ctx);
                    sr = node.Resolve(_ctx);

                    sr = node.Resolve(_ctx);
                    sr = node.Resolve(_ctx);
                    sr = node.Resolve(_ctx);
                    sr = node.Resolve(_ctx);
                    sr = node.Resolve(_ctx);
                }

                sw.Stop();
                Console.WriteLine($"Elapsed: {sw.ElapsedMilliseconds}ms; {sw.ElapsedMilliseconds * 100.0 / N}us/each");
            }

            Console.WriteLine("Process complete.");
            Console.ReadKey();
        }
 public abstract T Serialize(XTreeNodeBase xtree);
Ejemplo n.º 14
0
 public XTreeNodeBinary(XToken token, XTreeNodeBase left, XTreeNodeBase right) : base(token)
 {
     this.LeftChild  = left;
     this.RightChild = right;
 }
Ejemplo n.º 15
0
 public XTreeNodeUnary(XToken token, XTreeNodeBase child) : base(token)
 {
     this.Child = child;
 }
Ejemplo n.º 16
0
 public override XSolverResult Resolve(IXSolverContext context, XTreeNodeBase na, XTreeNodeBase nb, XTreeNodeBase nc)
 {
     return(XSolverResult.FromData(this.Data));
 }
Ejemplo n.º 17
0
 public abstract XSolverResult Resolve(IXSolverContext context, XTreeNodeBase na, XTreeNodeBase nb, XTreeNodeBase nc);
Ejemplo n.º 18
0
        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
        }