internal SyntaxToken(SyntaxKind kind, SyntaxKind contextualKind,
                             bool isMissing, SourceRange sourceRange, string text, object value,
                             IEnumerable <SyntaxNode> leadingTrivia, IEnumerable <SyntaxNode> trailingTrivia,
                             IEnumerable <Diagnostic> diagnostics)
            : base(kind, diagnostics)
        {
            ContextualKind = contextualKind;

            IsMissing = isMissing;

            Text        = text;
            SourceRange = sourceRange;
            Value       = value;

            LeadingTrivia = leadingTrivia.ToImmutableArray();
            foreach (var triviaNode in LeadingTrivia)
            {
                ((SyntaxNodeBase)triviaNode).Parent = this;
            }

            TrailingTrivia = trailingTrivia.ToImmutableArray();
            foreach (var triviaNode in TrailingTrivia)
            {
                ((SyntaxNodeBase)triviaNode).Parent = this;
            }

            FullSourceRange = ComputeFullSpan(sourceRange, LeadingTrivia, TrailingTrivia);

            ContainsDiagnostics = Diagnostics.Any() ||
                                  LeadingTrivia.Any(x => x.ContainsDiagnostics) ||
                                  TrailingTrivia.Any(x => x.ContainsDiagnostics);
        }
Beispiel #2
0
 public SyntaxToken DeepClone()
 {
     return(new SyntaxToken(Location, Kind, Text, Value,
                            LeadingTrivia.Select(x => x.DeepClone()).ToImmutableArray(),
                            TrailingTrivia.Select(x => x.DeepClone()).ToImmutableArray(), Diagnostics)
     {
         Parent = Parent,
     });
 }
 public override void Write([DisallowNull] StringBuilder target)
 {
     if (target == null)
     {
         throw new ArgumentNullException(nameof(target));
     }
     LeadingTrivia.WriteAll(target);
     target.Append(Value);
     TrailingTrivia.WriteAll(target);
 }
Beispiel #4
0
        internal override DirectiveStack ApplyDirectives(DirectiveStack stack)
        {
            if (ContainsDirectives)
            {
                foreach (var trivia in LeadingTrivia.Where(x => x.ContainsDirectives))
                {
                    stack = trivia.ApplyDirectives(stack);
                }

                foreach (var trivia in TrailingTrivia.Where(x => x.ContainsDirectives))
                {
                    stack = trivia.ApplyDirectives(stack);
                }
            }

            return(stack);
        }
        internal SyntaxToken(PretokenizedSyntaxToken pretokenizedToken, SourceLocation position)
            : base((SyntaxKind)pretokenizedToken.RawKind, ConvertDiagnostics(pretokenizedToken.Diagnostics, position))
        {
            ContextualKind = (SyntaxKind)pretokenizedToken.RawContextualKind;

            Text  = pretokenizedToken.Text;
            Value = pretokenizedToken.Value;

            SourceRange     = new SourceRange(position + pretokenizedToken.LeadingTriviaWidth, pretokenizedToken.Width);
            FullSourceRange = new SourceRange(position, pretokenizedToken.FullWidth);

            var triviaPosition         = position;
            var convertedLeadingTrivia = new List <SyntaxNode>();

            foreach (var trivia in pretokenizedToken.LeadingTrivia)
            {
                convertedLeadingTrivia.Add(new SyntaxTrivia(
                                               (SyntaxKind)trivia.RawKind,
                                               trivia.Text,
                                               new SourceRange(triviaPosition, trivia.FullWidth),
                                               ConvertDiagnostics(trivia.Diagnostics, triviaPosition)));
                triviaPosition += trivia.FullWidth;
            }
            LeadingTrivia = convertedLeadingTrivia.ToImmutableArray();

            triviaPosition = SourceRange.End;
            var convertedTrailingTrivia = new List <SyntaxNode>();

            foreach (var trivia in pretokenizedToken.TrailingTrivia)
            {
                convertedTrailingTrivia.Add(new SyntaxTrivia(
                                                (SyntaxKind)trivia.RawKind,
                                                trivia.Text,
                                                new SourceRange(triviaPosition, trivia.FullWidth),
                                                ConvertDiagnostics(trivia.Diagnostics, triviaPosition)));
                triviaPosition += trivia.FullWidth;
            }
            TrailingTrivia = convertedTrailingTrivia.ToImmutableArray();

            ContainsDiagnostics = Diagnostics.Any() ||
                                  LeadingTrivia.Any(x => x.ContainsDiagnostics) ||
                                  TrailingTrivia.Any(x => x.ContainsDiagnostics);
        }
Beispiel #6
0
        internal SyntaxToken(SyntaxKind kind, SyntaxKind contextualKind,
                             bool isMissing, SourceRange sourceRange, TextSpan span, string text, object value,
                             IEnumerable <SyntaxNode> leadingTrivia, IEnumerable <SyntaxNode> trailingTrivia,
                             IEnumerable <Diagnostic> diagnostics,
                             MacroReference macroReference, bool isFirstTokenInMacroExpansion)
            : base(kind, text, span, diagnostics)
        {
            ContextualKind = contextualKind;

            IsMissing = isMissing;

            SourceRange = sourceRange;
            Value       = value;

            LeadingTrivia = leadingTrivia.ToImmutableArray();
            foreach (var triviaNode in LeadingTrivia)
            {
                triviaNode.Parent = this;
            }

            TrailingTrivia = trailingTrivia.ToImmutableArray();
            foreach (var triviaNode in TrailingTrivia)
            {
                triviaNode.Parent = this;
            }

            FullSourceRange = ComputeFullSpan(sourceRange, LeadingTrivia, TrailingTrivia);

            ContainsDiagnostics = Diagnostics.Any() ||
                                  LeadingTrivia.Any(x => x.ContainsDiagnostics) ||
                                  TrailingTrivia.Any(x => x.ContainsDiagnostics);

            ContainsDirectives = LeadingTrivia.OfType <DirectiveTriviaSyntax>().Any() ||
                                 TrailingTrivia.OfType <DirectiveTriviaSyntax>().Any();

            MacroReference = macroReference;
            IsFirstTokenInMacroExpansion = isFirstTokenInMacroExpansion;
        }
Beispiel #7
0
        public override void Write([DisallowNull] StringBuilder target)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            LeadingTrivia.WriteAll(target);

            switch (Kind)
            {
            case SyntaxKind.NumberToken:
                target.Append(Value);
                break;

            case SyntaxKind.StringToken:
                target.Append('"');
                target.Append(Value.Replace("\"", "\\\""));
                target.Append('"');
                break;

            case SyntaxKind.IdToken:
                target.Append(Value);
                break;

            case SyntaxKind.HtmlStringToken:
                target.Append('<');
                target.Append(Value);
                target.Append('>');
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            TrailingTrivia.WriteAll(target);
        }
Beispiel #8
0
 public override IEnumerable <Diagnostic> GetDiagnostics() => Diagnostics
 .Union(LeadingTrivia.SelectMany(x => x.GetDiagnostics()))
 .Union(TrailingTrivia.SelectMany(x => x.GetDiagnostics()));
Beispiel #9
0
 public override IEnumerable <DirectiveTriviaSyntax> GetDirectives()
 {
     return(LeadingTrivia.OfType <DirectiveTriviaSyntax>().Union(TrailingTrivia.OfType <DirectiveTriviaSyntax>()));
 }