Exemple #1
0
        public MetaItem(Node node)
        {
            Node = node;
            Name = node.GetAttributeValue("name");
            FormattedName = StringUtils.CapitalizeFirstLetter(Name);

            // Get item comment
            if (Node.Children != null)
            {
                var descNode = Node.GetChildByType(NodeType.Desc);
                if (descNode != null && !String.IsNullOrWhiteSpace(descNode.Attributes["text"]))
                {
                    Comment = descNode.Attributes["text"];
                }
            }

            OverriddenFrom = node.GetAttributeValue("overriddenFrom");
            if (OverriddenFrom != null)
            {
                OverriddenFrom = TypeMapper.MapType(OverriddenFrom);
            }

            DocFrom = node.GetAttributeValue("docFrom");
            if (DocFrom != null)
            {
                DocFrom = TypeMapper.MapType(DocFrom);
            }
        }
Exemple #2
0
 public MetaEvent(Node node) : base(node)
 {
     var typesNode = node.GetChildByType(NodeType.Types);
     if (typesNode != null)
     {
         EventType = TypeMapper.MapType(typesNode.Children[0].GetAttributeValue("type"));
     }
     // Add 'On' in front of all the event names because many times there will be properties or methods with the same names
     FormattedName = "On" + FormattedName;
 }
Exemple #3
0
        public MetaProperty(Node node)
            : base(node)
        {
            AccessType = node.GetAttributeValue("access");
            if (AccessType == null) AccessType = "public";

            // Check means property type (wtf?)
            Type = node.GetAttributeValue("check");
            if (Type == null || Type.Contains(" "))
                Type = "object";
            else
                Type = TypeMapper.MapType(Type);
        }
        public MetaMethodParameter(Node node)
            : base(node)
        {
            if (node.Children != null)
            {
                // There could be more than one parameter type, in which case make it an object
                var typesNode = node.GetChildByType(NodeType.Types);
                if (typesNode.Children.Count == 1)
                {
                    Type = typesNode.Children[0].GetAttributeValue("type");
                }
                else
                {
                    // If only one of the parameter types is non-null, set it as a parameter type
                    // otherwise set the parameter type to object
                    var nonNullTypes = new List<string>();
                    foreach (var typeNode in typesNode.Children)
                    {
                        var type = typeNode.GetAttributeValue("type");
                        if (type != "null")
                        {
                            nonNullTypes.Add(type);
                        }
                    }
                    if (nonNullTypes.Count == 1)
                    {
                        Type = nonNullTypes[0];
                    }
                    else
                    {
                        Type = "object";
                    }
                }
            }

            if (Type == "Function" && Name == "listener")
                Type = "FunctionListener";

            Type = TypeMapper.MapType(Type);
            if (Type == null)
            {
                Type = "object";
            }

            DefaultValue = node.GetAttributeValue("defaultValue");
            FormatDefaultValue();

            Name = TypeMapper.EscapeIdentifier(Name);
        }
Exemple #5
0
        public MetaClass(Node node) : base(node)
        {
            Usings = new List<string>();
            Methods = new List<MetaMethod>();
            Properties = new List<MetaProperty>();
            Events = new List<MetaEvent>();
            MixinClasses = new List<MetaClass>();

            OriginalFullName = node.Attributes["fullName"];
            FullName = TypeMapper.MapType(OriginalFullName);
            BaseClass = TypeMapper.MapType(node.GetAttributeValue("superClass"));
            Namespace = TypeMapper.MapNamespace(node.Attributes["packageName"]);
            if (String.IsNullOrWhiteSpace(Namespace))
            {
                Namespace = "qx";
                if (!FullName.Contains(".")) FullName = Namespace + "." + FullName;
            }

            IsStatic = node.Attributes.ContainsKey("isStatic") && node.GetAttributeValue("isStatic") == "True";
            IsAbstract = node.Attributes.ContainsKey("isAbstract") && node.GetAttributeValue("isAbstract") == "True";
            IsMixin = node.Attributes.ContainsKey("type") && node.GetAttributeValue("type") == "mixin";
            IsInterface = node.Attributes.ContainsKey("type") && node.GetAttributeValue("type") == "interface";

            if (BaseClass != null)
            {
                InheritanceList = ": " + TypeMapper.MapNamespace(BaseClass);
            }

            var interfaceList = node.GetAttributeValue("interfaces");
            if (interfaceList != null)
            {
                Interfaces = interfaceList.Split(',').ToList();
                for (int i = 0; i < Interfaces.Count; i++)
                    Interfaces[i] = TypeMapper.MapNamespace(Interfaces[i]);

                if (InheritanceList == null)
                    InheritanceList = ": " + String.Join(", ", Interfaces);
                else
                    InheritanceList += ", " + String.Join(", ", Interfaces);
            }
            else
            {
                Interfaces = new List<string>();
            }
            if (InheritanceList == null) InheritanceList = "";

            var mixinList = node.GetAttributeValue("mixins");
            if (mixinList != null)
            {
                Mixins = mixinList.Split(',').ToList();
                for (int i = 0; i < Mixins.Count; i++)
                    Mixins[i] = TypeMapper.MapNamespace(Mixins[i]);
            }
            else
            {
                Mixins = new List<string>();
            }

            // Add all the properties first (some methods depend on them for lookups)
            foreach (var child in node.Children)
            {
                switch (child.Type)
                {
                    case NodeType.Properties:
                        foreach (var propNode in child.Children)
                        {
                            var metaProperty = new MetaProperty(propNode);
                            // Add 'x' in the end if property name = class name
                            if (metaProperty.FormattedName == FormattedName)
                            {
                                metaProperty.FormattedName += "x";
                            }
                            Properties.Add(metaProperty);
                        }
                        break;
                }
            }

            // Add methods and events 
            foreach (var child in node.Children)
            {
                switch (child.Type)
                {
                    case NodeType.Constructor:
                        var ctorNode = child.GetChildByType(NodeType.Method);
                        Methods.Add(new MetaMethod(ctorNode, this));
                        break;

                    case NodeType.Methods:
                    case NodeType.MethodsStatic:
                        foreach (var methodNode in child.Children)
                        {
                            var attachStatic = methodNode.GetChildByType(NodeType.AttachStatic);
                            if (attachStatic == null) // Not sure what is attackStatic, but we don't need them
                            {
                                var metaMethod = new MetaMethod(methodNode, this);
                                // Add all public methods
                                if (metaMethod.AccessType == "public")
                                {
                                    // Add 'x' in the end if method name = class name
                                    if (metaMethod.FormattedName == FormattedName)
                                    {
                                        metaMethod.FormattedName += "x";
                                    }
                                    Methods.Add(metaMethod);
                                }
                            }
                        }
                        break;

                    case NodeType.Events:
                        foreach (var eventNode in child.Children)
                        {
                            if (Events.FirstOrDefault(e => e.Name == eventNode.GetAttributeValue("name")) == null)
                            {
                                Events.Add(new MetaEvent(eventNode));
                            }
                        }
                        break;
                }
            }

            // In some cases there will be instance & static methods with the same name, adding 'S' in the end for those
            foreach (var method in Methods)
            {
                if (method.IsStatic)
                {
                    var existingNonStatic = Methods.FirstOrDefault(m => !m.IsStatic && m.FormattedName == method.FormattedName);
                    if (existingNonStatic != null)
                    {
                        method.FormattedName += "S";
                    }
                }
            }

            // Adding parameterless constructor if not present
            if (!IsInterface)
            {
                var parameterlessConstructor = Methods.FirstOrDefault(m => m.IsConstructor && m.Parameters.Count == 0);
                if (parameterlessConstructor == null)
                {
                    Methods.Insert(0, new MetaMethod
                    {
                        IsConstructor = true,
                        AccessType = "public",
                        Name = Name,
                        FormattedName = Name,
                        ReturnType = "",
                        Parameters = new List<MetaMethodParameter>(),
                        AutoInsert = true
                    });
                }
            }
        }
Exemple #6
0
        public MetaMethod(Node node, MetaClass metaClass) : base(node)
        {
            IsConstructor = node.Attributes.ContainsKey("isCtor") && node.GetAttributeValue("isCtor") == "True";
            if (IsConstructor)
            {
                Name = metaClass.Name;
                FormattedName = Name;
                ReturnType = "";
            }
            else
            {
                FormattedName = StringUtils.CapitalizeFirstLetter(FormattedName.Replace("$", ""));
            }

            IsStatic = node.Attributes.ContainsKey("isStatic") && node.GetAttributeValue("isStatic") == "True";

            AccessType = node.GetAttributeValue("access");
            if (AccessType == null) AccessType = "public";

            Parameters = new List<MetaMethodParameter>();
            var paramsNode = node.GetChildByType(NodeType.Params);
            if (paramsNode != null)
            {
                foreach (var el in paramsNode.Children)
                {
                    Parameters.Add(new MetaMethodParameter(el));
                }
            }

            // Make all parameters following the optional ones to also be optional (not always the case)
            bool hasOptional = false;
            foreach (var parameter in Parameters)
            {
                if (parameter.DefaultValue != null)
                {
                    hasOptional = true;
                }
                else if (hasOptional)
                {
                    parameter.DefaultValue = "null";
                    parameter.FormatDefaultValue();
                }
            }

            // Get the return / parameter type from the relevant property (if there is one)
            var fromProperty = node.GetAttributeValue("fromProperty");
            if (fromProperty != null)
            {
                var property = metaClass.Properties.FirstOrDefault(p => p.Name == fromProperty);
                if (property != null)
                {
                    if (Name.StartsWith("get"))
                    {
                        ReturnType = property.Type;
                    }
                    else if (Name.StartsWith("set"))
                    {
                        Parameters[0].Type = property.Type;
                    }
                }
            }

            // Get return type from the metadata
            if (ReturnType == null && fromProperty == null)
            {
                var returnNode = node.GetChildByType(NodeType.Return);
                if (returnNode != null)
                {
                    var typesNode = returnNode.GetChildByType(NodeType.Types);
                    if (typesNode != null)
                    {
                        // There could be more than one return type, in which case make it an object
                        if (typesNode.Children.Count == 1)
                        {
                            ReturnType = typesNode.Children[0].GetAttributeValue("type");
                        }
                        else
                        {
                            // If only one of the return types is non-null, set it as a return type
                            // otherwise set the return type to object
                            var nonNullTypes = new List<string>();
                            foreach (var typeNode in typesNode.Children)
                            {
                                var type = typeNode.GetAttributeValue("type");
                                if (type != "null")
                                {
                                    nonNullTypes.Add(type);
                                }
                            }
                            if (nonNullTypes.Count == 1)
                            {
                                ReturnType = nonNullTypes[0];
                            }
                            else
                            {
                                ReturnType = "object";
                            }
                        }
                    }
                    var commentNode = returnNode.GetChildByType(NodeType.Desc);
                    if (commentNode != null)
                    {
                        ReturnComment = commentNode.GetAttributeValue("text");
                    }
                }
            }
            if (ReturnType == "null")
                ReturnType = "object";

            if (!IsConstructor)
            {
                if (ReturnType == null)
                    ReturnType = "void";
                else
                    ReturnType = TypeMapper.MapType(ReturnType);
            }
        }