示例#1
0
        public override void VisitInterfaceBlock(InterfaceBlockSyntax node)
        {
            var statementNode = node.ChildNodes().OfType <InterfaceStatementSyntax>().FirstOrDefault();
            var isPartial     = statementNode.ChildTokens().Any(x => x.Kind() == SyntaxKind.PartialKeyword);
            var defineName    = statementNode.ChildTokens().FirstOrDefault(x => x.Kind() == SyntaxKind.IdentifierToken).ToString();

            // ジェネリック型を定義している場合
            if (statementNode.ChildNodes().OfType <TypeParameterListSyntax>().Any())
            {
                var listNode     = statementNode.ChildNodes().OfType <TypeParameterListSyntax>().FirstOrDefault();
                var genericTypes = listNode
                                   .ChildNodes()
                                   .OfType <TypeParameterSyntax>()
                                   .Select(x => x.ChildTokens().FirstOrDefault(y => y.Kind() == SyntaxKind.IdentifierToken).ToString());

                defineName = $"{defineName}<{string.Join(", ", genericTypes)}>";
            }

            var startLength     = node.Span.Start;
            var endLength       = node.Span.End;
            var parentNamespace = GetNamespace(DefineKinds.Interface, startLength, endLength);

            var baseTypeInfos = new List <BaseTypeInfo>();

            // 継承元クラス、またはインターフェースがある場合
            var hasInherits   = node.ChildNodes().OfType <InheritsStatementSyntax>().Any();
            var hasImplements = node.ChildNodes().OfType <ImplementsStatementSyntax>().Any();

            if (hasInherits || hasImplements)
            {
                var baseTypes = new List <SyntaxNode>();

                if (hasInherits)
                {
                    var inheritsNode = node.ChildNodes().OfType <InheritsStatementSyntax>().FirstOrDefault();
                    var childNodes   = inheritsNode.ChildNodes();

                    // Interface の場合、Inherits, IInterface1, IInterface2 などと記述する
                    // Implements ではなく Inherits でインターフェースを継承する
                    foreach (var childNode in childNodes)
                    {
                        baseTypes.Add(childNode);
                    }
                }

                if (hasImplements)
                {
                    // 上記仕様から以下はありえないのだが、将来仕様変更されるか?されないと思う
                    var implementsNode = node.ChildNodes().OfType <ImplementsStatementSyntax>().FirstOrDefault();
                    var childNodes     = implementsNode.ChildNodes();

                    foreach (var childNode in childNodes)
                    {
                        baseTypes.Add(childNode);
                    }
                }

                baseTypeInfos = GetBaseTypeInfos(baseTypes, parentNamespace);
            }

            UserDefinitions.Add(new UserDefinition
            {
                DefineKinds    = DefineKinds.Interface,
                IsPartial      = isPartial,
                Namespace      = parentNamespace,
                DefineName     = defineName,
                DefineFullName = $"{parentNamespace}.{defineName}",
                BaseTypeInfos  = baseTypeInfos,
                SourceFile     = SourceFile,
                StartLength    = startLength,
                EndLength      = endLength,
            });

            base.VisitInterfaceBlock(node);
        }
示例#2
0
        private Interface TraverseInterface(InterfaceBlockSyntax ibs)
        {
            Interface retInterface = new Interface();

            /*
            Encapsulation = new List<Encapsulation>();
            Qualifiers = new List<Qualifiers>();
            Attributes = new List<Variables>();
            Methods = new List<Method>();
            InheritsStrings = new List<string>();
            InheritsInterfaces = new List<Interface>();
            */

            InterfaceStatementSyntax iss = ibs.Begin;

            foreach (SyntaxToken st in iss.Modifiers)
            {
                string modifier = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(st.ValueText);
                Encapsulation encap;
                Qualifiers qual;
                if (System.Enum.TryParse<Encapsulation>(modifier, out encap))
                {
                    retInterface.Encapsulation.Add(encap);
                }
                else if (System.Enum.TryParse<Qualifiers>(modifier, out qual))
                {
                    retInterface.Qualifiers.Add(qual);
                }
            }

            retInterface.Name = iss.Identifier.ValueText;

            foreach(InheritsStatementSyntax inherits in ibs.Inherits){
                foreach (SyntaxNode sn in inherits.ChildNodes())
                {
                    if (sn is IdentifierNameSyntax)
                    {
                        retInterface.InheritsStrings.Add((sn as IdentifierNameSyntax).Identifier.ValueText);
                    }
                }
            }

            List<MethodBlockSyntax> methods = new List<MethodBlockSyntax>();
            List<FieldDeclarationSyntax> Fields = new List<FieldDeclarationSyntax>();
            List<PropertyBlockSyntax> properties = new List<PropertyBlockSyntax>();

            foreach (SyntaxNode sn in ibs.ChildNodes())
            {
                if (sn is MethodBlockSyntax)
                {
                    methods.Add(sn as MethodBlockSyntax);
                }
                else if (sn is FieldDeclarationSyntax)
                {
                    Fields.Add(sn as FieldDeclarationSyntax);
                }
                else if (sn is PropertyBlockSyntax)
                {
                    properties.Add(sn as PropertyBlockSyntax);
                }
            }

            foreach (MethodBlockSyntax mbs in methods)
            {
                bool isConstructor = false;
                retInterface.Methods.Add(TraverseMethod(mbs, ref isConstructor));
            }

            foreach (FieldDeclarationSyntax fds in Fields)
            {
                retInterface.Fields.Add(TraverseField(fds));
            }

            foreach (PropertyBlockSyntax pbs in properties)
            {
                retInterface.Properties.Add(TraverseProperties(pbs));
            }

            return retInterface;
        }