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

            res.OptionGroupResult = Some(group);
            return(res);
        }
        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);
        }
Exemple #3
0
        public static SyntaxVisitorResult <IN, OUT> NewOptionSome(OUT value)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.OptionResult = Some(value);
            return(res);
        }
        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);
        }
Exemple #5
0
        private SyntaxVisitorResult <IN, OUT> Visit(OptionSyntaxNode <IN> node)
        {
            var child = node.Children != null && node.Children.Any() ? node.Children[0] : null;

            if (child == null || node.IsEmpty)
            {
                return(SyntaxVisitorResult <IN, OUT> .NewOptionNone());
            }
            else
            {
                SyntaxVisitorResult <IN, OUT> innerResult = Visit(child);
                if (child is SyntaxLeaf <IN> leaf)
                {
                    return(SyntaxVisitorResult <IN, OUT> .NewToken(leaf.Token));
                }
                else if (child is GroupSyntaxNode <IN> group)
                {
                    return(SyntaxVisitorResult <IN, OUT> .NewOptionGroupSome(innerResult.GroupResult));
                }
                else
                {
                    return(SyntaxVisitorResult <IN, OUT> .NewOptionSome(innerResult.ValueResult));
                }
            }
        }
Exemple #6
0
        private SyntaxVisitorResult <IN, OUT> Visit(GroupSyntaxNode <IN> node)
        {
            Group <IN, OUT> group = new Group <IN, OUT>();
            List <SyntaxVisitorResult <IN, OUT> > values = new List <SyntaxVisitorResult <IN, OUT> >();

            foreach (ISyntaxNode <IN> n in node.Children)
            {
                SyntaxVisitorResult <IN, OUT> v = Visit(n);

                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);
        }
Exemple #7
0
        public static SyntaxVisitorResult <IN, OUT> NewOptionNone()
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.OptionResult = None <OUT>();
            return(res);
        }
Exemple #8
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);
        }
Exemple #9
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);
        }
Exemple #10
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);
        }
Exemple #11
0
        public static SyntaxVisitorResult <IN, OUT> NewValue(OUT val)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.ValueResult = val;
            res.IsValue     = true;
            return(res);
        }
Exemple #12
0
        public static SyntaxVisitorResult <IN, OUT> NewValueList(List <OUT> values)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.ValueListResult = values;
            res.IsValueList     = true;
            return(res);
        }
Exemple #13
0
        public static SyntaxVisitorResult <IN, OUT> NewToken(Token <IN> tok)
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            res.TokenResult = tok;
            res.IsToken     = true;
            return(res);
        }
Exemple #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>();
                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);
        }
Exemple #15
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);
        }
Exemple #16
0
        public OUT VisitSyntaxTree(ISyntaxNode <IN> root)
        {
            SyntaxVisitorResult <IN, OUT> result = Visit(root);

            return(result.ValueResult);
        }
Exemple #17
0
 private SyntaxVisitorResult <IN, OUT> Visit(SyntaxLeaf <IN> leaf)
 {
     return(SyntaxVisitorResult <IN, OUT> .NewToken(leaf.Token));
 }
Exemple #18
0
        public static SyntaxVisitorResult <IN, OUT> NoneResult()
        {
            var res = new SyntaxVisitorResult <IN, OUT>();

            return(res);
        }