예제 #1
0
        /// <summary>
        /// Initializes a new instance of the NewArrayExpression class.
        /// </summary>
        /// <param name="proxy">Proxy object for the expression.</param>
        /// <param name="type">The array type.</param>
        /// <param name="initializer">The array initializer expression.</param>
        internal NewArrayExpression(
            CodeUnitProxy proxy, ArrayAccessExpression type, ArrayInitializerExpression initializer)
            : base(proxy, ExpressionType.NewArray)
        {
            Param.AssertNotNull(proxy, "proxy");
            Param.Ignore(type, "type");
            Param.Ignore(initializer);

            this.type.Value        = type;
            this.initializer.Value = initializer;
        }
        /// <summary>
        /// Reads an array initializer expression.
        /// </summary>
        /// <param name="parentProxy">Represents the parent item.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the expression.</returns>
        private ArrayInitializerExpression GetArrayInitializerExpression(CodeUnitProxy parentProxy, bool unsafeCode)
        {
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.Ignore(unsafeCode);

            this.AdvanceToNextCodeSymbol(parentProxy);
            var expressionProxy = new CodeUnitProxy(this.document);

            // Get the first symbol and make sure it is an opening curly bracket.
            BracketToken openingBracket = (BracketToken)this.GetToken(expressionProxy, TokenType.OpenCurlyBracket, SymbolType.OpenCurlyBracket);

            // Get each of the initializers.
            var initializers = new List<Expression>();

            while (true)
            {
                // If this initializer starts with an opening curly bracket, it is another
                // array initializer expression. Otherwise, parse it like a normal expression.
                Symbol symbol = this.PeekNextSymbol();

                if (symbol.SymbolType == SymbolType.OpenCurlyBracket)
                {
                    initializers.Add(this.GetArrayInitializerExpression(expressionProxy, unsafeCode));
                }
                else if (symbol.SymbolType == SymbolType.CloseCurlyBracket)
                {
                    break;
                }
                else
                {
                    initializers.Add(this.GetNextExpression(expressionProxy, ExpressionPrecedence.None, unsafeCode));
                }

                // Now check the type of the next symbol and see if it is a comma.
                symbol = this.PeekNextSymbol();

                if (symbol.SymbolType == SymbolType.Comma)
                {
                    // Add the comma and advance.
                    this.GetToken(expressionProxy, TokenType.Comma, SymbolType.Comma);
                }
            }

            // Add the closing curly bracket.
            BracketToken closingBracket = (BracketToken)this.GetToken(expressionProxy, TokenType.CloseCurlyBracket, SymbolType.CloseCurlyBracket);

            openingBracket.MatchingBracket = closingBracket;
            closingBracket.MatchingBracket = openingBracket;

            // Return the expression.
            var expression = new ArrayInitializerExpression(expressionProxy, initializers.AsReadOnly());
            parentProxy.Children.Add(expression);

            return expression;
        }