Пример #1
0
 public MemberListVisitor(AspNetParsedDocument doc, DocumentReferenceManager refMan)
 {
     this.doc     = doc;
     this.refMan  = refMan;
     this.Errors  = new List <Error> ();
     this.Members = new Dictionary <string, CodeBehindMember> ();
 }
		public MemberListVisitor (AspNetParsedDocument doc, DocumentReferenceManager refMan)
		{
			this.doc = doc;
			this.refMan = refMan;
			this.Errors = new List<Error> ();
			this.Members = new Dictionary<string,CodeBehindMember> ();
		}
Пример #3
0
        public override ParsedDocument Parse(bool storeAst, string fileName, TextReader tr, Project project = null)
        {
            var info     = new PageInfo();
            var rootNode = new RootNode();
            var errors   = new List <Error> ();

            try {
                rootNode.Parse(fileName, tr);
            } catch (Exception ex) {
                LoggingService.LogError("Unhandled error parsing ASP.NET document '" + (fileName ?? "") + "'", ex);
                errors.Add(new Error(ErrorType.Error, "Unhandled error parsing ASP.NET document: " + ex.Message));
            }


            foreach (var pe in rootNode.ParseErrors)
            {
                errors.Add(new Error(ErrorType.Error, pe.Message, pe.Location.BeginLine, pe.Location.BeginColumn));
            }

            info.Populate(rootNode, errors);

            var type = AspNetAppProject.DetermineWebSubtype(fileName);

            if (type != info.Subtype)
            {
                if (info.Subtype == WebSubtype.None)
                {
                    errors.Add(new Error(ErrorType.Error, "File directive is missing", 1, 1));
                }
                else
                {
                    type = info.Subtype;
                    errors.Add(new Error(ErrorType.Warning, "File directive does not match page extension", 1, 1));
                }
            }

            var result = new AspNetParsedDocument(fileName, type, rootNode, info);

            result.Add(errors);

            /*
             * if (MonoDevelop.Core.LoggingService.IsLevelEnabled (MonoDevelop.Core.Logging.LogLevel.Debug)) {
             *      DebugStringVisitor dbg = new DebugStringVisitor ();
             *      rootNode.AcceptVisit (dbg);
             *      System.Text.StringBuilder sb = new System.Text.StringBuilder ();
             *      sb.AppendLine ("Parsed AspNet file:");
             *      sb.AppendLine (dbg.DebugString);
             *      if (errors.Count > 0) {
             *              sb.AppendLine ("Errors:");
             *              foreach (ParserException ex in errors)
             *                      sb.AppendLine (ex.ToString ());
             *      }
             *      MonoDevelop.Core.LoggingService.LogDebug (sb.ToString ());
             * }*/

            return(result);
        }
		public DocumentInfo (AspNetParsedDocument aspNetParsedDocument, IEnumerable<string> imports,
		                     IList<ProjectDom> references)
		{
			this.AspNetDocument = aspNetParsedDocument;
			this.Imports = imports;
			this.References = references;
			ScriptBlocks = new List<TagNode> ();
			Expressions = new List<ExpressionNode> ();
			aspNetParsedDocument.RootNode.AcceptVisit (new ExpressionCollector (this));
		}
Пример #5
0
		public override ParsedDocument Parse (bool storeAst, string fileName, TextReader tr, Project project = null)
		{
			var info = new PageInfo ();
			var errors = new List<Error> ();

			Xml.StateEngine.Parser parser = new Xml.StateEngine.Parser (
				new AspNetFreeState (),
				true
			);
			
			try {
				parser.Parse (tr);
			} catch (Exception ex) {
				LoggingService.LogError ("Unhandled error parsing ASP.NET document '" + (fileName ?? "") + "'", ex);
				errors.Add (new Error (ErrorType.Error, "Unhandled error parsing ASP.NET document: " + ex.Message));
			}

			// get the errors from the StateEngine parser
			errors.AddRange (parser.Errors);

			// populating the PageInfo instance
			XDocument xDoc = parser.Nodes.GetRoot ();
			info.Populate (xDoc, errors);
			
			var type = AspNetAppProject.DetermineWebSubtype (fileName);
			if (type != info.Subtype) {
				if (info.Subtype == WebSubtype.None) {
					errors.Add (new Error (ErrorType.Error, "File directive is missing", 1, 1));
				} else {
					type = info.Subtype;
					errors.Add (new Error (ErrorType.Warning, "File directive does not match page extension", 1, 1));
				}
			}
			
			var result = new AspNetParsedDocument (fileName, type, info, xDoc);
			result.Add (errors);
							
			/*
			if (MonoDevelop.Core.LoggingService.IsLevelEnabled (MonoDevelop.Core.Logging.LogLevel.Debug)) {
				DebugStringVisitor dbg = new DebugStringVisitor ();
				rootNode.AcceptVisit (dbg);
				System.Text.StringBuilder sb = new System.Text.StringBuilder ();
				sb.AppendLine ("Parsed AspNet file:");
				sb.AppendLine (dbg.DebugString);
				if (errors.Count > 0) {
					sb.AppendLine ("Errors:");
					foreach (ParserException ex in errors)
						sb.AppendLine (ex.ToString ());
				}
				MonoDevelop.Core.LoggingService.LogDebug (sb.ToString ());
			}*/
			
			return result;
		}
		public override ParsedDocument Parse (ProjectDom dom, string fileName, string fileContent)
		{
			using (var tr = new StringReader (fileContent)) {
				var info = new PageInfo ();
				var rootNode = new RootNode ();
				var errors = new List<Error> ();
				
				try {
					rootNode.Parse (fileName, tr);
				} catch (Exception ex) {
					LoggingService.LogError ("Unhandled error parsing ASP.NET document '" + (fileName ?? "") + "'", ex);
					errors.Add (new Error (ErrorType.Error, 0, 0, "Unhandled error parsing ASP.NET document: " + ex.Message));
				}
				
				
				foreach (var pe in rootNode.ParseErrors)
					errors.Add (new Error (ErrorType.Error, pe.Location.BeginLine, pe.Location.BeginColumn, pe.Message));
				
				info.Populate (rootNode, errors);
				
				var type = AspNetAppProject.DetermineWebSubtype (fileName);
				if (type != info.Subtype) {
					if (info.Subtype == WebSubtype.None) {
						errors.Add (new Error (ErrorType.Error, 1, 1, "File directive is missing"));
					} else {
						type = info.Subtype;
						errors.Add (new Error (ErrorType.Warning, 1, 1, "File directive does not match page extension"));
					}
				}
				
				var result = new AspNetParsedDocument (fileName, type, rootNode, info);
				result.Add (errors);
								
				/*
				if (MonoDevelop.Core.LoggingService.IsLevelEnabled (MonoDevelop.Core.Logging.LogLevel.Debug)) {
					DebugStringVisitor dbg = new DebugStringVisitor ();
					rootNode.AcceptVisit (dbg);
					System.Text.StringBuilder sb = new System.Text.StringBuilder ();
					sb.AppendLine ("Parsed AspNet file:");
					sb.AppendLine (dbg.DebugString);
					if (errors.Count > 0) {
						sb.AppendLine ("Errors:");
						foreach (ParserException ex in errors)
							sb.AppendLine (ex.ToString ());
					}
					MonoDevelop.Core.LoggingService.LogDebug (sb.ToString ());
				}*/
				
				return result;
			}
		}
Пример #7
0
        public override ParsedDocument Parse(bool storeAst, string fileName, TextReader tr, Project project = null)
        {
            var info   = new PageInfo();
            var errors = new List <Error> ();

            Xml.StateEngine.Parser parser = new Xml.StateEngine.Parser(
                new AspNetFreeState(),
                true
                );

            try {
                parser.Parse(tr);
            } catch (Exception ex) {
                LoggingService.LogError("Unhandled error parsing ASP.NET document '" + (fileName ?? "") + "'", ex);
                errors.Add(new Error(ErrorType.Error, "Unhandled error parsing ASP.NET document: " + ex.Message));
            }

            // get the errors from the StateEngine parser
            errors.AddRange(parser.Errors);

            // populating the PageInfo instance
            XDocument xDoc = parser.Nodes.GetRoot();

            info.Populate(xDoc, errors);

            var type = AspNetAppProject.DetermineWebSubtype(fileName);

            if (type != info.Subtype)
            {
                if (info.Subtype == WebSubtype.None)
                {
                    errors.Add(new Error(ErrorType.Error, "File directive is missing", 1, 1));
                }
                else
                {
                    type = info.Subtype;
                    errors.Add(new Error(ErrorType.Warning, "File directive does not match page extension", 1, 1));
                }
            }

            var result = new AspNetParsedDocument(fileName, type, info, xDoc);

            result.Add(errors);

            return(result);
        }
Пример #8
0
		public override ParsedDocument Parse (bool storeAst, string fileName, TextReader tr, Project project = null)
		{
			var info = new PageInfo ();
			var errors = new List<Error> ();

			Xml.StateEngine.Parser parser = new Xml.StateEngine.Parser (
				new AspNetFreeState (),
				true
			);
			
			try {
				parser.Parse (tr);
			} catch (Exception ex) {
				LoggingService.LogError ("Unhandled error parsing ASP.NET document '" + (fileName ?? "") + "'", ex);
				errors.Add (new Error (ErrorType.Error, "Unhandled error parsing ASP.NET document: " + ex.Message));
			}

			// get the errors from the StateEngine parser
			errors.AddRange (parser.Errors);

			// populating the PageInfo instance
			XDocument xDoc = parser.Nodes.GetRoot ();
			info.Populate (xDoc, errors);
			
			var type = AspNetAppProject.DetermineWebSubtype (fileName);
			if (type != info.Subtype) {
				if (info.Subtype == WebSubtype.None) {
					errors.Add (new Error (ErrorType.Error, "File directive is missing", 1, 1));
				} else {
					type = info.Subtype;
					errors.Add (new Error (ErrorType.Warning, "File directive does not match page extension", 1, 1));
				}
			}
			
			var result = new AspNetParsedDocument (fileName, type, info, xDoc);
			result.Add (errors);
			
			return result;
		}
		public static System.CodeDom.CodeCompileUnit GenerateCodeBehind (AspNetAppProject project,
		                                                                 string filename,
		                                                                 AspNetParsedDocument document, 
		                                                                 List<CodeBehindWarning> errors)
		{
			string className = document.Info.InheritedClass;
			
			if (document.HasErrors) {
				AddFail (errors, document, document.Errors.Where (x => x.ErrorType == ErrorType.Error).First ());
				return null;
			}
			
			if (string.IsNullOrEmpty (className))
				return null;
			
			var refman = new DocumentReferenceManager (project) { Doc = document };
			var memberList = new MemberListVisitor (document, refman);
			document.RootNode.AcceptVisit (memberList);
			
			var err = memberList.Errors.Where (x => x.ErrorType == ErrorType.Error).FirstOrDefault ();
			if (err != null) {
				AddFail (errors, document, err);
				return null;
			}
			
			//initialise the generated type
			var ccu = new CodeCompileUnit ();
			var namespac = new CodeNamespace ();
			ccu.Namespaces.Add (namespac); 
			var typeDecl = new System.CodeDom.CodeTypeDeclaration () {
				IsClass = true,
				IsPartial = true,
			};
			namespac.Types.Add (typeDecl);
			
			//name the class and namespace
			int namespaceSplit = className.LastIndexOf ('.');
			string namespaceName = null;
			if (namespaceSplit > -1) {
				namespac.Name = project.StripImplicitNamespace (className.Substring (0, namespaceSplit));
				typeDecl.Name = className.Substring (namespaceSplit + 1);
			} else {
				typeDecl.Name = className;
			}
			
			string masterTypeName = null;
			if (!String.IsNullOrEmpty (document.Info.MasterPageTypeName)) {
				masterTypeName = document.Info.MasterPageTypeName;
			} else if (!String.IsNullOrEmpty (document.Info.MasterPageTypeVPath)) {
				try {
					ProjectFile resolvedMaster = project.ResolveVirtualPath (document.Info.MasterPageTypeVPath, document.FileName);
					AspNetParsedDocument masterParsedDocument = null;
					if (resolvedMaster != null)
						masterParsedDocument = ProjectDomService.Parse (project, resolvedMaster.FilePath, null)	as AspNetParsedDocument;
					if (masterParsedDocument != null && !String.IsNullOrEmpty (masterParsedDocument.Info.InheritedClass)) {
						masterTypeName = masterParsedDocument.Info.InheritedClass;
					} else {
						errors.Add (new CodeBehindWarning (String.Format ("Could not find type for master '{0}'",
						                                                  document.Info.MasterPageTypeVPath),
						                                   document.FileName));
					}
				} catch (Exception ex) {
					errors.Add (new CodeBehindWarning (String.Format ("Could not find type for master '{0}'",
					                                                  document.Info.MasterPageTypeVPath),
					                                   document.FileName));
					LoggingService.LogWarning ("Error resolving master page type", ex);
				}
			}
			
			if (masterTypeName != null) {
				var masterProp = new CodeMemberProperty () {
					Name = "Master",
					Type = new CodeTypeReference (masterTypeName),
					HasGet = true,
					HasSet = false,
					Attributes = System.CodeDom.MemberAttributes.Public | System.CodeDom.MemberAttributes.New 
						| System.CodeDom.MemberAttributes.Final,
				};
				masterProp.GetStatements.Add (new System.CodeDom.CodeMethodReturnStatement (
						new System.CodeDom.CodeCastExpression (masterTypeName, 
							new System.CodeDom.CodePropertyReferenceExpression (
								new System.CodeDom.CodeBaseReferenceExpression (), "Master"))));
				typeDecl.Members.Add (masterProp);
			}
			
			//shortcut building the existing members type map
			if (memberList.Members.Count == 0)
				return ccu;
			
			var dom = refman.TypeCtx.ProjectDom;
			var cls = dom.GetType (className);
			var members = GetDesignerMembers (memberList.Members.Values, cls, filename, dom, dom);
			
			//add fields for each control in the page
			
			foreach (var member in members) {
				var type = new CodeTypeReference (member.Type.FullName);
				typeDecl.Members.Add (new CodeMemberField (type, member.Name) { Attributes = MemberAttributes.Family });
			}
			return ccu;
		}
		static void AddFail (List<CodeBehindWarning> errors, AspNetParsedDocument document, Error err)
		{
			errors.Add (new CodeBehindWarning (GettextCatalog.GetString (
					"Parser failed with error {0}. CodeBehind members for this file will not be added.", err.Message),
					document.FileName, err.Region.Start.Line, err.Region.Start.Column));
		}
Пример #11
0
		public static BuildResult GenerateCodeBehind (
			AspNetAppProject project,
			string filename,
			AspNetParsedDocument document,
			out CodeCompileUnit ccu)
		{
			ccu = null;
			var result = new BuildResult ();
			string className = document.Info.InheritedClass;

			foreach (var err in document.Errors)
				result.AddError (filename, err.Region.BeginLine, err.Region.BeginColumn, null, err.Message);
			if (result.ErrorCount > 0)
				return result;
			
			if (string.IsNullOrEmpty (className))
				return result;
			
			var refman = new DocumentReferenceManager (project) { Doc = document };
			var memberList = new MemberListBuilder (refman, document.XDocument);
			memberList.Build ();

			foreach (var err in memberList.Errors)
				result.AddError (filename, err.Region.BeginLine, err.Region.BeginColumn, null, err.Message);
			if (result.ErrorCount > 0)
				return result;
			
			//initialise the generated type
			ccu = new CodeCompileUnit ();
			var namespac = new CodeNamespace ();
			ccu.Namespaces.Add (namespac); 
			var typeDecl = new CodeTypeDeclaration {
				IsClass = true,
				IsPartial = true,
			};
			namespac.Types.Add (typeDecl);
			
			//name the class and namespace
			int namespaceSplit = className.LastIndexOf ('.');
			if (namespaceSplit > -1) {
				namespac.Name = project.StripImplicitNamespace (className.Substring (0, namespaceSplit));
				typeDecl.Name = className.Substring (namespaceSplit + 1);
			} else {
				typeDecl.Name = className;
			}
			
			string masterTypeName = null;
			if (!String.IsNullOrEmpty (document.Info.MasterPageTypeName)) {
				masterTypeName = document.Info.MasterPageTypeName;
			} else if (!String.IsNullOrEmpty (document.Info.MasterPageTypeVPath)) {
				try {
					ProjectFile resolvedMaster = project.ResolveVirtualPath (document.Info.MasterPageTypeVPath, document.FileName);
					AspNetParsedDocument masterParsedDocument = null;
					if (resolvedMaster != null)
						masterParsedDocument = TypeSystemService.ParseFile (project, resolvedMaster.FilePath) as AspNetParsedDocument;
					if (masterParsedDocument != null && !String.IsNullOrEmpty (masterParsedDocument.Info.InheritedClass))
						masterTypeName = masterParsedDocument.Info.InheritedClass;
				} catch (Exception ex) {
					LoggingService.LogWarning ("Error resolving master page type", ex);
				}
				if (string.IsNullOrEmpty (masterTypeName)) {
					var msg = string.Format ("Could not find type for master '{0}'", document.Info.MasterPageTypeVPath);
					result.AddError (filename, msg);
					return result;
				}
			}
			
			if (masterTypeName != null) {
				var masterProp = new CodeMemberProperty {
					Name = "Master",
					Type = new CodeTypeReference (masterTypeName),
					HasGet = true,
					HasSet = false,
					Attributes = MemberAttributes.Public | MemberAttributes.New | MemberAttributes.Final,
				};
				masterProp.GetStatements.Add (new CodeMethodReturnStatement (
						new CodeCastExpression (masterTypeName, 
							new CodePropertyReferenceExpression (
								new CodeBaseReferenceExpression (), "Master"))));
				typeDecl.Members.Add (masterProp);
			}
			
			//shortcut building the existing members type map
			if (memberList.Members.Count == 0)
				return result;
			
			var dom = refman.TypeCtx.Compilation;
			var cls = ReflectionHelper.ParseReflectionName (className).Resolve (dom);
			var members = GetDesignerMembers (memberList.Members.Values, cls, filename);
			
			//add fields for each control in the page
			
			foreach (var member in members) {
				var type = new CodeTypeReference (member.Type.FullName);
				typeDecl.Members.Add (new CodeMemberField (type, member.Name) { Attributes = MemberAttributes.Family });
			}
			return result;
		}
Пример #12
0
		void GetHtmlFoldingRegions (List<FoldingRegion> foldingRegions)
		{
			if (htmlParsedDocument != null) {
				var d = new AspNetParsedDocument (null, WebSubtype.Html, null, htmlParsedDocument);
				foldingRegions.AddRange (d.Foldings);
			}
		}
		public DocumentInfo (ICompilation dom, AspNetParsedDocument aspNetParsedDocument, IEnumerable<string> imports,
		                     IList<ICompilation> references)
		{
			this.Dom = dom;
			this.AspNetDocument = aspNetParsedDocument;
			this.Imports = imports;
			this.References = references;
			BuildExpressionAndScriptsLists ();
		}
		public DocumentInfo (ICompilation dom, AspNetParsedDocument aspNetParsedDocument, IEnumerable<string> imports,
		                     IList<ICompilation> references)
		{
			this.Dom = dom;
			this.AspNetDocument = aspNetParsedDocument;
			this.Imports = imports;
			this.References = references;
			ScriptBlocks = new List<TagNode> ();
			Expressions = new List<ExpressionNode> ();
			//aspNetParsedDocument.RootNode.AcceptVisit (new ExpressionCollector (this));
			BuildExpressionAndScriptsLists ();
		}