Ejemplo n.º 1
0
        private void Print(SyntaxNodeOrToken node, bool dump)
        {
            if (dump)
            {
                switch (node.Kind())
                {
                case SyntaxKind.IdentifierToken:
                case SyntaxKind.NumericLiteralToken:
                    if (node.IsMissing)
                    {
                        goto default;
                    }
                    _output.WriteLine(
                        @"N(SyntaxKind.{0}, ""{1}"");",
                        node.Kind(),
                        node.ToString()
                        );
                    break;

                default:
                    _output.WriteLine(
                        "{0}(SyntaxKind.{1});",
                        node.IsMissing ? "M" : "N",
                        node.Kind()
                        );
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        private void Print(SyntaxNodeOrToken node, bool dump)
        {
            if (dump)
            {
                switch (node.Kind())
                {
                case SyntaxKind.IdentifierToken:
                case SyntaxKind.NumericLiteralToken:
                case SyntaxKind.StringLiteralToken:
                case SyntaxKind.UTF8StringLiteralToken:
                case SyntaxKind.SingleLineRawStringLiteralToken:
                case SyntaxKind.UTF8SingleLineRawStringLiteralToken:
                case SyntaxKind.MultiLineRawStringLiteralToken:
                case SyntaxKind.UTF8MultiLineRawStringLiteralToken:
                    if (node.IsMissing)
                    {
                        goto default;
                    }
                    var value = node.ToString().Replace("\"", "\\\"");
                    _output.WriteLine(@"N(SyntaxKind.{0}, ""{1}"");", node.Kind(), value);
                    break;

                default:
                    _output.WriteLine("{0}(SyntaxKind.{1});", node.IsMissing ? "M" : "N", node.Kind());
                    break;
                }
            }
        }
Ejemplo n.º 3
0
 private void WriteSourceMap(SyntaxNodeOrToken node, Position pos, string str)
 {
     if (node.Kind() == SyntaxKind.IdentifierName || node.Kind() == SyntaxKind.IdentifierToken || node.Kind() == SyntaxKind.ObjectCreationExpression)
     {
         System.Diagnostics.Debug.WriteLine($"{node} ---> [{node.Kind()} | {pos}] ---> {str}");
         _sourceMapOutput.AddMapping(node, pos);
     }
 }
Ejemplo n.º 4
0
 private void WriteSourceMap(SyntaxNodeOrToken node, Position pos, string str)
 {
     if (node.Kind() == SyntaxKind.IdentifierName || node.Kind() == SyntaxKind.IdentifierToken || node.Kind() == SyntaxKind.ObjectCreationExpression)
     {
         System.Diagnostics.Debug.WriteLine($"{node} ---> [{node.Kind()} | {pos}] ---> {str}");
         _sourceMapOutput.AddMapping(node, pos);
     }
 }
Ejemplo n.º 5
0
 private static void Print(SyntaxNodeOrToken node)
 {
     if (node.Kind() == SyntaxKind.IdentifierToken && !node.IsMissing)
     {
         Debug.WriteLine(@"N(SyntaxKind.{0}, ""{1}"");", node.Kind(), node.ToString());
     }
     else
     {
         Debug.WriteLine("{0}(SyntaxKind.{1});", node.IsMissing ? "M" : "N", node.Kind());
     }
 }
        public SyntaxNodeOrTokenListItem(SyntaxNodeOrToken syntaxNodeOrToken)
        {
            _lineSpan = syntaxNodeOrToken.GetLocation().GetLineSpan();

            Content = $"{_lineSpan}: {syntaxNodeOrToken.Kind()} {Truncate(syntaxNodeOrToken.ToString())}";
            ToolTip = syntaxNodeOrToken.ToString();
        }
        public SyntaxNodeOrTokenListItem(SyntaxNodeOrToken syntaxNodeOrToken)
        {
            _lineSpan = syntaxNodeOrToken.GetLocation().GetLineSpan();

            Content = $"{_lineSpan}: {syntaxNodeOrToken.Kind()} {Truncate(syntaxNodeOrToken.ToString())}";
            ToolTip = syntaxNodeOrToken.ToString();
        }
Ejemplo n.º 8
0
        private static CompletionSyntax GetCompletionSyntax(SyntaxNodeOrToken nodeOrToken)
        {
            SyntaxNodeOrToken dot;
            SyntaxNode        prefix = null;

            if (nodeOrToken.IsNode)
            {
                prefix = nodeOrToken.AsNode();
                dot    = nodeOrToken.GetPreviousSibling();
                if (dot.Kind() != SyntaxKind.DotToken)
                {
                    return(new CompletionSyntax(null, prefix));
                }
            }
            else
            {
                if (nodeOrToken.Kind() != SyntaxKind.DotToken)
                {
                    return(new CompletionSyntax(null, prefix));
                }
                dot = nodeOrToken;
            }

            var previous = dot.GetPreviousSibling();

            return(previous.IsNode ? new CompletionSyntax(previous.AsNode(), prefix) : new CompletionSyntax(null, prefix));
        }
        private bool MatchesIgnoringAxis(SyntaxNodeOrToken nodeOrToken, SyntaxQuery query)
        {
            var node = nodeOrToken.AsNode();

            if (node is ExpressionStatementSyntax statement && node.Kind() == ExpressionStatement)
            {
                return(MatchesIgnoringAxis(statement.Expression, query));
            }

            if (node is SwitchSectionSyntax switchSection)
            {
                foreach (var label in switchSection.Labels)
                {
                    if (MatchesSyntaxKindAndFilter(switchSection, label.Kind(), query))
                    {
                        return(true);
                    }
                }
            }

            if (node is PredefinedTypeSyntax predefinedType)
            {
                return(MatchesSyntaxKindAndFilter(predefinedType, predefinedType.Keyword.Kind(), query));
            }

            return(MatchesSyntaxKindAndFilter(nodeOrToken, nodeOrToken.Kind(), query));
        }
Ejemplo n.º 10
0
        private void NodeToJson(JsonWriter jw, SyntaxNodeOrToken node, Dictionary <SyntaxNodeOrToken, int> nodeToIdx)
        {
            jw.WriteStartObject();

            jw.WritePropertyName("id");
            jw.WriteValue(nodeToIdx[node]);

            jw.WritePropertyName("type");
            jw.WriteValue(node.Kind().ToString());

            if (node.IsKind(SyntaxKind.IdentifierName) || node.IsKind(SyntaxKind.PredefinedType) || RoslynUtils.IsSimpleLiteral(node) || node.IsToken || node.AsNode().ChildNodes().Count() == 0)
            {
                jw.WritePropertyName("value");
                jw.WriteValue(node.ToString());
            }
            else
            {
                jw.WritePropertyName("children");
                jw.WriteStartArray();
                foreach (var child in node.AsNode().ChildNodesAndTokens())
                {
                    if (!nodeToIdx.TryGetValue(child, out int idx))
                    {
                        idx = int.MaxValue;
                    }
                    jw.WriteValue(idx);
                }
                jw.WriteEndArray();
            }

            jw.WriteEndObject();
        }
Ejemplo n.º 11
0
        public override Match Run(SyntaxNodeOrToken nodeOrToken)
        {
            if (nodeOrToken.Kind() != _kind)
                return Match.NoMatch;

            return nodeOrToken.AsToken().ValueText == _text ? Match.Success : Match.NoMatch;
        }
Ejemplo n.º 12
0
        public override Match Run(SyntaxNodeOrToken nodeOrToken)
        {
            if (nodeOrToken.Kind() != SyntaxKind.Argument)
                return Match.NoMatch;

            var argument = (ArgumentSyntax) nodeOrToken.AsNode();
            if (_variable.MinOccurrences == 1 && _variable.MaxOccurrences == 1)
                return Match.Success.WithSyntaxNodeOrToken(argument);

            var argumentList = argument.Parent as ArgumentListSyntax;
            if (argumentList == null)
                return Match.NoMatch;

            var currentIndex = argumentList.Arguments.IndexOf(argument);
            var availableCount = argumentList.Arguments.Count - currentIndex - _following;
            if (availableCount == 0)
                return Match.NoMatch;

            var captureCount = _variable.MaxOccurrences == null
                ? availableCount
                : Math.Min(availableCount, _variable.MaxOccurrences.Value);

            if (captureCount < _variable.MinOccurrences)
                return Match.NoMatch;

            var endIndex = currentIndex + captureCount - 1;
            var endArgument = argumentList.Arguments[endIndex];
            return Match.Success.AddCapture(_variable, argument, endArgument);
        }
Ejemplo n.º 13
0
 protected SyntaxNodeOrToken M(SyntaxKind kind)
 {
     Assert.True(_treeEnumerator.MoveNext());
     SyntaxNodeOrToken current = _treeEnumerator.Current;
     Assert.Equal(kind, current.Kind());
     Assert.True(current.IsMissing);
     return current;
 }
Ejemplo n.º 14
0
        private static void Print(SyntaxNodeOrToken node)
        {
            switch (node.Kind())
            {
            case SyntaxKind.IdentifierToken:
            case SyntaxKind.NumericLiteralToken:
                if (node.IsMissing)
                {
                    goto default;
                }
                Debug.WriteLine(@"N(SyntaxKind.{0}, ""{1}"");", node.Kind(), node.ToString());
                break;

            default:
                Debug.WriteLine("{0}(SyntaxKind.{1});", node.IsMissing ? "M" : "N", node.Kind());
                break;
            }
        }
Ejemplo n.º 15
0
        public ShwingNode(SyntaxNodeOrToken node, int precedingId, int currentDepth, int maxDepth)
        {
            Id = S_UID++;

            Kind      = node.Kind();
            _filename = node.GetLocation().SourceSpan;

            Add(node, precedingId, currentDepth, maxDepth);
        }
Ejemplo n.º 16
0
            private bool CanReuse(SyntaxNodeOrToken nodeOrToken)
            {
                // Zero width nodes and tokens always indicate that the parser had to do
                // something tricky, so don't reuse them.
                // NOTE: this is slightly different from IsMissing because of omitted type arguments
                // and array size expressions.
                if (nodeOrToken.FullWidth == 0)
                {
                    return(false);
                }

                // As of 2013/03/14, the compiler never attempts to incrementally parse a tree containing
                // annotations.  Our goal in instituting this restriction is to prevent API clients from
                // taking a dependency on the survival of annotations.
                if (nodeOrToken.ContainsAnnotations)
                {
                    return(false);
                }

                // We can't reuse a node or token if it intersects a changed text range.
                if (IntersectsNextChange(nodeOrToken))
                {
                    return(false);
                }

                // don't reuse nodes or tokens with skipped text or diagnostics attached to them
                if (nodeOrToken.ContainsDiagnostics ||
                    (nodeOrToken.IsToken && ((LuaSyntaxNode)nodeOrToken.AsToken().Node).ContainsSkippedText && nodeOrToken.Parent.ContainsDiagnostics))
                {
                    return(false);
                }

                // fabricated tokens did not come from the lexer (likely from parser)
                if (IsFabricatedToken(nodeOrToken.Kind()))
                {
                    return(false);
                }

                // don't reuse nodes that are incomplete. this helps cases were an incomplete node
                // completes differently after a change with far look-ahead.
                //
                // NOTE(cyrusn): It is very unfortunate that we even need this check given that we
                // have already checked for ContainsDiagnostics above.  However, there is a case where we
                // can have a node with a missing token *and* there are no diagnostics.
                // Specifically, this happens in the REPL when you have the last statement without a
                // trailing semicolon.  We treat this as an ExpressionStatement with a missing
                // semicolon, but we do not report errors.  It would be preferable to fix that so
                // that the semicolon can be optional rather than abusing the system.
                if ((nodeOrToken.IsToken && nodeOrToken.AsToken().IsMissing) ||
                    (nodeOrToken.IsNode && IsIncomplete((Lua.LuaSyntaxNode)nodeOrToken.AsNode())))
                {
                    return(false);
                }

                return(true);
            }
Ejemplo n.º 17
0
        private void AssertNodesAreEquivalent(SyntaxNodeOrToken expectedNode, SyntaxNodeOrToken actualNode)
        {
            Assert.Equal(expectedNode.Kind(), actualNode.Kind());
            Assert.Equal(expectedNode.FullSpan, actualNode.FullSpan);
            Assert.Equal(expectedNode.ChildNodesAndTokens().Count, actualNode.ChildNodesAndTokens().Count);

            for (var i = 0; i < expectedNode.ChildNodesAndTokens().Count; i++)
            {
                AssertNodesAreEquivalent(expectedNode.ChildNodesAndTokens()[i],
                                         actualNode.ChildNodesAndTokens()[i]);
            }
        }
Ejemplo n.º 18
0
        private Nodes SyntaxNodeToNodes(SyntaxNodeOrToken s)
        {
            Nodes node = new Nodes();

            node.Kind       = s.Kind();
            node.Language   = s.Kind().ToString() + " [" + s.SpanStart + "..." + s.Span.End + "]";
            node.syntaxNode = s;

            if (s.HasLeadingTrivia)
            {
                SyntaxTriviaList d = s.GetLeadingTrivia();
                foreach (SyntaxTrivia t in d)
                {
                    Nodes tr = new Nodes();
                    tr.Language = "Leading: " + t.ToString() + " [" + t.SpanStart + "..." + t.Span.End + "]";
                    node.nodes.Add(tr);
                }
                node.IsLeadingTrivia = true;
            }
            if (s.HasTrailingTrivia)
            {
                SyntaxTriviaList d = s.GetTrailingTrivia();
                foreach (SyntaxTrivia t in d)
                {
                    Nodes tr = new Nodes();
                    tr.Language = "Trailing: " + t.ToString() + " [" + t.SpanStart + "..." + t.Span.End + "]";
                    node.nodes.Add(tr);
                }

                node.IsTrailingTrivia = true;
            }
            foreach (SyntaxNodeOrToken c in s.ChildNodesAndTokens())
            {
                Nodes ng = SyntaxNodeToNodes(c);
                node.nodes.Add(ng);
            }
            return(node);
        }
Ejemplo n.º 19
0
 protected SyntaxNodeOrToken M(SyntaxKind kind)
 {
     try
     {
         Assert.True(_treeEnumerator !.MoveNext());
         SyntaxNodeOrToken current = _treeEnumerator.Current;
         Assert.Equal(kind, current.Kind());
         Assert.True(current.IsMissing);
         return(current);
     }
     catch when(DumpAndCleanup())
     {
         throw;
     }
 }
Ejemplo n.º 20
0
        public override Match Run(SyntaxNodeOrToken nodeOrToken)
        {
            if (nodeOrToken.Kind() != _syntaxKind)
                return Match.NoMatch;

            var result = Match.Success;

            if (nodeOrToken.IsNode)
            {
                var node = nodeOrToken.AsNode();
                var children = node.ChildNodesAndTokens();

                var matcherIndex = 0;
                var matchedEnd = 0;

                for (var i = 0; i < children.Count; i++)
                {
                    if (matcherIndex >= _childMatchers.Length)
                        return Match.NoMatch;

                    while (i < children.Count && children[i].Span.Start < matchedEnd)
                        i++;

                    if (i >= children.Count)
                        break;

                    var child = children[i];
                    var matcher = _childMatchers[matcherIndex];
                    var match = matcher.Run(child);
                    if (!match.IsMatch)
                        return Match.NoMatch;

                    // Depending on much was captured, we need to skip some matchers.

                    if (match.Captures.Any())
                        matchedEnd = match.Captures.Max(c => c.EndNodeOrToken.Span.End);

                    result = result.AddCaptures(match.Captures);
                    matcherIndex++;
                }

                var allMatchersUsed = matcherIndex >= _childMatchers.Length;
                if (!allMatchersUsed)
                    return Match.NoMatch;
            }

            return result;
        }
Ejemplo n.º 21
0
        private static bool LookForBetterMatch(SyntaxNodeOrToken first, SyntaxNodeOrToken[] second, int secondPos, out int maxSecond)
        {
            maxSecond = secondPos;

            for (int i = secondPos; i < second.Length; i++)
            {
                if (first.Kind() == second[i].Kind() &&
                    IsTextSame(first, second[i]))
                {
                    maxSecond = i;
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 22
0
        public override Match Run(SyntaxNodeOrToken nodeOrToken)
        {
            if (nodeOrToken.Kind() != SyntaxKind.IdentifierToken)
                return Match.NoMatch;

            var identifier = nodeOrToken.AsToken().ValueText;
            var regex = _variable.Regex;
            if (!string.IsNullOrEmpty(regex))
            {
                var regexOptions = _variable.CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase;
                if (!Regex.IsMatch(identifier, regex, regexOptions))
                    return Match.NoMatch;
            }

            return Match.Success.AddCapture(_variable, nodeOrToken);
        }
Ejemplo n.º 23
0
        void ParseAccessNode(SyntaxNode sn, ParsingState state)
        {
            SyntaxNodeOrToken snt = sn.ChildNodesAndTokens().First();

            if (snt.Kind().Equals(SyntaxKind.SimpleMemberAccessExpression))
            {
                ParseAccessNode(snt.AsNode(), state);
            }
            else if (snt.IsNode)
            {
                ParseNode(snt.AsNode(), state);
            }
            else if (snt.IsToken)
            {
                ParseCommonToken(snt.AsToken(), state);
            }
        }
Ejemplo n.º 24
0
        public override Match Run(SyntaxNodeOrToken nodeOrToken)
        {
            if (nodeOrToken.Kind() != SyntaxKind.ArgumentList)
                return Match.NoMatch;

            var argumentList = (ArgumentListSyntax) nodeOrToken.AsNode();
            if (_variable.MinOccurrences > argumentList.Arguments.Count)
                return Match.NoMatch;

            if (_variable.MaxOccurrences != null && _variable.MaxOccurrences < argumentList.Arguments.Count)
                return Match.NoMatch;

            if (argumentList.Arguments.Count == 0)
                return Match.Success;

            var first = argumentList.Arguments.First();
            var last = argumentList.Arguments.Last();
            return Match.Success.AddCapture(_variable, first, last);
        }
Ejemplo n.º 25
0
        public void Add(SyntaxNodeOrToken newNode, int precedingId, int currentDepth, int maxDepth = int.MaxValue)
        {
            Counter += 1;
            _parentIds.Add(precedingId);

            if (currentDepth >= maxDepth)
            {
                return;
            }
            // TODO: Throw, or error check or something.
            Debug.Assert(newNode.Kind() == Kind, "Uh oh. Kinds must be the same at ShwingNodes.");

            var        newChildren     = newNode.ChildNodesAndTokens();
            ShwingNode lastTouchedNode = null;

            for (int i = 0; i < newChildren.Count; i++)
            {
                var newKid = newChildren[i];

                if (i == Children.Count)
                {
                    Children.Add(new List <ShwingNode>());
                }

                var j = Children[i].FindIndex(x => x.Kind == newKid.Kind());

                var lastId = lastTouchedNode?.Id ?? -2;

                if (j != -1)
                {
                    Children[i][j].Add(newKid, lastId, currentDepth + 1, maxDepth);
                    lastTouchedNode = Children[i][j];
                }
                else
                {
                    lastTouchedNode = new ShwingNode(newKid, lastId, currentDepth + 1, maxDepth);
                    Children[i].Add(lastTouchedNode);
                }
            }
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Prints full hierarchy of the specified syntax node or token as string representation.
        /// </summary>
        /// <param name="builder">The builder used to create a string representation.</param>
        /// <param name="nodeOrToken">The syntax node or token to print.</param>
        /// <param name="depth">The initial indent depth.</param>
        /// <param name="indent">The indent value used for nested nodes.</param>
        public static void PrintSyntaxNodeOrToken(StringBuilder builder, SyntaxNodeOrToken nodeOrToken, int depth = 0, int indent = 4)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (depth < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(depth));
            }
            if (indent < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(indent));
            }

            builder.Append(' ', indent * depth);
            builder.Append($"{nodeOrToken.Kind()} {nodeOrToken.Span}");
            builder.AppendLine();

            foreach (SyntaxTrivia trivia in nodeOrToken.GetLeadingTrivia())
            {
                builder.Append(' ', indent * (depth + 1));
                builder.Append($"Lead: {trivia.Kind()} {trivia.Span}");
                builder.AppendLine();
            }

            foreach (SyntaxTrivia trivia in nodeOrToken.GetTrailingTrivia())
            {
                builder.Append(' ', indent * (depth + 1));
                builder.Append($"Trail: {trivia.Kind()} {trivia.Span}");
                builder.AppendLine();
            }

            foreach (SyntaxNodeOrToken childNodeOrToken in nodeOrToken.ChildNodesAndTokens())
            {
                PrintSyntaxNodeOrToken(builder, childNodeOrToken, depth + 1, indent);
            }
        }
Ejemplo n.º 27
0
        private static CodeCompletionExpression FindExpression(SyntaxNodeOrToken node, int endOfCodeIdx)
        {
            if (node.FullSpan.End == endOfCodeIdx && node.Kind() == SyntaxKind.SimpleMemberAccessExpression)
            {
                var children = node.ChildNodesAndTokens();
                return(new CodeCompletionExpression
                {
                    Expression = children.First().ToString(),
                    Prefix = children.ElementAt(2).ToString(),
                });
            }

            foreach (var c in node.ChildNodesAndTokens())
            {
                var expression = FindExpression(c, endOfCodeIdx);
                if (expression != NoExpression)
                {
                    return(expression);
                }
            }

            return(NoExpression);
        }
Ejemplo n.º 28
0
        private void NamedImpl(SyntaxNodeOrToken nodeOrToken)
        {
            string name = null;
            string astType = null;
            string comments = nodeOrToken.GetLeadingTrivia().ToFullString();
            string parentComments = nodeOrToken.Parent != null ? nodeOrToken.Parent.GetLeadingTrivia().ToFullString() : null;

            //if this is the hightest ast node for this comment, look for 1 bang, else 2 bangs
            string regex = @"@(?<ast_type>[^:\s]+):(?<name>\S+)";

            Match match = Regex.Match(comments, regex);
            if (match.Success)
            {
                name = match.Groups["name"].Value;
                astType = match.Groups["ast_type"].Value;

                SyntaxKind syntaxKind = (SyntaxKind)Enum.Parse(typeof(SyntaxKind), astType);

                if (nodeOrToken.Kind() == syntaxKind)
                {
                    names.Add(nodeOrToken, name);
                }
            }
        }
Ejemplo n.º 29
0
 private static bool IsNonZeroWidthOrIsEndOfFile(SyntaxNodeOrToken token)
 {
     return(token.Kind() == SyntaxKind.EndOfFileToken || token.FullWidth != 0);
 }
Ejemplo n.º 30
0
        private static void CompareTreeEquivalence(SyntaxNodeOrToken parsedTreeNode, SyntaxNodeOrToken incrementalTreeNode)
        {
            Assert.Equal(parsedTreeNode.Kind(), incrementalTreeNode.Kind());

            Assert.Equal(parsedTreeNode.ChildNodesAndTokens().Count, incrementalTreeNode.ChildNodesAndTokens().Count);

            for (int i = 0; i < parsedTreeNode.ChildNodesAndTokens().Count; i++)
            {
                CompareTreeEquivalence(parsedTreeNode.ChildNodesAndTokens()[i], incrementalTreeNode.ChildNodesAndTokens()[i]);
            }
        }
        private (StyleIndex Style, TextSpan TextSpan)? GetStyle(SyntaxNodeOrToken syntaxToken)
        {
            switch (syntaxToken.Kind())
            {
            case SyntaxKind.NumericLiteralToken:
                return(StyleIndex.Number, syntaxToken.Span);

            case SyntaxKind.StringLiteralToken:
            case SyntaxKind.CharacterLiteralToken:
            case SyntaxKind.InterpolatedStringStartToken:
            case SyntaxKind.InterpolatedStringEndToken:
            case SyntaxKind.InterpolatedStringText:
            case SyntaxKind.InterpolatedStringTextToken:
            case SyntaxKind.InterpolatedStringToken:
            case SyntaxKind.InterpolatedVerbatimStringStartToken:
                return(StyleIndex.String, syntaxToken.Span);

            case SyntaxKind.BoolKeyword:
            case SyntaxKind.SByteKeyword:
            case SyntaxKind.ShortKeyword:
            case SyntaxKind.UShortKeyword:
            case SyntaxKind.IntKeyword:
            case SyntaxKind.UIntKeyword:
            case SyntaxKind.LongKeyword:
            case SyntaxKind.ULongKeyword:
            case SyntaxKind.DoubleKeyword:
            case SyntaxKind.FloatKeyword:
            case SyntaxKind.DecimalKeyword:
            case SyntaxKind.StringKeyword:
            case SyntaxKind.CharKeyword:
            case SyntaxKind.VoidKeyword:
            case SyntaxKind.ObjectKeyword:
            case SyntaxKind.ByteKeyword:
                return(StyleIndex.TypeKeyword, syntaxToken.Span);

            case SyntaxKind.IfKeyword:
            case SyntaxKind.ElseKeyword:
            case SyntaxKind.WhileKeyword:
            case SyntaxKind.ForKeyword:
            case SyntaxKind.ForEachKeyword:
            case SyntaxKind.DoKeyword:
            case SyntaxKind.SwitchKeyword:
            case SyntaxKind.CaseKeyword:
            case SyntaxKind.DefaultKeyword:
            case SyntaxKind.TryKeyword:
            case SyntaxKind.CatchKeyword:
            case SyntaxKind.FinallyKeyword:
            case SyntaxKind.GotoKeyword:
            case SyntaxKind.BreakKeyword:
            case SyntaxKind.ContinueKeyword:
            case SyntaxKind.ReturnKeyword:
            case SyntaxKind.ThrowKeyword:
            case SyntaxKind.YieldKeyword:
            case SyntaxKind.WhenKeyword:
                return(StyleIndex.ControlKeyword, syntaxToken.Span);

            case SyntaxKind.InKeyword:
                return(syntaxToken.Parent is ForEachStatementSyntax ? StyleIndex.ControlKeyword : StyleIndex.Keyword, syntaxToken.Span);

            case SyntaxKind.TypeKeyword:
            case SyntaxKind.TypeOfKeyword:
            case SyntaxKind.SizeOfKeyword:
            case SyntaxKind.NullKeyword:
            case SyntaxKind.TrueKeyword:
            case SyntaxKind.FalseKeyword:
            case SyntaxKind.LockKeyword:
            case SyntaxKind.PublicKeyword:
            case SyntaxKind.PrivateKeyword:
            case SyntaxKind.InternalKeyword:
            case SyntaxKind.ProtectedKeyword:
            case SyntaxKind.StaticKeyword:
            case SyntaxKind.ReadOnlyKeyword:
            case SyntaxKind.SealedKeyword:
            case SyntaxKind.ConstKeyword:
            case SyntaxKind.FixedKeyword:
            case SyntaxKind.StackAllocKeyword:
            case SyntaxKind.VolatileKeyword:
            case SyntaxKind.NewKeyword:
            case SyntaxKind.OverrideKeyword:
            case SyntaxKind.AbstractKeyword:
            case SyntaxKind.VirtualKeyword:
            case SyntaxKind.EventKeyword:
            case SyntaxKind.ExternKeyword:
            case SyntaxKind.RefKeyword:
            case SyntaxKind.OutKeyword:
            case SyntaxKind.IsKeyword:
            case SyntaxKind.AsKeyword:
            case SyntaxKind.ParamsKeyword:
            case SyntaxKind.ArgListKeyword:
            case SyntaxKind.MakeRefKeyword:
            case SyntaxKind.RefTypeKeyword:
            case SyntaxKind.RefValueKeyword:
            case SyntaxKind.ThisKeyword:
            case SyntaxKind.BaseKeyword:
            case SyntaxKind.NamespaceKeyword:
            case SyntaxKind.UsingKeyword:
            case SyntaxKind.ClassKeyword:
            case SyntaxKind.StructKeyword:
            case SyntaxKind.InterfaceKeyword:
            case SyntaxKind.EnumKeyword:
            case SyntaxKind.DelegateKeyword:
            case SyntaxKind.CheckedKeyword:
            case SyntaxKind.UncheckedKeyword:
            case SyntaxKind.UnsafeKeyword:
            case SyntaxKind.OperatorKeyword:
            case SyntaxKind.ExplicitKeyword:
            case SyntaxKind.ImplicitKeyword:
            case SyntaxKind.PartialKeyword:
            case SyntaxKind.AliasKeyword:
            case SyntaxKind.GlobalKeyword:
            case SyntaxKind.AssemblyKeyword:
            case SyntaxKind.ModuleKeyword:
            case SyntaxKind.FieldKeyword:
            case SyntaxKind.MethodKeyword:
            case SyntaxKind.ParamKeyword:
            case SyntaxKind.PropertyKeyword:
            case SyntaxKind.TypeVarKeyword:
            case SyntaxKind.GetKeyword:
            case SyntaxKind.SetKeyword:
            case SyntaxKind.AddKeyword:
            case SyntaxKind.RemoveKeyword:
            case SyntaxKind.WhereKeyword:
            case SyntaxKind.FromKeyword:
            case SyntaxKind.GroupKeyword:
            case SyntaxKind.JoinKeyword:
            case SyntaxKind.IntoKeyword:
            case SyntaxKind.LetKeyword:
            case SyntaxKind.ByKeyword:
            case SyntaxKind.SelectKeyword:
            case SyntaxKind.OrderByKeyword:
            case SyntaxKind.OnKeyword:
            case SyntaxKind.EqualsKeyword:
            case SyntaxKind.AscendingKeyword:
            case SyntaxKind.DescendingKeyword:
            case SyntaxKind.NameOfKeyword:
            case SyntaxKind.AsyncKeyword:
            case SyntaxKind.AwaitKeyword:
            case SyntaxKind.ElifKeyword:
            case SyntaxKind.EndIfKeyword:
            case SyntaxKind.RegionKeyword:
            case SyntaxKind.EndRegionKeyword:
            case SyntaxKind.DefineKeyword:
            case SyntaxKind.UndefKeyword:
            case SyntaxKind.WarningKeyword:
            case SyntaxKind.ErrorKeyword:
            case SyntaxKind.LineKeyword:
            case SyntaxKind.PragmaKeyword:
            case SyntaxKind.HiddenKeyword:
            case SyntaxKind.ChecksumKeyword:
            case SyntaxKind.DisableKeyword:
            case SyntaxKind.RestoreKeyword:
            case SyntaxKind.ReferenceKeyword:
            case SyntaxKind.LoadKeyword:
            case SyntaxKind.NullableKeyword:
            case SyntaxKind.EnableKeyword:
            case SyntaxKind.SafeOnlyKeyword:
            case SyntaxKind.VarKeyword:
                return(StyleIndex.Keyword, syntaxToken.Span);

            case SyntaxKind.NamespaceDeclaration: {
                var namespaceDeclaration = syntaxToken.AsNode() as NamespaceDeclarationSyntax;
                return(StyleIndex.Namespace, namespaceDeclaration.Name.Span);
            }

            case SyntaxKind.VariableDeclarator: {
                var variableDeclarator = syntaxToken.AsNode() as VariableDeclaratorSyntax;
                switch (variableDeclarator.Parent)
                {
                case VariableDeclarationSyntax parent:
                    switch (parent.Parent)
                    {
                    case FieldDeclarationSyntax fieldDeclaration:
                        if (fieldDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword))
                        {
                            return(StyleIndex.StaticField, variableDeclarator.Identifier.Span);
                        }
                        else
                        {
                            return(StyleIndex.Field, variableDeclarator.Identifier.Span);
                        }

                    case LocalDeclarationStatementSyntax _:
                    case UsingStatementSyntax _:
                        return(StyleIndex.Local, variableDeclarator.Identifier.Span);
                    }
                    break;
                }
                return(null);
            }

            case SyntaxKind.IdentifierName: {
                switch (syntaxToken.Parent)
                {
                case AttributeSyntax {
                        Name: var name
                } :
                    return(GetSymbolStyle(name, name.Span));

                case NameEqualsSyntax {
                        Parent: UsingDirectiveSyntax @using
                } :
                    return(GetSymbolStyle(@using.Name, @using.Alias.Name.Span));
Ejemplo n.º 32
0
            private bool CanReuse(SyntaxNodeOrToken nodeOrToken)
            {
                // Zero width nodes and tokens always indicate that the parser had to do
                // something tricky, so don't reuse them.
                // NOTE: this is slightly different from IsMissing because of omitted type arguments
                // and array size expressions.
                if (nodeOrToken.FullWidth == 0)
                {
                    return false;
                }

                // As of 2013/03/14, the compiler never attempts to incrementally parse a tree containing
                // annotations.  Our goal in instituting this restriction is to prevent API clients from
                // taking a dependency on the survival of annotations.
                if (nodeOrToken.ContainsAnnotations)
                {
                    return false;
                }

                // We can't reuse a node or token if it intersects a changed text range.
                if (this.IntersectsNextChange(nodeOrToken))
                {
                    return false;
                }

                // don't reuse nodes or tokens with skipped text or diagnostics attached to them
                if (nodeOrToken.ContainsDiagnostics ||
                    (nodeOrToken.IsToken && ((CSharpSyntaxNode)nodeOrToken.AsToken().Node).ContainsSkippedText && nodeOrToken.Parent.ContainsDiagnostics))
                {
                    return false;
                }

                // fabricated tokens did not come from the lexer (likely from parser)
                if (IsFabricatedToken(nodeOrToken.Kind()))
                {
                    return false;
                }

                // don't reuse nodes that are incomplete. this helps cases were an incomplete node
                // completes differently after a change with far look-ahead.
                //
                // NOTE(cyrusn): It is very unfortunate that we even need this check given that we
                // have already checked for ContainsDiagnostics above.  However, there is a case where we
                // can have a node with a missing token *and* there are no diagnostics.
                // Specifically, this happens in the REPL when you have the last statement without a
                // trailing semicolon.  We treat this as an ExpressionStatement with a missing
                // semicolon, but we do not report errors.  It would be preferable to fix that so
                // that the semicolon can be optional rather than abusing the system.
                if ((nodeOrToken.IsToken && nodeOrToken.AsToken().IsMissing) ||
                    (nodeOrToken.IsNode && IsIncomplete((CSharp.CSharpSyntaxNode)nodeOrToken.AsNode())))
                {
                    return false;
                }

                if (!nodeOrToken.ContainsDirectives)
                {
                    return true;
                }

                return _newDirectives.IncrementallyEquivalent(_oldDirectives);
            }
Ejemplo n.º 33
0
 private static void Print(SyntaxNodeOrToken node)
 {
     switch (node.Kind())
     {
         case SyntaxKind.IdentifierToken:
         case SyntaxKind.NumericLiteralToken:
             if (node.IsMissing)
             {
                 goto default;
             }
             Debug.WriteLine(@"N(SyntaxKind.{0}, ""{1}"");", node.Kind(), node.ToString());
             break;
         default:
             Debug.WriteLine("{0}(SyntaxKind.{1});", node.IsMissing ? "M" : "N", node.Kind());
             break;
     }
 }
Ejemplo n.º 34
0
        private void AssertNodesAreEquivalent(SyntaxNodeOrToken expectedNode, SyntaxNodeOrToken actualNode)
        {
            Assert.Equal(expectedNode.Kind(), actualNode.Kind());
            Assert.Equal(expectedNode.FullSpan, actualNode.FullSpan);
            Assert.Equal(expectedNode.ChildNodesAndTokens().Count, actualNode.ChildNodesAndTokens().Count);

            for (var i = 0; i < expectedNode.ChildNodesAndTokens().Count; i++)
            {
                AssertNodesAreEquivalent(expectedNode.ChildNodesAndTokens()[i],
                    actualNode.ChildNodesAndTokens()[i]);
            }
        }
Ejemplo n.º 35
0
 private static bool IsNonZeroWidthOrIsEndOfFile(SyntaxNodeOrToken token)
 {
     return token.Kind() == SyntaxKind.EndOfFileToken || token.FullWidth != 0;
 }
Ejemplo n.º 36
0
 private static void Print(SyntaxNodeOrToken node)
 {
     if (node.Kind() == SyntaxKind.IdentifierToken && !node.IsMissing)
     {
         Debug.WriteLine(@"N(SyntaxKind.{0}, ""{1}"");", node.Kind(), node.ToString());
     }
     else
     {
         Debug.WriteLine("{0}(SyntaxKind.{1});", node.IsMissing ? "M" : "N", node.Kind());
     }
 }
Ejemplo n.º 37
0
 private static void Print(SyntaxNodeOrToken node)
 {
     Debug.WriteLine("{0}(SyntaxKind.{1});", node.IsMissing ? "M" : "N", node.Kind());
 }
Ejemplo n.º 38
0
 private static void Print(SyntaxNodeOrToken node)
 {
     Debug.WriteLine("{0}(SyntaxKind.{1});", node.IsMissing ? "M" : "N", node.Kind());
 }