-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathTurtleDocument.cs
More file actions
1010 lines (847 loc) · 23.1 KB
/
TurtleDocument.cs
File metadata and controls
1010 lines (847 loc) · 23.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using Waher.Content.Getters;
using Waher.Content.Semantic.Model;
using Waher.Content.Semantic.Model.Literals;
using Waher.Content.Semantic.Ontologies;
using Waher.Runtime.Collections;
using Waher.Runtime.Inventory;
namespace Waher.Content.Semantic
{
/// <summary>
/// How blank node IDs are generated
/// </summary>
public enum BlankNodeIdMode
{
/// <summary>
/// Sequentially
/// </summary>
Sequential,
/// <summary>
/// Using GUIDs
/// </summary>
Guid
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// https://www.w3.org/TR/rdf12-turtle/
/// https://w3c.github.io/rdf-star/cg-spec/20 21-12-17.html
/// </summary>
public class TurtleDocument : InMemorySemanticCube, IWebServerMetaContent
{
private readonly Dictionary<string, string> namespaces = new Dictionary<string, string>
{
{ "xsd", XmlSchema.Namespace },
{ "ttl", "http://www.w3.org/2008/turtle#" }
};
private readonly Dictionary<string, ISemanticLiteral> dataTypes = new Dictionary<string, ISemanticLiteral>();
private readonly string text;
private readonly string blankNodeIdPrefix;
private readonly int len;
private readonly BlankNodeIdMode blankNodeIdMode;
private DateTimeOffset? date = null;
private Uri baseUri = null;
private int blankNodeIndex = 0;
private int pos = 0;
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Text">Text content of Turtle document.</param>
public TurtleDocument(string Text)
: this(Text, null)
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Text">Text content of Turtle document.</param>
/// <param name="BaseUri">Base URI</param>
public TurtleDocument(string Text, Uri BaseUri)
: this(Text, BaseUri, "n")
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Text">Text content of Turtle document.</param>
/// <param name="BaseUri">Base URI</param>
/// <param name="BlankNodeIdPrefix">Prefix to use when creating blank nodes.</param>
public TurtleDocument(string Text, Uri BaseUri, string BlankNodeIdPrefix)
: this(Text, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode.Sequential)
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Text">Text content of Turtle document.</param>
/// <param name="BaseUri">Base URI</param>
/// <param name="BlankNodeIdPrefix">Prefix to use when creating blank nodes.</param>
/// <param name="BlankNodeIdMode">How Blank Node IDs are generated</param>
public TurtleDocument(string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
{
this.text = Text;
this.len = this.text.Length;
this.baseUri = BaseUri;
this.blankNodeIdPrefix = BlankNodeIdPrefix;
this.blankNodeIdMode = BlankNodeIdMode;
if (!(this.baseUri is null))
this.namespaces[string.Empty] = this.baseUri.ToString();
this.ParseTriples();
}
/// <summary>
/// Original text of document.
/// </summary>
public string Text => this.text;
/// <summary>
/// Server timestamp of document.
/// </summary>
public DateTimeOffset? Date => this.date;
private void ParseTriples()
{
this.ParseTriples(null);
if (this.pos < this.len)
throw this.ParsingException("Unexpected end of document.");
}
private void ParseTriples(ISemanticElement Subject)
{
ISemanticElement Predicate = null;
ISemanticElement Object;
int TriplePosition = Subject is null ? 0 : 1;
bool InBlankNode = !(Subject is null);
while (this.pos < this.len)
{
Object = this.ParseElement(TriplePosition,
out ChunkedList<ISemanticTriple> AdditionalTriples);
if (Object is null)
{
if (Subject is null)
return;
else if (Predicate is null)
{
if (InBlankNode)
return;
else
throw this.ParsingException("Expected predicate.");
}
else
throw this.ParsingException("Expected object.");
}
if (Subject is null)
{
Subject = Object;
TriplePosition++;
if (!(AdditionalTriples is null))
this.triples.AddRange(AdditionalTriples);
}
else if (Predicate is null)
{
Predicate = Object;
TriplePosition++;
if (!(AdditionalTriples is null))
this.triples.AddRange(AdditionalTriples);
}
else
{
this.Add(new SemanticTriple(Subject, Predicate, Object));
if (!(AdditionalTriples is null))
this.triples.AddRange(AdditionalTriples);
switch (this.NextNonWhitespaceChar())
{
case '.':
if (InBlankNode)
throw this.ParsingException("Expected ]");
Subject = null;
Predicate = null;
TriplePosition = 0;
break;
case ';':
Predicate = null;
TriplePosition = 1;
this.SkipWhiteSpace();
if (this.PeekNextChar() == '.')
{
if (InBlankNode)
throw this.ParsingException("Expected ]");
this.pos++;
Subject = null;
TriplePosition = 0;
}
break;
case ',':
break;
case ']':
case '|':
return;
default:
if (InBlankNode)
throw this.ParsingException("Expected triple delimiter ] ; or ,");
else
throw this.ParsingException("Expected triple delimiter . ; or ,");
}
}
}
}
/// <summary>
/// Adds a triple to the cube.
/// </summary>
/// <param name="Triple">Semantic triple.</param>
public override void Add(ISemanticTriple Triple)
{
base.Add(Triple);
this.SkipWhiteSpace();
if (this.PeekNextChars(2) == "{|")
{
this.pos += 2;
if (!(Triple is SemanticTriple T))
T = new SemanticTriple(Triple.Subject, Triple.Predicate, Triple.Object);
this.ParseTriples(T);
this.SkipWhiteSpace();
if (this.NextChar() != '}')
throw this.ParsingException("Expected }");
}
}
private ISemanticElement ParseElement(int TriplePosition,
out ChunkedList<ISemanticTriple> AdditionalTriples)
{
AdditionalTriples = null;
while (true)
{
char ch = this.NextNonWhitespaceChar();
switch (ch)
{
case (char)0:
return null;
case '@':
string s = this.ParseName();
switch (s.ToLower())
{
case "base":
ch = this.NextNonWhitespaceChar();
if (ch != '<')
throw this.ParsingException("Expected <");
this.baseUri = this.ParseUri().Uri;
if (this.NextNonWhitespaceChar() != '.')
throw this.ParsingException("Expected .");
break;
case "prefix":
this.SkipWhiteSpace();
s = this.ParseName();
if (this.NextNonWhitespaceChar() != ':')
throw this.ParsingException("Expected :");
if (this.NextNonWhitespaceChar() != '<')
throw this.ParsingException("Expected <");
this.namespaces[s] = this.ParseUri().Uri.ToString();
if (this.NextNonWhitespaceChar() != '.')
throw this.ParsingException("Expected .");
break;
default:
throw this.ParsingException("Unrecognized keyword.");
}
break;
case '[':
switch (TriplePosition)
{
case 0:
BlankNode Node = this.CreateBlankNode();
this.ParseTriples(Node);
this.SkipWhiteSpace();
if (this.PeekNextChar() == '.')
{
this.pos++;
ISemanticElement Result = this.ParseElement(0, out AdditionalTriples);
if (!(AdditionalTriples is null))
this.triples.AddRange(AdditionalTriples);
return Result;
}
return Node;
case 1:
throw this.ParsingException("Predicate cannot be a blank node.");
case 2:
ChunkedList<ISemanticTriple> Bak = this.triples;
this.triples = AdditionalTriples = new ChunkedList<ISemanticTriple>();
Node = this.CreateBlankNode();
this.ParseTriples(Node);
this.triples = Bak;
return Node;
default:
throw this.ParsingException("Unrecognized triple position.");
}
case '(':
return this.ParseCollection(out AdditionalTriples);
case ']':
return null;
case '<':
if (this.PeekNextChar() == '<') // Quoted triples, part of RDF-star
{
this.pos++;
ISemanticElement Subject = this.ParseElement(0, out ChunkedList<ISemanticTriple> AdditionalTriples1);
ISemanticElement Predicate = this.ParseElement(1, out ChunkedList<ISemanticTriple> AdditionalTriples2);
ISemanticElement Object = this.ParseElement(2, out ChunkedList<ISemanticTriple> AdditionalTriples3);
if (this.NextNonWhitespaceChar() != '>')
throw this.ParsingException("Expected >");
if (this.NextNonWhitespaceChar() != '>')
throw this.ParsingException("Expected >");
if (!(AdditionalTriples1 is null))
this.triples.AddRange(AdditionalTriples1);
if (!(AdditionalTriples2 is null))
this.triples.AddRange(AdditionalTriples2);
if (!(AdditionalTriples3 is null))
this.triples.AddRange(AdditionalTriples3);
return new SemanticTriple(Subject, Predicate, Object);
}
else
return this.ParseUri();
case '"':
case '\'':
if (TriplePosition != 2)
throw this.ParsingException("Literals can only occur in object position.");
if (this.pos < this.len - 1 && this.text[this.pos] == ch && this.text[this.pos + 1] == ch)
{
this.pos += 2;
s = this.ParseString(ch, true, true);
}
else
s = this.ParseString(ch, false, true);
string Language = null;
if (this.pos < this.len && this.text[this.pos] == '@')
{
this.pos++;
Language = this.ParseName();
}
if (this.pos < this.len - 1 && this.text[this.pos] == '^' && this.text[this.pos + 1] == '^')
{
this.pos += 2;
string DataType = this.ParseUriOrPrefixedToken().Uri.ToString();
if (!this.dataTypes.TryGetValue(DataType, out ISemanticLiteral LiteralType))
{
LiteralType = Types.FindBest<ISemanticLiteral, string>(DataType)
?? new CustomLiteral(string.Empty, DataType);
this.dataTypes[DataType] = LiteralType;
}
return LiteralType.Parse(s, DataType, Language);
}
else if (!string.IsNullOrEmpty(Language))
return new StringLiteral(s, Language);
else
return new StringLiteral(s);
case ':':
return this.ParsePrefixedToken(string.Empty);
default:
if (char.IsWhiteSpace(ch))
break;
if (ch == '_')
{
if (this.NextNonWhitespaceChar() != ':')
throw this.ParsingException("Expected :");
return new BlankNode(this.ParseName());
}
else if (char.IsLetter(ch) || ch == ':')
{
this.pos--;
s = this.ParseName();
if (this.PeekNextChar() == ':')
{
this.pos++;
return this.ParsePrefixedToken(s);
}
switch (s)
{
case "a":
if (TriplePosition == 1)
return RdfDocument.RdfType;
break;
case "true":
if (TriplePosition == 2)
return new BooleanLiteral(true);
break;
case "false":
if (TriplePosition == 2)
return new BooleanLiteral(false);
break;
}
throw this.ParsingException("Expected :");
}
else
{
if (TriplePosition != 2)
throw this.ParsingException("Literals can only occur in object position.");
this.pos--;
return this.ParseNumber();
}
}
}
}
private BlankNode CreateBlankNode()
{
if (this.blankNodeIdMode == BlankNodeIdMode.Guid)
return new BlankNode(this.blankNodeIdPrefix + Guid.NewGuid().ToString());
else
return new BlankNode(this.blankNodeIdPrefix + (++this.blankNodeIndex).ToString());
}
private ISemanticElement ParseCollection(out ChunkedList<ISemanticTriple> AdditionalTriples)
{
ChunkedList<ISemanticElement> Elements = null;
AdditionalTriples = null;
this.SkipWhiteSpace();
while (this.pos < this.len)
{
if (this.text[this.pos] == ')')
{
this.pos++;
if (Elements is null)
return RdfDocument.RdfNil;
ChunkNode<ISemanticElement> Loop = Elements.FirstChunk;
BlankNode Result = this.CreateBlankNode();
BlankNode Current = Result;
int i, c;
while (!(Loop is null))
{
for (i = Loop.Start, c = Loop.Pos; i < c; i++)
{
this.Add(new SemanticTriple(Current, RdfDocument.RdfFirst, Loop[i]));
if (i < c - 1 || !(Loop.Next is null))
{
BlankNode Next = this.CreateBlankNode();
this.Add(new SemanticTriple(Current, RdfDocument.RdfRest, Next));
Current = Next;
}
}
Loop = Loop.Next;
}
this.Add(new SemanticTriple(Current, RdfDocument.RdfRest, RdfDocument.RdfNil));
return Result;
}
ISemanticElement Element = this.ParseElement(2,
out ChunkedList<ISemanticTriple> AdditionalTriples2);
if (!(AdditionalTriples2 is null))
{
if (AdditionalTriples is null)
AdditionalTriples = new ChunkedList<ISemanticTriple>();
AdditionalTriples.AddRange(AdditionalTriples2);
}
if (Element is null)
break;
if (Elements is null)
Elements = new ChunkedList<ISemanticElement>();
Elements.Add(Element);
this.SkipWhiteSpace();
}
throw this.ParsingException("Expected )");
}
private UriNode ParseUriOrPrefixedToken()
{
if (this.pos >= this.len)
throw this.ParsingException("Expected URI or prefixed token.");
if (this.text[this.pos] == '<')
{
this.pos++;
return this.ParseUri();
}
string Prefix = this.ParseName();
if (this.NextChar() != ':')
throw this.ParsingException("Expected :");
return this.ParsePrefixedToken(Prefix);
}
private UriNode ParsePrefixedToken(string Prefix)
{
if (!this.namespaces.TryGetValue(Prefix, out string Namespace))
throw this.ParsingException("Prefix unknown.");
this.SkipWhiteSpace();
string LocalName = this.ParseName();
return new UriNode(new Uri(Namespace + LocalName), Prefix + ":" + LocalName);
}
private string ParseName()
{
if (!IsNameStartChar(this.PeekNextChar()))
return string.Empty;
int Start = this.pos++;
bool LastPeriod = false;
while (IsNameChar(this.PeekNextChar(), ref LastPeriod))
this.pos++;
if (LastPeriod)
this.pos--;
return this.text.Substring(Start, this.pos - Start);
}
/// <summary>
/// Checks if a character is a character that can start a name.
/// </summary>
/// <param name="ch">Character</param>
/// <returns>If the character can start a name.</returns>
public static bool IsNameStartChar(char ch)
{
if (ch < 'A')
return false;
else if (ch <= 'Z')
return true;
else if (ch < '_')
return false;
else if (ch == '_')
return true;
else if (ch < 'a')
return false;
else if (ch <= 'z')
return true;
else if (ch < '\xc0')
return false;
else if (ch <= '\xd6')
return true;
else if (ch < '\xd8')
return false;
else if (ch <= '\xf6')
return true;
else if (ch < '\xf8')
return false;
else if (ch <= '\x02ff')
return true;
else if (ch < '\x0370')
return false;
else if (ch <= '\x037d')
return true;
else if (ch < '\x037f')
return false;
else if (ch <= '\x1fff')
return true;
else if (ch < '\x037f')
return false;
else if (ch <= '\x1fff')
return true;
else if (ch < '\x200c')
return false;
else if (ch <= '\x200d')
return true;
else if (ch < '\x2070')
return false;
else if (ch <= '\x218f')
return true;
else if (ch < '\x2C00')
return false;
else if (ch <= '\x2FEF')
return true;
else if (ch < '\x3001')
return false;
else if (ch <= '\xD7FF')
return true;
else if (ch < '\xF900')
return false;
else if (ch <= '\xFDCF')
return true;
else if (ch < '\xFDF0')
return false;
else if (ch <= '\xFFFD')
return true;
else
return false;
}
/// <summary>
/// Checks if a character can be included in a name.
/// </summary>
/// <param name="ch">Character</param>
/// <param name="LastPeriod">If the last character was a period character.</param>
/// <returns>If characters can be included in a name.</returns>
public static bool IsNameChar(char ch, ref bool LastPeriod)
{
if (IsNameStartChar(ch))
{
LastPeriod = false;
return true;
}
else if (ch < '-')
return false;
else if (ch == '-')
{
LastPeriod = false;
return true;
}
else if (ch == '.')
{
LastPeriod = true;
return true;
}
else if (ch < '0')
return false;
else if (ch <= '9')
{
LastPeriod = false;
return true;
}
else if (ch < '\x00B7')
return false;
else if (ch == '\x00B7')
{
LastPeriod = false;
return true;
}
else if (ch < '\x0300')
return false;
else if (ch <= '\x036F')
{
LastPeriod = false;
return true;
}
else if (ch < '\x203F')
return false;
else if (ch <= '\x2040')
{
LastPeriod = false;
return true;
}
else
return false;
}
private SemanticLiteral ParseNumber()
{
int Start = this.pos;
char ch = this.PeekNextChar();
bool HasDigits = false;
bool HasDecimal = false;
bool HasExponent = false;
if (ch == '+' || ch == '-')
{
this.pos++;
ch = this.PeekNextChar();
}
while (char.IsDigit(ch))
{
this.pos++;
ch = this.PeekNextChar();
HasDigits = true;
}
if (ch == '.')
{
HasDecimal = true;
this.pos++;
ch = this.PeekNextChar();
while (char.IsDigit(ch))
{
this.pos++;
ch = this.PeekNextChar();
}
}
if (ch == 'e' || ch == 'E')
{
HasExponent = true;
this.pos++;
ch = this.PeekNextChar();
if (ch == '+' || ch == '-')
{
this.pos++;
ch = this.PeekNextChar();
}
while (char.IsDigit(ch))
{
this.pos++;
ch = this.PeekNextChar();
}
}
if (this.pos > Start)
{
string s = this.text.Substring(Start, this.pos - Start);
if (HasExponent)
{
if (CommonTypes.TryParse(s, out double dbl))
return new DoubleLiteral(dbl, s);
else
throw this.ParsingException("Invalid double number.");
}
else if (HasDecimal)
{
if (CommonTypes.TryParse(s, out decimal dec))
return new DecimalLiteral(dec, s);
else
throw this.ParsingException("Invalid decimal number.");
}
else if (HasDigits)
{
if (BigInteger.TryParse(s, out BigInteger bi))
return new IntegerLiteral(bi, s);
else
throw this.ParsingException("Invalid integer number.");
}
}
throw this.ParsingException("Expected value element.");
}
private string ParseString(char EndChar, bool MultiLine, bool IncludeWhiteSpace)
{
StringBuilder sb = null;
int Start = this.pos;
char ch;
while ((ch = this.PeekNextChar()) != (char)0)
{
this.pos++;
if (ch == EndChar)
{
if (MultiLine)
{
if (this.pos < this.len - 1 && this.text[this.pos] == EndChar && this.text[this.pos + 1] == EndChar)
{
this.pos += 2;
return sb?.ToString() ?? this.text.Substring(Start, this.pos - Start - 3);
}
else
sb?.Append(ch);
}
else
return sb?.ToString() ?? this.text.Substring(Start, this.pos - Start - 1);
}
else if (ch == '\\')
{
if (sb is null)
{
sb = new StringBuilder();
if (this.pos > Start + 1)
sb.Append(this.text.Substring(Start, this.pos - Start - 1));
}
switch (ch = this.NextChar())
{
case (char)0:
throw this.ParsingException("Expected escape code.");
case 't':
sb.Append('\t');
break;
case 'n':
sb.Append('\n');
break;
case 'r':
sb.Append('\r');
break;
case 'v':
sb.Append('\v');
break;
case 'f':
sb.Append('\f');
break;
case 'b':
sb.Append('\b');
break;
case 'a':
sb.Append('\a');
break;
case 'u':
if (this.pos < this.len - 3 && int.TryParse(this.text.Substring(this.pos, 4), System.Globalization.NumberStyles.HexNumber, null, out int i))
{
sb.Append((char)i);
this.pos += 4;
}
else
throw this.ParsingException("Expected 4-character hexadecimal code.");
break;
case 'U':
if (this.pos < this.len - 7 && int.TryParse(this.text.Substring(this.pos, 8), System.Globalization.NumberStyles.HexNumber, null, out i))
{
sb.Append((char)i);
this.pos += 8;
}
else
throw this.ParsingException("Expected 8-character hexadecimal code.");
break;
default:
sb.Append(ch);
break;
}
}
else if (IncludeWhiteSpace || !char.IsWhiteSpace(ch))
sb?.Append(ch);
}
throw this.ParsingException("Expected " + new string(EndChar, MultiLine ? 3 : 1));
}
private ParsingException ParsingException(string Message)
{
return new ParsingException(Message, this.text, this.pos);
}
private UriNode ParseUri()
{
string Short = this.ParseString('>', false, false);
if (this.baseUri is null)
{
if (Uri.TryCreate(Short, UriKind.RelativeOrAbsolute, out Uri URI))
return new UriNode(URI, Short);
else
throw this.ParsingException("Invalid URI.");
}
else
{
if (string.IsNullOrEmpty(Short))
return new UriNode(this.baseUri, Short);
else if (Uri.TryCreate(this.baseUri, Short, out Uri URI))
return new UriNode(URI, Short);
else
throw this.ParsingException("Invalid URI.");
}
}
private void SkipLine()
{
char ch;
while ((ch = this.PeekNextChar()) != '\r' && ch != '\n' && ch != 0)
this.pos++;
}
private void SkipWhiteSpace()
{
char ch;
while (true)
{
ch = this.PeekNextChar();
if (char.IsWhiteSpace(ch))
this.pos++;
else if (ch == '#')
this.SkipLine();
else
break;
}
}
private char PeekNextChar()
{
if (this.pos < this.len)
return this.text[this.pos];
else
return (char)0;
}
private string PeekNextChars(int NrChars)
{
if (this.pos + NrChars <= this.len)
return this.text.Substring(this.pos, NrChars);
else
return string.Empty;
}
private char NextChar()
{
if (this.pos < this.len)
{
char ch = this.text[this.pos++];
while (ch == '#')
{
this.SkipLine();
if (this.pos < this.len)
ch = this.text[this.pos++];
else
return (char)0;
}
return ch;
}
else
return (char)0;
}
private char NextNonWhitespaceChar()
{
char ch;
do
{
ch = this.NextChar();
}
while (ch != 0 && char.IsWhiteSpace(ch));
return ch;
}
/// <summary>