Skip to content

Commit

Permalink
Struct Scraping Support (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
HurricanKai authored May 15, 2022
1 parent ef62a5c commit 94036b5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/generators/Silk.NET.SilkTouch.Scraper/ClangScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public sealed class ClangScraper
/// <returns>Any number of symbols scraped from the given xml</returns>
public IEnumerable<Symbol> ScrapeXML(XmlDocument document)
{
var bindings = document.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.LocalName == "bindings" && x is XmlElement) as XmlElement;
var bindings = document.ChildNodes.Cast<XmlNode>().OfType<XmlElement>().FirstOrDefault();

if (bindings is null)
{
Expand Down
14 changes: 14 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,27 @@ public IEnumerable<Symbol> Visit(XmlNode node)
return VisitBinding(bindings);
case XmlElement { Name: "namespace" } @namespace:
return VisitNamespace(@namespace);
case XmlElement { Name: "struct" } @struct:
return VisitStruct(@struct);
default:
{
throw new NotImplementedException();
}
}
}

private IEnumerable<Symbol> VisitStruct(XmlElement @struct)
{
return new[]
{
new StructSymbol
(
new IdentifierSymbol(@struct.Attributes?["name"]?.Value ?? throw new InvalidOperationException()),
StructLayout.Empty
)
};
}

private IEnumerable<Symbol> VisitBinding(XmlElement bindings)
{
return bindings.ChildNodes.Cast<XmlNode>().Where(x => x is not null).SelectMany(Visit);
Expand Down
21 changes: 21 additions & 0 deletions tests/Silk.NET.SilkTouch.Scraper.Tests/StructScrapingTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Xml;
using Silk.NET.SilkTouch.Symbols;
using Xunit;

namespace Silk.NET.SilkTouch.Scraper.Tests;
Expand All @@ -10,6 +12,25 @@ public class StructScrapingTests
[Fact]
public void StructXMLGeneratesStructSymbol()
{
var doc = new XmlDocument();
doc.LoadXml(@"<struct name=""Test""></struct>");

var symbols = new ClangScraper().ScrapeXML(doc);

var symbol = Assert.Single(symbols);
var @struct = Assert.IsType<StructSymbol>(symbol);
}

[Fact]
public void StructXMLGeneratesCorrectIdentifier()
{
var doc = new XmlDocument();
doc.LoadXml(@"<struct name=""Test""></struct>");

var symbols = new ClangScraper().ScrapeXML(doc);

var symbol = Assert.Single(symbols);
var @struct = Assert.IsType<StructSymbol>(symbol);
Assert.Equal("Test", @struct.Identifier.Value);
}
}

0 comments on commit 94036b5

Please sign in to comment.