Esempio n. 1
0
        public static SyntaxVisitorResult <IN, OUT> NewOptionGroupSome(Group <IN, OUT> group)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.OptionGroupResult = Some(group);
            return(res);
        }
Esempio n. 2
0
        private SyntaxVisitorResult <IN, OUT> Visit(OptionSyntaxNode <IN> node, object context = null)
        {
            var child = node.Children != null && node.Children.Any() ? node.Children[0] : null;

            if (child == null || node.IsEmpty)
            {
                if (node.IsGroupOption)
                {
                    return(SyntaxVisitorResult <IN, OUT> .NewOptionGroupNone());
                }
                else
                {
                    return(SyntaxVisitorResult <IN, OUT> .NewOptionNone());
                }
            }

            var innerResult = Visit(child, context);

            if (child is SyntaxLeaf <IN> leaf)
            {
                return(SyntaxVisitorResult <IN, OUT> .NewToken(leaf.Token));
            }
            if (child is GroupSyntaxNode <IN> group)
            {
                return(SyntaxVisitorResult <IN, OUT> .NewOptionGroupSome(innerResult.GroupResult));
            }
            return(SyntaxVisitorResult <IN, OUT> .NewOptionSome(innerResult.ValueResult));
        }
Esempio n. 3
0
        private SyntaxVisitorResult <IN, OUT> Visit(GroupSyntaxNode <IN> node, object context = null)
        {
            var group  = new Group <IN, OUT>();
            var values = new List <SyntaxVisitorResult <IN, OUT> >();

            foreach (var n in node.Children)
            {
                var v = Visit(n, context);

                if (v.IsValue)
                {
                    group.Add(n.Name, v.ValueResult);
                }
                if (v.IsToken)
                {
                    if (!v.Discarded)
                    {
                        group.Add(n.Name, v.TokenResult);
                    }
                }
            }


            var res = SyntaxVisitorResult <IN, OUT> .NewGroup(group);

            return(res);
        }
Esempio n. 4
0
        private SyntaxVisitorResult <IN, OUT> Visit(ManySyntaxNode <IN> node, object context = null)
        {
            SyntaxVisitorResult <IN, OUT> result = null;

            var values = new List <SyntaxVisitorResult <IN, OUT> >();

            foreach (var n in node.Children)
            {
                var v = Visit(n, context);
                values.Add(v);
            }

            if (node.IsManyTokens)
            {
                var tokens = new List <Token <IN> >();
                values.ForEach(v => tokens.Add(v.TokenResult));
                result = SyntaxVisitorResult <IN, OUT> .NewTokenList(tokens);
            }
            else if (node.IsManyValues)
            {
                var vals = new List <OUT>();
                values.ForEach(v => vals.Add(v.ValueResult));
                result = SyntaxVisitorResult <IN, OUT> .NewValueList(vals);
            }
            else if (node.IsManyGroups)
            {
                var vals = new List <Group <IN, OUT> >();
                values.ForEach(v => vals.Add(v.GroupResult));
                result = SyntaxVisitorResult <IN, OUT> .NewGroupList(vals);
            }


            return(result);
        }
Esempio n. 5
0
        public static SyntaxVisitorResult <IN, OUT> NewOptionSome(OUT value)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.OptionResult = Some(value);
            return(res);
        }
Esempio n. 6
0
        public static SyntaxVisitorResult <IN, OUT> NewOptionNone()
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.OptionResult = None <OUT>();
            return(res);
        }
Esempio n. 7
0
        public static SyntaxVisitorResult <IN, OUT> NewTokenList(List <Token <IN> > tokens)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.TokenListResult = tokens;
            res.IsTokenList     = true;
            return(res);
        }
Esempio n. 8
0
        public static SyntaxVisitorResult <IN, OUT> NewValueList(List <OUT> values)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.ValueListResult = values;
            res.IsValueList     = true;
            return(res);
        }
Esempio n. 9
0
        public static SyntaxVisitorResult <IN, OUT> NewGroupList(List <Group <IN, OUT> > values)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.GroupListResult = values;
            res.IsGroupList     = true;
            return(res);
        }
Esempio n. 10
0
        public static SyntaxVisitorResult <IN, OUT> NewToken(Token <IN> tok)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.TokenResult = tok;
            res.IsToken     = true;
            return(res);
        }
Esempio n. 11
0
        public static SyntaxVisitorResult <IN, OUT> NewValue(OUT val)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.ValueResult = val;
            res.IsValue     = true;
            return(res);
        }
Esempio n. 12
0
        public static SyntaxVisitorResult <IN, OUT> NewGroup(Group <IN, OUT> group)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.GroupResult = group;
            res.IsGroup     = true;
            return(res);
        }
        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)
                    {
                        throw new ParserConfigurationException($"ERROR : calling visitor method {method?.Name} with   {node.Name}");
                    }
                }
            }

            return(result);
        }
Esempio n. 14
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>();

                foreach (var n in node.Children)
                {
                    var v = Visit(n, context);

                    if (v.IsToken)
                    {
                        if (!n.Discarded)
                        {
                            args.Add(v.TokenResult);
                        }
                    }
                    else if (v.IsValue)
                    {
                        args.Add(v.ValueResult);
                    }
                    else if (v.IsOption)
                    {
                        args.Add(v.OptionResult);
                    }
                    else if (v.IsOptionGroup)
                    {
                        args.Add(v.OptionGroupResult);
                    }
                    else if (v.IsGroup)
                    {
                        args.Add(v.GroupResult);
                    }
                    else if (v.IsTokenList)
                    {
                        args.Add(v.TokenListResult);
                    }
                    else if (v.IsValueList)
                    {
                        args.Add(v.ValueListResult);
                    }
                    else if (v.IsGroupList)
                    {
                        args.Add(v.GroupListResult);
                    }
                }

                if (node.IsByPassNode)
                {
                    result = SyntaxVisitorResult <IN, OUT> .NewValue((OUT)args[0]);
                }
                else
                {
                    MethodInfo method = null;
                    try
                    {
                        if (method == null)
                        {
                            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($"OUTCH {e.Message} calling {node.Name} =>  {method.Name}");
                    }
                }
            }

            return(result);
        }
Esempio n. 15
0
 private SyntaxVisitorResult <IN, OUT> Visit(SyntaxLeaf <IN> leaf)
 {
     return(SyntaxVisitorResult <IN, OUT> .NewToken(leaf.Token));
 }
Esempio n. 16
0
        public static SyntaxVisitorResult <IN, OUT> NoneResult()
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            return(res);
        }
Esempio n. 17
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>();

                foreach (var n in node.Children)
                {
                    var v = Visit(n, context);

                    if (v.IsToken)
                    {
                        if (!n.Discarded)
                        {
                            args.Add(v.TokenResult);
                        }
                    }
                    else if (v.IsValue)
                    {
                        args.Add(v.ValueResult);
                    }
                    else if (v.IsOption)
                    {
                        args.Add(v.OptionResult);
                    }
                    else if (v.IsOptionGroup)
                    {
                        args.Add(v.OptionGroupResult);
                    }
                    else if (v.IsGroup)
                    {
                        args.Add(v.GroupResult);
                    }
                    else if (v.IsTokenList)
                    {
                        args.Add(v.TokenListResult);
                    }
                    else if (v.IsValueList)
                    {
                        args.Add(v.ValueListResult);
                    }
                    else if (v.IsGroupList)
                    {
                        args.Add(v.GroupListResult);
                    }
                }

                if (node.IsByPassNode)
                {
                    result = SyntaxVisitorResult <IN, OUT> .NewValue((OUT)args[0]);
                }
                else
                {
                    MethodInfo method = null;
                    try
                    {
                        if (!(context is NoContext))
                        {
                            args.Add(context);
                        }

                        if (method == null)
                        {
                            method = node.Visitor;
                        }
                        var t   = method.Invoke(ParserVsisitorInstance, args.ToArray());
                        var res = (OUT)t;
                        result = SyntaxVisitorResult <IN, OUT> .NewValue(res);
                    }
                    catch (TargetInvocationException tie)
                    {
                        if (tie.InnerException != null)
                        {
                            throw tie.InnerException;
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }

            return(result);
        }