⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csharpast.cs

📁 C#编写的c#编译器
💻 CS
📖 第 1 页 / 共 5 页
字号:
			{
				Console.WriteLine("Not resolving expressions due to some unresolved symbols.");
			}
			return graph;
		}
	}
	#endregion 
	#region NamespaceNode 
	public class				NamespaceNode : CSharpAST
	{
		public override Dom.IGraph		GetGraph()				
		{	
			Dom.NamespaceDecl graph = 
				new Dom.NamespaceDecl();
			return this.GetGraph(graph);
		}	
		public Dom.NamespaceDecl	
			GetGraph(Dom.NamespaceDecl graph)
		{	
			//graph.Clear();
			base.GetGraph(graph);
			this.OpenScope(graph);

			AST tok = this.getFirstChild();			
			while(tok != null)
			{
				if(tok is QualIdent)
				{
					graph.Name = QualIdent.GetFullName((QualIdent)tok);
				}				
				else if(tok is Types) 
					graph.Types = ((Types)tok).GetGraph(graph.Types);
				// todo: imports

				tok = tok.getNextSibling();
			}
			this.AddDefinition(graph, graph.Name, graph);
			this.CloseScope();
			return graph;
		}
	}
	#endregion 
	#region UsingNode 
	public class				UsingNode : CSharpAST
	{
		public override Dom.IGraph		GetGraph()				
		{	
			Dom.Import graph = 
				new Dom.Import();
			return this.GetGraph(graph);
		}	
		public Dom.Import	
			GetGraph(Dom.Import graph)
		{	
			base.GetGraph(graph);
			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is Ident) 
				{
					AST ctok = tok.getFirstChild();
					if(ctok != null) graph.Alias = ctok.getText();
				}
				if(tok is QualIdent) 
				{		
					graph.Namespace = QualIdent.GetFullName((QualIdent)tok);
				}
				tok = tok.getNextSibling();
			}
			return graph;
		}
	}
	#endregion 

	#region Types 
	public class				Types : CSharpAST
	{
		public override Dom.IGraph		GetGraph()				
		{	
			Dom.TypeDeclCollection graph = 
				new Dom.TypeDeclCollection();
			return this.GetGraph(graph);
		}	
		public Dom.TypeDeclCollection	
			GetGraph(Dom.TypeDeclCollection graph)
		{	
			base.GetGraph(graph);
			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is InterfaceNode) 
					graph.Add((Dom.InterfaceDecl)((InterfaceNode)tok).GetGraph());
				if(tok is ClassNode) 
					graph.Add((Dom.ClassDecl)((ClassNode)tok).GetGraph());
				if(tok is EnumNode) 
					graph.Add((Dom.EnumDecl)((EnumNode)tok).GetGraph());
				if(tok is StructNode) 
					graph.Add((Dom.StructDecl)((StructNode)tok).GetGraph());
				if(tok is DelegateNode) 
					graph.Add((Dom.DelegateDecl)((DelegateNode)tok).GetGraph());

				tok = tok.getNextSibling();
			}

			return graph;
		}
		public static void	ParseTypesIntoNamespace(AST tok, Dom.NamespaceDecl graph)
		{	
			if(tok == null || graph == null) return;

			if(tok is InterfaceNode) 
				graph.Types.Add((Dom.InterfaceDecl)((InterfaceNode)tok).GetGraph());
			if(tok is ClassNode) 
				graph.Types.Add((Dom.ClassDecl)((ClassNode)tok).GetGraph());
			if(tok is EnumNode) 
				graph.Types.Add((Dom.EnumDecl)((EnumNode)tok).GetGraph());
			if(tok is StructNode) 
				graph.Types.Add((Dom.StructDecl)((StructNode)tok).GetGraph());
			if(tok is DelegateNode) 
				graph.Types.Add((Dom.DelegateDecl)((DelegateNode)tok).GetGraph());

		}
	}
	#endregion 
    	#region InterfaceNode *
	public class						InterfaceNode : TypeNode
	{
		public override Dom.IGraph		GetGraph()				
		{	
			Dom.InterfaceDecl graph = 
				new Dom.InterfaceDecl();
			return this.GetGraph(graph);
		}	
		public Dom.InterfaceDecl	
			GetGraph(Dom.InterfaceDecl graph)
		{	
			//graph.Clear();
			this.OpenScope(graph);
			base.GetGraph(graph);
			this.CloseScope();
			this.AddDefinition(graph, graph.Name, graph);
			return graph;
		}
	}
		#endregion 
		#region ClassNode *
	public class						ClassNode : TypeNode
	{
		public override Dom.IGraph		GetGraph()				
		{	
			Dom.ClassDecl graph = 
				new Dom.ClassDecl();
			return this.GetGraph(graph);
		}	
		public Dom.ClassDecl	
			GetGraph(Dom.ClassDecl graph)
		{	
			//graph.Clear();
			this.OpenScope(graph);
			base.GetGraph(graph);
			this.AddDefinition(graph, graph.Name, graph);
			this.CloseScope();
			return graph;
		}
	}
	#endregion 
		#region EnumNode *
	public class						EnumNode : TypeNode
	{
		public override Dom.IGraph		GetGraph()				
		{	
			Dom.EnumDecl graph = 
				new Dom.EnumDecl();
			return this.GetGraph(graph);
		}	
		public Dom.EnumDecl	
			GetGraph(Dom.EnumDecl graph)
		{	
			//graph.Clear();
			this.OpenScope(graph);
			base.GetGraph(graph);
			this.AddDefinition(graph, graph.Name, graph);
			this.CloseScope();
			return graph;
		}
	}
	#endregion 
		#region StructNode *
	public class						StructNode : TypeNode
	{
		public override Dom.IGraph		GetGraph()				
		{	
			Dom.StructDecl graph = 
				new Dom.StructDecl();
			return this.GetGraph(graph);
		}	
		public Dom.StructDecl	
			GetGraph(Dom.StructDecl graph)
		{
			//graph.Clear();
			this.OpenScope(graph);
			base.GetGraph(graph);
			this.AddDefinition(graph, graph.Name, graph);
			this.CloseScope();
			return graph;
		}
	}
	#endregion 
		#region DelegateNode *
	public class						DelegateNode : TypeNode
	{
		public override Dom.IGraph		GetGraph()				
		{	
			Dom.DelegateDecl graph = 
				new Dom.DelegateDecl();
			return this.GetGraph(graph);
		}	
		public Dom.DelegateDecl	
			GetGraph(Dom.DelegateDecl graph)
		{	
			//graph.Clear();
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is TypeRef) 
				{
					graph.ReturnType = (Dom.TypeRef)((TypeRef)tok).GetGraph();
				}
				else if(tok is DeclArgs) 
					((DeclArgs)tok).GetGraph(graph.Parameters);
				tok = tok.getNextSibling();
			}
			AddDefinition(graph, graph.Name, graph);
			return graph;
		}
	}
	#endregion 

	#region Members 
	public class				Members : CSharpAST
	{
		public override Dom.IGraph		GetGraph()				
		{	
			Dom.MemberDeclCollection graph = 
				new Dom.MemberDeclCollection();
			return this.GetGraph(graph);
		}	
		public Dom.MemberDeclCollection	
			GetGraph(Dom.MemberDeclCollection graph)
		{	
			base.GetGraph(graph);
			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is ConstantNode) 
					graph.Add( (Dom.ConstantDecl)((ConstantNode)tok).GetGraph() );

				else if(tok is	FieldNode) 
					graph.Add( (Dom.FieldDecl)((FieldNode)tok).GetGraph() );

				else if(tok is	MethodNode) 
					graph.Add( (Dom.MethodDecl)((MethodNode)tok).GetGraph() );

				else if(tok is	PropertyNode) 
					graph.Add( (Dom.PropertyDecl)((PropertyNode)tok).GetGraph() );

				else if(tok is EventNode) 
					graph.Add( (Dom.EventDecl)((EventNode)tok).GetGraph() );

				else if(tok is IndexerNode) 
					graph.Add( (Dom.IndexerDecl)((IndexerNode)tok).GetGraph() );

				else if(tok is OperatorNode)
					graph.Add( (Dom.OperatorDecl)((OperatorNode)tok).GetGraph() );

				else if(tok is ConstructorNode)
						graph.Add( (Dom.ConstructorDecl)((ConstructorNode)tok).GetGraph() );

				else if(tok is DestructorNode) 
					graph.Add( (Dom.DestructorDecl)((DestructorNode)tok).GetGraph() );

				else if(tok is AccessorNode) 
					graph.Add( (Dom.AccessorDecl)((AccessorNode)tok).GetGraph() );

				else if(tok is EnumMemberNode) 
					graph.Add( (Dom.EnumMemberDecl)((EnumMemberNode)tok).GetGraph() );

				tok = tok.getNextSibling();
			}

			return graph;
		}

	}
	#endregion 
	#region TypeMemberNode *
	/// <summary>
	/// Abstract base class for member types (Class, Method, Prop..)
	/// Name Attributes CustomAttributes Comments
	/// </summary>
	public abstract class TypeMemberNode : CSharpAST		
	{
//		public override Dom.IGraph			GetGraph()				
//		{	
//			Dom.MemberDecl graph = new Dom.MemberDecl();
//			return this.GetGraph(graph);
//		}	
		public Dom.MemberDecl	GetGraph(Dom.MemberDecl graph)
		{	
			// Name, Modifiers, CustomAttributes, Comments
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is ModifierAttributes) 
					graph.Attributes = ParseModifiers((ModifierAttributes)tok);
				else if(tok is CustomAttributes)
					((CustomAttributes)tok).GetGraph(graph.CustomAttributes);
				else if(tok is CommentNode)
					((CommentNode)tok).GetGraph(graph.Comments);

				tok = tok.getNextSibling();
			}
			return graph;
		}
		protected Dom.Modifiers ParseModifiers(ModifierAttributes tok)	
		{
			if(tok.getNumberOfChildren() == 0) return Dom.Modifiers.Empty;
			AST curtok = tok.getFirstChild();
			Dom.Modifiers mods = Dom.Modifiers.Empty;
			while(curtok != null)
			{
				switch (curtok.getText())
				{
					case "new":
						mods |= Dom.Modifiers.New;
						break;
					case "public":
						mods |= Dom.Modifiers.Public;
						break;
					case "protected":
						mods |= Dom.Modifiers.Protected;
						break;
					case "internal":
						mods |= Dom.Modifiers.Internal;
						break;
					case "private":
						mods |= Dom.Modifiers.Private;
						break;
					case "static":
						mods |= Dom.Modifiers.Static;
						break;
					case "virtual":
						mods |= Dom.Modifiers.Virtual;
						break;
					case "sealed":
						mods |= Dom.Modifiers.Sealed;
						break;
					case "override":
						mods |= Dom.Modifiers.Override;
						break;
					case "abstract":
						mods |= Dom.Modifiers.Abstract;
						break;
					case "extern":
						mods |= Dom.Modifiers.Extern;
						break;
					case "readonly":
						mods |= Dom.Modifiers.Readonly;
						break;
					case "volatile":
						mods |= Dom.Modifiers.Volatile;
						break;
				}
				curtok = curtok.getNextSibling();
			}
			return mods;
		}
	}
	#endregion 
		#region TypeNode *
	public abstract class				TypeNode : TypeMemberNode
	{
		public Dom.TypeDecl	GetGraph(Dom.TypeDecl graph)
		{	
			// required for constructors and enum members, so they can know their return type
			this.curType = graph;
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is ModifierAttributes) 
					graph.TypeAttributes = ParseTypeModifiers((ModifierAttributes)tok);
				else if(tok is Ident) 
					graph.Name = tok.getText();
				else if(tok is Members) 
					((Members)tok).GetGraph(graph.Members);
				else if(tok is BaseTypes) 
					((BaseTypes)tok).GetGraph(graph.BaseTypes);
				else if(tok is CommentNode)
					((CommentNode)tok).GetGraph(graph.Comments);

				tok = tok.getNextSibling();
			}
			return graph;
		}
		protected Dom.TypeModifiers ParseTypeModifiers(ModifierAttributes tok)	
		{
			if(tok.getNumberOfChildren() == 0) return Dom.TypeModifiers.Empty;
			AST curtok = tok.getFirstChild();
			Dom.TypeModifiers mods = Dom.TypeModifiers.Empty;
			while(curtok != null)
			{
				switch (curtok.getText())
				{
					case "new":
						mods |= Dom.TypeModifiers.Abstract;
						break;
					case "public":
						mods |= Dom.TypeModifiers.Class;
						break;
					case "protected":
						mods |= Dom.TypeModifiers.Interface;
						break;
					case "internal":
						mods |= Dom.TypeModifiers.NestedPrivate;
						break;
					case "private":
						mods |= Dom.TypeModifiers.NestedPublic;
						break;
					case "static":
						mods |= Dom.TypeModifiers.NotPublic;
						break;
					case "virtual":
						mods |= Dom.TypeModifiers.Public;
						break;
					case "sealed":
						mods |= Dom.TypeModifiers.Sealed;
						break;
				}
				curtok = curtok.getNextSibling();
			}
			return mods;
		}

	}
		#endregion 
		#region ConstantNode *
	public class						ConstantNode : TypeMemberNode
	{
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.ConstantDecl graph = 
				new Dom.ConstantDecl();
			return this.GetGraph(graph);
		}	
		public Dom.ConstantDecl	
			GetGraph(Dom.ConstantDecl graph)
		{		
			base.GetGraph(graph);
			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is TypeRef)
				{
					graph.Type = (Dom.TypeRef)((TypeRef)tok).GetGraph();
				}
				else if(tok is Declarator) 
				{
					((Declarator)tok).GetGraph(graph.Delcarators);
				}				
				tok = tok.getNextSibling();
			}
			foreach(Dom.Declarator d in graph.Delcarators)
			{
				d.Definition.Type = graph.Type;
			}
			return graph;
		}


	}
		#endregion 
		#region FieldNode *
	public class						FieldNode : TypeMemberNode
	{
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.FieldDecl graph = new Dom.FieldDecl();
			return this.GetGraph(graph);
		}	
		public Dom.FieldDecl	GetGraph(Dom.FieldDecl graph)
		{		
			base.GetGraph(graph);
			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is TypeRef)
				{
					graph.Type = (Dom.TypeRef)((TypeRef)tok).GetGraph();
				}
				else if(tok is Declarator) 
				{
					((Declarator)tok).GetGraph(graph.Delcarators);
				}	
				
				tok = tok.getNextSibling();
			}
			foreach(Dom.Declarator d in graph.Delcarators)
			{
				d.Definition.Type = graph.Type;
			}
			return graph;
		}

	}
		#endregion 
		#region MethodNode *
	public class						MethodNode : TypeMemberNode
	{
		public override Dom.IGraph			GetGraph()				

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -