コード例 #1
0
        protected bool IsTokenExactMatch(SymToken aToken, SymTokenContainer aContainerToSearch)
        {
            bool ret = false;

            //
            foreach (SymToken t in aContainerToSearch)
            {
                if (t.Equals(aToken))
                {
                    if (aToken.Tag != null && t.Tag != null)
                    {
                        if ((aToken.Tag is SymTokenBalancerMatchCriteria) && (t.Tag is SymTokenBalancerMatchCriteria))
                        {
                            SymTokenBalancerMatchCriteria extendedInfo1 = (SymTokenBalancerMatchCriteria)aToken.Tag;
                            SymTokenBalancerMatchCriteria extendedInfo2 = (SymTokenBalancerMatchCriteria)t.Tag;
                            //
                            if (extendedInfo1.Equals(extendedInfo2))
                            {
                                ret = true;
                                break;
                            }
                        }
                    }
                }
            }
            //
            return(ret);
        }
コード例 #2
0
        protected bool IsClosingTokenMatch(SymToken aToken, out SymTokenBalancerMatchCriteria aCriteria, int aLevelNumber)
        {
            aCriteria = null;
            bool matchFound = false;
            //
            int index = iClosingTokens.IndexOf(aToken);

            while (index >= 0 && matchFound == false)
            {
                SymToken token = iClosingTokens[index];
                System.Diagnostics.Debug.Assert(token.Tag != null && token.Tag is SymTokenBalancerMatchCriteria);
                SymTokenBalancerMatchCriteria criteria = (SymTokenBalancerMatchCriteria)token.Tag;

                if (criteria.Matches(aLevelNumber))
                {
                    aCriteria  = criteria;
                    matchFound = true;
                }
                else
                {
                    index = iClosingTokens.IndexOf(aToken, index + 1);
                }
            }

            return(matchFound);
        }
コード例 #3
0
        public void RegisterClosingToken(SymToken aToken, SymTokenBalancerMatchCriteria aCriteria)
        {
            SymToken copy = new SymToken(aToken);

            copy.Tag = aCriteria;
            //
            if (IsTokenExactMatch(copy, iClosingTokens) == false)
            {
                iClosingTokens.Append(copy);
            }
        }
コード例 #4
0
        protected virtual void PerformEndLevelBehaviour(SymNode aLevel, SymTokenBalancerMatchCriteria aCriteria)
        {
            #region Example step (11a) from LevelFinished method
            //
            // 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]
            //
            // aLevel would be the @ node at level |1|.
            //
            // We remove redundant bracketing from our children, i.e. those on level |2|, not from our own level.
            // Our parent takes care of removing this level's redundant bracketing (when it's level is completed)
            // or then when an argument separator token is handled.
            //
            // We must iterate through level |1|'s children to find other level nodes. We check whether each
            // child level node can be simplified by checking its children (i.e. our grandchildren).
            //
            #endregion

            if (aCriteria.IsAssociatedBehaviourRemoveRedundantBracketing)
            {
                int    index = 0;
                object childLevelNodeObject = aLevel.ChildByType(typeof(SymTokenBalancerMarkerLevelNode), ref index);
                while (childLevelNodeObject != null)
                {
                    SymTokenBalancerMarkerLevelNode childLevelNode = (SymTokenBalancerMarkerLevelNode)childLevelNodeObject;
                    if (childLevelNode.CanBeSubsumed)
                    {
                        childLevelNode.Subsume();
                    }

                    // Try to find next level node
                    ++index;
                    childLevelNodeObject = aLevel.ChildByType(typeof(SymTokenBalancerMarkerLevelNode), ref index);
                }
            }
        }
コード例 #5
0
        public override bool Equals(object aObject)
        {
            bool same = false;

            //
            if (aObject is SymTokenBalancerMatchCriteria)
            {
                SymTokenBalancerMatchCriteria otherInfo = (SymTokenBalancerMatchCriteria)aObject;
                //
                same = (Emit == otherInfo.Emit) &&
                       (ChangesLevel == otherInfo.ChangesLevel) &&
                       (LevelExpectations == otherInfo.LevelExpectations) &&
                       (AssociatedLevel == otherInfo.AssociatedLevel) &&
                       (AssociatedBehaviour == otherInfo.AssociatedBehaviour);
            }
            //
            return(same);
        }
コード例 #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;
        }
コード例 #7
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();
                }
            }
        }
コード例 #8
0
        public void RegisterClosingToken(SymToken aToken, SymToken aDiametricToken, bool aEmit, bool aStartsNewLevel, TLevelExpectations aLevelExpectations, int aAssociatedLevel, TAssociatedBehaviour aBehaviour)
        {
            SymTokenBalancerMatchCriteria criteria = new SymTokenBalancerMatchCriteria(aDiametricToken, aEmit, aStartsNewLevel, aLevelExpectations, aAssociatedLevel, aBehaviour);

            RegisterClosingToken(aToken, criteria);
        }
コード例 #9
0
 public SymTokenBalancerMarkerNode(SymTokenBalancerMatchCriteria aMatchCriteria)
 {
     iMatchCriteria = aMatchCriteria;
 }
コード例 #10
0
 public SymTokenBalancerNodeEmittedElement(SymToken aToken, SymTokenBalancerMatchCriteria aMatchCriteria)
     : base(aMatchCriteria)
 {
     iToken = aToken;
 }
コード例 #11
0
 public SymTokenBalancerMarkerArgumentNode(SymTokenBalancerMatchCriteria aMatchCriteria)
     : base(aMatchCriteria)
 {
 }
コード例 #12
0
 public SymTokenBalancerMarkerLevelNode(SymTokenBalancerMatchCriteria aMatchCriteria)
     : base(aMatchCriteria)
 {
 }