Ejemplo n.º 1
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;
        }
		private void print(AnnotationSectionNode a, bool newline, StringBuilder sb) {
			sb.append("[");
			foreach (var attr in a.getAnnotations()) {
				print(attr.Type, sb);
				if (attr.Initializer != null) {
					sb.append("(");
					var first = true;
					var expr = attr.getInitializer();
					if (first) {
						first = false;
					} else {
						sb.append(", ");
					}
					print(expr, sb);
					sb.append(")");
				}
			}
			sb.append("]");
			if (newline) {
				sb.append("\r\n");
			}
		}