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;
		}
Exemple #2
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;
		}
		void ParseHtmlDocument (List<Error> errors)
		{
			var sb = new StringBuilder ();
			var spanList = new List<Span> ();
			comments = new List<Comment> ();

			Action<Span> action = (Span span) =>
			{
				if (span.Kind == SpanKind.Markup) {
					sb.Append (span.Content);
					spanList.Add (span);
				} else {
					for (int i = 0; i < span.Content.Length; i++) {
						char ch = span.Content[i];
						if (ch != '\r' && ch != '\n')
							sb.Append (' ');
						else
							sb.Append (ch);
					}
					if (span.Kind == SpanKind.Comment) {
						var comment = new Comment (span.Content)
						{
							OpenTag = "@*",
							ClosingTag = "*@",
							CommentType = CommentType.Block,
						};
						comment.Region = new DomRegion (
							currentDocument.OffsetToLocation (span.Start.AbsoluteIndex - comment.OpenTag.Length),
							currentDocument.OffsetToLocation (span.Start.AbsoluteIndex + span.Length + comment.ClosingTag.Length));
						comments.Add (comment);
					}
				}
			};

			editorParser.CurrentParseTree.Accept (new CallbackVisitor (action));

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

			try {
				parser.Parse (new StringReader (sb.ToString ()));
			} catch (Exception ex) {
				LoggingService.LogError ("Unhandled error parsing html in Razor document '" + (lastParsedFile ?? "") + "'", ex);
			}

			htmlParsedDocument = parser.Nodes.GetRoot ();
			errors.AddRange (parser.Errors);
		}