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(); } } }
public override void EnterClass_definition(CSharpParser.Class_definitionContext context) { /*Console.WriteLine(context.GetText()); * Console.WriteLine(); * Console.WriteLine(context.class_base().GetText()); */ var nameString = $"{_parentFileName}|{context.identifier().GetText()}"; var name = new ClassName(nameString); Console.WriteLine($"Parse class {nameString}"); var classBodyListener = new ClassBodyListener(name, _parentFileName); context.class_body().EnterRule(classBodyListener); Console.WriteLine($"TODO: Implement innerClasses"); Console.WriteLine($"TODO: Implement flags"); var headerText = context.GetText(); ClassInfo = new ClassInfo( name, classBodyListener.MethodInfos, classBodyListener.FieldInfos, AccessFlags.None, new ClassInfo[0], new SourceCodeSnippet(headerText, SourceCodeLanguage.CSharp), false); }
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); }