Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the SwitchStatement class.
        /// </summary>
        /// <param name="proxy">Proxy object for the statement.</param>
        /// <param name="switchExpression">The expression to switch off of.</param>
        /// <param name="caseStatements">The list of case statements under the switch statement.</param>
        /// <param name="defaultStatement">The default statement under the switch statement.</param>
        internal SwitchStatement(
            CodeUnitProxy proxy,
            Expression switchExpression,
            ICollection <SwitchCaseStatement> caseStatements,
            SwitchDefaultStatement defaultStatement)
            : base(proxy, StatementType.Switch)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.AssertNotNull(switchExpression, "switchExpression");
            Param.AssertNotNull(caseStatements, "caseStatements");
            Param.Ignore(defaultStatement);

            this.switchExpression.Value = switchExpression;
            this.caseStatements.Value   = caseStatements;
            this.defaultStatement.Value = defaultStatement;

            CsLanguageService.Debug.Assert(caseStatements.IsReadOnly, "The collection of case statements should be read-only.");
        }
        /// <summary>
        /// Initializes a new instance of the SwitchStatement class.
        /// </summary>
        /// <param name="proxy">Proxy object for the statement.</param>
        /// <param name="switchExpression">The expression to switch off of.</param>
        /// <param name="caseStatements">The list of case statements under the switch statement.</param>
        /// <param name="defaultStatement">The default statement under the switch statement.</param>
        internal SwitchStatement(
            CodeUnitProxy proxy, 
            Expression switchExpression, 
            ICollection<SwitchCaseStatement> caseStatements, 
            SwitchDefaultStatement defaultStatement)
            : base(proxy, StatementType.Switch)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.AssertNotNull(switchExpression, "switchExpression");
            Param.AssertNotNull(caseStatements, "caseStatements");
            Param.Ignore(defaultStatement);

            this.switchExpression.Value = switchExpression;
            this.caseStatements.Value = caseStatements;
            this.defaultStatement.Value = defaultStatement;

            CsLanguageService.Debug.Assert(caseStatements.IsReadOnly, "The collection of case statements should be read-only.");
        }
        /// <summary>
        /// Reads the next default-statement from the file and returns it.
        /// </summary>
        /// <param name="parentProxy">Represents the parent item.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the statement.</returns>
        private SwitchDefaultStatement GetSwitchDefaultStatement(CodeUnitProxy parentProxy, bool unsafeCode)
        {
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.Ignore(unsafeCode);

            var statementProxy = new CodeUnitProxy(this.document);

            // Move past the default keyword.
            this.GetToken(statementProxy, TokenType.Default, SymbolType.Default);

            // Get the colon.
            this.GetToken(statementProxy, TokenType.LabelColon, SymbolType.Colon);

            // Create the statement.
            var defaultStatement = new SwitchDefaultStatement(statementProxy);
            parentProxy.Children.Add(defaultStatement);

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

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

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

            return defaultStatement;
        }
        /// <summary>
        /// Parses the case and default statements within a switch statement.
        /// </summary>
        /// <param name="switchStatementProxy">Proxy object for the statement being created.</param>
        /// <param name="unsafeCode">Indicates whether the statement lies within a block of unsafe code.</param>
        /// <param name="defaultStatement">Returns the default statement.</param>
        /// <returns>Returns the list of case statements.</returns>
        private List<SwitchCaseStatement> GetSwitchStatementCaseStatements(
            CodeUnitProxy switchStatementProxy, bool unsafeCode, out SwitchDefaultStatement defaultStatement)
        {
            Param.AssertNotNull(switchStatementProxy, "switchStatementProxy");
            Param.Ignore(unsafeCode);

            defaultStatement = null;
            var caseStatements = new List<SwitchCaseStatement>();

            // Find each of the case and default blocks.
            while (true)
            {
                // Get the next symbol and check the type.
                Symbol symbol = this.PeekNextSymbol();

                if (symbol.SymbolType == SymbolType.Case)
                {
                    caseStatements.Add(this.GetSwitchCaseStatement(switchStatementProxy, unsafeCode));
                }
                else if (symbol.SymbolType == SymbolType.Default)
                {
                    if (defaultStatement != null)
                    {
                        throw new SyntaxException(this.document, symbol.LineNumber);
                    }

                    defaultStatement = this.GetSwitchDefaultStatement(switchStatementProxy, unsafeCode);
                }
                else if (symbol.SymbolType == SymbolType.CloseCurlyBracket)
                {
                    break;
                }
                else
                {
                    // Unexpected symbol.
                    throw new SyntaxException(this.document, symbol.LineNumber);
                }
            }

            return caseStatements;
        }