コード例 #1
0
        public void ConvertEmittedElementsToRealTokenNodes(bool aRecurse)
        {
            int i = ChildCount;

            //
            while (i > 0)
            {
                SymNode child = this[--i];
                //
                if (child is SymTokenBalancerNodeEmittedElement)
                {
                    SymTokenBalancerNodeEmittedElement emittedElement = (SymTokenBalancerNodeEmittedElement)child;
                    if (emittedElement.Emit)
                    {
                        SymNodeToken replacement = new SymNodeToken(emittedElement.Token);
                        InsertChild(replacement, child);
                    }
                    child.Remove();
                }
                else if (child is SymTokenBalancerMarkerLevelNode && aRecurse)
                {
                    SymTokenBalancerMarkerLevelNode childLevel = (SymTokenBalancerMarkerLevelNode)child;
                    childLevel.ConvertEmittedElementsToRealTokenNodes(aRecurse);
                }
            }
        }
コード例 #2
0
        public static void RemoveWhiteSpace(SymNode aNode, bool aRecurse)
        {
            int count = aNode.ChildCount;

            for (int i = count - 1; i >= 0; i--)
            {
                SymNode basicNode = aNode[i];

                // If the node is whitespace, then remove it
                if (basicNode is SymNodeToken)
                {
                    SymNodeToken tokenNode    = (SymNodeToken)basicNode;
                    bool         isWhiteSpace = (tokenNode.Token.Class == SymToken.TClass.EClassWhiteSpace);
                    //
                    if (isWhiteSpace)
                    {
                        System.Diagnostics.Debug.Assert(basicNode.HasChildren == false);
                        basicNode.Remove();
                    }
                }

                // Remove whitespace from this node's children
                if (basicNode.HasChildren && aRecurse)
                {
                    RemoveWhiteSpace(basicNode, aRecurse);
                }
            }
        }
コード例 #3
0
        public virtual SymArgument MakeArgument(SymNode aLevelToMakeArgumentsFrom)
        {
            SymArgument argument = new SymArgument();

            // Convert (recursively) any emitted elements to real tokens
            if (aLevelToMakeArgumentsFrom is SymTokenBalancerMarkerLevelNode)
            {
                SymTokenBalancerMarkerLevelNode levelNode = (SymTokenBalancerMarkerLevelNode)aLevelToMakeArgumentsFrom;
                levelNode.ConvertEmittedElementsToRealTokenNodes(true /*recurse*/);
            }

            // Now actually obtain the argument tokens
            int count = aLevelToMakeArgumentsFrom.ChildCount;
            int i     = 0;

            //
            while (i < count)
            {
                SymNode n = aLevelToMakeArgumentsFrom[0];

                // We always remove any other nodes, irrespective of their type.
                // This is to ensure that the document tree does not get cluttered
                // with redundant argument token info.
                n.Remove();

                // Now we decide what to do...
                if (n is SymTokenBalancerMarkerArgumentNode)
                {
                    // We've reached the argument itself. This is the
                    // signal to stop processing. We remove the argument node
                    // since its not relevant to the production of the tree.
                    break;
                }
                else if (n is SymTokenBalancerMarkerLevelNode)
                {
                    // Create a new sub-argument node and copy over the
                    // children.
                    SymTokenBalancerMarkerLevelNode levelNode = (SymTokenBalancerMarkerLevelNode)n;
                    SymArgumentSubLevel             subLevel  = levelNode.AsArgumentSubLevel(true);
                    argument.CurrentNode.Add(subLevel);
                }
                else if (n is SymTokenBalancerNodeEmittedElement)
                {
                    System.Diagnostics.Debug.Assert(false);                       // shouldn't get here anymore!
                }
                else if (n is SymNodeToken)
                {
                    // Node is implicitly removed since it transfers
                    // from one tree to another.
                    argument.CurrentNode.Add(n);
                }

                count = aLevelToMakeArgumentsFrom.ChildCount;
            }
            //
            SymTokenUtils.RemoveWhiteSpace(argument, true);
            return(argument);
        }
コード例 #4
0
        protected void SimplifyLevel(SymTokenBalancerMarkerLevelNode aLevel)
        {
            System.Diagnostics.Debug.Assert(aLevel.IsRoot == false && aLevel.HasParent);
            SymNode parent      = aLevel.Parent;
            int     levelNumber = parent.Depth;
            //
            int childCount = parent.ChildCount;

            while (--childCount >= 0)
            {
                SymNode possibleLevelNode = parent[childCount];

                // We're looking to remove redundant bracketing from either side of the level
                // node. First check if we have a level node...
                if (possibleLevelNode is SymTokenBalancerMarkerLevelNode)
                {
                    // Then check whether it has a previous and a next node. These should
                    // be the SymTokenBalancerNodeEmittedElement nodes
                    if (possibleLevelNode.HasPrevious && possibleLevelNode.HasNext)
                    {
                        if (possibleLevelNode.Previous is SymTokenBalancerNodeEmittedElement && possibleLevelNode.Next is SymTokenBalancerNodeEmittedElement)
                        {
                            if (LevelCanBeMergedWithParent(possibleLevelNode as SymTokenBalancerMarkerLevelNode))
                            {
                                SymTokenBalancerNodeEmittedElement previous = (SymTokenBalancerNodeEmittedElement)possibleLevelNode.Previous;
                                SymTokenBalancerNodeEmittedElement next     = (SymTokenBalancerNodeEmittedElement)possibleLevelNode.Next;

                                // Insert value node prior to previous node (which is the opening bracket token).
                                parent.InsertChildrenFrom(possibleLevelNode, previous.Previous);

                                // Remove the opening bracket token
                                previous.Remove();

                                // Remove the level marker token
                                possibleLevelNode.Remove();

                                // Remove the closing bracket token.
                                next.Remove();
                            }
                        }
                    }
                }
            }
        }