/// <summary>
        /// Invert the all of the paths in an expandToken, such that they are now in the same order as they are present in the
        /// base url
        /// </summary>
        /// <param name="treeToInvert">the tree to invert paths on</param>
        /// <returns>a new tree with all of its paths inverted</returns>
        public ExpandToken NormalizePaths(ExpandToken treeToInvert)
        {
            // iterate through each expand term token, and reverse the tree in its path property
            List <ExpandTermToken> updatedTerms = new List <ExpandTermToken>();

            foreach (ExpandTermToken term in treeToInvert.ExpandTerms)
            {
                PathReverser     pathReverser = new PathReverser();
                PathSegmentToken reversedPath = term.PathToNavProp.Accept(pathReverser);

                // we also need to call the select token normalizer for this level to reverse the select paths
                SelectToken newSelectToken = term.SelectOption;
                if (term.SelectOption != null)
                {
                    SelectTreeNormalizer selectTreeNormalizer = new SelectTreeNormalizer();
                    newSelectToken = selectTreeNormalizer.NormalizeSelectTree(term.SelectOption);
                }

                ExpandToken subExpandTree;
                if (term.ExpandOption != null)
                {
                    subExpandTree = this.NormalizePaths(term.ExpandOption);
                }
                else
                {
                    subExpandTree = null;
                }

                ExpandTermToken newTerm = new ExpandTermToken(reversedPath, term.FilterOption, term.OrderByOptions, term.TopOption, term.SkipOption, term.CountQueryOption, term.LevelsOption, term.SearchOption, newSelectToken, subExpandTree);
                updatedTerms.Add(newTerm);
            }

            return(new ExpandToken(updatedTerms));
        }
        /// <summary>
        /// Normalize a SelectToken into something that can be used to trim an expand tree.
        /// </summary>
        /// <param name="treeToNormalize">The select token to normalize</param>
        /// <returns>Normalized SelectToken</returns>
        public SelectToken NormalizeSelectTree(SelectToken treeToNormalize)
        {
            PathReverser            pathReverser  = new PathReverser();
            List <PathSegmentToken> invertedPaths = (from property in treeToNormalize.Properties
                                                     select property.Accept(pathReverser)).ToList();

            // to normalize a select token we just need to invert its paths, so that
            // we match the ordering on an ExpandToken.
            return(new SelectToken(invertedPaths));
        }
        /// <summary>
        /// Build the next level PathReverser
        /// </summary>
        /// <param name="nextLevelToken">the next level token</param>
        /// <param name="nextChildToken">the next levels child token</param>
        /// <returns>the path token from the next level.</returns>
        private static PathSegmentToken BuildNextStep(PathSegmentToken nextLevelToken, PathSegmentToken nextChildToken)
        {
            PathReverser nextStepReverser = new PathReverser(nextChildToken);

            return(nextLevelToken.Accept(nextStepReverser));
        }