public static void Parse(this CSharpParser.Class_definitionContext context)
        {
            Console.WriteLine("  " + context.identifier().GetText());

            if (context.type_parameter_list() != null)
            {
                throw new NotImplementedException("Type parameters not implemented yet");
            }
            if (context.class_base() != null)
            {
                throw new NotImplementedException("Base class not supported yet");
            }
            if (context.type_parameter_constraints_clauses() != null)
            {
                throw new NotImplementedException("Type parameter contrainsts not implemented yet");
            }

            CSharpParser.Class_bodyContext cbody = context.class_body();
            if (cbody.class_member_declarations() != null)
            {
                foreach (
                    CSharpParser.Class_member_declarationContext declarationContext in
                    cbody.class_member_declarations().class_member_declaration())
                {
                    declarationContext.Parse();
                }
            }
        }
Example #2
0
        public override void EnterClass_body(CSharpParser.Class_bodyContext context)
        {
            Console.WriteLine();
            Console.WriteLine(context.GetText());

            foreach (var classMemberDeclaration in context.class_member_declarations().class_member_declaration())
            {
                var typedMemberDeclarationListener = new TypedMemberDeclarationListener(_className);
                classMemberDeclaration.common_member_declaration().typed_member_declaration().EnterRule(typedMemberDeclarationListener);

                if (typedMemberDeclarationListener.MethodInfo != null)
                {
                    MethodInfos.Add(typedMemberDeclarationListener.MethodInfo);
                }

                if (typedMemberDeclarationListener.FieldInfo != null)
                {
                    FieldInfos.Add(typedMemberDeclarationListener.FieldInfo);
                }

                /*Console.WriteLine();
                 * Console.WriteLine(classMemberDeclaration.attributes().GetText());
                 * Console.WriteLine();
                 * Console.WriteLine(classMemberDeclaration.all_member_modifiers().GetText());
                 * Console.WriteLine(classMemberDeclaration.common_member_declaration().GetText());*/
            }
        }
Example #3
0
        public override void EnterClass_definition(CSharpParser.Class_definitionContext context)
        {
            Console.WriteLine("Entering class_definition context.");

            // Getting the current scope node:
            Node currentScopeNode = ast.GetNode(symbolTable.CurrentScopeNode);

            // Add class symbol to symbol table: parent class, owner class and other informations and add to node data:

            // Getting the parent classes:
            List <IToken> baseTokens = new List <IToken>(); // Will hold the base class tokens

            // Getting the base classes' names identifiers:
            CSharpParser.Class_baseContext classBaseCtx = context.class_base();
            if (classBaseCtx != null)
            {
                CSharpParser.Class_typeContext classTypeCtx = classBaseCtx.class_type();

                if (classTypeCtx != null)
                {
                    CSharpParser.Namespace_or_type_nameContext typeNameCtx = classTypeCtx.namespace_or_type_name();
                    if (typeNameCtx != null)
                    {
                        CSharpParser.IdentifierContext[] typeIDCtxs = typeNameCtx.identifier();
                        foreach (CSharpParser.IdentifierContext id in typeIDCtxs)
                        {
                            baseTokens.Add(id.Start);
                        }
                    }
                    else
                    {
                        baseTokens.Add(typeNameCtx.Start);
                    }
                }
            }

            // Getting the base classes' symbols:
            Symbol[]           baseSymbols      = symbolTable.FindSymbols(baseTokens.ToArray(), ast);
            List <ClassSymbol> baseClassSymbols = new List <ClassSymbol>();

            foreach (Symbol bs in baseSymbols)
            {
                baseClassSymbols.Add((ClassSymbol)bs);
            }

            // Getting the class' modifiers:
            Symbol.ModifierFlag modFlags = TreatModTokens();
            modifiersTokens.Clear();

            // Creating the class symbol:
            ClassSymbol classSymbol = new ClassSymbol(modFlags, baseClassSymbols.ToArray());

            // Adding the class node as a child to the current scope's AST node:
            Type type           = new Type(context.CLASS().Symbol);
            Node classNode      = new Node(context.CLASS().Symbol, Node.Kind.ClassDefinition, type);
            int  classNodeIndex = ast.NodeIndex(classNode);

            currentScopeNode.AddChildIndex(classNodeIndex);

            // Adding the class node:
            ast.AddNode(classNode);

            // Adding an identifier node as a class node child:
            CSharpParser.IdentifierContext idCtx = context.identifier();
            IToken idToken = idCtx.Start;

            // Adding the class body node as a class node child:
            CSharpParser.Class_bodyContext bodyCtx = context.class_body();
            ClassType classType = new ClassType(idToken, ClassTag.Class, classSymbol);

            symbolTable.AddType(classType);
            Node bodyNode = new Node(idToken, Node.Kind.ClassBody, classType);

            ast.AddNode(bodyNode);
            int bodyNodeIndex = ast.NodeIndex(bodyNode);

            classNode.AddChildIndex(bodyNodeIndex);

            // Enter scope in the symbol table
            symbolTable.EnterScope(bodyNodeIndex);

            symbolTable.AddSymbol(idToken, classSymbol);
        }