Ejemplo n.º 1
0
        private InitializerExpressionNode parseObjectOrCollectionInitializer() {
            if (isIdentifier(lexicalUnit)) {
                var restorePoint = this.createRestorePoint();
                int sp = scanner.StartPosition;
                int len = getLexicalUnitLength();
                saveScannerState();
                if (nextLexicalUnit(true) == LexicalUnit.Assign) {
                    var objectInitializer = new ObjectInitializerExpressionNode();
                    setSavedScannerState(objectInitializer);
                    var memberInitializer = new MemberInitializerNode { NameOffset = sp, NameLength = len };
                    setSavedScannerState(memberInitializer);
                    if (nextLexicalUnit(true) == LexicalUnit.OpenBrace) {
                        nextLexicalUnit(true);
                        memberInitializer.Value = parseObjectOrCollectionInitializer();
                    } else {
                        memberInitializer.Value = parseExpression();
                    }
                    memberInitializer.EndPosition = memberInitializer.Value.EndPosition;
                    objectInitializer.MemberInitializers.add(memberInitializer);
                    while (lexicalUnit == LexicalUnit.Comma) {
                        if (!isIdentifier(nextLexicalUnit(true))) {
                            break;
                        }
                        sp = scanner.StartPosition;
                        len = getLexicalUnitLength();
		                saveScannerState();
                        if (nextLexicalUnit(true) != LexicalUnit.Assign) {
                            throw error(ParseErrorId.AssignExpected);
                        }
                        memberInitializer = new MemberInitializerNode { NameOffset = sp, NameLength = len };
	                    setSavedScannerState(memberInitializer);
                        if (nextLexicalUnit(true) == LexicalUnit.OpenBrace) {
                            nextLexicalUnit(true);
                            memberInitializer.Value = parseObjectOrCollectionInitializer();
                        } else {
                            memberInitializer.Value = parseExpression();
                        }
	                    memberInitializer.EndPosition = memberInitializer.Value.EndPosition;
                        objectInitializer.MemberInitializers.add(memberInitializer);
                    }
                    if (lexicalUnit != LexicalUnit.CloseBrace) {
                        throw error(ParseErrorId.CloseBraceExpected);
                    }
                   	objectInitializer.EndPosition = scanner.EndPosition;
                    nextLexicalUnit(false);
                    return objectInitializer;
                }
                this.restore(restorePoint);
            }
            var collectionInitializer = new CollectionInitializerExpressionNode();
            setScannerState(collectionInitializer);
            while (lexicalUnit != LexicalUnit.CloseBrace) {
                if (lexicalUnit == LexicalUnit.OpenBrace) {
                    nextLexicalUnit(true);
                    var values = new ArrayList<ExpressionNode>();
                    while (lexicalUnit != LexicalUnit.CloseBrace) {
                        values.add(parseExpression());
                        parseCommaOrCloseBrace();
                    }
                    nextLexicalUnit(false);
                    collectionInitializer.Values.add(values);
                } else {
                    var value = new ArrayList<ExpressionNode>();
                    value.add(parseExpression());
                    collectionInitializer.Values.add(value);
                }
                parseCommaOrCloseBrace();
            }
            collectionInitializer.EndPosition = scanner.EndPosition;
            nextLexicalUnit(false);
            return collectionInitializer;
        }
Ejemplo n.º 2
0
        private AnnotationSectionNode parseAnnotationSection() {
            var result = new AnnotationSectionNode();
            setScannerState(result);
            if (!isIdentifier(nextLexicalUnit(true))) {
                throw error(ParseErrorId.IdentifierExpected);
            }
            do {
                var attribute = new AnnotationCreationNode();
                setScannerState(attribute);
                attribute.Type = parseType(true);
                if (lexicalUnit == LexicalUnit.OpenParenthesis) {
                    if (nextLexicalUnit(true) != LexicalUnit.CloseParenthesis) {
                        var namedValue = false;
                        if (isIdentifier(lexicalUnit)) {
                            var restorePoint = this.createRestorePoint();
                            int sp = scanner.StartPosition;
                            int len = getLexicalUnitLength();
                            saveScannerState();
                            if (nextLexicalUnit(true) == LexicalUnit.Assign) {
                                var objectInitializer = new ObjectInitializerExpressionNode();
                                setSavedScannerState(objectInitializer);
                                var memberInitializer = new MemberInitializerNode();
                                setSavedScannerState(memberInitializer);
                                memberInitializer.NameOffset = sp;
                                memberInitializer.NameLength = len;
                                nextLexicalUnit(true);
                                memberInitializer.Value = parseAnnotationValue();
                                memberInitializer.EndPosition = memberInitializer.Value.EndPosition;
                                objectInitializer.MemberInitializers.add(memberInitializer);
                                while (lexicalUnit == LexicalUnit.Comma) {
                                    if (!isIdentifier(nextLexicalUnit(true))) {
                                        break;
                                    }
                                    sp = scanner.StartPosition;
                                    len = getLexicalUnitLength();
                                    saveScannerState();
                                    if (nextLexicalUnit(true) != LexicalUnit.Assign) {
                                        throw error(ParseErrorId.AssignExpected);
                                    }
                                    memberInitializer = new MemberInitializerNode();
                                    setSavedScannerState(memberInitializer);
                                    memberInitializer.NameOffset = sp;
                                    memberInitializer.NameLength = len;
                                    nextLexicalUnit(true);
                                    memberInitializer.Value = parseAnnotationValue();
	                                memberInitializer.EndPosition = memberInitializer.Value.EndPosition;
                                    objectInitializer.MemberInitializers.add(memberInitializer);
                                    objectInitializer.EndPosition = memberInitializer.EndPosition;
                                }
                                attribute.Initializer = objectInitializer;
                                namedValue = true;
                            }
                            if (!namedValue) {
                                this.restore(restorePoint);
                            }
                        }
                        if (!namedValue) {
                            attribute.Initializer = parseAnnotationValue();
                        }
                        if (lexicalUnit != LexicalUnit.CloseParenthesis) {
                            throw error(ParseErrorId.CloseParenthesisExpected);
                        }
                    }
					attribute.EndPosition = scanner.EndPosition;
                    nextLexicalUnit(true);
                } else {
					attribute.EndPosition = attribute.Type.EndPosition;
				}
                result.Annotations.add(attribute);
            } while (lexicalUnit == LexicalUnit.Comma);
            if (lexicalUnit != LexicalUnit.CloseBracket) {
                throw error(ParseErrorId.CloseBracketExpected);
            }
			result.EndPosition = scanner.EndPosition;
            nextLexicalUnit(false);
            return result;
        }
Ejemplo n.º 3
0
        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;
        }