-
-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathXmlVisitor.cs
More file actions
71 lines (65 loc) · 2.1 KB
/
XmlVisitor.cs
File metadata and controls
71 lines (65 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
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
{
public IEnumerable<Symbol> Visit(XmlNode node)
{
switch (node)
{
case XmlElement { Name: "bindings" } bindings:
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);
}
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()
)
};
}
}