/// <summary> /// Reads a new non-array type allocation expression from the code. /// </summary> /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param> /// <param name="firstTokenNode">The first token in the expression.</param> /// <param name="type">The type of the array.</param> /// <param name="expressionReference">A reference to the expression being created.</param> /// <returns>Returns the expression.</returns> private NewExpression GetNewNonArrayTypeExpression( bool unsafeCode, Node<CsToken> firstTokenNode, Expression type, Reference<ICodePart> expressionReference) { Param.Ignore(unsafeCode); Param.AssertNotNull(firstTokenNode, "firstTokenNode"); Param.AssertNotNull(type, "type"); Param.AssertNotNull(expressionReference, "expressionReference"); Expression typeCreationExpression = type; // If the next symbol is an opening parenthesis, then there is an argument // list which must be attached to the type creation expression. Symbol symbol = this.GetNextSymbol(expressionReference); if (symbol.SymbolType == SymbolType.OpenParenthesis) { typeCreationExpression = this.GetMethodInvocationExpression(type, ExpressionPrecedence.None, unsafeCode); if (typeCreationExpression == null || typeCreationExpression.Tokens.First == null) { throw this.CreateSyntaxException(); } } // If the next symbol is an opening curly bracket, then there is an object // or collection initializer attached which must also be parsed. symbol = this.GetNextSymbol(expressionReference); Expression initializer = null; if (symbol.SymbolType == SymbolType.OpenCurlyBracket) { initializer = this.GetObjectOrCollectionInitializerExpression(expressionReference, unsafeCode); if (initializer == null || initializer.Tokens.First == null) { throw this.CreateSyntaxException(); } } // Create the token list for the expression. CsTokenList partialTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last); // Create and return the expression. var expression = new NewExpression(partialTokens, typeCreationExpression, initializer); expressionReference.Target = expression; return expression; }
/// <summary> /// Reads a new anonymous type allocation expression from the code. /// </summary> /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param> /// <param name="firstTokenNode">The first token in the expression.</param> /// <param name="expressionReference">A reference to the expression being created.</param> /// <returns>Returns the expression.</returns> private NewExpression GetNewAnonymousTypeExpression(bool unsafeCode, Node<CsToken> firstTokenNode, Reference<ICodePart> expressionReference) { Param.Ignore(unsafeCode); Param.AssertNotNull(firstTokenNode, "firstTokenNode"); Param.AssertNotNull(expressionReference, "expressionReference"); // Get the anonymous type initializer expression. CollectionInitializerExpression initializer = this.GetAnonymousTypeInitializerExpression(expressionReference, unsafeCode); // Create the token list for the expression. CsTokenList partialTokens = new CsTokenList(this.tokens, firstTokenNode, initializer.Tokens.Last); // Create and return the expression. var expression = new NewExpression(partialTokens, null, initializer); expressionReference.Target = expression; return expression; }