/// <summary> /// Looks for an else-statement, and if it is found, parses and returns it. /// </summary> /// <param name="parentReference">The parent code unit.</param> /// <param name="parentStatement">The parent of the else-statement.</param> /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param> /// <returns>Returns the statement.</returns> private ElseStatement GetAttachedElseStatement(Reference<ICodePart> parentReference, Statement parentStatement, bool unsafeCode) { Param.AssertNotNull(parentReference, "parentReference"); Param.AssertNotNull(parentStatement, "parentStatement"); Param.Ignore(unsafeCode); ElseStatement statement = null; // Check if the next keyword is an else. Symbol symbol = this.GetNextSymbol(parentReference); if (symbol.SymbolType == SymbolType.Else) { var statementReference = new Reference<ICodePart>(); // Advance to this keyword and add it. Node<CsToken> firstTokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Else, SymbolType.Else, statementReference)); // Check if the next keyword is an if. Expression conditional = null; symbol = this.GetNextSymbol(statementReference); if (symbol != null && symbol.SymbolType == SymbolType.If) { // Advance to this keyword and add it. this.tokens.Add(this.GetToken(CsTokenType.If, SymbolType.If, statementReference)); // Get the opening parenthesis. Bracket openParenthesis = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis, statementReference); Node<CsToken> openParenthesisNode = this.tokens.InsertLast(openParenthesis); // Get the expression within the parenthesis. conditional = this.GetNextExpression(ExpressionPrecedence.None, statementReference, unsafeCode); if (conditional == null) { throw new SyntaxException(this.document.SourceCode, symbol.LineNumber); } // Get the closing parenthesis. Bracket closeParenthesis = this.GetBracketToken(CsTokenType.CloseParenthesis, SymbolType.CloseParenthesis, statementReference); Node<CsToken> closeParenthesisNode = this.tokens.InsertLast(closeParenthesis); openParenthesis.MatchingBracketNode = closeParenthesisNode; closeParenthesis.MatchingBracketNode = openParenthesisNode; } // Get the embedded statement. Statement childStatement = this.GetNextStatement(statementReference, unsafeCode); if (childStatement == null) { throw this.CreateSyntaxException(); } // Create the token list for the statement. CsTokenList partialTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last); // Create the else-statement. statement = new ElseStatement(partialTokens, conditional); statement.EmbeddedStatement = childStatement; ((IWriteableCodeUnit)statement).SetParent(parentStatement); // Check if there is another else or an else-if attached to this statement. ElseStatement attached = this.GetAttachedElseStatement(statementReference, statement, unsafeCode); if (attached != null) { statement.AttachedElseStatement = attached; } statementReference.Target = statement; } return statement; }
private ElseStatement GetAttachedElseStatement(bool unsafeCode) { ElseStatement statement = null; if (this.GetNextSymbol().SymbolType == SymbolType.Else) { Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Else, SymbolType.Else)); Expression conditionExpression = null; Symbol nextSymbol = this.GetNextSymbol(); if ((nextSymbol != null) && (nextSymbol.SymbolType == SymbolType.If)) { this.tokens.Add(this.GetToken(CsTokenType.If, SymbolType.If)); Bracket bracketToken = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis); Microsoft.StyleCop.Node<CsToken> node2 = this.tokens.InsertLast(bracketToken); conditionExpression = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode); if (conditionExpression == null) { throw new SyntaxException(this.document.SourceCode, nextSymbol.LineNumber); } Bracket item = this.GetBracketToken(CsTokenType.CloseParenthesis, SymbolType.CloseParenthesis); Microsoft.StyleCop.Node<CsToken> node3 = this.tokens.InsertLast(item); bracketToken.MatchingBracketNode = node3; item.MatchingBracketNode = node2; } Statement nextStatement = this.GetNextStatement(unsafeCode); if (nextStatement == null) { throw this.CreateSyntaxException(); } CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last); statement = new ElseStatement(tokens, conditionExpression); statement.EmbeddedStatement = nextStatement; ElseStatement attachedElseStatement = this.GetAttachedElseStatement(unsafeCode); if (attachedElseStatement != null) { statement.AttachedElseStatement = attachedElseStatement; } } return statement; }