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

Create & Test Namespace Scraping #913

Merged
merged 1 commit into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
)
};
}
}
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()
{

}
}