Ejemplo n.º 1
0
        private ConstructorDeclarationSyntax ParseConstructorDeclaration(string typeName, SyntaxListBuilder <AnnotationSyntax> attributes, SyntaxListBuilder modifiers, TypeParameterListSyntax typeParameterList)
        {
            var name = this.ParseIdentifierToken();

            Debug.Assert(name.ValueText == typeName);

            var saveTerm = this._termState;

            this._termState |= TerminatorState.IsEndOfMethodSignature;
            try
            {
                var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: true, allowAttributes: true, allowFieldModifiers: false);


                JavaThrowsListClauseSyntax throws = this.ParseJavaThrowsListClause(true);

                ConstructorInitializerSyntax initializer = null;
                if (this.CurrentToken.Kind == SyntaxKind.ColonToken)
                {
                    bool isStatic = modifiers != null && modifiers.Any(SyntaxKind.StaticKeyword);
                    initializer = this.ParseConstructorInitializer(name.ValueText, isStatic);
                }

                BlockSyntax body;
                SyntaxToken semicolon;
                this.ParseBodyOrSemicolon(out body, out semicolon);

                return(_syntaxFactory.ConstructorDeclaration(attributes, modifiers.ToTokenList(), typeParameterList, name, paramList, throws, initializer, body, semicolon));
            }
            finally
            {
                this._termState = saveTerm;
            }
        }
Ejemplo n.º 2
0
        private FieldDeclarationSyntax ParseNormalFieldDeclaration(
            SyntaxListBuilder <AnnotationSyntax> attributes,
            SyntaxListBuilder modifiers,
            TypeSyntax type,
            SyntaxKind parentKind)
        {
            var saveTerm = this._termState;

            this._termState |= TerminatorState.IsEndOfFieldDeclaration;
            var variables = this._pool.AllocateSeparated <VariableDeclaratorSyntax>();

            try
            {
                this.ParseVariableDeclarators(type, flags: 0, variables: variables, parentKind: parentKind);

                var semicolon = this.EatToken(SyntaxKind.SemicolonToken);
                return(_syntaxFactory.FieldDeclaration(
                           attributes,
                           modifiers.ToTokenList(),
                           _syntaxFactory.VariableDeclaration(type, variables),
                           semicolon));
            }
            finally
            {
                this._termState = saveTerm;
                this._pool.Free(variables);
            }
        }
Ejemplo n.º 3
0
        private MethodDeclarationSyntax ParseMethodDeclaration(
            SyntaxListBuilder <AnnotationSyntax> attributes,
            SyntaxListBuilder modifiers,
            TypeSyntax type,
            SyntaxToken identifier,
            TypeParameterListSyntax typeParameterList, bool isDtor)
        {
            // Parse the name (it could be qualified)
            var saveTerm = this._termState;

            this._termState |= TerminatorState.IsEndOfMethodSignature;

            var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: true, allowDefaults: true,
                                                                 allowAttributes: true, allowFieldModifiers: true);

            JavaThrowsListClauseSyntax throws = this.ParseJavaThrowsListClause(true);


            if (this.CurrentToken.Kind == SyntaxKind.ColonToken)
            {
                // Use else if, rather than if, because if we see both a constructor initializer and a constraint clause, we're too lost to recover.
                var colonToken = this.CurrentToken;
                // Set isStatic to false because pretending we're in a static constructor will just result in more errors.
                ConstructorInitializerSyntax initializer = this.ParseConstructorInitializer(identifier.ValueText, isStatic: false);
                initializer = this.AddErrorToFirstToken(initializer, ErrorCode.ERR_UnexpectedCharacter, colonToken.Text);
                //CONSIDER: better error code?
                paramList = AddTrailingSkippedSyntax(paramList, initializer);

                // CONSIDER: Parsing an invalid constructor initializer could, conceivably, get us way
                // off track.  If this becomes a problem, an alternative approach would be to generalize
                // EatTokenWithPrejudice in such a way that we can just skip everything until we recognize
                // our context again (perhaps an open brace).
            }


            this._termState = saveTerm;

            BlockSyntax body;
            SyntaxToken semicolon;

            this.ParseBodyOrSemicolon(out body, out semicolon);


            return
                (_syntaxFactory.MethodDeclaration(isDtor ? SyntaxKind.DestructorDeclaration : SyntaxKind.MethodDeclaration,
                                                  attributes,
                                                  modifiers.ToTokenList(),
                                                  typeParameterList,
                                                  type,
                                                  identifier,
                                                  paramList,
                                                  throws,
                                                  body,
                                                  semicolon));
        }
        public JavaMemberModifierSyntax CreateJavaMemberModifierSyntax(SyntaxListBuilder <AnnotationSyntax> attributes,
                                                                       SyntaxListBuilder modifiers)
        {
            JavaMemberModifierSyntax modifier = default(JavaMemberModifierSyntax);


            if (attributes.Count != 0 || modifiers.Count != 0)
            {
                modifier = _syntaxFactory.JavaMemberModifier(attributes, modifiers.ToTokenList());
            }

            return(modifier);
        }
Ejemplo n.º 5
0
        private ParameterSyntax ParseParameter(
            SyntaxListBuilder <AnnotationSyntax> attributes,
            SyntaxListBuilder modifiers,
            bool allowThisKeyword,
            bool allowDefaults,
            bool allowAttributes,
            bool allowFieldModifiers)
        {
            if (this.IsIncrementalAndFactoryContextMatches && CanReuseParameter(this.CurrentNode as CSharp.Syntax.ParameterSyntax, attributes, modifiers))
            {
                return((ParameterSyntax)this.EatNode());
            }

            this.ParseAnnotationDeclarations(attributes, allowAttributes);
            this.ParseParameterModifiers(modifiers, allowThisKeyword, allowFieldModifiers);



            TypeSyntax type = null;

            type = this.ParseType(true);


            var hasArgList = this.CurrentToken.Kind == SyntaxKind.DotDotDotToken;

            SyntaxToken dotdotdotTk = default(SyntaxToken);

            if (hasArgList)
            {
                dotdotdotTk = this.EatToken(SyntaxKind.DotDotDotToken);
            }


            SyntaxToken name = null;
            //if (!hasArgList)
            {
                name = this.ParseIdentifierToken();

                // When the user type "int foo[]", give them a useful error
                if (this.CurrentToken.Kind == SyntaxKind.OpenBracketToken && this.PeekToken(1).Kind == SyntaxKind.CloseBracketToken)
                {
                    var open  = this.EatToken();
                    var close = this.EatToken();
                    open = this.AddError(open, ErrorCode.ERR_BadArraySyntax);
                    name = AddTrailingSkippedSyntax(name, SyntaxList.List(open, close));
                }
            }
            //else if (this.IsPossibleName())
            //{
            //	// Current token is an identifier token, we expected a CloseParenToken.
            //	// Get the expected token error for the missing token with correct diagnostic
            //	// span and then parse the identifier token.

            //	SyntaxDiagnosticInfo diag = this.GetExpectedTokenError(SyntaxKind.CloseParenToken, SyntaxKind.IdentifierToken);
            //	name = this.ParseIdentifierToken();
            //	name = WithAdditionalDiagnostics(name, diag);
            //}
            //else
            //{
            //	// name is not optional on ParameterSyntax
            //	name = this.EatToken(SyntaxKind.ArgListKeyword);
            //}

            EqualsValueClauseSyntax def = null;

            if (this.CurrentToken.Kind == SyntaxKind.EqualsToken)
            {
                var equals = this.EatToken(SyntaxKind.EqualsToken);
                var expr   = this.ParseExpression();
                def = _syntaxFactory.EqualsValueClause(equals, expr);

                if (!allowDefaults)
                {
                    def = this.AddError(def, equals, ErrorCode.ERR_DefaultValueNotAllowed);
                }
                else
                {
                    def = CheckFeatureAvailability(def, MessageID.IDS_FeatureOptionalParameter);
                }
            }

            return(_syntaxFactory.Parameter(attributes, modifiers.ToTokenList(), type, dotdotdotTk, name, def));
        }
Ejemplo n.º 6
0
        private JavaInitializerMethodDeclarationSyntax ParseJavaInitializerMethodDeclaration(SyntaxListBuilder modifiers)
        {
            //InstanceInitializer:
            //	Block
            //StaticInitializer:
            //	static Block

            SyntaxToken staticKeyword = default(SyntaxToken);

            var modifiersTokens = modifiers.ToTokenList();


            //if (modifiersTokens.Count > 0)
            //{
            //	var token = modifiersTokens[0];
            //	if (token.Kind != SyntaxKind.StaticKeyword)
            //	{
            //		token = this.AddError(token, ErrorCode.ERR_BadModifierLocation);
            //		//token = this.ConvertToMissingWithTrailingTrivia(token, SyntaxKind.StaticKeyword);
            //	}

            //	staticKeyword = token;
            //}

            bool haveStatic = modifiersTokens.Any(SyntaxKind.StaticKeyword);

            //ÓÐstatic ÇÒ ÐÞÊηû¾ÍÒ»¸ö
            if (haveStatic && modifiersTokens.Count == 1)
            {
                staticKeyword = modifiersTokens[0];
            }
            else if (haveStatic && modifiersTokens.Count != 1)
            {
                int staticIndex = -1;

                for (int i = 0; i < modifiersTokens.Count; i++)
                {
                    if (modifiersTokens[i].Kind == SyntaxKind.StaticKeyword)
                    {
                        staticIndex = i;
                        break;
                    }
                }
                staticKeyword = modifiersTokens[staticIndex];

                for (int i = 0; i < modifiersTokens.Count; i++)
                {
                    if (i != staticIndex)
                    {
                        if (i < staticIndex)
                        {
                            staticKeyword = this.AddLeadingSkippedSyntax(staticKeyword, this.AddErrorToFirstToken(modifiersTokens[i], ErrorCode.ERR_BadModifierLocation));
                        }
                        else if (i > staticIndex)
                        {
                            staticKeyword = this.AddTrailingSkippedSyntax(staticKeyword, this.AddErrorToFirstToken(modifiersTokens[i], ErrorCode.ERR_BadModifierLocation));
                        }
                    }
                }
            }
            else if (!haveStatic && modifiersTokens.Count != 0)
            {
                staticKeyword = Syntax.InternalSyntax.SyntaxFactory.MissingToken(SyntaxKind.StaticKeyword);
                foreach (SyntaxToken t in modifiersTokens)
                {
                    staticKeyword = this.AddLeadingSkippedSyntax(staticKeyword, this.AddErrorToFirstToken(t, ErrorCode.ERR_BadModifierLocation));
                }
            }

            var saveTerm = this._termState;

            this._termState |= TerminatorState.IsEndOfMethodSignature;
            try
            {
                if (this.CurrentToken.Kind == SyntaxKind.OpenBraceToken)
                {
                    BlockSyntax body = this.ParseBlock(isMethodBody: true);

                    return(_syntaxFactory.JavaInitializerMethodDeclaration(staticKeyword, body));
                }
            }
            finally
            {
                this._termState = saveTerm;
            }
            return(null);
        }