private Sequence<InstructionNode> Create(IEnumerator<XamlInstruction> stream)
        {
            var nodes = new Sequence<InstructionNode>();

            while (IsLeading(stream.Current))
            {
                var currentNode = new InstructionNode { Leading = stream.Current };
                var continueWorking = true;
                while (stream.MoveNext() && continueWorking)
                {
                    if (IsLeading(stream.Current))
                    {
                        currentNode.Children = Create(stream);
                    }

                    var xamlNode = stream.Current;

                    if (IsTrailing(xamlNode))
                    {
                        continueWorking = false;
                        currentNode.Trailing = stream.Current;
                    }
                    else
                    {
                        currentNode.Body.Add(stream.Current);
                    }
                }

                nodes.Add(currentNode);
            }

            return nodes;
        }
        private IEnumerable<XamlInstruction> SortNodes(IEnumerator<XamlInstruction> enumerator)
        {
            var subSet = LookaheadBuffer.GetUntilEndOfRoot(enumerator);
            var nodes = new InstructionTreeBuilder().CreateHierarchy(subSet);
            var root = new InstructionNode { Children = new Sequence<InstructionNode>(nodes.ToList()) };
            root.AcceptVisitor(new DependencySortingVisitor());

            foreach (var instruction in root.Children.SelectMany(node => node.Dump()))
            {
                yield return instruction;
            }
        }
        public void Visit(InstructionNode instructionNode)
        {
            var list = GetMutableMembers(instructionNode).ToList();
            list.Reverse();
            var others = GetOthers(instructionNode);

            var newList = new List<InstructionNode>();

            newList.AddRange(list);
            newList.AddRange(others);

            instructionNode.Children = new Sequence<InstructionNode>(newList);

            foreach (var node in instructionNode.Children)
            {
                node.AcceptVisitor(this);
            }
        }
        public void Visit(InstructionNode instructionNode)
        {
            var mutable = GetMutableMembers(instructionNode).ToList();
            var sorted = ShortByDependencies(mutable);
            var others = GetOthers(instructionNode);

            var newList = new List<InstructionNode>();

            newList.AddRange(sorted);
            newList.AddRange(others);

            instructionNode.Children = new Sequence<InstructionNode>(newList);

            foreach (var node in instructionNode.Children)
            {
                node.AcceptVisitor(this);
            }
        }
        public void Visit(InstructionNode instructionNode)
        {
            var directives = GetDirectives(instructionNode);
            var sortable = GetSortableMembers(instructionNode);
            var others = GetOthers(instructionNode);

            var rearrangedNodes = new List<InstructionNode>();

            var sorted = ShortByDependencies(sortable.ToList());

            rearrangedNodes.AddRange(directives);
            rearrangedNodes.AddRange(others);
            rearrangedNodes.AddRange(sorted);

            instructionNode.Children = new Sequence<InstructionNode>(rearrangedNodes);

            foreach (var node in instructionNode.Children)
            {
                node.AcceptVisitor(this);
            }
        }
 private static bool IsOther(InstructionNode node)
 {
     return !IsDirective(node) && !IsSortable(node);
 }
 private static IEnumerable<InstructionNode> GetOthers(InstructionNode instructionNode)
 {
     return instructionNode.Children.Where(IsOther);
 }
 private static bool IsSortable(InstructionNode node)
 {
     return node.Leading.InstructionType == InstructionType.StartMember && node.Leading.Member is MutableMember && !IsDirective(node);
 }
 private static IEnumerable<InstructionNode> GetSortableMembers(InstructionNode instructionNode)
 {
     return instructionNode.Children.Where(IsSortable);
 }
 private static bool IsDirective(InstructionNode node)
 {
     return node.Leading.InstructionType == InstructionType.StartMember && node.Leading.Member.IsDirective;
 }
 private IEnumerable<InstructionNode> GetDirectives(InstructionNode instructionNode)
 {
     return instructionNode.Children.Where(node => IsDirective(node));
 }
 private static IEnumerable<InstructionNode> GetOthers(InstructionNode instructionNode)
 {
     return instructionNode.Children.Where(node => !(node.Leading.InstructionType == InstructionType.StartMember && node.Leading.Member is MutableMember));
 }
 protected bool Equals(InstructionNode other)
 {
     var eq = Equals(Body, other.Body);
     return Leading.Equals(other.Leading) && Equals(Children, other.Children) && Trailing.Equals(other.Trailing) && eq;
 }