/// <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); } }
/// <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); } }
/// <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); Reference<ICodePart> 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; }
/// <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> /// The save. /// </summary> /// <param name="switchDefaultStatement"> /// The switch default statement. /// </param> private void Save(SwitchDefaultStatement switchDefaultStatement) { this.cppWriter.WriteLine("default: "); this.Save(switchDefaultStatement, SaveICodeUnit.IfNotEmpty | SaveICodeUnit.NoBrackets); }