private PostSkipAction SkipBadBaseListTokens(ref SyntaxToken colon, SeparatedSyntaxListBuilder <TypeSyntax> list, SyntaxKind expected)
 {
     return(this.SkipBadSeparatedListTokensWithExpectedKind(ref colon, list,
                                                            p => p.CurrentToken.Kind != SyntaxKind.CommaToken && !p.IsPossibleAttribute(),
                                                            p => p.CurrentToken.Kind == SyntaxKind.OpenBraceToken || p.IsPossibleTypeParameterConstraintClauseStart() || p.IsTerminator(),
                                                            expected));
 }
 private PostSkipAction SkipBadArgumentListTokens(ref SyntaxToken open, SeparatedSyntaxListBuilder <ArgumentSyntax> list, SyntaxKind expected, SyntaxKind closeKind)
 {
     return(this.SkipBadSeparatedListTokensWithExpectedKind(ref open, list,
                                                            p => p.CurrentToken.Kind != SyntaxKind.CommaToken && !p.IsPossibleArgumentExpression(),
                                                            p => p.CurrentToken.Kind == closeKind || p.CurrentToken.Kind == SyntaxKind.SemicolonToken || p.IsTerminator(),
                                                            expected));
 }
 private PostSkipAction SkipBadParameterListTokens(ref SyntaxToken open, SeparatedSyntaxListBuilder <ParameterSyntax> list, SyntaxKind expected, SyntaxKind closeKind, bool allowThisKeyword, bool allowFieldModifiers)
 {
     return(this.SkipBadSeparatedListTokensWithExpectedKind(ref open, list,
                                                            p => p.CurrentToken.Kind != SyntaxKind.CommaToken && !p.IsPossibleParameter(allowThisKeyword, allowFieldModifiers),
                                                            p => p.CurrentToken.Kind == closeKind || p.IsTerminator(),
                                                            expected));
 }
        private void ParseForStatementExpressionList(ref SyntaxToken startToken, SeparatedSyntaxListBuilder <ExpressionSyntax> list)
        {
            if (this.CurrentToken.Kind != SyntaxKind.CloseParenToken && this.CurrentToken.Kind != SyntaxKind.SemicolonToken)
            {
tryAgain:
                if (this.IsPossibleExpression() || this.CurrentToken.Kind == SyntaxKind.CommaToken)
                {
                    // first argument
                    list.Add(this.ParseExpression());

                    // additional arguments
                    while (true)
                    {
                        if (this.CurrentToken.Kind == SyntaxKind.CloseParenToken || this.CurrentToken.Kind == SyntaxKind.SemicolonToken)
                        {
                            break;
                        }
                        else if (this.CurrentToken.Kind == SyntaxKind.CommaToken || this.IsPossibleExpression())
                        {
                            list.AddSeparator(this.EatToken(SyntaxKind.CommaToken));
                            list.Add(this.ParseExpression());
                            continue;
                        }
                        else if (this.SkipBadForStatementExpressionListTokens(ref startToken, list, SyntaxKind.CommaToken) == PostSkipAction.Abort)
                        {
                            break;
                        }
                    }
                }
                else if (this.SkipBadForStatementExpressionListTokens(ref startToken, list, SyntaxKind.IdentifierToken) == PostSkipAction.Continue)
                {
                    goto tryAgain;
                }
            }
        }
Пример #5
0
        //private FieldDeclarationSyntax ParseConstantFieldDeclaration(SyntaxListBuilder<AnnotationSyntax> attributes, SyntaxListBuilder modifiers, SyntaxKind parentKind)
        //{
        //	var constToken = this.EatToken(SyntaxKind.ConstKeyword);
        //	modifiers.Add(constToken);

        //	var type = this.ParseType(false);

        //	var variables = this._pool.AllocateSeparated<VariableDeclaratorSyntax>();
        //	try
        //	{
        //		this.ParseVariableDeclarators(type, VariableFlags.Const, variables, parentKind);
        //		var semicolon = this.EatToken(SyntaxKind.SemicolonToken);
        //		return _syntaxFactory.FieldDeclaration(
        //			attributes,
        //			modifiers.ToTokenList(),
        //			_syntaxFactory.VariableDeclaration(type, variables),
        //			semicolon);
        //	}
        //	finally
        //	{
        //		this._pool.Free(variables);
        //	}
        //}



        private void ParseVariableDeclarators(TypeSyntax type, VariableFlags flags, SeparatedSyntaxListBuilder <VariableDeclaratorSyntax> variables, SyntaxKind parentKind)
        {
            // Although we try parse variable declarations in contexts where they are not allowed (non-interactive top-level or a namespace)
            // the reported errors should take into consideration whether or not one expects them in the current context.
            bool variableDeclarationsExpected = (parentKind != SyntaxKind.CompilationUnit);

            ParseVariableDeclarators(type, flags, variables, variableDeclarationsExpected);
        }
        private PostSkipAction SkipBadVariableListTokens(SeparatedSyntaxListBuilder <VariableDeclaratorSyntax> list, SyntaxKind expected)
        {
            CSharpSyntaxNode tmp = null;

            Debug.Assert(list.Count > 0);
            return(this.SkipBadSeparatedListTokensWithExpectedKind(ref tmp, list,
                                                                   p => this.CurrentToken.Kind != SyntaxKind.CommaToken,
                                                                   p => this.CurrentToken.Kind == SyntaxKind.SemicolonToken || this.IsTerminator(),
                                                                   expected));
        }
        private PostSkipAction SkipBadTypeParameterConstraintTokens(SeparatedSyntaxListBuilder <TypeParameterConstraintSyntax> list, SyntaxKind expected)
        {
            CSharpSyntaxNode tmp = null;

            Debug.Assert(list.Count > 0);
            return(this.SkipBadSeparatedListTokensWithExpectedKind(ref tmp, list,
                                                                   p => this.CurrentToken.Kind != SyntaxKind.CommaToken && !this.IsPossibleTypeParameterConstraint(),
                                                                   p => this.CurrentToken.Kind == SyntaxKind.OpenBraceToken || this.IsPossibleTypeParameterConstraintClauseStart() || this.IsTerminator(),
                                                                   expected));
        }
        private PostSkipAction SkipBadTypeArgumentListTokens(SeparatedSyntaxListBuilder <TypeSyntax> list, SyntaxKind expected)
        {
            CSharpSyntaxNode tmp = null;

            Debug.Assert(list.Count > 0);
            return(this.SkipBadSeparatedListTokensWithExpectedKind(ref tmp, list,
                                                                   p => this.CurrentToken.Kind != SyntaxKind.CommaToken && !this.IsPossibleType(),
                                                                   p => this.CurrentToken.Kind == SyntaxKind.GreaterThanToken || this.IsTerminator(),
                                                                   expected));
        }
        private void ParseAnonymousTypeMemberInitializers(ref SyntaxToken openBrace, ref SeparatedSyntaxListBuilder <AnonymousObjectMemberDeclaratorSyntax> list)
        {
            if (this.CurrentToken.Kind != SyntaxKind.CloseBraceToken)
            {
tryAgain:
                if (this.IsPossibleExpression() || this.CurrentToken.Kind == SyntaxKind.CommaToken)
                {
                    // first argument
                    list.Add(this.ParseAnonymousTypeMemberInitializer());

                    // additional arguments
                    while (true)
                    {
                        if (this.CurrentToken.Kind == SyntaxKind.CloseBraceToken)
                        {
                            break;
                        }
                        else if (this.CurrentToken.Kind == SyntaxKind.CommaToken || this.IsPossibleExpression())
                        {
                            list.AddSeparator(this.EatToken(SyntaxKind.CommaToken));

                            // check for exit case after legal trailing comma
                            if (this.CurrentToken.Kind == SyntaxKind.CloseBraceToken)
                            {
                                break;
                            }
                            else if (!this.IsPossibleExpression())
                            {
                                goto tryAgain;
                            }

                            list.Add(this.ParseAnonymousTypeMemberInitializer());
                            continue;
                        }
                        else if (this.SkipBadInitializerListTokens(ref openBrace, list, SyntaxKind.CommaToken) == PostSkipAction.Abort)
                        {
                            break;
                        }
                    }
                }
                else if (this.SkipBadInitializerListTokens(ref openBrace, list, SyntaxKind.IdentifierToken) == PostSkipAction.Continue)
                {
                    goto tryAgain;
                }
            }
        }
        private void ParseDeclaration(bool isFinal, out TypeSyntax type, SeparatedSyntaxListBuilder <VariableDeclaratorSyntax> variables)
        {
            type = this.ParseType(false);

            VariableFlags flags = VariableFlags.Local;

            if (isFinal)
            {
                flags |= VariableFlags.Final;
            }

            var saveTerm = this._termState;

            this._termState |= TerminatorState.IsEndOfDeclarationClause;
            this.ParseVariableDeclarators(type, flags, variables, variableDeclarationsExpected: true);
            this._termState = saveTerm;
        }
Пример #11
0
        /*
         * 1. parameter是指函数定义中参数,而argument指的是函数调用时的实际参数。
         * 2. 简略描述为:parameter=形参(formal parameter), argument=实参(actual parameter)。
         */


        // ParseInstantiation: Parses the generic argument/parameter parts of the name.
        private void ParseTypeArgumentList(out SyntaxToken open, SeparatedSyntaxListBuilder <TypeSyntax> types, out SyntaxToken close)
        {
            Debug.Assert(this.CurrentToken.Kind == SyntaxKind.LessThanToken);
            open = this.EatToken(SyntaxKind.LessThanToken);
            open = CheckFeatureAvailability(open, MessageID.IDS_FeatureGenerics);

            if (this.IsOpenName())
            {
                // NOTE: trivia will be attached to comma, not omitted type argument
                var omittedTypeArgumentInstance = _syntaxFactory.OmittedTypeArgument(SyntaxFactory.Token(SyntaxKind.OmittedTypeArgumentToken));
                types.Add(omittedTypeArgumentInstance);
                while (this.CurrentToken.Kind == SyntaxKind.CommaToken)
                {
                    types.AddSeparator(this.EatToken(SyntaxKind.CommaToken));
                    types.Add(omittedTypeArgumentInstance);
                }

                close = this.EatToken(SyntaxKind.GreaterThanToken);

                return;
            }

            // first type
            types.Add(this.ParseTypeArgument());

            // remaining types & commas
            while (true)
            {
                if (this.CurrentToken.Kind == SyntaxKind.GreaterThanToken || this.IsPossibleTypeParameterConstraintClauseStart())
                {
                    break;
                }
                else if (this.CurrentToken.Kind == SyntaxKind.CommaToken || this.IsPossibleType())
                {
                    types.AddSeparator(this.EatToken(SyntaxKind.CommaToken));
                    types.Add(this.ParseTypeArgument());
                }
                else if (this.SkipBadTypeArgumentListTokens(types, SyntaxKind.CommaToken) == PostSkipAction.Abort)
                {
                    break;
                }
            }

            close = this.EatToken(SyntaxKind.GreaterThanToken);
        }
Пример #12
0
        private PostSkipAction SkipBadSeparatedListTokensWithExpectedKind <T, TNode>(
            ref T startToken,
            SeparatedSyntaxListBuilder <TNode> list,
            Func <LanguageParser, bool> isNotExpectedFunction,
            Func <LanguageParser, bool> abortFunction,
            SyntaxKind expected)
            where T : CSharpSyntaxNode
            where TNode : CSharpSyntaxNode
        {
            // We're going to cheat here and pass the underlying SyntaxListBuilder of "list" to the helper method so that
            // it can append skipped trivia to the last element, regardless of whether that element is a node or a token.
            CSharpSyntaxNode trailingTrivia;
            var action = this.SkipBadListTokensWithExpectedKindHelper(list.UnderlyingBuilder, isNotExpectedFunction, abortFunction, expected, out trailingTrivia);

            if (trailingTrivia != null)
            {
                startToken = AddTrailingSkippedSyntax(startToken, trailingTrivia);
            }
            return(action);
        }
Пример #13
0
        private void ParseVariableDeclarators(TypeSyntax type, VariableFlags flags, SeparatedSyntaxListBuilder <VariableDeclaratorSyntax> variables, bool variableDeclarationsExpected)
        {
            variables.Add(this.ParseVariableDeclarator(type, flags, isFirst: true));

            while (true)
            {
                if (this.CurrentToken.Kind == SyntaxKind.SemicolonToken)
                {
                    break;
                }
                else if (this.CurrentToken.Kind == SyntaxKind.CommaToken)
                {
                    variables.AddSeparator(this.EatToken(SyntaxKind.CommaToken));
                    variables.Add(this.ParseVariableDeclarator(type, flags, isFirst: false));
                }
                else if (!variableDeclarationsExpected || this.SkipBadVariableListTokens(variables, SyntaxKind.CommaToken) == PostSkipAction.Abort)
                {
                    break;
                }
            }
        }
Пример #14
0
 internal void Free <TNode>(SeparatedSyntaxListBuilder <TNode> item) where TNode : CSharpSyntaxNode
 {
     Free(item.UnderlyingBuilder);
 }
Пример #15
0
        private DirectiveTriviaSyntax ParsePragmaDirective(SyntaxToken hash, SyntaxToken pragma, bool 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 || this.CurrentToken.Kind == SyntaxKind.StringLiteralToken)
                        {
                            id = this.EatToken();
                            if (isActive)
                            {
                                if (id.Kind == SyntaxKind.NumericLiteralToken)
                                {
                                    int compilerWarningNumber = (int)id.Value;
                                    if (ErrorFacts.GetSeverity((ErrorCode)compilerWarningNumber) != DiagnosticSeverity.Warning)
                                    {
                                        id = this.AddError(id, ErrorCode.WRN_BadWarningNumber, compilerWarningNumber);
                                    }
                                }
                                else
                                {
                                    string value           = (string)id.Value;
                                    var    messageProvider = MessageProvider.Instance;
                                    if (value.StartsWith(messageProvider.CodePrefix))
                                    {
                                        // For diagnostic IDs of the form "CS[0-9]*", verify the error code is that of a warning
                                        int compilerWarningNumber;
                                        if (int.TryParse(value.Substring(messageProvider.CodePrefix.Length), NumberStyles.None, CultureInfo.InvariantCulture, out compilerWarningNumber) &&
                                            (messageProvider.GetIdForErrorCode(compilerWarningNumber) != value ||
                                             ErrorFacts.GetSeverity((ErrorCode)compilerWarningNumber) != DiagnosticSeverity.Warning))
                                        {
                                            id = this.AddError(id, ErrorCode.WRN_BadWarningNumber, value);
                                        }
                                    }
                                }
                            }

                            var expressionKind = id.Kind == SyntaxKind.NumericLiteralToken ? SyntaxKind.NumericLiteralExpression : SyntaxKind.StringLiteralExpression;
                            idExpression = SyntaxFactory.LiteralExpression(expressionKind, id);
                        }
                        else
                        {
                            id           = this.EatToken(SyntaxKind.NumericLiteralToken, ErrorCode.WRN_StringOrNumericLiteralExpected, 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));
            }
        }
Пример #16
0
        private void ParseJavaEnumConstants(
            ref SyntaxToken openBrace,
            SeparatedSyntaxListBuilder <JavaEnumConstantSyntax> members, SyntaxToken name)
        {
            if (this.CurrentToken.Kind != SyntaxKind.SemicolonToken && this.CurrentToken.Kind != SyntaxKind.CloseBraceToken)
            {
tryAgain:

                if (this.IsPossibleJavaEnumConstant() ||
                    this.CurrentToken.Kind == SyntaxKind.CommaToken
                    //|| this.CurrentToken.Kind == SyntaxKind.SemicolonToken
                    )
                {
                    // first member
                    members.Add(this.ParseJavaEnumConstant());

                    // additional members
                    while (true)
                    {
                        if (this.CurrentToken.Kind == SyntaxKind.CloseBraceToken)
                        {
                            break;
                        }
                        else if (this.CurrentToken.Kind == SyntaxKind.CommaToken ||
                                 this.CurrentToken.Kind == SyntaxKind.SemicolonToken ||
                                 this.IsPossibleJavaEnumConstant())
                        {
                            if (this.CurrentToken.Kind == SyntaxKind.SemicolonToken)
                            {
                                //// semicolon instead of comma.. consume it with error and act as if it were a comma.
                                //members.AddSeparator(this.EatTokenWithPrejudice(SyntaxKind.CommaToken));
                                break;
                            }
                            //else
                            {
                                members.AddSeparator(this.EatToken(SyntaxKind.CommaToken));
                            }

                            // check for exit case after legal trailing comma
                            if (this.CurrentToken.Kind == SyntaxKind.CloseBraceToken)
                            {
                                break;
                            }
                            else if (this.CurrentToken.Kind == SyntaxKind.SemicolonToken)
                            {
                                break;
                            }
                            else if (!this.IsPossibleJavaEnumConstant())
                            {
                                goto tryAgain;
                            }

                            members.Add(this.ParseJavaEnumConstant());
                            continue;
                        }
                        else if (this.SkipBadJavaEnumConstantListTokens(ref openBrace, members, SyntaxKind.CommaToken) == PostSkipAction.Abort)
                        {
                            break;
                        }
                    }
                }
                else if (this.SkipBadJavaEnumConstantListTokens(ref openBrace, members, SyntaxKind.IdentifierToken) == PostSkipAction.Continue)
                {
                    goto tryAgain;
                }
            }
        }
Пример #17
0
 private PostSkipAction SkipBadLambdaParameterListTokens(ref SyntaxToken openParen, SeparatedSyntaxListBuilder <ParameterSyntax> list, SyntaxKind expected, SyntaxKind closeKind)
 {
     return(this.SkipBadSeparatedListTokensWithExpectedKind(ref openParen, list,
                                                            p => p.CurrentToken.Kind != SyntaxKind.CommaToken && !p.IsPossibleLambdaParameter(),
                                                            p => p.CurrentToken.Kind == closeKind || p.IsTerminator(),
                                                            expected));
 }
Пример #18
0
 private PostSkipAction SkipBadArrayInitializerTokens(ref SyntaxToken openBrace, SeparatedSyntaxListBuilder <ExpressionSyntax> list, SyntaxKind expected)
 {
     return(this.SkipBadSeparatedListTokensWithExpectedKind(ref openBrace, list,
                                                            p => p.CurrentToken.Kind != SyntaxKind.CommaToken && !p.IsPossibleVariableInitializer(false),
                                                            p => this.CurrentToken.Kind == SyntaxKind.CloseBraceToken || this.IsTerminator(),
                                                            expected));
 }
Пример #19
0
 private PostSkipAction SkipBadAttributeArgumentTokens(ref SyntaxToken openParen, SeparatedSyntaxListBuilder <AnnotationArgumentSyntax> list, SyntaxKind expected)
 {
     return(this.SkipBadSeparatedListTokensWithExpectedKind(ref openParen, list,
                                                            p => p.CurrentToken.Kind != SyntaxKind.CommaToken && !p.IsPossibleAttributeArgument(),
                                                            p => p.CurrentToken.Kind == SyntaxKind.CloseParenToken || p.IsTerminator(),
                                                            expected));
 }
Пример #20
0
 private PostSkipAction SkipBadForStatementExpressionListTokens(ref SyntaxToken startToken, SeparatedSyntaxListBuilder <ExpressionSyntax> list, SyntaxKind expected)
 {
     return(this.SkipBadSeparatedListTokensWithExpectedKind(ref startToken, list,
                                                            p => p.CurrentToken.Kind != SyntaxKind.CommaToken && !p.IsPossibleExpression(),
                                                            p => p.CurrentToken.Kind == SyntaxKind.CloseParenToken || p.CurrentToken.Kind == SyntaxKind.SemicolonToken || p.IsTerminator(),
                                                            expected));
 }
        private void ParseObjectOrCollectionInitializerMembers(ref SyntaxToken startToken, SeparatedSyntaxListBuilder <ExpressionSyntax> list, out bool isObjectInitializer)
        {
            // Empty initializer list must be parsed as an object initializer.
            isObjectInitializer = true;

            if (this.CurrentToken.Kind != SyntaxKind.CloseBraceToken)
            {
tryAgain:
                if (this.IsInitializerMember() || this.CurrentToken.Kind == SyntaxKind.CommaToken)
                {
                    // We have at least one initializer expression.
                    // If at least one initializer expression is a named assignment, this is an object initializer.
                    // Otherwise, this is a collection initializer.
                    isObjectInitializer = false;

                    // first argument
                    list.Add(this.ParseObjectOrCollectionInitializerMember(ref isObjectInitializer));

                    // additional arguments
                    while (true)
                    {
                        if (this.CurrentToken.Kind == SyntaxKind.CloseBraceToken)
                        {
                            break;
                        }
                        else if (this.CurrentToken.Kind == SyntaxKind.CommaToken || this.IsInitializerMember())
                        {
                            list.AddSeparator(this.EatToken(SyntaxKind.CommaToken));

                            // check for exit case after legal trailing comma
                            if (this.CurrentToken.Kind == SyntaxKind.CloseBraceToken)
                            {
                                break;
                            }

                            list.Add(this.ParseObjectOrCollectionInitializerMember(ref isObjectInitializer));
                            continue;
                        }
                        else if (this.SkipBadInitializerListTokens(ref startToken, list, SyntaxKind.CommaToken) == PostSkipAction.Abort)
                        {
                            break;
                        }
                    }
                }
                else if (this.SkipBadInitializerListTokens(ref startToken, list, SyntaxKind.IdentifierToken) == PostSkipAction.Continue)
                {
                    goto tryAgain;
                }
            }

            // We may have invalid initializer elements. These will be reported during binding.
        }
Пример #22
0
 private PostSkipAction SkipBadJavaEnumConstantListTokens(ref SyntaxToken openBrace, SeparatedSyntaxListBuilder <JavaEnumConstantSyntax> list, SyntaxKind expected)
 {
     return(this.SkipBadSeparatedListTokensWithExpectedKind(ref openBrace, list,
                                                            p => p.CurrentToken.Kind != SyntaxKind.CommaToken && p.CurrentToken.Kind != SyntaxKind.SemicolonToken && !p.IsPossibleJavaEnumConstant(),
                                                            p => p.CurrentToken.Kind == SyntaxKind.CloseBraceToken || p.IsTerminator(),
                                                            expected));
 }
        private void ParseArgumentList(
            out SyntaxToken openToken,
            out SeparatedSyntaxList <ArgumentSyntax> arguments,
            out SyntaxToken closeToken,
            SyntaxKind openKind,
            SyntaxKind closeKind)
        {
            bool isIndexer = openKind == SyntaxKind.OpenBracketToken;
            var  open      = this.EatToken(openKind);
            var  saveTerm  = this._termState;

            this._termState |= TerminatorState.IsEndOfArgumentList;

            SeparatedSyntaxListBuilder <ArgumentSyntax> list = default(SeparatedSyntaxListBuilder <ArgumentSyntax>);

            try
            {
                if (this.CurrentToken.Kind != closeKind && this.CurrentToken.Kind != SyntaxKind.SemicolonToken)
                {
tryAgain:
                    if (list.IsNull)
                    {
                        list = this._pool.AllocateSeparated <ArgumentSyntax>();
                    }

                    if (this.IsPossibleArgumentExpression() || this.CurrentToken.Kind == SyntaxKind.CommaToken)
                    {
                        // first argument
                        list.Add(this.ParseArgumentExpression(isIndexer));

                        // additional arguments
                        while (true)
                        {
                            if (this.CurrentToken.Kind == closeKind || this.CurrentToken.Kind == SyntaxKind.SemicolonToken)
                            {
                                break;
                            }
                            else if (this.CurrentToken.Kind == SyntaxKind.CommaToken || this.IsPossibleArgumentExpression())
                            {
                                list.AddSeparator(this.EatToken(SyntaxKind.CommaToken));
                                list.Add(this.ParseArgumentExpression(isIndexer));
                                continue;
                            }
                            else if (this.SkipBadArgumentListTokens(ref open, list, SyntaxKind.CommaToken, closeKind) == PostSkipAction.Abort)
                            {
                                break;
                            }
                        }
                    }
                    else if (this.SkipBadArgumentListTokens(ref open, list, SyntaxKind.IdentifierToken, closeKind) == PostSkipAction.Continue)
                    {
                        goto tryAgain;
                    }
                }
                else if (isIndexer && this.CurrentToken.Kind == closeKind)
                {
                    // An indexer always expects at least one value. And so we need to give an error
                    // for the case where we see only "[]". ParseArgumentExpression gives it.

                    if (list.IsNull)
                    {
                        list = this._pool.AllocateSeparated <ArgumentSyntax>();
                    }

                    list.Add(this.ParseArgumentExpression(isIndexer));
                }

                this._termState = saveTerm;

                openToken  = open;
                closeToken = this.EatToken(closeKind);
                arguments  = list.ToList();
            }
            finally
            {
                if (!list.IsNull)
                {
                    this._pool.Free(list);
                }
            }
        }
        private void ParseExpressionsForComplexElementInitializer(ref SyntaxToken openBrace, SeparatedSyntaxListBuilder <ExpressionSyntax> list, out DiagnosticInfo closeBraceError)
        {
            closeBraceError = null;

            if (this.CurrentToken.Kind != SyntaxKind.CloseBraceToken)
            {
tryAgain:
                if (this.IsPossibleExpression() || this.CurrentToken.Kind == SyntaxKind.CommaToken)
                {
                    // first argument
                    list.Add(this.ParseExpression());

                    // additional arguments
                    while (true)
                    {
                        if (this.CurrentToken.Kind == SyntaxKind.CloseBraceToken)
                        {
                            break;
                        }
                        else if (this.CurrentToken.Kind == SyntaxKind.CommaToken || this.IsPossibleExpression())
                        {
                            list.AddSeparator(this.EatToken(SyntaxKind.CommaToken));
                            if (this.CurrentToken.Kind == SyntaxKind.CloseBraceToken)
                            {
                                closeBraceError = MakeError(this.CurrentToken, ErrorCode.ERR_ExpressionExpected);
                                break;
                            }
                            list.Add(this.ParseExpression());
                            continue;
                        }
                        else if (this.SkipBadInitializerListTokens(ref openBrace, list, SyntaxKind.CommaToken) == PostSkipAction.Abort)
                        {
                            break;
                        }
                    }
                }
                else if (this.SkipBadInitializerListTokens(ref openBrace, list, SyntaxKind.IdentifierToken) == PostSkipAction.Continue)
                {
                    goto tryAgain;
                }
            }
        }
Пример #25
0
        private void ParseParameterList(
            out SyntaxToken open,
            SeparatedSyntaxListBuilder <ParameterSyntax> nodes,
            out SyntaxToken close,
            SyntaxKind openKind,
            SyntaxKind closeKind,
            bool allowThisKeyword,
            bool allowDefaults,
            bool allowAttributes,
            bool allowFieldModifiers)
        {
            open = this.EatToken(openKind);

            var saveTerm = this._termState;

            this._termState |= TerminatorState.IsEndOfParameterList;

            var attributes = this._pool.Allocate <AnnotationSyntax>();
            var modifiers  = this._pool.Allocate();

            try
            {
                if (this.CurrentToken.Kind != closeKind)
                {
tryAgain:
                    int mustBeLastIndex = -1;

                    bool hasArgList = false;

                    if (this.IsPossibleParameter(allowThisKeyword, allowFieldModifiers) || this.CurrentToken.Kind == SyntaxKind.CommaToken)
                    {
                        // first parameter
                        attributes.Clear();
                        modifiers.Clear();
                        var parameter = this.ParseParameter(attributes, modifiers, allowThisKeyword, allowDefaults, allowAttributes, allowFieldModifiers);
                        nodes.Add(parameter);
                        hasArgList = parameter.DotDotDotToken != null;
                        bool mustBeLast = hasArgList;
                        if (mustBeLast && mustBeLastIndex == -1)
                        {
                            mustBeLastIndex = nodes.Count - 1;
                        }

                        // additional parameters
                        while (true)
                        {
                            if (this.CurrentToken.Kind == closeKind)
                            {
                                break;
                            }
                            else if (this.CurrentToken.Kind == SyntaxKind.CommaToken || this.IsPossibleParameter(allowThisKeyword, allowFieldModifiers))
                            {
                                nodes.AddSeparator(this.EatToken(SyntaxKind.CommaToken));
                                attributes.Clear();
                                modifiers.Clear();
                                parameter = this.ParseParameter(attributes, modifiers, allowThisKeyword, allowDefaults, allowAttributes, allowFieldModifiers);
                                nodes.Add(parameter);
                                hasArgList = parameter.DotDotDotToken != null;
                                mustBeLast = hasArgList;
                                if (mustBeLast && mustBeLastIndex == -1)
                                {
                                    mustBeLastIndex = nodes.Count - 1;
                                }

                                continue;
                            }
                            else if (this.SkipBadParameterListTokens(ref open, nodes, SyntaxKind.CommaToken, closeKind, allowThisKeyword, allowFieldModifiers) == PostSkipAction.Abort)
                            {
                                break;
                            }
                        }
                    }
                    else if (this.SkipBadParameterListTokens(ref open, nodes, SyntaxKind.IdentifierToken, closeKind, allowThisKeyword, allowFieldModifiers) == PostSkipAction.Continue)
                    {
                        goto tryAgain;
                    }

                    if (mustBeLastIndex >= 0 && mustBeLastIndex < nodes.Count - 1)
                    {
                        nodes[mustBeLastIndex] = this.AddError(nodes[mustBeLastIndex], ErrorCode.ERR_VarargsLast);
                    }
                }

                this._termState = saveTerm;
                close           = this.EatToken(closeKind);
            }
            finally
            {
                this._pool.Free(modifiers);
                this._pool.Free(attributes);
            }
        }
Пример #26
0
 private PostSkipAction SkipBadInitializerListTokens <T>(ref SyntaxToken startToken, SeparatedSyntaxListBuilder <T> list, SyntaxKind expected)
     where T : CSharpSyntaxNode
 {
     return(this.SkipBadSeparatedListTokensWithExpectedKind(ref startToken, list,
                                                            p => p.CurrentToken.Kind != SyntaxKind.CommaToken && !p.IsPossibleExpression(),
                                                            p => p.CurrentToken.Kind == SyntaxKind.CloseBraceToken || p.IsTerminator(),
                                                            expected));
 }
Пример #27
0
        private DirectiveTriviaSyntax ParsePragmaDirective(SyntaxToken hash, SyntaxToken pragma, bool 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));
            }
        }
Пример #28
0
 private PostSkipAction SkipBadArrayRankSpecifierTokens(ref SyntaxToken openBracket, SeparatedSyntaxListBuilder <ExpressionSyntax> list, SyntaxKind expected)
 {
     return(this.SkipBadSeparatedListTokensWithExpectedKind(ref openBracket, list,
                                                            p => p.CurrentToken.Kind != SyntaxKind.CommaToken && !p.IsPossibleExpression(),
                                                            p => p.CurrentToken.Kind == SyntaxKind.CloseBracketToken || p.IsTerminator(),
                                                            expected));
 }