示例#1
0
        // routines related to "reconstructing" the syntax tree from the saved tokens to do context checks

        /// <summary>
        /// Returns the context object for the given token index, ignoring empty fragments.
        /// Throws an ArgumentNullException if the given token index is null.
        /// </summary>
        private static Context.SyntaxTokenContext GetContext(this CodeFragment.TokenIndex tokenIndex)
        {
            if (tokenIndex == null)
            {
                throw new ArgumentNullException(nameof(tokenIndex));
            }
            QsNullable <QsFragmentKind> Nullable(CodeFragment fragment) =>
            fragment?.Kind == null
                ? QsNullable <QsFragmentKind> .Null
                : fragment.IncludeInCompilation
                ? QsNullable <QsFragmentKind> .NewValue(fragment.Kind)
                : QsNullable <QsFragmentKind> .NewValue(QsFragmentKind.InvalidFragment);

            var self         = tokenIndex.GetFragment();
            var previous     = tokenIndex.PreviousOnScope()?.GetFragment(); // excludes empty tokens
            var next         = tokenIndex.NextOnScope()?.GetFragment();     // excludes empty tokens
            var parents      = tokenIndex.GetNonEmptyParents().Select(tIndex => Nullable(tIndex.GetFragment())).ToArray();
            var nullableSelf = self?.Kind == null                           // special treatment such that errors for fragments excluded from compilation still get logged...
                ? QsNullable <QsFragmentKind> .Null
                : QsNullable <QsFragmentKind> .NewValue(self.Kind);

            var headerRange = self?.HeaderRange ?? QsCompilerDiagnostic.DefaultRange;

            return(new Context.SyntaxTokenContext(headerRange, nullableSelf, Nullable(previous), Nullable(next), parents));
        }
        // routines related to "reconstructing" the syntax tree from the saved tokens to do context checks

        /// <summary>
        /// Returns the context object for the given token index, ignoring empty fragments.
        /// </summary>
        private static Context.SyntaxTokenContext GetContext(this CodeFragment.TokenIndex tokenIndex)
        {
            QsNullable <QsFragmentKind> Nullable(CodeFragment?token, bool precedesSelf) =>
            token?.Kind == null
                ? QsNullable <QsFragmentKind> .Null
                : precedesSelf && !token.IncludeInCompilation // fragments that *follow * self need to be re-evaluated first
                    ? QsNullable <QsFragmentKind> .NewValue(QsFragmentKind.InvalidFragment)
                    : QsNullable <QsFragmentKind> .NewValue(token.Kind);

            var fragment    = tokenIndex.GetFragment();
            var headerRange = fragment?.HeaderRange ?? Range.Zero;

            var self     = Nullable(fragment, false);                                   // making sure that errors for fragments excluded from compilation still get logged
            var previous = Nullable(tokenIndex.PreviousOnScope()?.GetFragment(), true); // excludes empty tokens
            var next     = Nullable(tokenIndex.NextOnScope()?.GetFragment(), false);    // excludes empty tokens
            var parents  = tokenIndex.GetNonEmptyParents().Select(tIndex => Nullable(tIndex.GetFragment(), true)).ToArray();

            return(new Context.SyntaxTokenContext(headerRange, self, previous, next, parents));
        }