-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestXml.cs
180 lines (155 loc) · 4.29 KB
/
TestXml.cs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System.Diagnostics.CodeAnalysis;
using static System.Char;
namespace Wivuu.Sprog;
public class XmlDocument
{
public Node? Root;
public ParserError? Error;
public override string? ToString() => Root?.ToString();
}
public class Item;
public class Content : Item
{
public string? Text;
public override string? ToString() => Text;
}
public class Node : Item
{
public string? Name;
public IEnumerable<Item>? Children;
public override string ToString()
{
if (Children != null)
return $"<{Name}>{Children.Aggregate("", (s, c) => s + c)}</{Name}>";
return $"<{Name}/>";
}
}
public static class SprogXmlParser
{
static Parser ParseIdentifier(this Parser input, out string identifier) =>
input.Skip(IsWhiteSpace)
.Take(IsLetter, out char first)
.Take(IsLetterOrDigit, out ReadOnlySpan<char> rest)
.Assert(first != default, "\"\" is an invalid identifier")
.Let(identifier = first.Concat(rest))
.Skip(IsWhiteSpace);
static Parser ParseTag(this Parser input, out string name, out bool selfClosing) =>
input.SkipOne('<')
.ParseIdentifier(out name)
.Peek(out var nextC)
.Let(selfClosing = nextC == '/')
.SkipOne('>')
.Skip(IsWhiteSpace);
static Parser ParseEndTag(this Parser input, string name) =>
input.Skip("</")
.ParseIdentifier(out var endName)
.Assert(endName == name ? null : $"Expected end tag </{name}>")
.SkipOne('>')
.Skip(IsWhiteSpace);
static Parser ParseItems(this Parser input, out List<Item> items)
{
static bool NextItem(ref Parser i, [NotNullWhen(true)] out Item? next)
{
if (i.StartsWith("</"))
{
next = null;
return false;
}
else if (i.StartsWith('<'))
{
i = i.ParseNode(out var n);
next = n;
return true;
}
else
{
i = i.Take(static c => c != '<', out string content);
next = new Content { Text = content };
return true;
}
}
items = [];
while (NextItem(ref input, out var next))
items.Add(next);
return input;
}
static Parser ParseNode(this Parser input, out Node n) =>
input.ParseTag(out var id, out var selfClosing)
.Rest(out var rest)
.Let(selfClosing ? rest.Let(n = new () { Name = id })
: rest.ParseItems(out var children)
.Let(n = new () { Name = id, Children = children })
.ParseEndTag(id));
public static bool TryParse(string xml, out XmlDocument document)
{
try
{
new Parser(xml)
.Skip(IsWhiteSpace)
.ParseNode(out var n);
document = new() { Root = n };
return true;
}
catch (ParserException e)
{
document = new()
{
Error = new (e, xml)
};
return false;
}
}
}
public class TestXml
{
readonly string[] GoodXml =
{
"""
<ul>
<li>Item 1</li>
<li>
<ul>
<li>Item 2.1</li>
<li>Item 2.2</li>
<li>Item 2.3</li>
</ul>
</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
"""
};
readonly string[] BadXml =
{
"""
<ul>
<li>Item 4</lli>
</ul>
""",
"""
<ul>
<li>Item 4
</ul>
""",
"""
<ul>
<>Item 4</>
</ul>
"""
};
[Fact]
public void TestGoodXml()
{
XmlDocument doc;
foreach (var good in GoodXml)
Assert.True(SprogXmlParser.TryParse(good, out doc));
}
[Fact]
public void TestBadXml()
{
XmlDocument doc;
foreach (var bad in BadXml)
Assert.False(SprogXmlParser.TryParse(bad, out doc));
}
}