Exemple #1
0
        /// <exception cref="UnparsableAst.NonCalculatedException">
        /// If topLeft is non-calculated or thrown out.
        /// </exception>
        private static IEnumerable <UtokenBase> _YieldIndentation(UnparsableAst self, ref BlockIndentation blockIndentationParameter, Unparser.Direction direction,
                                                                  FormatYielder formatYielder, bool left)
        {
            if (!left && direction == Unparser.Direction.RightToLeft && blockIndentationParameter.IsDeferred() && blockIndentationParameter.LeftDeferred != null)
            {
                /*
                 * We are in a right-to-left unparse and this deferred right indentation depends on a deferred left indentation,
                 * so let's try to calculate the deferred left indentation, which - if succeed - will change the shared blockIndentation
                 * object state from deferred to a valid indentation.
                 *
                 * (If the left indentation wasn't deferred then it would be calculated which means that the shared blockIndentation
                 * object won't be deferred.)
                 * */
                blockIndentationParameter.LeftDeferred.CalculateUtokens();

                // either CalculateUtokens succeeded, and blockIndentation is not deferred, or CalculateUtokens threw a NonCalculatedException exception

                Debug.Assert(!blockIndentationParameter.IsDeferred(), "CalculateUtokens succeeded, but blockIndentation remained deferred");
            }

            if (blockIndentationParameter == BlockIndentation.ToBeSet || blockIndentationParameter.IsDeferred())
            {
                UnparsableAst    leftObject       = GetLeftTerminalLeave(self);
                BlockIndentation blockIndentation = formatYielder._GetBlockIndentation(leftObject, self);

                // NOTE: topAncestorCacheForLeft gets updated by GetUsedLeftsFromTopToBottomB

                if (blockIndentation != BlockIndentation.IndentNotNeeded)
                {
                    Unparser.tsUnparse.Debug("blockindentation {0} for leftTarget '{1}' and for target '{2}'", blockIndentation, leftObject, self);
                }

                Debug.Assert(!blockIndentation.IsDeferred());

                if (blockIndentationParameter == BlockIndentation.ToBeSet)
                {
                    blockIndentationParameter = blockIndentation;
                }
                else
                {
                    blockIndentationParameter.CopyKindFrom(blockIndentation);
                }
            }

            if (direction == Unparser.Direction.LeftToRight)
            {
                return(left
                    ? BlockIndentationToUtokenControlBefore(blockIndentationParameter)
                    : BlockIndentationToUtokenControlAfter(blockIndentationParameter));
            }
            else
            {
                return(left
                    ? BlockIndentationToUtokenControlAfter(blockIndentationParameter)
                    : BlockIndentationToUtokenControlBefore(blockIndentationParameter));
            }
        }
Exemple #2
0
        /// <exception cref="UnparsableAst.NonCalculatedException">
        /// If topLeft is non-calculated or thrown out.
        /// </exception>
        private static IEnumerable <UtokenBase> _YieldIndentation(UnparsableAst self, BlockIndentation blockIndentationParameter, Unparser.Direction direction,
                                                                  FormatYielder formatYielder, bool left)
        {
#if DEBUG
            BlockIndentation originalBlockIndentationParameter = blockIndentationParameter;
#endif
            var utokens = _YieldIndentation(self, ref blockIndentationParameter, direction, formatYielder, left);
#if DEBUG
            Debug.Assert(object.ReferenceEquals(blockIndentationParameter, originalBlockIndentationParameter),
                         string.Format("unwanted change of blockIndentationParameter reference in _YieldIndentation ({0})", left ? "left" : "right"));
#endif
            return(utokens);
        }
Exemple #3
0
        /// <exception cref="UnparsableAst.NonCalculatedException">
        /// If topLeft is non-calculated or thrown out.
        /// </exception>
        private static IEnumerable <UtokenBase> _YieldBetween(UnparsableAst self, FormatYielder formatYielder)
        {
            // NOTE: topAncestorCacheForLeft may get updated by GetUsedLeftsFromTopToBottomB

            UnparsableAst leftObject = GetLeftTerminalLeave(self);

            if (leftObject != null)
            {
                InsertedUtokens insertedUtokensBetween = formatYielder._GetUtokensBetween(leftObject, self);

                if (insertedUtokensBetween != InsertedUtokens.None)
                {
                    Unparser.tsUnparse.Debug("inserted utokens: {0}", insertedUtokensBetween);
                    yield return(insertedUtokensBetween);
                }
            }
        }
Exemple #4
0
        private static UnparsableAst GetTopAncestorForLeft(UnparsableAst self, FormatYielder formatYielder = null)
        {
            // NOTE: topAncestorCacheForLeft will not be changed if we have no formatter, that's why we use topAncestorForLeft + the static vs. instance behavior
            UnparsableAst topAncestorForLeft;

            if (formatYielder == null || !UnparsableAst.IsCalculated(formatYielder.topAncestorCacheForLeft))
            {
                topAncestorForLeft = CalculateTopAncestorForLeft(self);

                if (formatYielder != null)
                {
                    formatYielder.topAncestorCacheForLeft = topAncestorForLeft;
                }
            }
            else
            {
                topAncestorForLeft = formatYielder.topAncestorCacheForLeft;
                //Unparser.tsUnparse.Debug(formatter.topAncestorCacheForLeft != CalculateTopAncestorForLeft(self),
                //    "!!!!!!!! should be equal for {0}, but topAncestorCacheForLeft is '{1}' and calculated value is '{2}'", self, formatter.topAncestorCacheForLeft, CalculateTopAncestorForLeft(self));
                Debug.Assert(formatYielder.topAncestorCacheForLeft == CalculateTopAncestorForLeft(self));
            }

            return(topAncestorForLeft);
        }
Exemple #5
0
        /// <exception cref="UnparsableAst.NonCalculatedException">
        /// If topLeft is non-calculated.
        /// </exception>
        private static UnparsableAst GetLeftTerminalLeave(UnparsableAst self, FormatYielder formatYielder = null)
        {
            // NOTE: topAncestorCacheForLeft will not be changed if we have no formatter, that's why we use topAncestorForLeft + the static vs. instance behavior
            UnparsableAst leftTerminalLeave;

            if (formatYielder == null || !UnparsableAst.IsCalculated(formatYielder.topAncestorCacheForLeft))
            {
                leftTerminalLeave = CalculateLeftTerminalLeave(self, formatYielder);

                if (formatYielder != null)
                {
                    formatYielder.leftTerminalLeaveCache = leftTerminalLeave;
                }
            }
            else
            {
                leftTerminalLeave = formatYielder.leftTerminalLeaveCache;
                //Unparser.tsUnparse.Debug(formatter.leftTerminalLeaveCache != CalculateLeftTerminalLeave(self, formatter),
                //    "!!!!!!!! should be equal for {0}, but leftTerminalLeaveCache is '{1}' and calculated value is '{2}'", self, formatter.leftTerminalLeaveCache, CalculateLeftTerminalLeave(self, formatter));
                Debug.Assert(formatYielder.leftTerminalLeaveCache == CalculateLeftTerminalLeave(self, formatYielder));
            }

            return(leftTerminalLeave);
        }
Exemple #6
0
        /// <exception cref="UnparsableAst.NonCalculatedException">
        /// If topLeft is non-calculated.
        /// </exception>
        private static IEnumerable <UnparsableAst> GetLeftsFromTopToBottom(UnparsableAst self, FormatYielder formatYielder = null)
        {
            UnparsableAst topAncestorForLeft = GetTopAncestorForLeft(self, formatYielder);

            return(topAncestorForLeft != null
                ? Util.RecurseStopBeforeNull(topAncestorForLeft.LeftSibling, current => current.RightMostChild)
                : Enumerable.Empty <UnparsableAst>());
        }
Exemple #7
0
        private FormatYielder(FormatYielder that)
        {
            this.formatter = that.formatter;

            this.direction = that.direction;
        }
Exemple #8
0
        private FormatYielder(FormatYielder that)
        {
            this.formatter = that.formatter;

            this.direction = that.direction;
        }
Exemple #9
0
        /// <exception cref="UnparsableAst.NonCalculatedException">
        /// If topLeft is non-calculated.
        /// </exception>
        private static UnparsableAst GetLeftTerminalLeave(UnparsableAst self, FormatYielder formatYielder = null)
        {
            // NOTE: topAncestorCacheForLeft will not be changed if we have no formatter, that's why we use topAncestorForLeft + the static vs. instance behavior
            UnparsableAst leftTerminalLeave;

            if (formatYielder == null || !UnparsableAst.IsCalculated(formatYielder.topAncestorCacheForLeft))
            {
                leftTerminalLeave = CalculateLeftTerminalLeave(self, formatYielder);

                if (formatYielder != null)
                    formatYielder.leftTerminalLeaveCache = leftTerminalLeave;
            }
            else
            {
                leftTerminalLeave = formatYielder.leftTerminalLeaveCache;
                //Unparser.tsUnparse.Debug(formatter.leftTerminalLeaveCache != CalculateLeftTerminalLeave(self, formatter),
                //    "!!!!!!!! should be equal for {0}, but leftTerminalLeaveCache is '{1}' and calculated value is '{2}'", self, formatter.leftTerminalLeaveCache, CalculateLeftTerminalLeave(self, formatter));
                Debug.Assert(formatYielder.leftTerminalLeaveCache == CalculateLeftTerminalLeave(self, formatYielder));
            }

            return leftTerminalLeave;
        }
Exemple #10
0
        /// <exception cref="UnparsableAst.NonCalculatedException">
        /// If topLeft is non-calculated.
        /// </exception>
        private static IEnumerable<UnparsableAst> GetLeftsFromTopToBottom(UnparsableAst self, FormatYielder formatYielder = null)
        {
            UnparsableAst topAncestorForLeft = GetTopAncestorForLeft(self, formatYielder);

            return topAncestorForLeft != null
                ? Util.RecurseStopBeforeNull(topAncestorForLeft.LeftSibling, current => current.RightMostChild)
                : Enumerable.Empty<UnparsableAst>();
        }
Exemple #11
0
        private static UnparsableAst GetTopAncestorForLeft(UnparsableAst self, FormatYielder formatYielder = null)
        {
            // NOTE: topAncestorCacheForLeft will not be changed if we have no formatter, that's why we use topAncestorForLeft + the static vs. instance behavior
            UnparsableAst topAncestorForLeft;

            if (formatYielder == null || !UnparsableAst.IsCalculated(formatYielder.topAncestorCacheForLeft))
            {
                topAncestorForLeft = CalculateTopAncestorForLeft(self);

                if (formatYielder != null)
                    formatYielder.topAncestorCacheForLeft = topAncestorForLeft;
            }
            else
            {
                topAncestorForLeft = formatYielder.topAncestorCacheForLeft;
                //Unparser.tsUnparse.Debug(formatter.topAncestorCacheForLeft != CalculateTopAncestorForLeft(self),
                //    "!!!!!!!! should be equal for {0}, but topAncestorCacheForLeft is '{1}' and calculated value is '{2}'", self, formatter.topAncestorCacheForLeft, CalculateTopAncestorForLeft(self));
                Debug.Assert(formatYielder.topAncestorCacheForLeft == CalculateTopAncestorForLeft(self));
            }

            return topAncestorForLeft;
        }
Exemple #12
0
        /// <exception cref="UnparsableAst.NonCalculatedException">
        /// If topLeft is non-calculated or thrown out.
        /// </exception>
        private static IEnumerable<UtokenBase> _YieldIndentation(UnparsableAst self, ref BlockIndentation blockIndentationParameter, Unparser.Direction direction,
            FormatYielder formatYielder, bool left)
        {
            if (!left && direction == Unparser.Direction.RightToLeft && blockIndentationParameter.IsDeferred() && blockIndentationParameter.LeftDeferred != null)
            {
                /*
                 * We are in a right-to-left unparse and this deferred right indentation depends on a deferred left indentation,
                 * so let's try to calculate the deferred left indentation, which - if succeed - will change the shared blockIndentation
                 * object state from deferred to a valid indentation.
                 * 
                 * (If the left indentation wasn't deferred then it would be calculated which means that the shared blockIndentation
                 * object won't be deferred.)
                 * */
                blockIndentationParameter.LeftDeferred.CalculateUtokens();

                // either CalculateUtokens succeeded, and blockIndentation is not deferred, or CalculateUtokens threw a NonCalculatedException exception

                Debug.Assert(!blockIndentationParameter.IsDeferred(), "CalculateUtokens succeeded, but blockIndentation remained deferred");
            }

            if (blockIndentationParameter == BlockIndentation.ToBeSet || blockIndentationParameter.IsDeferred())
            {
                UnparsableAst leftObject = GetLeftTerminalLeave(self);
                BlockIndentation blockIndentation = formatYielder._GetBlockIndentation(leftObject, self);

                // NOTE: topAncestorCacheForLeft gets updated by GetUsedLeftsFromTopToBottomB

                if (blockIndentation != BlockIndentation.IndentNotNeeded)
                    Unparser.tsUnparse.Debug("blockindentation {0} for leftTarget '{1}' and for target '{2}'", blockIndentation, leftObject, self);

                Debug.Assert(!blockIndentation.IsDeferred());

                if (blockIndentationParameter == BlockIndentation.ToBeSet)
                    blockIndentationParameter = blockIndentation;
                else
                    blockIndentationParameter.CopyKindFrom(blockIndentation);
            }

            if (direction == Unparser.Direction.LeftToRight)
            {
                return left
                    ? BlockIndentationToUtokenControlBefore(blockIndentationParameter)
                    : BlockIndentationToUtokenControlAfter(blockIndentationParameter);
            }
            else
            {
                return left
                    ? BlockIndentationToUtokenControlAfter(blockIndentationParameter)
                    : BlockIndentationToUtokenControlBefore(blockIndentationParameter);
            }
        }
Exemple #13
0
        /// <exception cref="UnparsableAst.NonCalculatedException">
        /// If topLeft is non-calculated or thrown out.
        /// </exception>
        private static IEnumerable<UtokenBase> _YieldIndentation(UnparsableAst self, BlockIndentation blockIndentationParameter, Unparser.Direction direction,
            FormatYielder formatYielder, bool left)
        {
#if DEBUG
            BlockIndentation originalBlockIndentationParameter = blockIndentationParameter;
#endif
            var utokens = _YieldIndentation(self, ref blockIndentationParameter, direction, formatYielder, left);
#if DEBUG
            Debug.Assert(object.ReferenceEquals(blockIndentationParameter, originalBlockIndentationParameter),
                string.Format("unwanted change of blockIndentationParameter reference in _YieldIndentation ({0})", left ? "left" : "right"));
#endif
            return utokens;
        }
Exemple #14
0
        /// <exception cref="UnparsableAst.NonCalculatedException">
        /// If topLeft is non-calculated or thrown out.
        /// </exception>
        private static IEnumerable<UtokenBase> _YieldBetween(UnparsableAst self, FormatYielder formatYielder)
        {
            // NOTE: topAncestorCacheForLeft may get updated by GetUsedLeftsFromTopToBottomB

            UnparsableAst leftObject = GetLeftTerminalLeave(self);

            if (leftObject != null)
            {
                InsertedUtokens insertedUtokensBetween = formatYielder._GetUtokensBetween(leftObject, self);

                if (insertedUtokensBetween != InsertedUtokens.None)
                {
                    Unparser.tsUnparse.Debug("inserted utokens: {0}", insertedUtokensBetween);
                    yield return insertedUtokensBetween;
                }
            }
        }
Exemple #15
0
 private static UnparsableAst CalculateLeftTerminalLeave(UnparsableAst self, FormatYielder formatYielder)
 {
     return GetLeftsFromTopToBottom(self, formatYielder).LastOrDefault();
 }
Exemple #16
0
 private static UnparsableAst CalculateLeftTerminalLeave(UnparsableAst self, FormatYielder formatYielder)
 {
     return(GetLeftsFromTopToBottom(self, formatYielder).LastOrDefault());
 }
Exemple #17
0
 internal static IEnumerable <Utoken> Cook(this IEnumerable <UtokenBase> utokens, IPostProcessHelper postProcessHelper)
 {
     return(FormatYielder.PostProcess(utokens, postProcessHelper));
 }