예제 #1
0
        /// <summary>
        /// Initializes a new instance of the QueryJoinClause class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the clause.</param>
        /// <param name="rangeVariable">The variable that ranges over the values in the query result.</param>
        /// <param name="inExpression">The expression after the 'in' keyword.</param>
        /// <param name="onKeyExpression">The expression after the 'on' keyword.</param>
        /// <param name="equalsKeyExpression">The expression after the 'equals' keyword.</param>
        /// <param name="intoVariable">The optional variable that the result is placed into.</param>
        internal QueryJoinClause(
            CsTokenList tokens, 
            Variable rangeVariable, 
            Expression inExpression, 
            Expression onKeyExpression,
            Expression equalsKeyExpression,
            Variable intoVariable)
            : base(QueryClauseType.Join, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(rangeVariable, "rangeVariable");
            Param.AssertNotNull(inExpression, "inExpression");
            Param.AssertNotNull(onKeyExpression, "onKeyExpression");
            Param.AssertNotNull(equalsKeyExpression, "equalsKeyExpression");
            Param.Ignore(intoVariable);

            this.rangeVariable = rangeVariable;
            this.inExpression = inExpression;
            this.onKeyExpression = onKeyExpression;
            this.equalsKeyExpression = equalsKeyExpression;
            this.intoVariable = intoVariable;

            this.AddExpression(this.inExpression);
            this.AddExpression(this.onKeyExpression);
            this.AddExpression(this.equalsKeyExpression);
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the QueryLetClause class.
        /// </summary>
        /// <param name="tokens">The list of tokens that form the clause.</param>
        /// <param name="rangeVariable">The variable that ranges over the values in the query result.</param>
        /// <param name="expression">The range expression.</param>
        internal QueryLetClause(CsTokenList tokens, Variable rangeVariable, Expression expression)
            : base(QueryClauseType.Let, tokens, expression)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(rangeVariable, "rangeVariable");
            Param.AssertNotNull(expression, "expression");

            this.rangeVariable = rangeVariable;
        }
예제 #3
0
 internal override void Initialize()
 {
     base.Initialize();
     if (this.parameters != null)
     {
         foreach (Parameter parameter in this.parameters)
         {
             Variable variable = new Variable(parameter.Type, parameter.Name, VariableModifiers.None, parameter.Location.StartPoint, parameter.Generated);
             base.Variables.Add(variable);
         }
     }
 }
        /// <summary>
        /// Reads the next for-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 ForStatement ParseForStatement(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            var statementReference = new Reference<ICodePart>();

            // Add the for keyword.
            CsToken firstToken = this.GetToken(CsTokenType.For, SymbolType.For, 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 each of the initializers.
            List<Expression> initializers = this.ParseForStatementInitializers(statementReference, unsafeCode);

            // Get the condition expression.
            Expression condition = this.ParseForStatementCondition(statementReference, unsafeCode);

            // Get the iterators.
            List<Expression> iterators = this.ParseForStatementIterators(statementReference, unsafeCode, openParenthesis, openParenthesisNode);

            // Get the embedded statement.
            Statement childStatement = this.GetNextStatement(statementReference, unsafeCode);
            if (childStatement == null || childStatement.Tokens.First == 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 and return the for-statement.
            ForStatement statement = new ForStatement(
                partialTokens, initializers.ToArray(), condition, iterators.ToArray());
            statement.EmbeddedStatement = childStatement;
            statementReference.Target = statement;

            // Add the variables declared in the statement.
            foreach (Expression initializer in initializers)
            {
                VariableDeclarationExpression variableDeclaration = initializer as VariableDeclarationExpression;
                if (variableDeclaration != null)
                {
                    var initializerReference = new Reference<ICodePart>(initializer);
                    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),
                            initializerReference,
                            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;
        }
        /// <summary>
        /// Reads the next foreach-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 ForeachStatement ParseForeachStatement(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            var statementReference = new Reference<ICodePart>();

            // Get the foreach keyword.
            CsToken firstToken = this.GetToken(CsTokenType.Foreach, SymbolType.Foreach, 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 variable.
            VariableDeclarationExpression variable = this.GetNextExpression(
                ExpressionPrecedence.None, statementReference, unsafeCode, true, false) as VariableDeclarationExpression;
            if (variable == null)
            {
                throw this.CreateSyntaxException();
            }

            // Get the 'in' keyword and add it.
            this.tokens.Add(this.GetToken(CsTokenType.In, SymbolType.In, statementReference));

            // Get the item being iterated over.
            Expression item = this.GetNextExpression(ExpressionPrecedence.None, statementReference, unsafeCode);
            if (item == 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 foreach-statement.
            ForeachStatement statement = new ForeachStatement(partialTokens, variable, item);
            statement.EmbeddedStatement = childStatement;
            statementReference.Target = statement;

            // Add the variable.
            foreach (VariableDeclaratorExpression declarator in variable.Declarators)
            {
                Variable localVariable = new Variable(
                    variable.Type,
                    declarator.Identifier.Token.Text,
                    VariableModifiers.None,
                    CodeLocation.Join(variable.Type.Location, declarator.Identifier.Token.Location),
                    statementReference,
                    variable.Type.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(localVariable);
                }
            }

            return statement;
        }
        /// <summary>
        /// Reads the next fixed-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 FixedStatement ParseFixedStatement(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            var statementReference = new Reference<ICodePart>();

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

            // Make sure we're sitting on the opening parenthesis now.
            Bracket openParenthesis = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis, statementReference);
            Node<CsToken> openParenthesisNode = this.tokens.InsertLast(openParenthesis);

            // Get the expression within the parenthesis. It must be a variable declaration.
            VariableDeclarationExpression expression = this.GetNextExpression(
                ExpressionPrecedence.None, statementReference, unsafeCode, true, false) as VariableDeclarationExpression;
            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 fixed-statement.
            FixedStatement statement = new FixedStatement(partialTokens, expression);
            statement.EmbeddedStatement = childStatement;
            statementReference.Target = statement;

            // Add the variable if there is one.
            foreach (VariableDeclaratorExpression declarator in expression.Declarators)
            {
                Variable variable = new Variable(
                    expression.Type,
                    declarator.Identifier.Token.Text,
                    VariableModifiers.None,
                    CodeLocation.Join(expression.Type.Location, declarator.Identifier.Token.Location),
                    statementReference,
                    expression.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;
        }
        /// <summary>
        /// Looks for a catch-statement, and if it is found, parses and returns it.
        /// </summary>
        /// <param name="tryStatement">The parent try statement.</param>
        /// <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 CatchStatement GetAttachedCatchStatement(TryStatement tryStatement, Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(tryStatement, "tryStatement");
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            CatchStatement catchStatement = null;

            // Look for a catch keyword.
            Symbol symbol = this.GetNextSymbol(parentReference);
            if (symbol.SymbolType == SymbolType.Catch)
            {
                var statementReference = new Reference<ICodePart>();

                // Move up to the catch keyword and add it.
                CsToken firstToken = this.GetToken(CsTokenType.Catch, SymbolType.Catch, statementReference);
                Node<CsToken> firstTokenNode = this.tokens.InsertLast(firstToken);

                Expression catchExpression = null;

                // Get the opening parenthesis, if there is one.
                symbol = this.GetNextSymbol(statementReference);
                if (symbol.SymbolType == SymbolType.OpenParenthesis)
                {
                    Bracket openParenthesis = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis, statementReference);
                    Node<CsToken> openParenthesisNode = this.tokens.InsertLast(openParenthesis);

                    // Get the type, if there is one.
                    symbol = this.GetNextSymbol(statementReference);
                    if (symbol.SymbolType == SymbolType.Other)
                    {
                        catchExpression = this.GetNextExpression(ExpressionPrecedence.None, statementReference, unsafeCode, true, true);
                    }

                    // 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. This must be a block statement.
                BlockStatement childStatement = this.GetNextStatement(statementReference, unsafeCode) as BlockStatement;
                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 catch statement.
                catchStatement = new CatchStatement(partialTokens, tryStatement, catchExpression, childStatement);
                ((IWriteableCodeUnit)catchStatement).SetParent(tryStatement);
                statementReference.Target = catchStatement;

                if (catchStatement.ClassType != null && catchStatement.Identifier != null)
                {
                    // Add the variable.
                    Variable variable = new Variable(
                        catchStatement.ClassType,
                        catchStatement.Identifier.Text,
                        VariableModifiers.None,
                        CodeLocation.Join(catchStatement.ClassType.Location, catchStatement.Identifier.Location),
                        statementReference,
                        catchStatement.ClassType.Generated);

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

            return catchStatement;
        }
        /// <summary>
        /// Reads the next variable declaration 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>
        /// <param name="variables">Returns the list of variables defined in the statement.</param>
        /// <returns>Returns the statement.</returns>
        private VariableDeclarationStatement ParseVariableDeclarationStatement(
            Reference<ICodePart> parentReference, bool unsafeCode, VariableCollection variables)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);
            Param.Ignore(variables);

            bool constant = false;

            // Get the first symbol and make sure it is an unknown word or a const.
            Symbol symbol = this.GetNextSymbol(parentReference);

            CsToken firstToken = null;
            Node<CsToken> firstTokenNode = null;

            var statementReference = new Reference<ICodePart>();

            if (symbol.SymbolType == SymbolType.Const)
            {
                constant = true;

                firstToken = new CsToken(symbol.Text, CsTokenType.Const, symbol.Location, statementReference, this.symbols.Generated);
                firstTokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Const, SymbolType.Const, statementReference));

                symbol = this.GetNextSymbol(statementReference);
            }

            if (symbol.SymbolType != SymbolType.Other)
            {
                throw this.CreateSyntaxException();
            }

            // Get the expression representing the type.
            LiteralExpression type = this.GetTypeTokenExpression(statementReference, unsafeCode, true);
            if (type == null || type.Tokens.First == null)
            {
                throw new SyntaxException(this.document.SourceCode, firstToken.LineNumber);
            }

            if (firstTokenNode == null)
            {
                firstTokenNode = type.Tokens.First;
            }

            // Get the rest of the declaration.
            VariableDeclarationExpression expression = this.GetVariableDeclarationExpression(type, ExpressionPrecedence.None, unsafeCode);

            // Get the closing semicolon.
            this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon, statementReference));

            // Add each of the variables defined in this statement to the variable list being returned.
            if (variables != null)
            {
                VariableModifiers modifiers = constant ? VariableModifiers.Const : VariableModifiers.None;
                foreach (VariableDeclaratorExpression declarator in expression.Declarators)
                {
                    Variable variable = new Variable(
                        expression.Type,
                        declarator.Identifier.Token.Text,
                        modifiers,
                        CodeLocation.Join(expression.Type.Location, declarator.Identifier.Token.Location),
                        statementReference,
                        expression.Tokens.First.Value.Generated || declarator.Identifier.Token.Generated);

                    // There might already be a variable in this scope with the same name. This can happen
                    // in valid situation when there are ifdef's surrounding portions of the code.
                    // Just accept the first variable and ignore others.
                    if (!variables.Contains(declarator.Identifier.Token.Text))
                    {
                        variables.Add(variable);
                    }
                }
            }

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

            var statement = new VariableDeclarationStatement(partialTokens, constant, expression);
            statementReference.Target = statement;

            return statement;
        }
예제 #9
0
 internal QueryFromClause(CsTokenList tokens, Variable rangeVariable, Expression expression)
     : base(QueryClauseType.From, tokens, expression)
 {
     this.rangeVariable = rangeVariable;
 }
예제 #10
0
        /// <summary>
        /// Checks the prefix for a variable defined within a method or property.
        /// </summary>
        /// <param name="variable">The variable to check.</param>
        /// <param name="element">The element that contains the variable.</param>
        /// <param name="validPrefixes">A list of valid prefixes that should not be considered hungarian.</param>
        private void CheckMethodVariablePrefix(
            Variable variable, CsElement element, Dictionary<string, string> validPrefixes)
        {
            Param.AssertNotNull(variable, "variable");
            Param.AssertNotNull(element, "element");
            Param.Ignore(validPrefixes);

            // Skip past any prefixes in the name.
            int index = NamingRules.MovePastPrefix(variable.Name);

            // Check whether the name starts with a lower-case letter.
            if (variable.Name.Length > index && char.IsLower(variable.Name, index))
            {
                // Check for hungarian notation.
                this.CheckHungarian(variable.Name, index, variable.Location.LineNumber, element, validPrefixes);

                // Check casing on the variable.
                if ((variable.Modifiers & VariableModifiers.Const) == VariableModifiers.Const)
                {
                    // Constants must start with an upper-case letter.
                    this.AddViolation(element, variable.Location.LineNumber, Rules.ConstFieldNamesMustBeginWithUpperCaseLetter, variable.Name);
                }
            }
            else if ((variable.Modifiers & VariableModifiers.Const) == 0)
            {
                // Method variables must start with a lower-case letter.
                this.AddViolation(element, variable.Location.LineNumber, Rules.FieldNamesMustBeginWithLowerCaseLetter, variable.Name);
            }
        }
예제 #11
0
 private void CheckMethodVariablePrefix(Variable variable, CsElement element, Dictionary<string, string> validPrefixes)
 {
     int index = MovePastPrefix(variable.Name);
     if (char.IsLower(variable.Name, index))
     {
         this.CheckHungarian(variable.Name, index, variable.Location.LineNumber, element, validPrefixes);
         if ((variable.Modifiers & VariableModifiers.Const) == VariableModifiers.Const)
         {
             base.AddViolation(element, variable.Location.LineNumber, Microsoft.StyleCop.CSharp.Rules.ConstFieldNamesMustBeginWithUpperCaseLetter, new object[] { variable.Name });
         }
     }
     else if ((variable.Modifiers & VariableModifiers.Const) == VariableModifiers.None)
     {
         base.AddViolation(element, variable.Location.LineNumber, Microsoft.StyleCop.CSharp.Rules.FieldNamesMustBeginWithLowerCaseLetter, new object[] { variable.Name });
     }
 }
예제 #12
0
 private VariableDeclarationStatement ParseVariableDeclarationStatement(bool unsafeCode, VariableCollection variables)
 {
     bool constant = false;
     Symbol nextSymbol = this.GetNextSymbol();
     CsToken token = null;
     Microsoft.StyleCop.Node<CsToken> firstItemNode = null;
     if (nextSymbol.SymbolType == SymbolType.Const)
     {
         constant = true;
         token = new CsToken(nextSymbol.Text, CsTokenType.Const, nextSymbol.Location, this.symbols.Generated);
         firstItemNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Const, SymbolType.Const));
         nextSymbol = this.GetNextSymbol();
     }
     if (nextSymbol.SymbolType != SymbolType.Other)
     {
         throw this.CreateSyntaxException();
     }
     LiteralExpression typeTokenExpression = this.GetTypeTokenExpression(unsafeCode, true);
     if ((typeTokenExpression == null) || (typeTokenExpression.Tokens.First == null))
     {
         throw new SyntaxException(this.document.SourceCode, token.LineNumber);
     }
     if (firstItemNode == null)
     {
         firstItemNode = typeTokenExpression.Tokens.First;
     }
     VariableDeclarationExpression expression = this.GetVariableDeclarationExpression(typeTokenExpression, ExpressionPrecedence.None, unsafeCode);
     this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon));
     if (variables != null)
     {
         VariableModifiers modifiers = constant ? VariableModifiers.Const : VariableModifiers.None;
         foreach (VariableDeclaratorExpression expression3 in expression.Declarators)
         {
             Variable variable = new Variable(expression.Type, expression3.Identifier.Token.Text, modifiers, expression3.Tokens.First.Value.Location.StartPoint, expression.Tokens.First.Value.Generated || expression3.Identifier.Token.Generated);
             if (!variables.Contains(expression3.Identifier.Token.Text))
             {
                 variables.Add(variable);
             }
         }
     }
     return new VariableDeclarationStatement(new CsTokenList(this.tokens, firstItemNode, this.tokens.Last), constant, expression);
 }
예제 #13
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;
 }
예제 #14
0
 private ForStatement ParseForStatement(bool unsafeCode)
 {
     CsToken item = this.GetToken(CsTokenType.For, SymbolType.For);
     Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(item);
     Bracket bracketToken = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis);
     Microsoft.StyleCop.Node<CsToken> openParenthesisNode = this.tokens.InsertLast(bracketToken);
     List<Expression> list = this.ParseForStatementInitializers(unsafeCode);
     Expression condition = this.ParseForStatementCondition(unsafeCode);
     List<Expression> list2 = this.ParseForStatementIterators(unsafeCode, bracketToken, openParenthesisNode);
     Statement nextStatement = this.GetNextStatement(unsafeCode);
     if ((nextStatement == null) || (nextStatement.Tokens.First == null))
     {
         throw new SyntaxException(this.document.SourceCode, item.LineNumber);
     }
     CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
     ForStatement statement2 = new ForStatement(tokens, list.ToArray(), condition, list2.ToArray());
     statement2.EmbeddedStatement = nextStatement;
     foreach (Expression expression2 in list)
     {
         VariableDeclarationExpression expression3 = expression2 as VariableDeclarationExpression;
         if (expression3 != null)
         {
             foreach (VariableDeclaratorExpression expression4 in expression3.Declarators)
             {
                 Variable variable = new Variable(expression3.Type, expression4.Identifier.Token.Text, VariableModifiers.None, expression4.Tokens.First.Value.Location.StartPoint, expression3.Type.Generated || expression4.Identifier.Token.Generated);
                 if (!statement2.Variables.Contains(expression4.Identifier.Token.Text))
                 {
                     statement2.Variables.Add(variable);
                 }
             }
             continue;
         }
     }
     return statement2;
 }
예제 #15
0
 private CatchStatement GetAttachedCatchStatement(TryStatement tryStatement, bool unsafeCode)
 {
     CatchStatement statement = null;
     if (this.GetNextSymbol().SymbolType == SymbolType.Catch)
     {
         CsToken item = this.GetToken(CsTokenType.Catch, SymbolType.Catch);
         Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(item);
         Expression classExpression = null;
         if (this.GetNextSymbol().SymbolType == SymbolType.OpenParenthesis)
         {
             Bracket bracketToken = this.GetBracketToken(CsTokenType.OpenParenthesis, SymbolType.OpenParenthesis);
             Microsoft.StyleCop.Node<CsToken> node2 = this.tokens.InsertLast(bracketToken);
             if (this.GetNextSymbol().SymbolType == SymbolType.Other)
             {
                 classExpression = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode, true, true);
             }
             Bracket bracket2 = this.GetBracketToken(CsTokenType.CloseParenthesis, SymbolType.CloseParenthesis);
             Microsoft.StyleCop.Node<CsToken> node3 = this.tokens.InsertLast(bracket2);
             bracketToken.MatchingBracketNode = node3;
             bracket2.MatchingBracketNode = node2;
         }
         BlockStatement nextStatement = this.GetNextStatement(unsafeCode) as BlockStatement;
         if (nextStatement == null)
         {
             throw new SyntaxException(this.document.SourceCode, item.LineNumber);
         }
         CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
         statement = new CatchStatement(tokens, tryStatement, classExpression, nextStatement);
         if ((statement.ClassType != null) && !string.IsNullOrEmpty(statement.Identifier))
         {
             Variable variable = new Variable(statement.ClassType, statement.Identifier, VariableModifiers.None, statement.ClassType.Location.StartPoint, statement.ClassType.Generated);
             if (!statement.Variables.Contains(statement.Identifier))
             {
                 statement.Variables.Add(variable);
             }
         }
     }
     return statement;
 }
 /// <summary>
 /// Gets line number for variable.
 /// </summary>
 public static int? GetVariableLineNumber(Variable variable)
 {
     return variable.Location.LineNumber + variable.Location.LineSpan - 1;
 }
예제 #17
0
        /// <summary>
        /// Initializes the contents of the accessor.
        /// </summary>
        internal override void Initialize()
        {
            base.Initialize();

            if (this.parameters != null)
            {
                var accessorReference = new Reference<ICodePart>(this);

                // Create a variable for each of the parameters.
                foreach (Parameter parameter in this.parameters)
                {
                    Variable variable = new Variable(
                        parameter.Type,
                        parameter.Name,
                        VariableModifiers.None,
                        parameter.Location,
                        accessorReference,
                        parameter.Generated);

                    this.Variables.Add(variable);
                }
            }
        }
예제 #18
0
        /// <summary>
        /// Gets the next variable.
        /// </summary>
        /// <param name="parentReference">The parent code unit.</param>
        /// <param name="unsafeCode">Indicates whether the code is within an unsafe block.</param>
        /// <param name="allowTypelessVariable">Indicates whether to allow a variable with no type defined.</param>
        /// <param name="onlyTypelessVariable">Indicates whether to only get a typeless variable.</param>
        /// <returns>Returns the variable.</returns>
        private Variable GetVariable(Reference<ICodePart> parentReference, bool unsafeCode, bool allowTypelessVariable, bool onlyTypelessVariable)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);
            Param.Ignore(allowTypelessVariable);
            Param.Ignore(onlyTypelessVariable);

            this.AdvanceToNextCodeSymbol(parentReference);

            var variableReference = new Reference<ICodePart>();

            // Get the type token representing either the type or the identifier.
            TypeToken type = this.GetTypeToken(variableReference, unsafeCode, true, false);
            if (type == null)
            {
                throw this.CreateSyntaxException();
            }

            Variable variable = null;

            if (onlyTypelessVariable)
            {
                // The token is not a type, just an identifier.
                Debug.Assert(type.ChildTokens.Count == 1, "The count is invalid");
                CsToken identifierToken = type.ChildTokens.First.Value;
                this.tokens.Add(identifierToken);
                variable = new Variable(null, type.Text, VariableModifiers.None, type.Location, parentReference, type.Generated);
                identifierToken.ParentRef = new Reference<ICodePart>(variable);
            }
            else
            {
                this.AdvanceToNextCodeSymbol(variableReference);

                // Look ahead to the next symbol to see what it is.
                Symbol symbol = this.symbols.Peek(1);
                if (symbol == null || symbol.SymbolType != SymbolType.Other)
                {
                    // This variable has no type, only an identifier.
                    if (!allowTypelessVariable)
                    {
                        throw this.CreateSyntaxException();
                    }

                    // The token is not a type, just an identifier.
                    Debug.Assert(type.ChildTokens.Count == 1, "The count is invalid");
                    this.tokens.Add(type.ChildTokens.First.Value);
                    variable = new Variable(
                        null,
                        type.Text,
                        VariableModifiers.None,
                        type.Location,
                        parentReference,
                        type.Generated);
                }
                else
                {
                    // There is a type so add the type token.
                    this.tokens.Add(type);

                    // Create and add the identifier token.
                    CsToken identifier = new CsToken(
                        symbol.Text, CsTokenType.Other, CsTokenClass.Token, symbol.Location, variableReference, this.symbols.Generated);

                    this.tokens.Add(identifier);
                    this.symbols.Advance();

                    // The variable has both a type and an identifier.
                    variable = new Variable(
                        type,
                        identifier.Text,
                        VariableModifiers.None,
                        CodeLocation.Join(type.Location, identifier.Location),
                        parentReference,
                        type.Generated || identifier.Generated);
                }
            }

            variableReference.Target = variable;
            return variable;
        }
 /// <summary>
 /// Gets line number for variable.
 /// </summary>
 public static int? GetVariableLineNumber(Variable variable)
 {
     return null;
 }