Skip to content

Commit

Permalink
Create & Test Namespace Scraping
Browse files Browse the repository at this point in the history
  • Loading branch information
HurricanKai committed May 7, 2022
1 parent 5e609aa commit 202461a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
4 changes: 1 addition & 3 deletions src/generators/Silk.NET.SilkTouch.Scraper/ClangScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public IEnumerable<Symbol> ScrapeXML(XmlDocument document)
}

var visitor = new XmlVisitor();
visitor.Visit(bindings);

return visitor.Symbols;
return visitor.Visit(bindings);
}

/// <summary>
Expand Down
46 changes: 33 additions & 13 deletions src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,55 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection.Metadata;
using System.Xml;
using Silk.NET.SilkTouch.Symbols;

namespace Silk.NET.SilkTouch.Scraper;

internal sealed class XmlVisitor
{
private List<Symbol> _symbols = new();

public IEnumerable<Symbol> Symbols => _symbols;

public void Visit(XmlNode node)
public IEnumerable<Symbol> Visit(XmlNode node)
{
switch (node)
{
case XmlElement { Name: "bindings" } bindings:
{
foreach (var child in bindings.ChildNodes.Cast<XmlNode>())
{
if (child is null) continue;
Visit(child);
}
break;
}
return VisitBinding(bindings);
case XmlElement { Name: "namespace" } @namespace:
return VisitNamespace(@namespace);
default:
{
throw new NotImplementedException();
}
}
}

private IEnumerable<Symbol> VisitBinding(XmlElement bindings)
{
return bindings.ChildNodes.Cast<XmlNode>().Where(x => x is not null).SelectMany(Visit);
}

private IEnumerable<Symbol> VisitNamespace(XmlElement @namespace)
{
return new[]
{
new NamespaceSymbol
(
new IdentifierSymbol(@namespace.Attributes?["name"]?.Value ?? throw new InvalidOperationException()),
@namespace.ChildNodes.Cast<XmlNode>()
.Select(Visit)
.Select
(
x =>
{
if (x is not TypeSymbol ts) throw new InvalidOperationException();
return ts;
}
)
.ToImmutableArray()
)
};
}
}
28 changes: 28 additions & 0 deletions tests/Silk.NET.SilkTouch.Scraper.Tests/NamespaceScrapingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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;

public class NamespaceScrapingTests
{
[Fact]
public void NamespaceXMLGeneratesNamespaceSymbol()
{
var doc = new XmlDocument();
doc.LoadXml(@"<bindings>
<namespace name=""" + ClangScraper.LibraryNamespacePlaceholder + @""">
</namespace>
</bindings>
");

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

var symbol = Assert.Single(symbols);
var @namespace = Assert.IsType<NamespaceSymbol>(symbol);
Assert.Equal(ClangScraper.LibraryNamespacePlaceholder, @namespace.Identifier.Value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class StructScrapingTests
[Fact]
public void StructXMLGeneratesStructSymbol()
{

}
}

0 comments on commit 202461a

Please sign in to comment.