/// <summary>
        /// Basic cleverness for cutting down the verbosity of expected token baselines
        /// (i.e. don't output a parameter if the default is correct).
        /// </summary>
        private static string ToExpectedTokenString(InternalSyntax.SyntaxToken token)
        {
            var canonicalText = SyntaxFacts.GetText(token.Kind);

            var builder = new StringBuilder();

            builder.AppendFormat("Token(SyntaxKind.{0}", token.Kind);

            if (token.Text != canonicalText)
            {
                builder.AppendFormat(", \"{0}\"", token.Text);

                if (token.ValueText != token.Text)
                {
                    builder.AppendFormat(", \"{0}\"", token.ValueText);

                    if (token.ContextualKind != token.Kind)
                    {
                        builder.AppendFormat(", SyntaxKind.{0}", token.ContextualKind);
                    }
                }
            }

            builder.Append(")");

            return(builder.ToString());
        }
Пример #2
0
        internal SyntaxToken(CSharpSyntaxNode parent, Syntax.InternalSyntax.SyntaxToken node, int position, int index)
        {
            Debug.Assert(parent == null || parent.Kind != SyntaxKind.List);
            Debug.Assert(node != null || (position == 0 && index == 0 && parent == null));
            Debug.Assert(position >= 0);

            this.position = position;
            this.parent   = parent;
            this.node     = node;
            this.index    = index;

#if DEBUG
            if (parent != null && node != null)
            {
                var nodeOrToken = ChildSyntaxList.ItemInternal(parent, index, fromTokenCtor: true);

                Debug.Assert(nodeOrToken.UnderlyingNode == node, "node was not found at given index");
                Debug.Assert(nodeOrToken.Position == position, "position mismatch");
            }
            else
            {
                Debug.Assert(parent == null || position >= parent.Position);
            }
#endif
        }
Пример #3
0
            public override InternalSyntax.CSharpSyntaxNode VisitToken(
                InternalSyntax.SyntaxToken token
                )
            {
                var visited = (InternalSyntax.SyntaxToken)base.VisitToken(token);

                return(_rewriteToken == null ? visited : _rewriteToken(visited));
            }
Пример #4
0
            private void Add(Syntax.InternalSyntax.SyntaxToken identifier)
            {
                if (identifier != null)
                {
                    var names = this.names ??
                                (this.names = new HashSet <string>());

                    names.Add(identifier.ValueText);
                }
            }
Пример #5
0
        internal SyntaxNodeOrToken(CSharpSyntaxNode node)
        {
            this.token        = null;
            this.tokenIndex   = 0;
            this.position     = 0;
            this.nodeOrParent = null;

            if (node != null)
            {
                this.position     = node.Position;
                this.nodeOrParent = node;
            }
        }
Пример #6
0
            private void PushToken(Syntax.InternalSyntax.SyntaxToken token)
            {
                var trailing = token.GetTrailingTrivia();

                if (trailing != null)
                {
                    this.Push(trailing);
                }

                this.Push(token);
                var leading = token.GetLeadingTrivia();

                if (leading != null)
                {
                    this.Push(leading);
                }
            }
Пример #7
0
        internal SyntaxNodeOrToken(CSharpSyntaxNode parent, Syntax.InternalSyntax.SyntaxToken token, int position, int index)
#endif
        {
            Debug.Assert(parent == null || parent.Kind != SyntaxKind.List);

            this.nodeOrParent = parent;
            this.token        = token;
            this.position     = position;
            this.tokenIndex   = index;

#if DEBUG
            if (!fromTokenCtor && token != null)
            {
                // create a token just for the purpose of argument validation.
                new SyntaxToken(parent, token, position, index);
            }
#endif
        }
Пример #8
0
 internal static bool IsIdentifierVar(this Syntax.InternalSyntax.SyntaxToken node)
 {
     return(node.ContextualKind == SyntaxKind.VarKeyword);
 }
Пример #9
0
        private DirectiveTriviaSyntax ParsePragmaDirective(SyntaxToken hash, SyntaxToken pragma, bool isActive)
        {
            if (isActive)
            {
                pragma = CheckFeatureAvailability(pragma, MessageID.IDS_FeaturePragma);
            }

            bool hasError = false;
            if (this.CurrentToken.ContextualKind == SyntaxKind.WarningKeyword)
            {
                var warning = this.EatContextualToken(SyntaxKind.WarningKeyword);
                SyntaxToken style;
                if (this.CurrentToken.Kind == SyntaxKind.DisableKeyword || this.CurrentToken.Kind == SyntaxKind.RestoreKeyword)
                {
                    style = this.EatToken();

                    var ids = new SeparatedSyntaxListBuilder<ExpressionSyntax>(10);
                    while (this.CurrentToken.Kind != SyntaxKind.EndOfDirectiveToken)
                    {
                        SyntaxToken id;
                        ExpressionSyntax idExpression;

                        if (this.CurrentToken.Kind == SyntaxKind.NumericLiteralToken)
                        {
                            // Previous versions of the compiler used to report a warning (CS1691)
                            // whenever an unrecognized warning code was supplied in a #pragma directive
                            // (or via /nowarn /warnaserror flags on the command line).
                            // Going forward, we won't generate any warning in such cases. This will make
                            // maintenance of backwards compatibility easier (we no longer need to worry
                            // about breaking existing projects / command lines if we deprecate / remove
                            // an old warning code).
                            id = this.EatToken();
                            idExpression = SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, id);
                        }
                        else if (this.CurrentToken.Kind == SyntaxKind.IdentifierToken)
                        {
                            // Lexing / parsing of identifiers inside #pragma warning directives is identical
                            // to that inside #define directives except that very long identifiers inside #define
                            // are truncated to 128 characters to maintain backwards compatibility with previous
                            // versions of the compiler. (See TruncateIdentifier() below.)
                            // Since support for identifiers inside #pragma warning directives is new, 
                            // we don't have any backwards compatibility constraints. So we can preserve the
                            // identifier exactly as it appears in source.
                            id = this.EatToken();
                            idExpression = SyntaxFactory.IdentifierName(id);
                        }
                        else
                        {
                            id = this.EatToken(SyntaxKind.NumericLiteralToken, ErrorCode.WRN_IdentifierOrNumericLiteralExpected, reportError: isActive);
                            idExpression = SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, id);
                        }

                        hasError = hasError || id.ContainsDiagnostics;
                        ids.Add(idExpression);

                        if (this.CurrentToken.Kind != SyntaxKind.CommaToken)
                        {
                            break;
                        }

                        ids.AddSeparator(this.EatToken());
                    }

                    var end = this.ParseEndOfDirective(hasError || !isActive, afterPragma: true);
                    return SyntaxFactory.PragmaWarningDirectiveTrivia(hash, pragma, warning, style, ids.ToList(), end, isActive);
                }
                else
                {
                    style = this.EatToken(SyntaxKind.DisableKeyword, ErrorCode.WRN_IllegalPPWarning, reportError: isActive);
                    var end = this.ParseEndOfDirective(ignoreErrors: true, afterPragma: true);
                    return SyntaxFactory.PragmaWarningDirectiveTrivia(hash, pragma, warning, style, default(SeparatedSyntaxList<ExpressionSyntax>), end, isActive);
                }
            }
            else if (this.CurrentToken.Kind == SyntaxKind.ChecksumKeyword)
            {
                var checksum = this.EatToken();
                var file = this.EatToken(SyntaxKind.StringLiteralToken, ErrorCode.WRN_IllegalPPChecksum, reportError: isActive);
                var guid = this.EatToken(SyntaxKind.StringLiteralToken, ErrorCode.WRN_IllegalPPChecksum, reportError: isActive && !file.IsMissing);
                if (isActive && !guid.IsMissing)
                {
                    Guid tmp;
                    if (!Guid.TryParse(guid.ValueText, out tmp))
                    {
                        guid = this.AddError(guid, ErrorCode.WRN_IllegalPPChecksum);
                    }
                }

                var bytes = this.EatToken(SyntaxKind.StringLiteralToken, ErrorCode.WRN_IllegalPPChecksum, reportError: isActive && !guid.IsMissing);
                if (isActive && !bytes.IsMissing)
                {
                    if (bytes.ValueText.Length % 2 != 0)
                    {
                        bytes = this.AddError(bytes, ErrorCode.WRN_IllegalPPChecksum);
                    }
                    else
                    {
                        foreach (char c in bytes.ValueText)
                        {
                            if (!SyntaxFacts.IsHexDigit(c))
                            {
                                bytes = this.AddError(bytes, ErrorCode.WRN_IllegalPPChecksum);
                                break;
                            }
                        }
                    }
                }

                hasError = file.ContainsDiagnostics | guid.ContainsDiagnostics | bytes.ContainsDiagnostics;
                var eod = this.ParseEndOfDirective(ignoreErrors: hasError, afterPragma: true);
                return SyntaxFactory.PragmaChecksumDirectiveTrivia(hash, pragma, checksum, file, guid, bytes, eod, isActive);
            }
            else
            {
                var warning = this.EatToken(SyntaxKind.WarningKeyword, ErrorCode.WRN_IllegalPragma, reportError: isActive);
                var style = this.EatToken(SyntaxKind.DisableKeyword, reportError: false);
                var eod = this.ParseEndOfDirective(ignoreErrors: true, afterPragma: true);
                return SyntaxFactory.PragmaWarningDirectiveTrivia(hash, pragma, warning, style, default(SeparatedSyntaxList<ExpressionSyntax>), eod, isActive);
            }
        }
Пример #10
0
 public override InternalSyntax.CSharpSyntaxNode VisitToken(InternalSyntax.SyntaxToken token)
 {
     return(InternalSyntax.SyntaxFactory.MissingToken(token.Kind));
 }
 private void ParseInterpolationStart(out SyntaxToken openBraceToken, out ExpressionSyntax expr, out SyntaxToken commaToken, out ExpressionSyntax alignmentExpression)
 {
     openBraceToken = this.EatToken(SyntaxKind.OpenBraceToken);
     expr           = this.ParseExpression();
     if (this.CurrentToken.Kind == SyntaxKind.CommaToken)
     {
         commaToken          = this.EatToken(SyntaxKind.CommaToken);
         alignmentExpression = ConsumeUnexpectedTokens(this.ParseExpression());
     }
     else
     {
         commaToken          = default(SyntaxToken);
         alignmentExpression = null;
         expr = ConsumeUnexpectedTokens(expr);
     }
 }
Пример #12
0
        private DirectiveTriviaSyntax ParseErrorOrWarningDirective(SyntaxToken hash, SyntaxToken keyword, bool isActive)
        {
            var eod = this.ParseEndOfDirectiveWithOptionalPreprocessingMessage();
            bool isError = keyword.Kind == SyntaxKind.ErrorKeyword;
            if (isActive)
            {
                var triviaBuilder = new System.IO.StringWriter(System.Globalization.CultureInfo.InvariantCulture);
                int triviaWidth = 0;

                // whitespace and single line comments are trailing trivia on the keyword, the rest
                // of the error message is leading trivia on the eod.
                //
                bool skipping = true;
                foreach (var t in keyword.TrailingTrivia)
                {
                    if (skipping)
                    {
                        if (t.Kind == SyntaxKind.WhitespaceTrivia)
                        {
                            continue;
                        }

                        skipping = false;
                    }

                    t.WriteTo(triviaBuilder, leading: true, trailing: true);
                    triviaWidth += t.FullWidth;
                }

                foreach (var node in eod.LeadingTrivia)
                {
                    node.WriteTo(triviaBuilder, leading: true, trailing: true);
                    triviaWidth += node.FullWidth;
                }

                //relative to leading trivia of eod
                //could be negative if part of the error text comes from the trailing trivia of the keyword token
                int triviaOffset = eod.GetLeadingTriviaWidth() - triviaWidth;

                string errorText = triviaBuilder.ToString();
                eod = this.AddError(eod, triviaOffset, triviaWidth, isError ? ErrorCode.ERR_ErrorDirective : ErrorCode.WRN_WarningDirective, errorText);

                if (isError)
                {
                    if (errorText.Equals("version", StringComparison.Ordinal))
                    {
                        string version = CommonCompiler.GetProductVersion(typeof(CSharpCompiler));
                        var specified = this.Options.SpecifiedLanguageVersion;
                        var effective = specified.MapSpecifiedToEffectiveVersion();

                        var displayLanguageVersion = specified == effective ? specified.ToDisplayString() : $"{specified.ToDisplayString()} ({effective.ToDisplayString()})";

                        eod = this.AddError(eod, triviaOffset, triviaWidth, ErrorCode.ERR_CompilerAndLanguageVersion, version,
                            displayLanguageVersion);
                    }
                    else
                    {
                        const string versionMarker = "version:";
                        if (this.Options.LanguageVersion != LanguageVersion.Preview &&
                            errorText.StartsWith(versionMarker, StringComparison.Ordinal) &&
                            LanguageVersionFacts.TryParse(errorText.Substring(versionMarker.Length), out var languageVersion))
                        {
                            ErrorCode error = this.Options.LanguageVersion.GetErrorCode();
                            eod = this.AddError(eod, triviaOffset, triviaWidth, error, "version", new CSharpRequiredLanguageVersion(languageVersion));
                        }
                    }
                }
            }

            if (isError)
            {
                return SyntaxFactory.ErrorDirectiveTrivia(hash, keyword, eod, isActive);
            }
            else
            {
                return SyntaxFactory.WarningDirectiveTrivia(hash, keyword, eod, isActive);
            }
        }
Пример #13
0
 internal SyntaxNodeOrToken(CSharpSyntaxNode parent, Syntax.InternalSyntax.SyntaxToken token, int position, int index, bool fromTokenCtor = false)
Пример #14
0
 internal static bool IsVar(this Syntax.InternalSyntax.SyntaxToken node)
 {
     return(node.Kind == SyntaxKind.IdentifierToken && (node.ValueText == "var" || node.ValueText == "变"));
 }
Пример #15
0
 private SyntaxLastTokenReplacer(SyntaxToken oldToken, SyntaxToken newToken)
 {
     this.oldToken = oldToken;
     this.newToken = newToken;
 }
Пример #16
0
 internal static SyntaxToken MissingToken(CSharpSyntaxNode leading, SyntaxKind kind, CSharpSyntaxNode trailing)
 {
     return(SyntaxToken.CreateMissing(kind, leading, trailing));
 }
Пример #17
0
 internal static SyntaxToken MissingToken(SyntaxKind kind)
 {
     return(SyntaxToken.CreateMissing(kind, null, null));
 }
Пример #18
0
 public static SyntaxToken Token(SyntaxKind kind)
 {
     return(SyntaxToken.Create(kind));
 }
Пример #19
0
 internal static bool IsVarOrPredefinedType(this Syntax.InternalSyntax.SyntaxToken node)
 {
     return(node.IsVar() || IsPredefinedType(node.Kind));
 }
Пример #20
0
 private DirectiveTriviaSyntax ParseRegionDirective(SyntaxToken hash, SyntaxToken keyword, bool isActive)
 {
     return SyntaxFactory.RegionDirectiveTrivia(hash, keyword, this.ParseEndOfDirectiveWithOptionalPreprocessingMessage(), isActive);
 }