示例#1
0
        private SyntaxVisitorResult <IN, OUT> Visit(SyntaxNode <IN> node, object context = null)
        {
            var result = SyntaxVisitorResult <IN, OUT> .NoneResult();

            if (node.Visitor != null || node.IsByPassNode)
            {
                var args = new List <object>();
                var i    = 0;
                foreach (var n in node.Children)
                {
                    var v = Visit(n, context);


                    if (v.IsToken)
                    {
                        if (!v.Discarded)
                        {
                            args.Add(v.TokenResult);
                        }
                    }
                    else if (v.IsValue)
                    {
                        args.Add(v.ValueResult);
                    }

                    i++;
                }

                if (node.IsByPassNode)
                {
                    result = SyntaxVisitorResult <IN, OUT> .NewValue((OUT)args[0]);
                }
                else
                {
                    MethodInfo method = null;
                    try
                    {
                        if (!(context is NoContext))
                        {
                            args.Add(context);
                        }
                        method = node.Visitor;
                        var t   = method?.Invoke(ParserVsisitorInstance, args.ToArray());
                        var res = (OUT)t;
                        result = SyntaxVisitorResult <IN, OUT> .NewValue(res);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"ERROR : {e.Message} calling {node.Name} =>  {method?.Name}");
                    }
                }
            }

            return(result);
        }
示例#2
0
        private SyntaxVisitorResult <IN, OUT> Visit(SyntaxNode <IN> node)
        {
            SyntaxVisitorResult <IN, OUT> result = SyntaxVisitorResult <IN, OUT> .NoneResult();

            if (node.Visitor != null || node.IsByPassNode)
            {
                List <object> args = new List <object>();
                int           i    = 0;
                foreach (ISyntaxNode <IN> n in node.Children)
                {
                    SyntaxVisitorResult <IN, OUT> v = Visit(n);


                    if (v.IsToken)
                    {
                        if (!v.Discarded)
                        {
                            args.Add(v.TokenResult);
                        }
                    }
                    else if (v.IsValue)
                    {
                        args.Add(v.ValueResult);
                    }

                    i++;
                }
                if (node.IsByPassNode)
                {
                    result = SyntaxVisitorResult <IN, OUT> .NewValue((OUT)args[0]);
                }
                else
                {
                    MethodInfo method = null;
                    try
                    {
                        method = node.Visitor;
                        object t = (method.Invoke(ParserVsisitorInstance, args.ToArray()));
                        if (t == null)
                        {
                            ;
                        }
                        OUT res = (OUT)t;
                        result = SyntaxVisitorResult <IN, OUT> .NewValue(res);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"OUTCH {e.Message} calling {node.Name} =>  {method?.Name}");
                    }
                }
            }

            return(result);
        }