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

            var statementReference = new Reference<ICodePart>();

            // Move past the using keyword.
            CsToken firstToken = this.GetToken(CsTokenType.Using, SymbolType.Using, 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, true, false);
            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 this.CreateSyntaxException();
            }

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

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

            // Add the variable if there is one.
            VariableDeclarationExpression variableDeclaration = expression as VariableDeclarationExpression;
            if (variableDeclaration != null)
            {
                foreach (VariableDeclaratorExpression declarator in variableDeclaration.Declarators)
                {
                    Variable variable = new Variable(
                        variableDeclaration.Type,
                        declarator.Identifier.Token.Text,
                        VariableModifiers.None,
                        CodeLocation.Join(variableDeclaration.Type.Location, declarator.Identifier.Token.Location),
                        statementReference,
                        variableDeclaration.Type.Generated || declarator.Identifier.Token.Generated);

                    // If there is already a variable in this scope with the same name, ignore this one.
                    if (!statement.Variables.Contains(declarator.Identifier.Token.Text))
                    {
                        statement.Variables.Add(variable);
                    }
                }
            }

            return statement;
        }
示例#2
0
 private UsingStatement ParseUsingStatement(bool unsafeCode)
 {
     CsToken item = this.GetToken(CsTokenType.Using, SymbolType.Using);
     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 resource = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode, true, false);
     if (resource == 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 this.CreateSyntaxException();
     }
     CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
     UsingStatement statement2 = new UsingStatement(tokens, resource);
     statement2.EmbeddedStatement = nextStatement;
     VariableDeclarationExpression expression2 = resource as VariableDeclarationExpression;
     if (expression2 != null)
     {
         foreach (VariableDeclaratorExpression expression3 in expression2.Declarators)
         {
             Variable variable = new Variable(expression2.Type, expression3.Identifier.Token.Text, VariableModifiers.None, expression3.Tokens.First.Value.Location.StartPoint, expression2.Type.Generated || expression3.Identifier.Token.Generated);
             if (!statement2.Variables.Contains(expression3.Identifier.Token.Text))
             {
                 statement2.Variables.Add(variable);
             }
         }
     }
     return statement2;
 }