Exemple #1
0
        public override void EnterConstant_declaration(CSharpParser.Constant_declarationContext context)
        {
            Console.WriteLine("Entering constant_declaration context.");

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

            Node.Kind parentKind = parentNode.NodeKind;

            // Getting the constants' type:
            Type t = TreatTypeContext(context.type_());

            // Getting all the modifiers:
            Symbol.ModifierFlag modFlags = TreatModTokens();
            modifiersTokens.Clear();
            modFlags |= Symbol.ModifierFlag.Const;

            // Getting the name of the constant(s):
            CSharpParser.Constant_declaratorContext[] declarators = context.constant_declarators().constant_declarator();
            foreach (CSharpParser.Constant_declaratorContext declarator in declarators)
            {
                CSharpParser.IdentifierContext idCtx = declarator.identifier();
                IToken idToken = idCtx.Start;

                // Creating the constant symbol:
                if (parentKind == Node.Kind.ClassBody)
                {
                    // Getting the owner class symbol:
                    ClassSymbol     ownerClass     = ((ClassType)(parentNode.Type)).Symbol;
                    AttributeSymbol constantSymbol = new AttributeSymbol(modFlags, ownerClass);

                    // Creating the constant member node:
                    Node constMemberNode = new Node(idToken, Node.Kind.MemberVariableDeclaration, t, constantSymbol);
                    ast.AddNode(constMemberNode);
                    parentNode.AddChildIndex(ast.NodeIndex(constMemberNode));

                    // Adding the symbol to the table:
                    symbolTable.AddSymbol(idToken, constantSymbol);
                }
                else
                {
                    // Creating a method variable
                    // Getting the owner method symbol:
                    MethodSymbol   ownerMethod    = (MethodSymbol)(symbolTable.FindSymbol(parentNode.Token, ast));
                    VariableSymbol constantSymbol = new VariableSymbol(modFlags, ownerMethod);

                    // Creating the constant variable node:
                    Node constVarNode = new Node(idToken, Node.Kind.MethodVariableDeclaration, t, constantSymbol);
                    ast.AddNode(constVarNode);
                    parentNode.AddChildIndex(ast.NodeIndex(constVarNode));

                    // Adding the symbol to the table:
                    symbolTable.AddSymbol(idToken, constantSymbol);
                }
            }
        }
Exemple #2
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);
        }
Exemple #3
0
        public override void EnterStruct_definition(CSharpParser.Struct_definitionContext context)
        {
            Console.WriteLine("Entering struct_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> interfaceTokens = new List <IToken>(); // Will hold the base class tokens

            // Getting the interfaces' names identifiers:
            CSharpParser.Struct_interfacesContext interfaces = context.struct_interfaces();
            if (interfaces != null)
            {
                CSharpParser.Interface_type_listContext typeListCtx = interfaces.interface_type_list();
                if (typeListCtx != null)
                {
                    CSharpParser.Namespace_or_type_nameContext[] types = typeListCtx.namespace_or_type_name();
                    foreach (CSharpParser.Namespace_or_type_nameContext name in types)
                    {
                        CSharpParser.IdentifierContext[] ids = name.identifier();
                        foreach (CSharpParser.IdentifierContext id in ids)
                        {
                            interfaceTokens.Add(id.Start);
                        }
                    }
                }
            }

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

            foreach (Symbol bs in interfaceSymbols)
            {
                interfaceClassSymbols.Add((ClassSymbol)bs);
            }

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

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

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

            currentScopeNode.AddChildIndex(structNodeIndex);

            // Adding the struct node:
            ast.AddNode(structNode);

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

            // Adding the struct body node as a class node child:
            CSharpParser.Struct_bodyContext bodyCtx = context.struct_body();
            ClassType structType = new ClassType(idToken, ClassTag.Struct, structSymbol);

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

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

            structNode.AddChildIndex(bodyNodeIndex);

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

            symbolTable.AddSymbol(idToken, structSymbol);
        }