/// <summary> /// Initializes a new instance of the SwitchStatement class. /// </summary> /// <param name="tokens">The list of tokens that form the statement.</param> /// <param name="switchItem">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( CsTokenList tokens, Expression switchItem, ICollection<SwitchCaseStatement> caseStatements, SwitchDefaultStatement defaultStatement) : base(StatementType.Switch, tokens) { Param.AssertNotNull(tokens, "tokens"); Param.AssertNotNull(switchItem, "switchItem"); Param.AssertNotNull(caseStatements, "caseStatements"); Param.Ignore(defaultStatement); this.switchItem = switchItem; this.caseStatements = caseStatements; this.defaultStatement = defaultStatement; Debug.Assert(caseStatements.IsReadOnly, "The collection of case statements should be read-only."); this.AddExpression(switchItem); foreach (Statement statement in caseStatements) { this.AddStatement(statement); } if (defaultStatement != null) { this.AddStatement(defaultStatement); } }
internal SwitchStatement(CsTokenList tokens, Expression switchItem, ICollection<SwitchCaseStatement> caseStatements, SwitchDefaultStatement defaultStatement) : base(StatementType.Switch, tokens) { this.switchItem = switchItem; this.caseStatements = caseStatements; this.defaultStatement = defaultStatement; base.AddExpression(switchItem); foreach (Statement statement in caseStatements) { base.AddStatement(statement); } if (defaultStatement != null) { base.AddStatement(defaultStatement); } }
/// <summary> /// Parses the case and default statements within a switch statement. /// </summary> /// <param name="statementReference">A reference to 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> ParseSwitchStatementCaseStatements( Reference<ICodePart> statementReference, bool unsafeCode, out SwitchDefaultStatement defaultStatement) { Param.AssertNotNull(statementReference, "statementReference"); Param.Ignore(unsafeCode); defaultStatement = null; List<SwitchCaseStatement> 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.GetNextSymbol(statementReference); if (symbol.SymbolType == SymbolType.Case) { caseStatements.Add(this.ParseSwitchCaseStatement(statementReference, unsafeCode)); } else if (symbol.SymbolType == SymbolType.Default) { if (defaultStatement != null) { throw new SyntaxException(this.document.SourceCode, symbol.LineNumber); } defaultStatement = this.ParseSwitchDefaultStatement(statementReference, unsafeCode); } else if (symbol.SymbolType == SymbolType.CloseCurlyBracket) { break; } else { // Unexpected symbol. throw new SyntaxException(this.document.SourceCode, symbol.LineNumber); } } return caseStatements; }
/// <summary> /// Reads the next default-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 SwitchDefaultStatement ParseSwitchDefaultStatement(Reference<ICodePart> parentReference, bool unsafeCode) { Param.AssertNotNull(parentReference, "parentReference"); Param.Ignore(unsafeCode); var statementReference = new Reference<ICodePart>(); // Move past the default keyword. CsToken firstToken = this.GetToken(CsTokenType.Default, SymbolType.Default, parentReference, statementReference); Node<CsToken> firstTokenNode = this.tokens.InsertLast(firstToken); // Get the colon. this.tokens.Add(this.GetToken(CsTokenType.LabelColon, SymbolType.Colon, statementReference)); // Create the statement. SwitchDefaultStatement defaultStatement = new SwitchDefaultStatement(); // Get each of the sub-statements beneath this statement. while (true) { // Check the type of the next symbol. Symbol symbol = this.GetNextSymbol(statementReference); // 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(statementReference, unsafeCode, defaultStatement.Variables); if (statement == null) { throw this.CreateSyntaxException(); } // Add it to the default statement. defaultStatement.AddStatement(statement); } // Create the token list for the default statement. defaultStatement.Tokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last); statementReference.Target = defaultStatement; return defaultStatement; }
private List<SwitchCaseStatement> ParseSwitchStatementCaseStatements(bool unsafeCode, out SwitchDefaultStatement defaultStatement) { Symbol symbol; defaultStatement = null; List<SwitchCaseStatement> list = new List<SwitchCaseStatement>(); Label_0009: symbol = this.GetNextSymbol(); if (symbol.SymbolType == SymbolType.Case) { list.Add(this.ParseSwitchCaseStatement(unsafeCode)); goto Label_0009; } if (symbol.SymbolType == SymbolType.Default) { if (defaultStatement != null) { throw new SyntaxException(this.document.SourceCode, symbol.LineNumber); } defaultStatement = this.ParseSwitchDefaultStatement(unsafeCode); goto Label_0009; } if (symbol.SymbolType != SymbolType.CloseCurlyBracket) { throw new SyntaxException(this.document.SourceCode, symbol.LineNumber); } return list; }
private SwitchDefaultStatement ParseSwitchDefaultStatement(bool unsafeCode) { CsToken item = this.GetToken(CsTokenType.Default, SymbolType.Default); Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(item); this.tokens.Add(this.GetToken(CsTokenType.LabelColon, SymbolType.Colon)); SwitchDefaultStatement statement = new SwitchDefaultStatement(); while (true) { Symbol 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; }