private ExpressionNode parseNewExpression() {
            ExpressionNode result = null;
            saveScannerState();
            switch (nextLexicalUnit(true)) {
            case OpenBracket: {
                int dims = parseDimensions();
                if (lexicalUnit != LexicalUnit.OpenBrace) {
                    throw error(ParseErrorId.OpenBraceExpected);
                }
                nextLexicalUnit(true);
                var arrayCreation = new ArrayCreationExpressionNode { Dimensions = dims, Initializer = parseArrayInitializer() };
                setSavedScannerState(arrayCreation);
                arrayCreation.EndPosition = arrayCreation.Initializer.EndPosition;
                result = arrayCreation;
            }
            break;

            case OpenBrace: {
                nextLexicalUnit(true);
                var objectCreation = new AnonymousObjectCreationExpressionNode();
                setSavedScannerState(objectCreation);
                result = objectCreation;
                while (lexicalUnit != LexicalUnit.CloseBrace) {
                    var memberDeclarator = new MemberInitializerNode();
                    setScannerState(memberDeclarator);
                    if (isIdentifier(lexicalUnit)) {
                        var restorePoint = this.createRestorePoint();
                        int sp = scanner.StartPosition;
                        int len = getLexicalUnitLength();
                        if (nextLexicalUnit(true) == LexicalUnit.Assign) {
                            nextLexicalUnit(true);
                            memberDeclarator.NameOffset = sp;
                            memberDeclarator.NameLength = len;
                            memberDeclarator.Value = parseExpression();
                            goto memberDeclaratorCreated;
                        }
                        this.restore(restorePoint);
                    }
                    memberDeclarator.Value = parsePrimaryExpression();
                    memberDeclarator.EndPosition = memberDeclarator.Value.EndPosition;
                    switch (memberDeclarator.Value.ExpressionKind) {
                    case SimpleName:
                    case MemberAccess:
                        break;

                    default:
                        throw error(ParseErrorId.NameOrMemberAccessExpected);
                    }
                memberDeclaratorCreated:
                    objectCreation.MemberDeclarators.add(memberDeclarator);
                    parseCommaOrCloseBrace();
                }
                result.EndPosition = scanner.EndPosition;
                nextLexicalUnit(false);
            }
            break;
            
            default: {
                var filename = savedFilename;
                int line = savedLine;
                int column = savedColumn;
                var disabledWarnings = savedDisabledWarnings;
                int startPosition = savedStartPosition;
                var type = parseType(true);
                if (type.TypeReferenceKind != TypeReferenceKind.Array) {
                    switch (lexicalUnit) {
                    case OpenBrace:
                        nextLexicalUnit(true);
                        var objectCreation = new ObjectCreationExpressionNode { Filename = filename, Line = line, Column = column,
                            DisabledWarnings = disabledWarnings, Type = type, Initializer = parseObjectOrCollectionInitializer(),
                            StartPosition = startPosition };
                        result = objectCreation;
                        result.EndPosition = objectCreation.Initializer.EndPosition;
                        break;

                    case OpenParenthesis:
                        objectCreation = new ObjectCreationExpressionNode { Filename = filename, Line = line, Column = column,
                            DisabledWarnings = disabledWarnings, Type = type, StartPosition = startPosition };
                        result = objectCreation;
                        nextLexicalUnit(true);
                        result.EndPosition = parseArguments(objectCreation.Arguments);
                        if (lexicalUnit == LexicalUnit.OpenBrace) {
                            nextLexicalUnit(true);
                            objectCreation.Initializer = parseObjectOrCollectionInitializer();
                            objectCreation.EndPosition = objectCreation.Initializer.EndPosition;
                        }
                        break;

                    case OpenBracket:
                        var arrayCreation = new ArrayCreationExpressionNode { Filename = filename, Line = line, Column = column,
                            DisabledWarnings = disabledWarnings, Type = type, StartPosition = startPosition };
                        result = arrayCreation;
                        bool noOpenBracket = false;
                        int endPosition = scanner.EndPosition;
                        while (nextLexicalUnit(true) != LexicalUnit.CloseBracket) {
                            arrayCreation.DimensionExpressions.add(parseExpression());
                            if (lexicalUnit != LexicalUnit.CloseBracket) {
                                throw error(ParseErrorId.CloseBracketExpected);
                            }
	                        endPosition = scanner.EndPosition;
                            if (nextLexicalUnit(false) != LexicalUnit.OpenBracket) {
                                noOpenBracket = true;
                                break;
                            }
                        }
                        if (!noOpenBracket && lexicalUnit == LexicalUnit.CloseBracket) {
	                        endPosition = scanner.EndPosition;
                            if (nextLexicalUnit(false) == LexicalUnit.OpenBracket) {
                                arrayCreation.Dimensions = parseDimensions() + 1;
	                        	endPosition = scanner.EndPosition;
                            } else {
                                arrayCreation.Dimensions = 1;
                            }
                        }
                        if (lexicalUnit == LexicalUnit.OpenBrace) {
                            nextLexicalUnit(true);
                            arrayCreation.Initializer = parseArrayInitializer();
                            result.EndPosition = arrayCreation.Initializer.EndPosition;
                        } else {
                        	result.EndPosition = endPosition;
                        }
                        break;
                    }
                } else if (lexicalUnit == LexicalUnit.OpenBrace) {
                    nextLexicalUnit(true);
                    var arrayCreation = new ArrayCreationExpressionNode { Type = type };
                    setSavedScannerState(arrayCreation);
                    arrayCreation.Initializer = parseArrayInitializer();
                    result = arrayCreation;
                    result.EndPosition = arrayCreation.Initializer.EndPosition;
                }
	            break;
            }
            }
            return result;
        }
Exemple #2
0
 protected virtual TResult handleArrayCreation(ArrayCreationExpressionNode arrayCreation, TSource source, bool nested)
 {
     return(defaultHandler());
 }