Exemple #1
0
        public List <Tree> Children(string name)
        {
            List <Tree> result = new List <Tree>();
            // Walk forward until next tree node found.
            int n = _current.ChildCount;

            for (int i = 0; i < n; ++i)
            {
                var t = _current.GetChild(i);
                AstParserParser.NodeContext decl = t.GetChild(0) as AstParserParser.NodeContext;
                var is_decl = decl != null;
                if (!is_decl)
                {
                    continue;
                }
                var    u = decl.GetChild(1);
                string v = u.GetText();
                if (v != name)
                {
                    continue;
                }
                Tree tt = new Tree(_parent_map, _ast, decl, _common_token_stream);
                result.Add(tt);
            }
            return(result);
        }
Exemple #2
0
        public Tree Child(int index)
        {
            Tree result = null;
            // Walk forward until next tree node found.
            int n = _current.ChildCount;

            for (int i = 0; i < n; ++i)
            {
                var t = _current.GetChild(i);
                AstParserParser.NodeContext decl = t as AstParserParser.NodeContext;
                var is_decl = decl != null;
                if (!is_decl)
                {
                    continue;
                }
                if (index > 0)
                {
                    index--;
                    continue;
                }
                Tree tt = new Tree(_parent_map, _ast, decl, _common_token_stream);
                return(tt);
            }
            return(result);
        }
Exemple #3
0
        public override List <IParseTree> VisitNode(AstParserParser.NodeContext context)
        {
            var id      = context.ID();
            var id_name = id.GetText().ToLower() + "context";
            var pt      = _parser.GetType();
            var list    = pt.GetTypeInfo().DeclaredNestedTypes.Where(t => t.Name.ToLower() == id_name);

            if (list.Count() != 1)
            {
                throw new Exception();
            }
            TypeInfo mapped_type = list.First();
            var      mapped_node = Activator.CreateInstance(mapped_type, new object[] { null, 0 }) as IParseTree;

            foreach (var c in context.children)
            {
                if (c is AstParserParser.NodeContext)
                {
                    var mcl = VisitNode(c as AstParserParser.NodeContext);
                    var mc  = mcl.First();
                    if (mc is TerminalNodeImpl)
                    {
                        var _mc          = mc as TerminalNodeImpl;
                        var _mapped_node = mapped_node as ParserRuleContext;
                        _mc.Parent = _mapped_node;
                        _mapped_node.AddChild(_mc);
                    }
                    else
                    {
                        var _mc          = mc as ParserRuleContext;
                        var _mapped_node = mapped_node as ParserRuleContext;
                        _mc.Parent = _mapped_node;
                        _mapped_node.AddChild(_mc);
                    }
                }
                else if (c is AstParserParser.ValContext)
                {
                    var mcl = VisitVal(c as AstParserParser.ValContext);
                    if (mcl != null)
                    {
                        foreach (var mc in mcl)
                        {
                            if (mc is TerminalNodeImpl)
                            {
                                var _mc          = mc as TerminalNodeImpl;
                                var _mapped_node = mapped_node as ParserRuleContext;
                                _mc.Parent = _mapped_node;
                                _mapped_node.AddChild(_mc);
                            }
                            else
                            {
                                var _mc          = mc as ParserRuleContext;
                                var _mapped_node = mapped_node as ParserRuleContext;
                                _mc.Parent = _mapped_node;
                                _mapped_node.AddChild(_mc);
                            }
                        }
                    }
                }
            }
            return(new List <IParseTree>()
            {
                mapped_node
            });
        }