Beispiel #1
0
        private bool TryParseParameterDeclarations(out ImmutableList <ParameterDeclarationSyntax> parameters)
        {
            parameters = ImmutableList <ParameterDeclarationSyntax> .Empty;

            if (!ExpectToken(TokenType.OpenParen, DiagnosticCode.ExpectedParameterList))
            {
                return(false);
            }

            // Read parameters unless the parameter list is empty
            if (_lexer.PeekTokenType() != TokenType.CloseParen)
            {
                while (true)
                {
                    var paramPosition = _lexer.Position;

                    // Read the type name
                    if (!TryParseType(DiagnosticCode.ExpectedParameterDeclaration, out var paramType))
                    {
                        return(false);
                    }

                    // Read the parameter name
                    if (!ExpectIdentifier(DiagnosticCode.ExpectedParameterName, out var paramName))
                    {
                        return(false);
                    }
                    if (!NameParsing.IsValidSimpleName(paramName) || NameParsing.IsReservedTypeName(paramName))
                    {
                        _diagnosticSink.Add(DiagnosticCode.InvalidVariableName, _lexer.LastPosition, paramName);
                        return(false);
                    }

                    // Add it to the list
                    parameters = parameters.Add(new ParameterDeclarationSyntax(paramType, paramName, paramPosition));

                    // If the next token is a comma, there is another parameter to parse
                    if (_lexer.PeekTokenType() == TokenType.Comma)
                    {
                        _lexer.GetToken();
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (!ExpectToken(TokenType.CloseParen, DiagnosticCode.ExpectedClosingParen))
            {
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        private bool TryParseVariableDeclaration([NotNullWhen(true)] out VariableDeclarationSyntax?declaration)
        {
            declaration = null;
            var startPosition = _lexer.Position;

            EatAndAssertToken(TokenType.Var);

            // Following the "var" keyword there is the type name
            if (!TryParseType(DiagnosticCode.ExpectedType, out var typeSyntax))
            {
                return(false);
            }

            // Then there is the variable name, which is a simple name
            if (_lexer.PeekTokenType() != TokenType.Identifier)
            {
                _diagnosticSink.Add(DiagnosticCode.ExpectedIdentifier, _lexer.Position, ReadTokenIntoString());
                return(false);
            }

            var variableName = ReadTokenIntoString();

            if (!NameParsing.IsValidSimpleName(variableName) || NameParsing.IsReservedTypeName(variableName))
            {
                _diagnosticSink.Add(DiagnosticCode.InvalidVariableName, _lexer.LastPosition, variableName);
                return(false);
            }

            // Then there is the initial value (a "=" followed by an expression)
            if (!ExpectToken(TokenType.Equals, DiagnosticCode.ExpectedInitialValue) ||
                !TryParseExpression(out var initialValue))
            {
                return(false);
            }
            Debug.Assert(initialValue != null);

            // Finally, a semicolon
            if (!ExpectToken(TokenType.Semicolon, DiagnosticCode.ExpectedSemicolon))
            {
                return(false);
            }

            declaration = new VariableDeclarationSyntax(typeSyntax, variableName, initialValue, startPosition);
            return(true);
        }
Beispiel #3
0
        private bool TryReadAndValidateIdentifier([NotNullWhen(true)] out IdentifierSyntax?identifier,
                                                  bool allowReservedTypeNames)
        {
            if (_lexer.PeekTokenType() != TokenType.Identifier)
            {
                _diagnosticSink.Add(DiagnosticCode.ExpectedIdentifier, _lexer.Position, ReadTokenIntoString());
                identifier = null;
                return(false);
            }

            var token = ReadTokenIntoString();

            if (!NameParsing.IsValidFullName(token) ||
                (!allowReservedTypeNames && NameParsing.IsReservedTypeName(token)))
            {
                _diagnosticSink.Add(DiagnosticCode.InvalidIdentifier, _lexer.LastPosition, token);
                identifier = null;
                return(false);
            }

            identifier = new IdentifierSyntax(token, _lexer.LastPosition);
            return(true);
        }