internal NewArrayExpression(CsTokenList tokens, ArrayAccessExpression type, ArrayInitializerExpression initializer) : base(ExpressionType.NewArray, tokens) { this.type = type; this.initializer = initializer; if (type != null) { base.AddExpression(type); } if (initializer != null) { base.AddExpression(initializer); } }
/// <summary> /// Initializes a new instance of the NewArrayExpression class. /// </summary> /// <param name="tokens">The list of tokens that form the expression.</param> /// <param name="type">The array type.</param> /// <param name="initializer">The array initializer expression.</param> internal NewArrayExpression( CsTokenList tokens, ArrayAccessExpression type, ArrayInitializerExpression initializer) : base(ExpressionType.NewArray, tokens) { Param.AssertNotNull(tokens, "tokens"); Param.Ignore(type, "type"); Param.Ignore(initializer); this.type = type; this.initializer = initializer; if (type != null) { this.AddExpression(type); } if (initializer != null) { this.AddExpression(initializer); } }
/// <summary> /// Reads an array initializer expression. /// </summary> /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param> /// <returns>Returns the expression.</returns> private ArrayInitializerExpression GetArrayInitializerExpression(bool unsafeCode) { Param.Ignore(unsafeCode); var expressionReference = new Reference<ICodePart>(); // Get the first symbol and make sure it is an opening curly bracket. Bracket openingBracket = this.GetBracketToken(CsTokenType.OpenCurlyBracket, SymbolType.OpenCurlyBracket, expressionReference); Node<CsToken> openingBracketNode = this.tokens.InsertLast(openingBracket); // Get each of the initializers. List<Expression> 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.GetNextSymbol(expressionReference); if (symbol.SymbolType == SymbolType.OpenCurlyBracket) { initializers.Add(this.GetArrayInitializerExpression(unsafeCode)); } else if (symbol.SymbolType == SymbolType.CloseCurlyBracket) { break; } else { initializers.Add(this.GetNextExpression(ExpressionPrecedence.None, expressionReference, unsafeCode)); } // Now check the type of the next symbol and see if it is a comma. symbol = this.GetNextSymbol(expressionReference); if (symbol.SymbolType == SymbolType.Comma) { // Add the comma and advance. this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma, expressionReference)); } } // Add the closing curly bracket. Bracket closingBracket = this.GetBracketToken(CsTokenType.CloseCurlyBracket, SymbolType.CloseCurlyBracket, expressionReference); Node<CsToken> closingBracketNode = this.tokens.InsertLast(closingBracket); openingBracket.MatchingBracketNode = closingBracketNode; closingBracket.MatchingBracketNode = openingBracketNode; // Create the token list for the expression. CsTokenList partialTokens = new CsTokenList(this.tokens, openingBracketNode, this.tokens.Last); // Return the expression. var expression = new ArrayInitializerExpression(partialTokens, initializers.ToArray()); expressionReference.Target = expression; return expression; }