示例#1
0
        /**
         * Removes the tree node that belongs to the stated <code>AST2JSOMBase
         * </code> from it's parent tree node. Furthermore the appropriate tokens
         * will be removed from the token stream.
         *
         * @param pNodeToRemove  The owner of the tree node that should get removed.
         * @param pLeftChangeTokenBorder  See the comments for the enumeration
         *                                declaration <code>
         *                                AST2JSOM.ChangeTokenBorder</code>. If this
         *                                argument is <code>null</code> removing
         *                                will start with the left position of the
         *                                stated node.
         * @param pRightChangeTokenBorder See the comments for the enumeration
         *                                declaration <code>
         *                                AST2JSOM.ChangeTokenBorder</code>. If this
         *                                argument is <code>null</code> removing
         *                                will end at the right position of the
         *                                stated node.
         *
         * __TEST__
         */
        protected static void removeTreeNode(
            AST2JSOMBase pNodeToRemove,
            ChangeTokenBorder? pLeftChangeTokenBorder, 
            ChangeTokenBorder? pRightChangeTokenBorder)
        {
            ITree nodeToRemove = pNodeToRemove.getTreeNode();
            ITree parent = nodeToRemove.Parent;

            // Remove the tree node from it's parent
            int childCount = parent.ChildCount;
            int childOffset = 0;
            while (childOffset < childCount) {
            if (parent.GetChild(childOffset) == nodeToRemove) {
                break;
            }
            childOffset++;
            }
            if (childOffset == childCount) {
            // If still here no matching child has been found.
            throw new JSourceObjectizerInternalException(
                    CommonJSOMMessages.getChildTreeNodeNotFoundMessage(
                            nodeToRemove.ToString(), parent.ToString()));
            }
            parent.DeleteChild(childOffset);

            // Remove the tokens from the stream.

            TokenRewriteStream stream = pNodeToRemove.getTokenRewriteStream();
            int startIndex = nodeToRemove.TokenStartIndex;
            int stopIndex = nodeToRemove.TokenStopIndex;
            if (pLeftChangeTokenBorder != null) {
            startIndex--;
            if (      pLeftChangeTokenBorder
                   == ChangeTokenBorder.FARTHEST_NEWLINE_EXCLUDING
                ||    pLeftChangeTokenBorder
                   == ChangeTokenBorder.FARTHEST_NEWLINE_INCLUDING
                ||    pLeftChangeTokenBorder
                   == ChangeTokenBorder.NEXT_NON_HIDDEN_TOKEN_EXCLUDING) {
                // At first search the first non hidden token from left.
                while (startIndex >= 0) {
                    IToken token = stream.Get(startIndex);
                    if (token.Channel != TokenChannels.Hidden) {
                        break;
                    }
                    startIndex--;
                }
                startIndex++;
                // If the farthest newline is needed search it from the current
                // position to right.
                if (   pLeftChangeTokenBorder
                    != ChangeTokenBorder.NEXT_NON_HIDDEN_TOKEN_EXCLUDING) {
                    while (true) {
                        IToken token = stream.Get(startIndex);
                        if (token.Channel != TokenChannels.Hidden) {
                            // The most left token of the node that should get
                            // removed has been reached again.
                            startIndex = nodeToRemove.TokenStartIndex;
                            break;
                        } else if (Constants.NL.Equals(token.Text)) {
                            if (   pLeftChangeTokenBorder
                                == ChangeTokenBorder.FARTHEST_NEWLINE_EXCLUDING) {
                                startIndex++;
                            }
                            break;
                        }
                        startIndex++;
                    }
                }
            } else if (      pLeftChangeTokenBorder
                          == ChangeTokenBorder.NEXT_NEWLINE_EXCLUDING
                       ||    pLeftChangeTokenBorder
                          == ChangeTokenBorder.NEXT_NEWLINE_INCLUDING) {
                while (startIndex >= 0) {
                    IToken token = stream.Get(startIndex);
                    if (Constants.NL.Equals(token.Text)) {
                        if (   pLeftChangeTokenBorder
                            == ChangeTokenBorder.NEXT_NEWLINE_EXCLUDING) {
                            startIndex++;
                        }
                        break;
                    } else if (token.Channel != TokenChannels.Hidden) {
                        // No new line found.
                        startIndex = nodeToRemove.TokenStartIndex;
                        break;
                    }
                    startIndex--;
                }
            } else {
                // TODO  Throw exception for unknown ChangeTokenBorder constant.
            }
            }
            if (pRightChangeTokenBorder != null) {
            stopIndex++;
            if (       pRightChangeTokenBorder
                    == ChangeTokenBorder.FARTHEST_NEWLINE_EXCLUDING
                 ||    pRightChangeTokenBorder
                    == ChangeTokenBorder.FARTHEST_NEWLINE_INCLUDING
                 ||    pRightChangeTokenBorder
                    == ChangeTokenBorder.NEXT_NON_HIDDEN_TOKEN_EXCLUDING) {
                // At first search the first non hidden token from right.
                while (true) {
                    IToken token = stream.Get(stopIndex);
                    if (   token.Channel != TokenChannels.Hidden
                        || token.Type == CharStreamConstants.EndOfFile)
                    {
                        break;
                    }
                    stopIndex++;
                }
                stopIndex--;
                // If the farthest newline is needed search it from the current
                // position to left.
                if (   pRightChangeTokenBorder
                    != ChangeTokenBorder.NEXT_NON_HIDDEN_TOKEN_EXCLUDING) {
                    while (true) {
                        IToken token = stream.Get(stopIndex);
                        if (token.Channel != TokenChannels.Hidden) {
                            // The most right token of the node that should get
                            //removed has been reached again.
                            stopIndex = nodeToRemove.TokenStopIndex;
                            break;
                        } else if (Constants.NL.Equals(token.Text)) {
                            if (   pRightChangeTokenBorder
                                == ChangeTokenBorder.FARTHEST_NEWLINE_EXCLUDING) {
                                stopIndex--;
                            }
                            break;
                        }
                        stopIndex--;
                    }
                }
            } else if (      pRightChangeTokenBorder
                          == ChangeTokenBorder.NEXT_NEWLINE_EXCLUDING
                       ||    pRightChangeTokenBorder
                          == ChangeTokenBorder.NEXT_NEWLINE_INCLUDING) {
                while (true) {
                    IToken token = stream.Get(stopIndex);
                    if (Constants.NL.Equals(token.Text)) {
                        if (   pRightChangeTokenBorder
                            == ChangeTokenBorder.NEXT_NEWLINE_EXCLUDING) {
                            stopIndex--;
                        }
                        break;
                    } else if (token.Channel != TokenChannels.Hidden) {
                        // No new line found.
                        stopIndex = nodeToRemove.TokenStopIndex;
                        break;
                    }
                    stopIndex++;
                }
            } else {
                // TODO  Throw exception for unknown ChangeTokenBorder constant.
            }
            }
            pNodeToRemove.getTokenRewriteStream().Delete(startIndex, stopIndex);
        }
示例#2
0
 /**
  * Removes the tree node that belongs to the stated <code>AST2JSOMBase
  * </code> from it's parent tree node. Furthermore the appropriate tokens
  * will be removed from the token stream.
  *
  * @param pNodeToRemove  The owner of the tree node that should get removed.
  * @param pFromNode If not <code>null</code> all tokens from the right of
  *                  this object up to the tokens that belong to <code>
  *                  pNodeToRemove</code> will be removed, too.
  * @param pToNode  If not <code>null</code> all tokens from the left of
  *                  this object up to the tokens that belong to <code>
  *                  pNodeToRemove</code> will be removed, too.
  *
  * __TEST__
  */
 protected static void removeTreeNode(
     AST2JSOMBase pNodeToRemove, AST2JSOMBase pFromNode, 
     AST2JSOMBase pToNode)
 {
     ITree nodeToRemove = pNodeToRemove.getTreeNode();
     int startIndex = 0;
     int stopIndex = 0;
     if (pFromNode != null) {
     startIndex = pFromNode.getTreeNode().TokenStopIndex + 1;
     } else {
     startIndex = nodeToRemove.TokenStartIndex;
     }
     if (pToNode != null) {
     stopIndex = pToNode.getTreeNode().TokenStartIndex - 1;
     } else {
     stopIndex = nodeToRemove.TokenStopIndex;
     }
     pNodeToRemove.getTokenRewriteStream().Delete(startIndex, stopIndex);
     ITree parent = nodeToRemove.Parent;
     int childCount = parent.ChildCount;
     for (int offset = 0; offset < childCount; offset++) {
     if (parent.GetChild(offset) == nodeToRemove) {
         parent.DeleteChild(offset);
         return;
     }
     }
     // If still here no matching child has been found.
     throw new JSourceObjectizerInternalException(
         CommonJSOMMessages.getChildTreeNodeNotFoundMessage(
                 nodeToRemove.ToString(), parent.ToString()));
 }
示例#3
0
 /**
  * Removes the tree node that belongs to the stated <code>AST2JSOMBase
  * </code> from it's parent tree node. Furthermore the appropriate tokens
  * will be removed from the token stream.
  *
  * @param pNodeToRemove  The owner of the tree node that should get removed.
  *
  * @throws JSourceObjectizerInternalException  If the stated tree node is
  *                                             not a child of the stated
  *                                             parent node. I.e. it's up to
  *                                             the caller to ensure that the
  *                                             child exists.
  *
  * __TEST__
  */
 protected static void removeTreeNode(AST2JSOMBase pNodeToRemove)
 {
     removeTreeNode(pNodeToRemove, (AST2JSOMBase) null, null);
 }