Ejemplo n.º 1
0
        /// <summary>
        /// Add Methode to Class
        /// </summary>
        public static MethodeContainer AddMethode(this ClassContainer inClass, string inMethodeName, TypeContainer inReturnType, params FieldContainer[] inFieldContainer)
        {
            var tmpMethode = new MethodeContainer()
            {
                Name       = inMethodeName,
                ReturnType = inReturnType,
                Parameter  = inFieldContainer.ToList(),
            };

            inClass.AddMethode(tmpMethode);
            return(tmpMethode);
        }
Ejemplo n.º 2
0
        internal static string ManageClassBodyContext(ClassContainer inClass, string tmpComment, ClassBodyContext tmpClassBoy)
        {
            var tmpChildList = tmpClassBoy.GetChildren().ToList();

            for (var tmpI = 0; tmpI < tmpChildList.Count(); tmpI++)
            {
                var tmpItem = tmpChildList[tmpI];
                var tmpType = tmpItem.GetType().Name;
                if (tmpItem is ClassBodyDeclarationContext)
                {
                    var tmpClassBody = tmpItem as ClassBodyDeclarationContext;
                    if (tmpClassBody.memberDeclaration() == null)
                    {
                        continue;
                    }
                    var tmpDeclaration = tmpClassBody.memberDeclaration();
                    if (tmpDeclaration == null)
                    {
                        continue;
                    }
                    var tmpModifierList = new List <string>();
                    //get modifiers together
                    foreach (var tmpClassBodyContextModifier in tmpClassBody.modifier())
                    {
                        var tmpModifierText = tmpClassBodyContextModifier.GetText();
                        //F**k it: If it starts with @ it probably is an override. If Not -> Fix the C# Code:D
                        if (tmpModifierText.StartsWith("@"))
                        {
                            tmpModifierText = tmpModifierText.Substring(1).ToLower();
                        }
                        tmpModifierList.Add(tmpModifierText);
                    }
                    if (tmpClassBody.children.Count > 1 &&
                        tmpClassBody.children[1].GetText() == "abstract" && !inClass.ModifierList.Contains("abstract"))
                    {
                        tmpModifierList.Add("abstract");
                        inClass.ModifierList.Add("abstract");
                    }

                    if (tmpDeclaration.methodDeclaration() != null)
                    {
                        var tmpMethodeContainer = AddMethodeDeclaration(inClass, tmpDeclaration.methodDeclaration(), tmpModifierList);
                        //In this case, we have a Generic methode Constructor
                        if (tmpI > 0 && tmpChildList[tmpI - 1].GetText().EndsWith(">"))
                        {
                            var tmpDataList  = new List <IParseTree>();
                            var tmpDepth     = 0;
                            var tmpNegativeI = 0;
                            while (tmpDepth != 0 || tmpNegativeI == 0)
                            {
                                tmpNegativeI++;
                                var tmpPreviiousCHild = tmpChildList[tmpI - tmpNegativeI];
                                foreach (var tmpChildOfChildOfPrechild in tmpPreviiousCHild.GetChildren().Reverse().SelectMany(inItem => inItem.GetChildren().Reverse()))
                                {
                                    tmpDataList.Insert(0, tmpChildOfChildOfPrechild);
                                    if (tmpChildOfChildOfPrechild.GetText() == ">")
                                    {
                                        tmpDepth++;
                                    }
                                    else if (tmpChildOfChildOfPrechild.GetText() == "<")
                                    {
                                        tmpDepth--;
                                    }
                                }
                            }
                            var tmpGenericListForMethode = GetGenericTypesFromGenericArgumentsChildren(inClass, tmpDataList);
                            tmpMethodeContainer.GenericTypes.AddRange(tmpGenericListForMethode);
                        }
                    }
                    else if (tmpDeclaration.fieldDeclaration() != null)
                    {
                        //Load field declaration into the container
                        var tmpModifier       = tmpDeclaration.fieldDeclaration();
                        var tmpFieldContainer = new FieldContainer
                        {
                            Name    = tmpModifier.variableDeclarators().variableDeclarator(0).variableDeclaratorId().IDENTIFIER().GetText(),
                            Type    = tmpModifier.typeType().GetText(),
                            Comment = tmpComment,
                        };
                        tmpComment = "";
                        if (tmpModifier.variableDeclarators().variableDeclarator().Length > 0)
                        {
                            tmpFieldContainer.DefaultValue = CreateBlockFromMethodeInizialiser(tmpModifier.variableDeclarators().variableDeclarator()[0].variableInitializer(), inClass, tmpFieldContainer);
                        }
                        tmpFieldContainer.ModifierList = tmpModifierList;
                        inClass.FieldList.Add(tmpFieldContainer);
                    }
                    else if (tmpDeclaration.constructorDeclaration() != null)
                    {
                        var tmpConstructor = tmpDeclaration.constructorDeclaration();
                        var tmpMethode     = new MethodeContainer
                        {
                            Name          = tmpConstructor.IDENTIFIER().GetText(),
                            AntlrCode     = tmpConstructor.block(),
                            IsConstructor = true,
                            Comment       = tmpComment,
                        };
                        tmpComment = "";
                        var tmpParams = tmpConstructor.formalParameters()?.formalParameterList()?.formalParameter();
                        if (tmpParams != null)
                        {
                            foreach (IFormalParameterContext tmpParam in tmpParams)
                            {
                                HandlMethodeParameterContext(tmpMethode, tmpParam);
                            }
                            if (tmpConstructor.formalParameters()?.formalParameterList()?.lastFormalParameter() != null)
                            {
                                HandlMethodeParameterContext(tmpMethode, tmpConstructor.formalParameters().formalParameterList().lastFormalParameter());
                            }
                        }
                        tmpMethode.ModifierList = tmpModifierList;
                        inClass.AddMethode(tmpMethode);
                    }
                    else if (tmpDeclaration.genericMethodDeclaration() != null)
                    {
                        var tmpMethodeDeclaration = tmpDeclaration.genericMethodDeclaration();

                        var tmpMethodeContainer      = AddMethodeDeclaration(inClass, tmpMethodeDeclaration.methodDeclaration(), tmpModifierList);
                        var tmpGenericListForMethode = GetGenericTypesFromGenericArgumentsChildren(inClass, tmpMethodeDeclaration.typeParameters().GetChildren());
                        tmpMethodeContainer.GenericTypes.AddRange(tmpGenericListForMethode);
                    }
                    else
                    {
                    }
                }
                else if (tmpItem is TerminalNodeImpl)
                {
                    if (tmpItem.GetText().StartsWith("/"))
                    {
                        tmpComment = tmpItem.GetText();
                    }
                    else if (tmpItem.GetText() == "{")
                    {
                    }
                    else if (tmpItem.GetText() == "}")
                    {
                    }
                    else
                    {
                    }
                }
                else
                {
                }
            }

            return(tmpComment);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Add Methode Declaration to Class
        /// </summary>
        /// <param name="inClass"></param>
        /// <param name="tmpDeclaration"></param>
        /// <param name="tmpModifierList"></param>
        /// <returns></returns>
        private static MethodeContainer AddMethodeDeclaration(ClassContainer inClass, MethodDeclarationContext inMethodeContext, List <string> tmpModifierList)
        {
            var tmpMethode = new MethodeContainer
            {
                Name       = inMethodeContext.IDENTIFIER().GetText(),
                AntlrCode  = inMethodeContext.methodBody(),
                ReturnType = CreateTypeContainerFromType(inMethodeContext.typeTypeOrVoid()),
            };
            var tmpParams = inMethodeContext.formalParameters()?.formalParameterList()?.formalParameter();

            if (tmpParams != null)
            {
                foreach (var tmpParam in tmpParams)
                {
                    var tmpNewMethode = new FieldContainer
                    {
                        Name = tmpParam.variableDeclaratorId().IDENTIFIER().GetText(),
                    }; if (tmpParam.typeType().classOrInterfaceType() != null)
                    {
                        tmpNewMethode.Type = tmpParam.typeType().classOrInterfaceType().IDENTIFIER(0).GetText();
                        foreach (var tmpGeneric in tmpParam.typeType().classOrInterfaceType().typeArguments().SelectMany(inItem => inItem.typeArgument()))
                        {
                            var tmpType = new TypeContainer(tmpGeneric.GetChild(0).GetText());
                            if (tmpGeneric.EXTENDS() != null)
                            {
                                tmpType.Extends.Add(tmpGeneric.typeType().GetText());
                            }
                            tmpNewMethode.Type.GenericTypes.Add(tmpType);
                        }

                        if (tmpParam.GetText().Contains("["))
                        {
                            tmpNewMethode.Type.IsArray = true;
                        }
                        if (tmpParam.typeType().classOrInterfaceType().IDENTIFIER().Length > 1)
                        {
                            throw new NotImplementedException("Multi-Identifier needs to be handled");
                        }
                    }
                    else if (tmpParam.typeType().primitiveType() != null)
                    {
                        tmpNewMethode.Type = tmpParam.typeType().primitiveType().GetText();
                    }
                    else
                    {
                        throw new NotImplementedException("Other Type Missing");
                    }
                    if (tmpParam.variableModifier().Length > 0)
                    {
                        foreach (var tmpModifierContext in tmpParam.variableModifier())
                        {
                            var tmpParamModifier = tmpModifierContext.GetText();
                            //F**k it: If it starts with @ it probably is an override. If Not -> Fix the C# Code:D
                            if (tmpParamModifier.StartsWith("@"))
                            {
                                tmpParamModifier = tmpParamModifier.Substring(1).ToLower();
                            }
                            tmpNewMethode.ModifierList.Add(tmpParamModifier);
                        }
                    }
                    tmpMethode.Parameter.Add(tmpNewMethode);
                }
            }
            tmpMethode.ModifierList = tmpModifierList;
            inClass.AddMethode(tmpMethode);
            return(tmpMethode);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Resolve an Interface Declaration
        /// </summary>
        /// <param name="tmpClass"></param>
        /// <param name="tmpComment"></param>
        /// <param name="tmpSubChild"></param>
        private static void ResolveInterfaceDeclaration(ClassContainer tmpClass, string tmpComment, IParseTree tmpSubChild)
        {
            var tmpBodyPart = (tmpSubChild as InterfaceBodyDeclarationContext).interfaceMemberDeclaration();

            if (tmpBodyPart.interfaceMethodDeclaration() != null)
            {
                var tmpIntDec = tmpBodyPart.interfaceMethodDeclaration();

                var tmpMethode = new MethodeContainer
                {
                    Name       = tmpIntDec.IDENTIFIER().GetText(),
                    ReturnType = GetTypeContainer(tmpIntDec.typeTypeOrVoid().typeType()),
                    Comment    = tmpComment,
                    AntlrCode  = tmpIntDec.methodBody(),
                };
                foreach (var tmpEntry in tmpIntDec.interfaceMethodModifier())
                {
                    if (tmpEntry.annotation() != null)
                    {
                        throw new NotImplementedException("Methode modifier annotation not Implemented");
                    }
                    tmpMethode.ModifierList.Add(tmpEntry.GetText());
                }
                var tmpParams = tmpIntDec.formalParameters()?.formalParameterList()?.formalParameter();
                if (tmpParams != null)
                {
                    foreach (var tmpParam in tmpParams)
                    {
                        var tmpModifier       = tmpParam.variableModifier();
                        var tmpFieldContainer = new FieldContainer
                        {
                            Name = tmpParam.variableDeclaratorId().IDENTIFIER().GetText(),
                            Type = GetTypeContainer(tmpParam.typeType()),
                        };
                        foreach (var tmpInfo in tmpParam.variableModifier())
                        {
                            tmpFieldContainer.ModifierList.Add(tmpInfo.GetText());
                        }
                        tmpMethode.Parameter.Add(tmpFieldContainer);
                    }
                }
                tmpClass.AddMethode(tmpMethode);
            }
            else if (tmpBodyPart.constDeclaration() != null)
            {
                var tmpConstDef   = tmpBodyPart.constDeclaration();
                var tmpDeclarator = tmpConstDef.constantDeclarator()[0];

                var tmpFieldContainer = new FieldContainer
                {
                    Name    = tmpDeclarator.IDENTIFIER().GetText(),
                    Type    = GetTypeContainer(tmpConstDef.typeType()),
                    Comment = tmpComment,
                };
                var tmpInizialiser = tmpDeclarator.variableInitializer();
                tmpFieldContainer.DefaultValue = CreateBlockFromMethodeInizialiser(tmpInizialiser, tmpClass, tmpFieldContainer);

                tmpClass.FieldList.Add(tmpFieldContainer);
            }
            else if (tmpBodyPart.classDeclaration() != null)
            {
                var tmpSubClass = new ClassContainer
                {
                    UsingList = tmpClass.UsingList,
                    Namespace = tmpClass.Namespace,
                    Comment   = tmpComment,
                };

                tmpSubClass.Type = tmpBodyPart.classDeclaration().IDENTIFIER().GetText();
                if (tmpBodyPart.classDeclaration().typeParameters() != null)
                {
                    //Fill Type Parameters
                    foreach (var tmpParam in tmpBodyPart.classDeclaration().typeParameters().typeParameter())
                    {
                        var tmpInnerType = new TypeContainer(tmpParam.IDENTIFIER().GetText());
                        if (tmpParam.annotation().Length > 0 || tmpParam.EXTENDS() != null)
                        {
                            throw new NotImplementedException("Type Param Fill error");
                        }
                        tmpSubClass.Type.GenericTypes.Add(tmpInnerType);
                    }
                }

                FillClassContext(tmpSubClass, tmpBodyPart.classDeclaration());
                tmpClass.InnerClasses.Add(tmpSubClass);
            }
            else
            {
            }
        }