コード例 #1
0
ファイル: WikiArticle.cs プロジェクト: cramt/gm_dotnet
        public static LuaFunctionInfo ConvertRoot(XRoot root)
        {
            bool isFunction = false;
            var  info       = new LuaFunctionInfo();

            foreach (var template in root.Templates)
            {
                var translated = TranslateTemplate.Translate(template);
                if (translated is FuncTemplate func)
                {
                    isFunction       = true;
                    info.Description = func.Description;
                }
                else if (translated is ArgTemplate arg)
                {
                    var argumentDeclaration = new IDescribeArgument {
                        Name = arg.Name, Type = TranslateType(arg.Type), Description = arg.Desc
                    };
                    if (!string.IsNullOrWhiteSpace(arg.Default))
                    {
                        argumentDeclaration.Attributes.Add(new CodeAttributeDeclaration(nameof(OptionalAttribute)));
                    }
                    info.Arguments.Add(argumentDeclaration);
                }
                else if (translated is RetTemplate ret)
                {
                    info.Returns.Add(new IDescribeReturn {
                        Type = TranslateType(ret.Type), Description = ret.Desc
                    });
                }
                else if (translated is ExampleTemplate example)
                {
                    info.Examples.Add(example);
                }
            }
            if (!isFunction)
            {
                return(null);
            }
            return(info);
        }
コード例 #2
0
        protected override void VisitChild(VisitorCursor cursor, List <VisitorCursor> parents)
        {
            if (cursor.Kind == CXCursorKind.CXCursor_ClassDecl)
            {
                if (!config.NativeInterfaces.Any(i => i.Name == cursor.Spelling) || cursor.IsForwardDeclaration)
                {
                    return;
                }

                var classInfo = new IDescribeInterface
                {
                    Name = cursor.Spelling
                };


                cursor.VisitChildren((methodCursor) =>
                {
                    if (methodCursor.Kind == CXCursorKind.CXCursor_CXXMethod ||
                        methodCursor.Kind == CXCursorKind.CXCursor_Destructor ||
                        methodCursor.Kind == CXCursorKind.CXCursor_Constructor)
                    {
                        var methodName = methodCursor.Spelling;
                        if (methodCursor.Kind == CXCursorKind.CXCursor_Constructor)
                        {
                            methodName = "ctor" + methodName;
                        }
                        if (methodCursor.Kind == CXCursorKind.CXCursor_Destructor)
                        {
                            methodName = "dtor" + (methodName.Replace("~", ""));
                        }
                        if (methodCursor.IsVirtual)
                        {
                            var methodInfo = new IDescribeMethod
                            {
                                Name    = methodName,
                                Returns = { new IDescribeReturn {
                                                Type = methodCursor.ReturnTypeCSharp
                                            } }
                            };

                            for (uint index = 0; index < methodCursor.NumArgs; ++index)
                            {
                                var paramSpelling = methodCursor.GetArgSpelling(index);

                                if (string.IsNullOrEmpty(paramSpelling))
                                {
                                    paramSpelling = "param" + index;
                                }

                                var info = new IDescribeArgument
                                {
                                    Type = methodCursor.GetArgType(index),
                                    Name = paramSpelling
                                };


                                var tokens      = methodCursor.GetArgCursor(index).GetTokens();
                                var foundEquals = false;
                                for (int i = 0; i < tokens.Length - 1; i++)
                                {
                                    var token = tokens[i];
                                    if (token.Kind == CXTokenKind.CXToken_Punctuation && token.Spelling == "=")
                                    {
                                        foundEquals = true;
                                        continue;
                                    }
                                    if (foundEquals && token.Kind == CXTokenKind.CXToken_Identifier)
                                    {
                                        info.Default = ""; break;
                                    }                                                                                             //probably an enum dont support this(yet(maybe))
                                    if (foundEquals)
                                    {
                                        info.Default += token.Spelling;
                                    }
                                }


                                methodInfo.Arguments.Add(info);
                            }
                            classInfo.Methods.Add(methodInfo);
                        }
                    }

                    if (methodCursor.Kind == CXCursorKind.CXCursor_CXXBaseSpecifier)
                    {
                        var definitionSpelling = methodCursor.CursorDefinition.Spelling;
                        classInfo.Parents.Add(definitionSpelling);
                    }
                });

                this.info.Interfaces.Add(classInfo);
            }
        }