Example #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);
                }
            }
        }
Example #2
0
 protected override void ExtractToDocument(SymNode aNode, SymTokenDocument aDocument)
 {
     if (aNode is SymTokenBalancerNodeEmittedElement)
     {
         SymTokenBalancerNodeEmittedElement node = (SymTokenBalancerNodeEmittedElement)aNode;
         node.AddToDocumentIfEmittable(aDocument);
     }
 }
Example #3
0
 protected override void ExtractToContainer(SymNode aNode, SymTokenContainer aContainer)
 {
     if (aNode is SymTokenBalancerNodeEmittedElement)
     {
         SymTokenBalancerNodeEmittedElement node = (SymTokenBalancerNodeEmittedElement)aNode;
         node.AddToContainerIfEmittable(aContainer);
     }
 }
Example #4
0
        protected override bool NodeIsExtractable(SymNode aNode)
        {
            bool ret = false;

            //
            if (aNode is SymTokenBalancerNodeEmittedElement)
            {
                SymTokenBalancerNodeEmittedElement node = (SymTokenBalancerNodeEmittedElement)aNode;
                ret = node.Emit;
            }
            //
            return(ret);
        }
Example #5
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();
                            }
                        }
                    }
                }
            }
        }
Example #6
0
        protected virtual void LevelStarted(SymToken aToken, SymTokenBalancerMatchCriteria aMatchCriteria)
        {
            System.Diagnostics.Debug.Write(System.Environment.NewLine + aToken.Value);

            // Always store the node element so that we can balance brackets
            SymTokenBalancerNodeEmittedElement node = new SymTokenBalancerNodeEmittedElement(aToken, aMatchCriteria);

            iTree.CurrentNode.Add(node);

            // Store the token (with the level) so we can check for open/close mis-matches
            SymTokenBalancerMarkerLevelNode levelNode = new SymTokenBalancerMarkerLevelNode(aMatchCriteria);

            iTree.CurrentNode.Add(levelNode);

            SymNode oldLevel = iTree.CurrentNode;
            SymNode newLevel = levelNode;

            NotifyEventLevelStarted(CurrentLevelNumber, oldLevel, newLevel);

            iTree.CurrentNode = levelNode;
        }
Example #7
0
        public void Subsume()
        {
            System.Diagnostics.Debug.Assert(IsComplete);
            //
            SymTokenBalancerNodeEmittedElement previous = EmittedElementPrevious;
            SymTokenBalancerNodeEmittedElement next     = EmittedElementNext;

            // Insert all my children as siblings of myself (i.e. make my parent's
            // grandchildren its direct children - i.e. promote them up the tree
            // by one level).
            SymNode   parent          = Parent;
            ArrayList parentsChildren = Parent.Children;

            parent.InsertChildrenFrom(this /* my children */, previous.Previous /* move them before the opening bracket */);

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

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

            // Remove the level marker token, i.e myself
            Remove();
        }
Example #8
0
        protected virtual void LevelFinished(SymToken aToken, SymTokenBalancerMatchCriteria aMatchCriteria)
        {
            #region Example

            // #define TEST1((( B )+( C ))+(E))

            #region Key
            // |y| = level y
            // [x] = token of value 'x'
            // [*] = level marker node
            // [@] = (current) level marker node
            // [,] = argument node
            // [(] = opening level emitted token node
            // [)] = closing level emitted token node
            #endregion

            #region 1) First wave of opening level tokens processed...
            //
            //  |0|                    [ROOT]
            //                           |
            //  |1|          [TEST] [(] [*]
            //                           |
            //  |2|                 [(] [*]
            //                           |
            //  |3|                 [(] [@]
            //                           |
            //  |4|                      B
            //
            #endregion

            #region 2) Add closing level token to level |3|, which means that we can now
            //    attempt to simplify level |4|. Level |4| does not contain any child
            //    level nodes, so there is nothing to do here.
            //
            //  |0|                    [ROOT]
            //                           |
            //  |1|          [TEST] [(] [*]
            //                           |
            //  |2|                 [(] [@]
            //                           |
            //  |3|                 [(] [*] [)]
            //                           |
            //  |4|                 [ ] [B] [ ]
            //
            #endregion

            #region 3) Add plus symbol. This obviously becomes a child of the current node,
            //    i.e. a child of level |2|.
            //
            //  |0|                            [ROOT]
            //                                   |
            //  |1|                  [TEST] [(] [*]
            //                                   |
            //  |2|                         [(] [@]
            //                                   |
            //  |3|                 [(] [*] [)] [+]
            //                           |
            //  |4|                 [ ] [B] [ ]
            //
            #endregion

            #region 4) Start new opening level node on level |3|. The newly added level node
            //    on level |3| becomes the current node.
            //
            //  |0|                            [ROOT]
            //                                   |
            //  |1|                  [TEST] [(] [*]
            //                                   |
            //  |2|                         [(] [*]
            //                                   |
            //  |3|                 [(] [*] [)] [+] [(] [@]
            //                           |               |
            //  |4|                 [ ] [B] [ ]
            //
            #endregion

            #region 5) Add the tokens near the 'C' to level |4|
            //
            //  |0|                            [ROOT]
            //                                   |
            //  |1|                  [TEST] [(] [*]
            //                                   |
            //  |2|                         [(] [*]
            //                                   |
            //  |3|                 [(] [*] [)] [+] [(] [@]
            //                           |               |
            //  |4|                 [ ] [B] [ ]     [ ] [C] [ ]
            //
            #endregion

            #region 6) Add closing level token to level |3|, which means that we can now
            //    attempt to simplify level |4|, this time the [C] branch.
            //    Since this branch does not contain any sub-level nodes, there is nothing
            //    to be done and therefore levels |3| and |4| remain unchanged.
            //    The level node at level |2| becomes the current node.
            //
            //  |0|                            [ROOT]
            //                                   |
            //  |1|                  [TEST] [(] [*]
            //                                   |
            //  |2|                         [(] [@]
            //                                   |
            //  |3|                 [(] [*] [)] [+] [(] [*] [)]
            //                           |               |
            //  |4|                 [ ] [B] [ ]     [ ] [C] [ ]
            //
            #endregion

            #region 7a) Add closing level token to level |2|, which means that we can now
            //     attempt to simplify level |3|.
            //
            //  |0|                            [ROOT]
            //                                   |
            //  |1|                  [TEST] [(] [*]
            //                                   |
            //  |2|                         [(] [@] [)]
            //                                   |
            //  |3|                 [(] [*] [)] [+] [(] [*] [)]
            //                           |               |
            //  |4|                 [ ] [B] [ ]     [ ] [C] [ ]
            //
            #endregion

            #region 7b) We iterate through all the child level nodes of level |3| looking
            //     to see if any of their children are simple enough to merge up into level
            //     |3| itself. There are two level nodes in level |3| and both contain
            //     children that are considered simple enough to merge up. This is because
            //     they contain only a single non-whitespace node so we can simplify the level by
            //     merging [B] from level |4| into level |3|. Likewise for [C].
            //
            //     This means that the bracketry on level |3| is redundant since level
            //     |4| contains two sets of only a single non-whitespace token. Level |4|
            //     is therefore entirely removed.
            //
            //     The node at level |1| becomes the current node now.
            //
            //  |0|                            [ROOT]
            //                                   |
            //  |1|                  [TEST] [(] [@]
            //                                   |
            //  |2|                         [(] [*] [)]
            //                                   |
            //  |3|                 [ ] [B] [ ] [+] [ ] [C] [ ]
            //
            #endregion

            #region 8) Add the plus symbol to level |2|. Then we start a new level
            //    as a result of adding the opening bracket prior to 'E.'
            //    This new level node at level |2| now becomes the current node.
            //
            //  |0|                                 [ROOT]
            //                                         |
            //  |1|                  [TEST]   [(]     [*]
            //                                         |
            //  |2|             [(] [*] [)]           [+] [(] [@]
            //                       |                         |
            //  |3|     [ ] [B] [ ] [+] [ ] [C] [ ]
            //
            #endregion

            #region 9) Now add the 'E' token to level |3|.
            //
            //  |0|                                 [ROOT]
            //                                         |
            //  |1|                  [TEST]   [(]     [*]
            //                                         |
            //  |2|             [(] [*] [)]           [+] [(] [@]
            //                       |                         |
            //  |3|     [ ] [B] [ ] [+] [ ] [C] [ ]           [E]
            #endregion

            #region 10) We then add the closing bracket to level |2| which means
            //  that we can attempt to simplify level |3|'s 'E' branch. There's nothing
            //  to do though, since it doesn't contain any child level nodes.
            //  The level node at level |1| again becomes current.
            //
            //  |0|                                 [ROOT]
            //                                         |
            //  |1|                  [TEST]   [(]     [@]
            //                                         |
            //  |2|             [(] [*] [)]           [+] [(] [*] [)]
            //                       |                         |
            //  |3|     [ ] [B] [ ] [+] [ ] [C] [ ]           [E]
            #endregion

            #region 11a) We then add the closing bracket to level |1| which means
            //  that we can attempt to simplify level |2| entirely. This is the
            //  situation prior to simplification.
            //
            //  |0|                                 [ROOT]
            //                                         |
            //  |1|                  [TEST]   [(]     [@] [)]
            //                                         |
            //  |2|             [(] [*] [)]           [+] [(] [*] [)]
            //                       |                         |
            //  |3|     [ ] [B] [ ] [+] [ ] [C] [ ]           [E]
            #endregion

            #region 11b) The two level nodes have been taged as *1 and *2 to make it
            //  easier to explain the process. First we attempt to simplify level *1.
            //  However, since its children consist of more than a single non-whitespace
            //  token, we cannot make level |3|'s " B + C " branch as a replacement
            //  for the bracket *1 tokens. Therefore this remains unchanged
            //
            //
            //  |0|                                [ROOT]
            //                                        |
            //  |1|                  [TEST]   [(]    [@]     [)]
            //                                        |
            //  |2|            [(] [*1] [)]          [+] [(] [*2] [)]
            //                       |                         |
            //  |3|     [ ] [B] [ ] [+] [ ] [C] [ ]           [E]
            #endregion

            #region 11c) The level *2 node, however, contains only a single non-whitespace
            //  child token, so it can be simplified. The level *2 node is replaced by
            //  its child (E).
            //
            // The final tree looks like this, with the root as the current node once more:
            //
            //
            //  |0|                                 [@ROOT@]
            //                                         |
            //  |1|        [TEST] [(]                 [*]                         [)]
            //                                         |
            //  |2|                   [(]             [*]             [)] [+] [E]
            //                                         |
            //  |3|                       [ ] [B] [ ] [+] [ ] [C] [ ]
            #endregion

            #endregion

            System.Diagnostics.Debug.Write(aToken.Value);
            System.Diagnostics.Debug.WriteLine(" ");

            // First of all, check if the current node has a parent. If we're at the root
            // node and we see a closing token, then we've got an imbalanced stack.
            if (iTree.CurrentNode.IsRoot)
            {
                NotifyEventLevelsImbalanced();
            }
            else
            {
                // We expect the parent to be a level node
                System.Diagnostics.Debug.Assert(iTree.CurrentNode is SymTokenBalancerMarkerLevelNode);

                // Notify that we're about to change levels
                SymTokenBalancerMarkerLevelNode currentLevel = (SymTokenBalancerMarkerLevelNode)iTree.CurrentNode;
                SymNode newLevel = currentLevel.Parent;

                // The new level should not be null in this case
                System.Diagnostics.Debug.Assert(newLevel != null);

                // Check whether the closing token type is the same type as was used to start
                // the level. E.g. for this case is "([ANDMORE)]" which has a mis-match
                // between the opening and closing braces on each level. We can't simplify this
                // during the 'end level behaviour' stage.
                bool levelsBalanced = currentLevel.MatchCriteria.DiametricToken.Equals(aToken);

                // Switch levels
                iTree.CurrentNode = newLevel;

                // We have to refetch up-to-date match info, since we've changed levels, and the match
                // info that was used to enter this method is associated with the previous level
                // (not the new level number, which is now one less).
                SymTokenBalancerMatchCriteria matchCriteria;
                if (IsClosingTokenMatch(aToken, out matchCriteria, CurrentLevelNumber) == false)
                {
                    matchCriteria = aMatchCriteria;
                }

                // Always store the node element so that we can balance brackets
                SymTokenBalancerNodeEmittedElement node = new SymTokenBalancerNodeEmittedElement(aToken, matchCriteria);
                iTree.CurrentNode.Add(node);

                // We have finished the current level. E.g. see step 6. Need to simplify level |2|, where possible.
                PerformEndLevelBehaviour(currentLevel);

                if (levelsBalanced)
                {
                    // Notify that we've finished some level
                    NotifyEventLevelFinished(CurrentLevelNumber, currentLevel, newLevel);
                }
                else
                {
                    // Imbalance
                    NotifyEventLevelsImbalanced();
                }
            }
        }
Example #9
0
        protected bool LevelCanBeMergedWithParent(SymTokenBalancerMarkerLevelNode aLevelNode)
        {
            #region Example
            // We can replace the opening bracket, the level marker and the closing bracket
            // with the level marker's child.
            //
            // E.g.
            // ( = opening bracket
            // * = level marker
            // ) = closing bracket
            // V = 'real' node value
            //
            // This:
            //
            //             ( -- * -- )
            //                  |
            //                  V
            //
            // Can become:
            //
            //                  V
            //
            //
            // Or this:
            //
            //             ( -- * -- )
            //                  |
            //             [ -- * -- ]
            //					|
            //                  V
            //
            // Can become:
            //
            //             [ -- * -- ]
            //					|
            //                  V
            //
            #endregion
            System.Diagnostics.Debug.Assert(aLevelNode.HasPrevious && aLevelNode.HasNext);
            System.Diagnostics.Debug.Assert(aLevelNode.Previous is SymTokenBalancerNodeEmittedElement && aLevelNode.Next is SymTokenBalancerNodeEmittedElement);
            //
            bool ret = false;
            //
            SymTokenBalancerNodeEmittedElement previous = (SymTokenBalancerNodeEmittedElement)aLevelNode.Previous;
            SymTokenBalancerNodeEmittedElement next     = (SymTokenBalancerNodeEmittedElement)aLevelNode.Next;

            // We should be able to remove these brackets, providing they are diametrically oposites.
            SymTokenBalancerMatchCriteria matchInfo;
            if (IsOpeningTokenMatch(previous.Token, out matchInfo, CurrentLevelNumber))
            {
                // We've now got the match info for this opening token. We can compare the
                // match info's diametric token against the closing token to check that they
                // really are equal and oposite.
                if (matchInfo.DiametricToken.Equals(next.Token))
                {
                    // Check whether the children are suitable for coalescing
                    int grandChildCount = aLevelNode.ChildCount;
                    int numberOfWhiteSpaceGrandChildren = CountTokenByType(aLevelNode, SymToken.TClass.EClassWhiteSpace);
                    int nonWhiteSpaceGrandChildrenCount = grandChildCount - numberOfWhiteSpaceGrandChildren;

                    if (nonWhiteSpaceGrandChildrenCount == 1)
                    {
                        // If there is just a single non-whitespace token then we are able to
                        // coalesce this branch
                        ret = true;
                    }
                    else if (nonWhiteSpaceGrandChildrenCount == 3)
                    {
                        // Branch contains several non-whitespace children. Must check further
                        // to see if they are simply bracketed regions themselves
                        object levelObject = aLevelNode.ChildByType(typeof(SymTokenBalancerMarkerLevelNode));
                        if (levelObject != null)
                        {
                            SymTokenBalancerMarkerLevelNode childLevel = (SymTokenBalancerMarkerLevelNode)levelObject;
                            //
                            if (childLevel.HasPrevious && childLevel.HasNext)
                            {
                                ret = (childLevel.Previous is SymTokenBalancerNodeEmittedElement && childLevel.Next is SymTokenBalancerNodeEmittedElement);
                            }
                        }
                        else
                        {
                            ret = true;
                        }
                    }
                }
            }
            //
            return(ret);
        }