public static TypeName ParseAsType(this CSharpParser.Namespace_or_type_nameContext context) { IParseTree[] children = context.children.ToArray(); IList <TypeName.Component> components = new List <TypeName.Component>(); int i = 0; string nsAlias = null; if (children.Length > 0 && children[0] is CSharpParser.Qualified_alias_memberContext) { CSharpParser.Qualified_alias_memberContext qamc = (CSharpParser.Qualified_alias_memberContext)children[0]; nsAlias = qamc.identifier(0).ParseAsText(); string name = qamc.identifier(1).ParseAsText(); IType[] typeArguments = null; if (qamc.type_argument_list() != null) { typeArguments = qamc.type_argument_list().Parse(); } components.Add(new TypeName.Component(name, typeArguments)); i++; } while (i < children.Length) { IParseTree child = children[i]; if (child is TerminalNodeImpl && ((TerminalNodeImpl)children[i]).IsType(CSharpLexer.DOT)) { continue; } if (!(child is CSharpParser.IdentifierContext)) { throw new ArgumentException("Expected identifier next"); } string name = ((CSharpParser.IdentifierContext)child).ParseAsText(); i++; IType[] typeArguments = null; if (i < children.Length && children[i] is CSharpParser.Type_argument_listContext) { typeArguments = ((CSharpParser.Type_argument_listContext)children[i]).Parse(); i++; } components.Add(new TypeName.Component(name, typeArguments)); } return(new TypeName(nsAlias, components.ToArray())); }
public static string ParseAsNamespace(this CSharpParser.Namespace_or_type_nameContext context) { if (context.type_argument_list().Length > 0) { throw new NotSupportedException("Type arguments not supported on namespace names"); } if (context.qualified_alias_member() != null) { throw new NotSupportedException("Qualified alias not supported on namespace names"); } return(context.GetText()); }
public override void ExitNamespace_or_type_name(CSharpParser.Namespace_or_type_nameContext context) { Console.WriteLine("Exiting namespace_or_type_name context."); }
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); }