/// <summary>
        /// Initializes a new instance of the QueryContinuationClause class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the clause.
        /// </param>
        /// <param name="variable">
        /// The continuation clause variable.
        /// </param>
        /// <param name="clauses">
        /// The collection of clauses in the expression.
        /// </param>
        internal QueryContinuationClause(CsTokenList tokens, Variable variable, ICollection<QueryClause> clauses)
            : base(QueryClauseType.Continuation, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(clauses, "clauses");
            Param.AssertNotNull(variable, "variable");

            this.variable = variable;
            this.clauses = new CodeUnitCollection<QueryClause>(this);
            this.clauses.AddRange(clauses);

            Debug.Assert(clauses.IsReadOnly, "The collection of query clauses should be read-only.");
        }
Esempio n. 2
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);

            Reference<ICodePart> 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
            {
                int index = this.GetNextCodeSymbolIndex(1);
                if (index != -1)
                {
                    // Look ahead to the next symbol to see what it is.
                    Symbol symbol = this.symbols.Peek(index);

                    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);
                        this.AdvanceToNextCodeSymbol(variableReference);

                        // 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>
        /// 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 && char.IsUpper(variable.Name, index))
            {
                // We check for IsUpper again to support languages that dont have upper or lower case like Chinese.
                // Method variables must start with a lower-case letter.
                this.AddViolation(element, variable.Location.LineNumber, Rules.FieldNamesMustBeginWithLowerCaseLetter, variable.Name);
            }
        }
Esempio n. 4
0
        /// <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);

            Reference<ICodePart> 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)
                {
                    Reference<ICodePart> 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;
        }
Esempio n. 5
0
        /// <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);

            Reference<ICodePart> 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;
        }
Esempio n. 6
0
        /// <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)
            {
                Reference<ICodePart> 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;
        }
Esempio n. 7
0
        /// <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;

            Reference<ICodePart> 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);

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

            return statement;
        }
Esempio n. 8
0
        /// <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);

            Reference<ICodePart> 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>
        /// Initializes the method.
        /// </summary>
        internal override void Initialize()
        {
            base.Initialize();

            if (this.parameters != null)
            {
                Reference<ICodePart> methodReference = 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, methodReference, parameter.Generated);

                    this.Variables.Add(variable);
                }
            }
        }