private DirectiveTriviaSyntax ParseElseDirective(SyntaxToken hash, SyntaxToken keyword, bool isActive, bool endIsActive)
        {
            var eod = ParseEndOfDirective(false);

            if (_directiveStack.HasPreviousIfOrElif())
            {
                var branchTaken = endIsActive && !_directiveStack.PreviousBranchTaken();
                return(new ElseDirectiveTriviaSyntax(
                           hash,
                           keyword,
                           eod,
                           endIsActive, branchTaken));
            }

            if (_directiveStack.HasUnfinishedIf())
            {
                return(WithDiagnostic(new BadDirectiveTriviaSyntax(hash, keyword, eod, isActive), DiagnosticId.EndIfDirectiveExpected, $"Expected #endif directive"));
            }

            return(WithDiagnostic(new BadDirectiveTriviaSyntax(hash, keyword, eod, isActive), DiagnosticId.UnexpectedDirective, $"Unexpected directive: '{keyword}'"));
        }
Example #2
0
        private void ReadToken()
        {
            switch (_charReader.Current)
            {
            case '\0':
                if (_includeStack.Count == 1 && _directives.HasUnfinishedIf())
                {
                    _diagnostics.Add(Diagnostic.Create(HlslMessageProvider.Instance, CurrentSpanStart, (int)DiagnosticId.EndIfDirectiveExpected));
                }
                _kind = SyntaxKind.EndOfFileToken;
                break;

            case '~':
                _kind = SyntaxKind.TildeToken;
                NextChar();
                break;

            case '&':
                NextChar();
                if (_charReader.Current == '&')
                {
                    _kind = SyntaxKind.AmpersandAmpersandToken;
                    NextChar();
                }
                else if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.AmpersandEqualsToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.AmpersandToken;
                }
                break;

            case '|':
                NextChar();
                if (_charReader.Current == '|')
                {
                    _kind = SyntaxKind.BarBarToken;
                    NextChar();
                }
                else if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.BarEqualsToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.BarToken;
                }
                break;

            case '^':
                NextChar();
                if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.CaretEqualsToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.CaretToken;
                }
                break;

            case '?':
                _kind = SyntaxKind.QuestionToken;
                NextChar();
                break;

            case '(':
                _kind = SyntaxKind.OpenParenToken;
                NextChar();
                break;

            case ')':
                _kind = SyntaxKind.CloseParenToken;
                NextChar();
                break;

            case '[':
                _kind = SyntaxKind.OpenBracketToken;
                NextChar();
                break;

            case ']':
                _kind = SyntaxKind.CloseBracketToken;
                NextChar();
                break;

            case '{':
                _kind = SyntaxKind.OpenBraceToken;
                NextChar();
                break;

            case '}':
                _kind = SyntaxKind.CloseBraceToken;
                NextChar();
                break;

            case '.':
                if (Char.IsDigit(_charReader.Peek()))
                {
                    ReadNumber();
                }
                else
                {
                    _kind = SyntaxKind.DotToken;
                    NextChar();
                }
                break;

            case '+':
                NextChar();
                if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.PlusEqualsToken;
                    NextChar();
                }
                else if (_charReader.Current == '+')
                {
                    _kind = SyntaxKind.PlusPlusToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.PlusToken;
                }
                break;

            case '-':
                NextChar();
                if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.MinusEqualsToken;
                    NextChar();
                }
                else if (_charReader.Current == '-')
                {
                    _kind = SyntaxKind.MinusMinusToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.MinusToken;
                }
                break;

            case '*':
                NextChar();
                if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.AsteriskEqualsToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.AsteriskToken;
                }
                break;

            case '/':
                NextChar();
                if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.SlashEqualsToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.SlashToken;
                }
                break;

            case '%':
                NextChar();
                if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.PercentEqualsToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.PercentToken;
                }
                break;

            case ',':
                _kind = SyntaxKind.CommaToken;
                NextChar();
                break;

            case ';':
                _kind = SyntaxKind.SemiToken;
                NextChar();
                break;

            case ':':
                NextChar();
                if (_charReader.Current == ':')
                {
                    _kind = SyntaxKind.ColonColonToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.ColonToken;
                }
                break;

            case '=':
                NextChar();
                if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.EqualsEqualsToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.EqualsToken;
                }
                break;

            case '!':
                NextChar();
                if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.ExclamationEqualsToken;
                    NextChar();
                }
                else
                {
                    _kind = SyntaxKind.NotToken;
                }
                break;

            case '<':
                NextChar();
                if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.LessThanEqualsToken;
                    NextChar();
                }
                else if (_charReader.Current == '<')
                {
                    NextChar();
                    if (_charReader.Current == '=')
                    {
                        _kind = SyntaxKind.LessThanLessThanEqualsToken;
                        NextChar();
                    }
                    else
                    {
                        _kind = SyntaxKind.LessThanLessThanToken;
                    }
                }
                else
                {
                    _kind = SyntaxKind.LessThanToken;
                }
                break;

            case '>':
                NextChar();
                if (_charReader.Current == '=')
                {
                    _kind = SyntaxKind.GreaterThanEqualsToken;
                    NextChar();
                }
                else if (_charReader.Current == '>')
                {
                    NextChar();
                    if (_charReader.Current == '=')
                    {
                        _kind = SyntaxKind.GreaterThanGreaterThanEqualsToken;
                        NextChar();
                    }
                    else
                    {
                        _kind = SyntaxKind.GreaterThanGreaterThanToken;
                    }
                }
                else
                {
                    _kind = SyntaxKind.GreaterThanToken;
                }
                break;

            case '\'':
                ReadCharacterLiteral();
                break;

            case '"':
                ReadString();
                break;

            default:
                if (Char.IsLetter(_charReader.Current) || _charReader.Current == '_')
                {
                    ReadIdentifierOrKeyword();
                }
                else if (Char.IsDigit(_charReader.Current))
                {
                    ReadNumber();
                }
                else
                {
                    ReadInvalidCharacter();
                }

                break;
            }
        }