示例#1
0
 public static IType Parse(this CSharpParser.Class_typeContext context)
 {
     if (context.namespace_or_type_name() != null)
     {
         return(new NamedType(context.namespace_or_type_name().ParseAsType()));
     }
     else if (context.OBJECT() != null)
     {
         return(new InbuiltType(InbuiltType.Types.Object));
     }
     else if (context.DYNAMIC() != null)
     {
         return(new InbuiltType(InbuiltType.Types.Dynamic));
     }
     else if (context.STRING() != null)
     {
         return(new InbuiltType(InbuiltType.Types.String));
     }
     else
     {
         throw new NotSupportedException("Unknown class type " + context.GetText());
     }
 }
示例#2
0
 public override void ExitClass_type(CSharpParser.Class_typeContext context)
 {
     Console.WriteLine("Exiting class_type context.");
 }
示例#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);
        }