-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathXmlParser.cs
More file actions
343 lines (269 loc) · 8.83 KB
/
XmlParser.cs
File metadata and controls
343 lines (269 loc) · 8.83 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System;
using Waher.Runtime.Collections;
using Waher.Script.Abstraction.Elements;
using Waher.Script.Model;
using Waher.Script.Xml.Model;
namespace Waher.Script.Xml
{
/// <summary>
/// Parses an XML document
/// </summary>
public class XmlParser : IKeyWord
{
/// <summary>
/// Parses an XML document
/// </summary>
public XmlParser()
{
}
/// <summary>
/// Keyword associated with custom parser.
/// </summary>
public string KeyWord => "<";
/// <summary>
/// Keyword aliases, if available, null if none.
/// </summary>
public string[] Aliases => null;
/// <summary>
/// Any keywords used internally by the custom parser.
/// </summary>
public string[] InternalKeywords => Array.Empty<string>();
/// <summary>
/// Tries to parse a script node.
/// </summary>
/// <param name="Parser">Custom parser.</param>
/// <param name="Result">Parsed Script Node.</param>
/// <returns>If successful in parsing a script node.</returns>
public bool TryParse(ScriptParser Parser, out ScriptNode Result)
{
Result = this.ParseDocument(Parser);
return !(Result is null);
}
private XmlScriptDocument ParseDocument(ScriptParser Parser)
{
ChunkedList<XmlScriptProcessingInstruction> ProcessingInstructions = null;
int Start = Parser.Start;
int Pos;
char ch;
while (Parser.PeekNextChar() == '?')
{
Parser.NextChar();
Pos = Parser.Position;
while ((ch = Parser.NextChar()) != '?' && ch != 0)
;
if (ch == 0)
return null;
if (ProcessingInstructions is null)
ProcessingInstructions = new ChunkedList<XmlScriptProcessingInstruction>();
string Text = Parser.Expression.Script.Substring(Pos, Parser.Position - Pos - 1);
if (Parser.NextChar() != '>')
throw Parser.SyntaxError("> expected.");
ProcessingInstructions.Add(new XmlScriptProcessingInstruction(Text,
Start, Parser.Position - Start, Parser.Expression));
Parser.SkipWhiteSpace();
if (Parser.NextChar() != '<')
return null;
Start = Parser.Position;
}
Parser.UndoChar();
XmlScriptElement Root = this.ParseElement(Parser);
if (Root is null)
return null;
return new XmlScriptDocument(Root, ProcessingInstructions?.ToArray() ?? Array.Empty<XmlScriptProcessingInstruction>(),
Start, Parser.Position - Start, Parser.Expression);
}
private XmlScriptElement ParseElement(ScriptParser Parser)
{
if (Parser.NextChar() != '<')
throw Parser.SyntaxError("< expected.");
ChunkedList<XmlScriptAttribute> Attributes = null;
XmlScriptAttribute Xmlns = null;
IElement ElementValue;
XmlScriptAttribute Attribute;
int ElementStart = Parser.Position;
string ElementName = ParseName(Parser);
string Value;
char ch;
Parser.SkipWhiteSpace();
while ((ch = Parser.PeekNextChar()) != 0)
{
if (ch == '>')
{
Parser.NextChar();
XmlScriptElement Element = new XmlScriptElement(ElementName, Xmlns, Attributes?.ToArray() ?? Array.Empty<XmlScriptAttribute>(),
ElementStart, Parser.Position - ElementStart, Parser.Expression);
XmlScriptWildcard LastWildCard = null;
XmlScriptNode NewChild;
while (true)
{
int Pos = Parser.Position;
int i = Parser.Expression.Script.IndexOf('<', Pos);
int Len;
if (i < 0)
throw Parser.SyntaxError("Open element.");
if (i > Pos)
{
Len = i - Pos;
Element.Add(NewChild = new XmlScriptText(Parser.Expression.Script.Substring(Pos, Len),
Pos, Len, Parser.Expression));
Parser.SkipChars(Len);
if (!(LastWildCard is null) && !NewChild.IsWhitespace)
{
LastWildCard.Next = NewChild;
LastWildCard = null;
}
}
NewChild = null;
switch (Parser.PeekNextChars(2))
{
case "</":
Parser.NextChar();
Parser.NextChar();
if (ParseName(Parser) != ElementName)
throw Parser.SyntaxError("Expected end of element " + ElementName);
if (Parser.NextChar() != '>')
throw Parser.SyntaxError("> expected.");
return Element;
case "<!":
if (Parser.IsNextChars("<!--"))
{
Parser.SkipChars(4);
Pos = Parser.Position;
i = Parser.Expression.Script.IndexOf("-->", Pos);
if (i < 0)
throw Parser.SyntaxError("Unterminated comment.");
Len = i - Pos;
Element.Add(NewChild = new XmlScriptComment(Parser.Expression.Script.Substring(Pos, Len),
Pos, Len, Parser.Expression));
Parser.SkipChars(Len + 3);
}
else if (Parser.IsNextChars("<![CDATA["))
{
Parser.SkipChars(9);
Pos = Parser.Position;
i = Parser.Expression.Script.IndexOf("]]>", Pos);
if (i < 0)
throw Parser.SyntaxError("Unterminated CDATA construct.");
Len = i - Pos;
Element.Add(NewChild = new XmlScriptCData(Parser.Expression.Script.Substring(Pos, Len),
Pos, Len, Parser.Expression));
Parser.SkipChars(Len + 3);
}
else
throw Parser.SyntaxError("Expected <!-- or <![CDATA[");
break;
case "<[":
Parser.SkipChars(2);
ScriptNode Node = Parser.ParseSequence();
Parser.SkipWhiteSpace();
if (!Parser.IsNextChars("]>"))
throw Parser.SyntaxError("]> expected.");
Parser.SkipChars(2);
if (!(Node is null))
Element.Add(NewChild = new XmlScriptValue(Node, Node.Start, Node.Length, Node.Expression));
break;
case "<(":
Parser.SkipChars(2);
Node = Parser.ParseSequence();
Parser.SkipWhiteSpace();
if (!Parser.IsNextChars(")>"))
throw Parser.SyntaxError(")> expected.");
Parser.SkipChars(2);
if (!(Node is null))
Element.Add(NewChild = new XmlScriptValue(Node, Node.Start, Node.Length, Node.Expression));
break;
case "<*":
Parser.SkipChars(2);
if (Parser.PeekNextChar() != '>')
throw Parser.SyntaxError("> expected.");
Parser.NextChar();
if (LastWildCard is null)
Element.Add(LastWildCard = new XmlScriptWildcard(Pos, Parser.Position - Pos, Parser.Expression));
continue;
default:
Element.Add(NewChild = this.ParseElement(Parser));
break;
}
if (!(LastWildCard is null) && !(NewChild is null) && !NewChild.IsWhitespace)
{
LastWildCard.Next = NewChild;
LastWildCard = null;
}
}
}
else if (ch == '/')
{
Parser.NextChar();
if (Parser.NextChar() != '>')
throw Parser.SyntaxError("> expected.");
return new XmlScriptElement(ElementName, Xmlns, Attributes?.ToArray() ?? Array.Empty<XmlScriptAttribute>(),
ElementStart, Parser.Position - ElementStart, Parser.Expression);
}
else if (char.IsLetter(ch) || ch == '_' || ch == ':')
{
int AttributeStart = Parser.Position;
string AttributeName = ParseName(Parser);
Parser.SkipWhiteSpace();
if (Parser.NextChar() != '=')
throw Parser.SyntaxError("= expected.");
bool Bak = Parser.CanSkipWhitespace;
Parser.CanSkipWhitespace = false;
ScriptNode Node = Parser.ParsePowers();
Parser.CanSkipWhitespace = Bak;
if (Node is ConstantElement Constant)
{
ElementValue = Constant.Constant;
Value = XmlScriptNode.EvaluateString(ElementValue);
Attribute = new XmlScriptAttributeString(AttributeName, Value,
AttributeStart, Parser.Position - AttributeStart, Parser.Expression);
}
else
{
Attribute = new XmlScriptAttributeScript(AttributeName, Node,
AttributeStart, Parser.Position - AttributeStart, Parser.Expression);
}
if (AttributeName == "xmlns")
Xmlns = Attribute;
else
{
if (Attributes is null)
Attributes = new ChunkedList<XmlScriptAttribute>();
Attributes.Add(Attribute);
}
}
else if (ch == '*')
{
int AttributeStart = Parser.Position;
Parser.NextChar();
if (Attributes is null)
Attributes = new ChunkedList<XmlScriptAttribute>();
Attributes.Add(new XmlScriptAttributeWildcard(AttributeStart,
Parser.Position - AttributeStart, Parser.Expression));
}
else if (char.IsWhiteSpace(ch))
Parser.NextChar();
else
throw Parser.SyntaxError("Invalid XML Element.");
}
return null;
}
private static string ParseName(ScriptParser Parser)
{
int Pos = Parser.Position;
char ch = Parser.PeekNextChar();
if (!(char.IsLetter(ch) || ch == '_' || ch == ':'))
throw Parser.SyntaxError("Name expected.");
while (Parser.NextChar() != 0)
{
ch = Parser.PeekNextChar();
if (char.IsLetterOrDigit(ch) || char.IsDigit(ch) ||
ch == '.' || ch == '-' || ch == '_' || ch == ':')
{
continue;
}
break;
}
return Parser.Expression.Script.Substring(Pos, Parser.Position - Pos);
}
}
}