/// <summary>
        /// Reads the next if-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 IfStatement ParseIfStatement(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            var statementReference = new Reference<ICodePart>();

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

            // 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.
            Expression expression = this.GetNextExpression(ExpressionPrecedence.None, statementReference, unsafeCode);
            if (expression == null)
            {
                throw this.CreateSyntaxException();
            }

            // 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 new SyntaxException(this.document.SourceCode, firstToken.LineNumber);
            }

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

            // Create the if-statement.
            IfStatement statement = new IfStatement(partialTokens, expression);
            statement.EmbeddedStatement = childStatement;
            statementReference.Target = statement;

            // Check if there is an else or an else-if attached to this statement.
            ElseStatement attached = this.GetAttachedElseStatement(statementReference, statement, unsafeCode);
            if (attached != null)
            {
                statement.AttachedElseStatement = attached;
            }

            return statement;
        }
Example #2
0
 private IfStatement ParseIfStatement(bool unsafeCode)
 {
     CsToken item = this.GetToken(CsTokenType.If, SymbolType.If);
     Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(item);
     Bracket bracketToken = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis);
     Microsoft.StyleCop.Node<CsToken> node2 = this.tokens.InsertLast(bracketToken);
     Expression nextExpression = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode);
     if (nextExpression == null)
     {
         throw this.CreateSyntaxException();
     }
     Bracket bracket2 = this.GetBracketToken(CsTokenType.CloseParenthesis, SymbolType.CloseParenthesis);
     Microsoft.StyleCop.Node<CsToken> node3 = this.tokens.InsertLast(bracket2);
     bracketToken.MatchingBracketNode = node3;
     bracket2.MatchingBracketNode = node2;
     Statement nextStatement = this.GetNextStatement(unsafeCode);
     if (nextStatement == null)
     {
         throw new SyntaxException(this.document.SourceCode, item.LineNumber);
     }
     CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
     IfStatement statement2 = new IfStatement(tokens, nextExpression);
     statement2.EmbeddedStatement = nextStatement;
     ElseStatement attachedElseStatement = this.GetAttachedElseStatement(unsafeCode);
     if (attachedElseStatement != null)
     {
         statement2.AttachedElseStatement = attachedElseStatement;
     }
     return statement2;
 }