/// <summary>
        /// Reads the next case-statement from the file and returns it.
        /// </summary>
        /// <param name="parentReference">The parent code unit.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the statement.</returns>
        private SwitchCaseStatement ParseSwitchCaseStatement(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            var statementReference = new Reference<ICodePart>();

            // Move past the case keyword.
            CsToken firstToken = this.GetToken(CsTokenType.Case, SymbolType.Case, parentReference, statementReference);
            Node<CsToken> firstTokenNode = this.tokens.InsertLast(firstToken);

            // Get the name.
            Symbol symbol = this.GetNextSymbol(statementReference);
            if (symbol.SymbolType != SymbolType.Other &&
                symbol.SymbolType != SymbolType.String &&
                symbol.SymbolType != SymbolType.Number &&
                symbol.SymbolType != SymbolType.Null &&
                symbol.SymbolType != SymbolType.OpenParenthesis &&
                symbol.SymbolType != SymbolType.Minus &&
                symbol.SymbolType != SymbolType.Plus &&
                symbol.SymbolType != SymbolType.True &&
                symbol.SymbolType != SymbolType.False &&
                symbol.SymbolType != SymbolType.Sizeof &&
                symbol.SymbolType != SymbolType.Typeof &&
                symbol.SymbolType != SymbolType.Checked &&
                symbol.SymbolType != SymbolType.Unchecked)
            {
                throw this.CreateSyntaxException();
            }

            Expression identifier = this.GetNextExpression(ExpressionPrecedence.None, statementReference, unsafeCode);

            // Get the colon.
            this.tokens.Add(this.GetToken(CsTokenType.LabelColon, SymbolType.Colon, statementReference));

            // Create the statement.
            SwitchCaseStatement caseStatement = new SwitchCaseStatement(identifier);

            // Get each of the sub-statements beneath this statement.
            while (true)
            {
                // Check the type of the next symbol.
                symbol = this.GetNextSymbol(statementReference);

                // Check if we've reached the end of the case statement.
                if (symbol.SymbolType == SymbolType.CloseCurlyBracket ||
                    symbol.SymbolType == SymbolType.Case ||
                    symbol.SymbolType == SymbolType.Default)
                {
                    break;
                }

                // Read the next child statement.
                Statement statement = this.GetNextStatement(statementReference, unsafeCode, caseStatement.Variables);
                if (statement == null)
                {
                    throw this.CreateSyntaxException();
                }

                // Add it to the case statement.
                caseStatement.AddStatement(statement);
            }

            // Create the token list for the case statement.
            caseStatement.Tokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last);

            statementReference.Target = caseStatement;
            return caseStatement;
        }
Example #2
0
 private SwitchCaseStatement ParseSwitchCaseStatement(bool unsafeCode)
 {
     CsToken item = this.GetToken(CsTokenType.Case, SymbolType.Case);
     Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(item);
     Symbol nextSymbol = this.GetNextSymbol();
     if (((((nextSymbol.SymbolType != SymbolType.Other) && (nextSymbol.SymbolType != SymbolType.String)) && ((nextSymbol.SymbolType != SymbolType.Number) && (nextSymbol.SymbolType != SymbolType.Null))) && (((nextSymbol.SymbolType != SymbolType.OpenParenthesis) && (nextSymbol.SymbolType != SymbolType.Minus)) && ((nextSymbol.SymbolType != SymbolType.Plus) && (nextSymbol.SymbolType != SymbolType.True)))) && (nextSymbol.SymbolType != SymbolType.False))
     {
         throw this.CreateSyntaxException();
     }
     Expression nextExpression = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode);
     this.tokens.Add(this.GetToken(CsTokenType.LabelColon, SymbolType.Colon));
     SwitchCaseStatement statement = new SwitchCaseStatement(nextExpression);
     while (true)
     {
         nextSymbol = this.GetNextSymbol();
         if (((nextSymbol.SymbolType == SymbolType.CloseCurlyBracket) || (nextSymbol.SymbolType == SymbolType.Case)) || (nextSymbol.SymbolType == SymbolType.Default))
         {
             break;
         }
         Statement nextStatement = this.GetNextStatement(unsafeCode, statement.Variables);
         if (nextStatement == null)
         {
             throw this.CreateSyntaxException();
         }
         statement.AddStatement(nextStatement);
     }
     statement.Tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
     return statement;
 }