Пример #1
0
        private void Cache(CstNonterminalNode node)
        {
            List <CstNonterminalNode> list;

            if (!cache.TryGetValue(node.Nonterminal, out list))
            {
                list = new List <CstNonterminalNode>();
                cache[node.Nonterminal] = list;
            }
            list.Add(node);
        }
Пример #2
0
        private void ScanNonterminal(CstNonterminalNode node)
        {
            Cache(node);

            foreach (var child in node.Children)
            {
                if (child is CstNonterminalNode)
                {
                    ScanNonterminal((CstNonterminalNode)child);
                }
            }
        }
Пример #3
0
        public ICstNode Transform(Func <CstNonterminalNode, ICstNode> transformer)
        {
            ICstNode result = transformer(this);

            if (result == null)
            {
                result = new CstNonterminalNode(Nonterminal, -1);

                foreach (var child in Children)
                {
                    if (child is CstNonterminalNode)
                    {
                        var nonterminalChild = (CstNonterminalNode)child;
                        ((CstNonterminalNode)result).Children.Add(nonterminalChild.Transform(transformer));
                    }
                    else
                    {
                        ((CstNonterminalNode)result).Children.Add(child);
                    }
                }
            }
            return(result);
        }
Пример #4
0
        public static CstNonterminalNode Build(IEnumerable <OutputRecord> outputStream)
        {
            if (!outputStream.Any())
            {
                return(null);
            }

            Stack <CstNonterminalNode> stack = new Stack <CstNonterminalNode>();

            int absoluteIndex          = 0;
            CstNonterminalNode current = new CstNonterminalNode(null, absoluteIndex);

            foreach (OutputRecord output in outputStream)
            {
                switch (output.OutputType)
                {
                case OutputType.None:
                    current.Children.Add((Terminal)output.Expression);
                    break;

                case OutputType.Begin:
                    CstNonterminalNode nonterminalNode = new CstNonterminalNode((Nonterminal)output.Expression, ++absoluteIndex);
                    stack.Push(current);
                    current = nonterminalNode;
                    break;

                case OutputType.End:
                    CstNonterminalNode node = current;
                    current = stack.Pop();
                    current.Children.Add(node);
                    break;
                }
            }

            return(current);
        }
Пример #5
0
 public CstCache(CstNonterminalNode node)
 {
     ScanNonterminal(node);
 }