Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the VariableDeclarationStatement class.
        /// </summary>
        /// <param name="proxy">Proxy object for the statement.</param>
        /// <param name="expression">The inner expression.</param>
        internal VariableDeclarationStatement(CodeUnitProxy proxy, VariableDeclarationExpression expression)
            : base(proxy, StatementType.VariableDeclaration)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.AssertNotNull(expression, "expression");

            this.expression.Value = expression;
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the FixedStatement class.
        /// </summary>
        /// <param name="proxy">Proxy object for the statement.</param>
        /// <param name="fixedVariable">The fixed variable.</param>
        internal FixedStatement(CodeUnitProxy proxy, VariableDeclarationExpression fixedVariable)
            : base(proxy, StatementType.Fixed)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.AssertNotNull(fixedVariable, "fixedVariable");

            this.fixedVariable.Value = fixedVariable;
        }
        /// <summary>
        /// Initializes a new instance of the VariableDeclarationStatement class.
        /// </summary>
        /// <param name="proxy">Proxy object for the statement.</param>
        /// <param name="expression">The inner expression.</param>
        internal VariableDeclarationStatement(CodeUnitProxy proxy, VariableDeclarationExpression expression)
            : base(proxy, StatementType.VariableDeclaration)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.AssertNotNull(expression, "expression");

            this.expression.Value = expression;
        }
Beispiel #4
0
        /// <summary>
        /// Initializes a new instance of the FixedStatement class.
        /// </summary>
        /// <param name="proxy">Proxy object for the statement.</param>
        /// <param name="fixedVariable">The fixed variable.</param>
        internal FixedStatement(CodeUnitProxy proxy, VariableDeclarationExpression fixedVariable)
            : base(proxy, StatementType.Fixed)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.AssertNotNull(fixedVariable, "fixedVariable");

            this.fixedVariable.Value = fixedVariable;
        }
        /// <summary>
        /// Initializes a new instance of the ForeachStatement class.
        /// </summary>
        /// <param name="proxy">Proxy object for the statement.</param>
        /// <param name="iterationVariable">The iteration variable declared in foreach statement declaration.</param>
        /// <param name="collection">The enumerable expression being iterated over.</param>
        internal ForeachStatement(CodeUnitProxy proxy, VariableDeclarationExpression iterationVariable, Expression collection)
            : base(proxy, StatementType.Foreach)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.AssertNotNull(iterationVariable, "iterationVariable");
            Param.AssertNotNull(collection, "collection");

            this.iterationVariable.Value = iterationVariable;
            this.collection.Value = collection;
        }
Beispiel #6
0
        /// <summary>
        /// Initializes a new instance of the ForeachStatement class.
        /// </summary>
        /// <param name="proxy">Proxy object for the statement.</param>
        /// <param name="iterationVariable">The iteration variable declared in foreach statement declaration.</param>
        /// <param name="collection">The enumerable expression being iterated over.</param>
        internal ForeachStatement(CodeUnitProxy proxy, VariableDeclarationExpression iterationVariable, Expression collection)
            : base(proxy, StatementType.Foreach)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.AssertNotNull(iterationVariable, "iterationVariable");
            Param.AssertNotNull(collection, "collection");

            this.iterationVariable.Value = iterationVariable;
            this.collection.Value        = collection;
        }
Beispiel #7
0
        /// <summary>
        /// Initializes a new instance of the VariableDeclarationStatement class.
        /// </summary>
        /// <param name="proxy">Proxy object for the statement.</param>
        /// <param name="expression">The inner expression.</param>
        /// <param name="constant">Indicates whether the item is constant.</param>
        internal VariableDeclarationStatement(
            CodeUnitProxy proxy, VariableDeclarationExpression expression, bool constant)
            : base(proxy, StatementType.VariableDeclaration)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.AssertNotNull(expression, "expression");
            Param.Ignore(constant);

            this.expression.Value = expression;
            this.constant.Value   = constant;
        }
        /// <summary>
        /// Initializes a new instance of the VariableDeclarationStatement class.
        /// </summary>
        /// <param name="proxy">Proxy object for the statement.</param>
        /// <param name="expression">The inner expression.</param>
        /// <param name="constant">Indicates whether the item is constant.</param>
        internal VariableDeclarationStatement(
            CodeUnitProxy proxy, VariableDeclarationExpression expression, bool constant)
            : base(proxy, StatementType.VariableDeclaration)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.AssertNotNull(expression, "expression");
            Param.Ignore(constant);

            this.expression.Value = expression;
            this.constant.Value = constant;
        }
Beispiel #9
0
        /// <summary>
        /// Initializes the contents of the statement.
        /// </summary>
        private void Initialize()
        {
            CodeUnit start = this.FindFirstChild <OpenParenthesisToken>();

            if (start == null)
            {
                throw new SyntaxException(this.Document, this.LineNumber);
            }

            Expression expression = start.FindNextSiblingExpression();

            if (expression != null)
            {
                this.catchExpression.Value = expression;

                if (expression.ExpressionType == ExpressionType.Literal)
                {
                    this.exceptionType.Value = CodeParser.ExtractTypeTokenFromLiteralExpression((LiteralExpression)expression);
                }
                else if (expression.ExpressionType == ExpressionType.VariableDeclaration)
                {
                    VariableDeclarationExpression variableDeclaration = (VariableDeclarationExpression)expression;

                    this.exceptionType.Value = variableDeclaration.Type;

                    foreach (VariableDeclaratorExpression declarator in variableDeclaration.Declarators)
                    {
                        this.identifier.Value = declarator.Identifier.Text;
                        break;
                    }
                }

                start = expression;
            }

            CloseParenthesisToken closeParen = start.FindNextSibling <CloseParenthesisToken>();

            if (closeParen == null)
            {
                throw new SyntaxException(this.Document, this.LineNumber);
            }

            this.body.Value = closeParen.FindNextSibling <BlockStatement>();
            if (this.body.Value == null)
            {
                throw new SyntaxException(this.Document, this.LineNumber);
            }

            // Look for the try-statement that this catch-statement is attached to.
            this.tryStatement.Value = null;

            for (CodeUnit c = this.FindPreviousSibling(); c != null; c = c.FindNext())
            {
                if (c.Is(StatementType.Try))
                {
                    this.tryStatement.Value = (TryStatement)c;
                }
                else if (!c.Is(StatementType.Catch) && (!c.Is(CodeUnitType.LexicalElement) || c.Is(LexicalElementType.Token)))
                {
                    break;
                }
            }

            if (this.tryStatement.Value == null)
            {
                throw new SyntaxException(this.Document, this.LineNumber);
            }
        }
        /// <summary>
        /// Reads an expression beginning with two unknown words.
        /// </summary>
        /// <param name="expressionProxy">Proxy object for the expression being created.</param>
        /// <param name="parentProxy">Represents the parent item.</param>
        /// <param name="type">The type of the variable.</param>
        /// <param name="previousPrecedence">The precedence of the previous expression.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the expression.</returns>
        private VariableDeclarationExpression GetVariableDeclarationExpression(
            CodeUnitProxy expressionProxy, CodeUnitProxy parentProxy, Expression type, ExpressionPrecedence previousPrecedence, bool unsafeCode)
        {
            Param.AssertNotNull(expressionProxy, "expressionProxy");
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.AssertNotNull(type, "type");
            Param.Ignore(previousPrecedence);
            Param.Ignore(unsafeCode);

            CsLanguageService.Debug.Assert(
                type.ExpressionType == ExpressionType.Literal || 
                type.ExpressionType == ExpressionType.MemberAccess ||
                type.ExpressionType == ExpressionType.QualifiedAlias,
                "The left side of a variable declaration must either be a literal or a member access.");

            VariableDeclarationExpression expression = null;
            if (CheckPrecedence(previousPrecedence, ExpressionPrecedence.None))
            {
                // Move past whitespace, etc, and add it to the expression proxy if we've already started the expression,
                // otherwise add it to the parent.
                this.AdvanceToNextCodeSymbol(expressionProxy.Children.Count > 0 ? expressionProxy : parentProxy);

                // Convert the type expression to a literal type token expression.
                LiteralExpression literalType = null;
                if (type.ExpressionType == ExpressionType.Literal)
                {
                    literalType = (LiteralExpression)type;
                    if (!literalType.Token.Is(TokenType.Type))
                    {
                        literalType = null;
                    }
                }

                if (literalType == null)
                {
                    literalType = this.ConvertTypeExpression(type);
                }

                // Get each declarator.
                while (true)
                {
                    var variableDeclaratorExpressionProxy = new CodeUnitProxy(this.document);

                    // Get the next word.
                    Symbol symbol = this.PeekNextSymbol();
                    if (symbol.SymbolType != SymbolType.Other)
                    {
                        throw this.CreateSyntaxException();
                    }

                    // Get the identifier.
                    LiteralExpression identifier = this.GetLiteralExpression(variableDeclaratorExpressionProxy, unsafeCode);
                    if (identifier == null || identifier.Children.Count == 0)
                    {
                        throw new SyntaxException(this.document, symbol.LineNumber);
                    }

                    // Get the initializer if it exists.
                    Expression initializer = null;

                    symbol = this.PeekNextSymbol();
                    if (symbol.SymbolType == SymbolType.Equals)
                    {
                        // Add the equals token.
                        this.GetOperatorSymbolToken(variableDeclaratorExpressionProxy, OperatorType.Equals);

                        // Check whether this is an array initializer.
                        symbol = this.PeekNextSymbol();

                        if (symbol.SymbolType == SymbolType.OpenCurlyBracket)
                        {
                            initializer = this.GetArrayInitializerExpression(variableDeclaratorExpressionProxy, unsafeCode);
                        }
                        else
                        {
                            initializer = this.GetNextExpression(variableDeclaratorExpressionProxy, ExpressionPrecedence.None, unsafeCode);
                        }
                    }

                    // Create and add the declarator.
                    var declarator = new VariableDeclaratorExpression(variableDeclaratorExpressionProxy, identifier, initializer);
                    expressionProxy.Children.Add(declarator);

                    // Now check if the next character is a comma. If so there is another declarator.
                    symbol = this.PeekNextSymbol();
                    if (symbol.SymbolType != SymbolType.Comma)
                    {
                        // There are no more declarators.
                        break;
                    }

                    // Add the comma.
                    this.GetToken(expressionProxy, TokenType.Comma, SymbolType.Comma);
                }

                // Create the expression.
                expression = new VariableDeclarationExpression(expressionProxy, literalType);
                parentProxy.Children.Add(expression);
            }

            return expression;
        }
        /// <summary>
        /// Parses and returns a field.
        /// </summary>
        /// <param name="elementProxy">Proxy for the element being created.</param>
        /// <param name="parent">The parent of the element.</param>
        /// <param name="unsafeCode">Indicates whether the code is marked as unsafe.</param>
        /// <param name="generated">Indicates whether the code is marked as generated code.</param>
        /// <param name="attributes">The attributes on the element.</param>
        /// <returns>Returns the element.</returns>
        private Field GetField(CodeUnitProxy elementProxy, Element parent, bool unsafeCode, bool generated, ICollection<Attribute> attributes)
        {
            Param.AssertNotNull(elementProxy, "elementProxy");
            Param.AssertNotNull(parent, "parent");
            Param.Ignore(unsafeCode);
            Param.Ignore(generated);
            Param.Ignore(attributes);

            // Get the modifiers and access.
            AccessModifierType accessModifier = AccessModifierType.Private;
            Dictionary<TokenType, Token> modifiers = this.GetElementModifiers(elementProxy, ref accessModifier, FieldModifiers);

            unsafeCode |= modifiers.ContainsKey(TokenType.Unsafe);

            var declarationExpressionProxy = new CodeUnitProxy(this.document);

            // Get the field type.
            var fieldTypeExpressionProxy = new CodeUnitProxy(this.document);
            TypeToken fieldType = this.GetTypeToken(fieldTypeExpressionProxy, unsafeCode, true);
            var fieldTypeExpression = new LiteralExpression(fieldTypeExpressionProxy, fieldType);
            declarationExpressionProxy.Children.Add(fieldTypeExpression);

            // Get all of the variable declarators.
            string firstFieldName = this.GetFieldDeclarators(declarationExpressionProxy, unsafeCode, fieldType);

            if (string.IsNullOrWhiteSpace(firstFieldName))
            {
                throw this.CreateSyntaxException();
            }

            var variableDeclarationStatementProxy = new CodeUnitProxy(this.document);
            var declarationExpression = new VariableDeclarationExpression(declarationExpressionProxy, fieldTypeExpression);
            variableDeclarationStatementProxy.Children.Add(declarationExpression);

            var variableDeclarationStatement = new VariableDeclarationStatement(variableDeclarationStatementProxy, declarationExpression);
            elementProxy.Children.Add(variableDeclarationStatement);

            var field = new Field(elementProxy, firstFieldName, attributes, fieldType, unsafeCode);

            // Get the trailing semicolon.
            this.GetToken(elementProxy, TokenType.Semicolon, SymbolType.Semicolon);

            return field;
        }