// 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)); }
/// <summary> /// Returnes an IEnumerable with the indices of all children of the given token. /// If deep is set to true (default value), then the returned children are all following tokens /// with a higher indentation level than the token corresponding to tIndex /// up to the point where we are at the same indentation level again. /// If deep is set to false, then of those only the tokens with an indentation level that is precisely /// one larger than the one of the parent token are returned. /// Throws an ArgumentNullException if tIndex is null. /// </summary> internal static IEnumerable <CodeFragment.TokenIndex> GetChildren(this CodeFragment.TokenIndex tIndex, bool deep = true) { if (tIndex == null) { throw new ArgumentNullException(nameof(tIndex)); } var tokenIndex = new CodeFragment.TokenIndex(tIndex); var indentation = tokenIndex.GetFragment().Indentation; while (++tokenIndex != null && tokenIndex.GetFragment().Indentation > indentation) { if (deep || tokenIndex.GetFragment().Indentation == indentation + 1) { yield return(tokenIndex); } } }
/// <summary> /// Returns the index of the next non-empty token on the same indenation level, or null if no such token exists. /// Includes empty tokens if includeEmpty is set to true. /// Throws an ArgumentNullException if tIndex is null. /// </summary> internal static CodeFragment.TokenIndex NextOnScope(this CodeFragment.TokenIndex tIndex, bool includeEmpty = false) { if (tIndex == null) { throw new ArgumentNullException(nameof(tIndex)); } var tokenIndex = new CodeFragment.TokenIndex(tIndex); var indentation = tokenIndex.GetFragment().Indentation; while (++tokenIndex != null) { var fragment = tokenIndex.GetFragment(); if (fragment.Indentation <= indentation && (fragment.Kind != null || includeEmpty)) { break; } } return(tokenIndex != null && tokenIndex.GetFragment().Indentation == indentation ? tokenIndex : null); }
/// <summary> /// Returns the index of the closest preceding non-empty token with the next lower indentation level. /// Returns null if no such token exists. /// Throws an ArgumentNullException if tIndex is null. /// </summary> private static CodeFragment.TokenIndex GetNonEmptyParent(this CodeFragment.TokenIndex tIndex) { if (tIndex == null) { throw new ArgumentNullException(nameof(tIndex)); } var tokenIndex = new CodeFragment.TokenIndex(tIndex); var indentation = tokenIndex.GetFragment().Indentation; while (--tokenIndex != null) { var fragment = tokenIndex.GetFragment(); if (fragment.Kind != null && fragment.Indentation < indentation) { break; // ignore empty fragments } } return(tokenIndex != null && tokenIndex.GetFragment().Indentation == indentation - 1 ? tokenIndex : null); }
// 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)); }