Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding all errors/diagnostics #23

Open
davidjamesb opened this issue Jun 12, 2019 · 9 comments
Open

Finding all errors/diagnostics #23

davidjamesb opened this issue Jun 12, 2019 · 9 comments

Comments

@davidjamesb
Copy link

After parsing the XML structure, does this library allow us to find all diagnostics/errors that occurred during the parse and where in the XML they occur?

I see this latest merged pull-request (https://github.com/KirillOsenkov/XmlParser/pull/22/files#) has added some more diagnostics support. Do I just need to wait for an updated NuGet package?

@KirillOsenkov
Copy link
Owner

I've just published 1.2.38 on https://www.nuget.org/packages/GuiLabs.Language.Xml

@davidjamesb
Copy link
Author

hmm..That url states that 1.2.35 is the latest version.

@KirillOsenkov
Copy link
Owner

@davidjamesb
Copy link
Author

I can see it now. Must have been slow updating. The nuget badge on the repository main page was also showing 1.2.35. Cheers.

@davidjamesb
Copy link
Author

So after updating to 1.2.38, I parsed some XML text that contains some errors (incorrect closing tags, missing closing tag, etc):

var parser = Parser.ParseText(xml);
var diags = parser.Body.GetDiagnostics();

Both parser.HasDiagonostics is false and diags is an empty array. Am I misunderstanding how diagnostics are supposed to work?

I'm trying to parse some XML and find all the syntax errors/diagnostics in it.

@garuma
Copy link
Collaborator

garuma commented Jun 12, 2019

Hey,

The errors will be attached to the node specifically where the error occurs, for instance if you have a mismatched tag like this:

<root>
</toor>

Then the diagnostic/error will be attached to the XmlElementEndTagSyntax node that represents </toor>.

Given a root document, this code should print out all the errors it has:

foreach (var node in root.DescendantNodesAndTokensAndSelf ()) {
	if (!node.ContainsDiagnostics)
		continue;
	var diagnostics = node.GetDiagnostics ();
	if (diagnostics == null || diagnostics.Length == 0)
		continue;
	foreach (var d in diagnostics) {
		var description = d.GetDescription ();\
 		Console.WriteLine ($"Error at position {node.Start}: {description}");
	}
}

@KirillOsenkov
Copy link
Owner

We should probably add a GetAllDiagnostics() extension method on the node that does the above. I agree that it's confusing that the root has no diagnostics.

@davidjamesb
Copy link
Author

davidjamesb commented Jun 12, 2019

Would it be possible to track errors in a 'global' collection on the first parse of the tree? The above code will work but it requires a second pass through the parse tree.

I'm looking to use this library to highlight syntax errors in a code editor so the above code would be quite inefficient.

@KirillOsenkov
Copy link
Owner

Perhaps we could pass an optional diagnostic bag to the parser, and it would add the diagnostics there as it's parsing, in addition to hanging them off the nodes as it does now. If we don't pass the diagnostic bag we don't pay the additional memory overhead. If we want diagnostics quickly we pass the bag and pay the memory cost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants