-
Notifications
You must be signed in to change notification settings - Fork 6
/
Tokens.cs
926 lines (879 loc) · 28 KB
/
Tokens.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
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Noxico
{
/// <summary>
/// Base class for all things Token.
/// </summary>
public class TokenCarrier
{
/// <summary>
/// The child Tokens for this Token.
/// </summary>
#if DEBUG
//Doesn't work quite right yet.
[System.ComponentModel.Editor(typeof(TokenEditor), typeof(System.Drawing.Design.UITypeEditor))]
#endif
public List<Token> Tokens { get; private set; }
/// <summary>
/// Initializes a new TokenCarrier.
/// </summary>
public TokenCarrier()
{
Tokens = new List<Token>();
}
/// <summary>
/// Checks to see if this Token has a child Token with the specified name.
/// </summary>
/// <param name="name">The Token to check for.</param>
/// <returns>Returns true if the Token exists, false otherwise.</returns>
public bool HasToken(string name)
{
var t = Tokens.Find(x => x.Name == name);
return t != null;
}
/// <summary>
/// Returns the first child Token with the specified name.
/// </summary>
/// <param name="name">The Token to return.</param>
/// <returns>Returns the Token or null.</returns>
public Token GetToken(string name)
{
var t = Tokens.Find(x => x.Name == name);
return t;
}
public IEnumerable<Token> GetAll(string name)
{
return Tokens.Where(t => t.Name == name);
}
/// <summary>
/// Adds a new child Token with the specified name, value, and text.
/// </summary>
/// <param name="name">The name of the Token to add.</param>
/// <param name="value">The value to assign to the new Token.</param>
/// <param name="text">The text to assign to the new Token.</param>
/// <returns>Returns the Token that was added.</returns>
public Token AddToken(string name, float value, string text)
{
var t = new Token(name, value, text);
Tokens.Add(t);
return t;
}
/// <summary>
/// Adds a new child Token with the specified name and text.
/// </summary>
/// <param name="name">The name of the Token to add.</param>
/// <param name="text">The text to assign to the new Token.</param>
/// <returns>Returns the Token that was added.</returns>
public Token AddToken(string name, string text)
{
var t = new Token(name, text);
Tokens.Add(t);
return t;
}
/// <summary>
/// Adds a new child Token with the specified name and value.
/// </summary>
/// <param name="name">The name of the Token to add.</param>
/// <param name="value">The value to assign to the new Token.</param>
/// <returns>Returns the Token that was added.</returns>
public Token AddToken(string name, float value)
{
var t = new Token(name, value);
Tokens.Add(t);
return t;
}
/// <summary>
/// Adds a new child Token with the specified name.
/// </summary>
/// <param name="name">The name of the Token to add.</param>
/// <returns>Returns the Token that was added.</returns>
public Token AddToken(string name)
{
var t = new Token(name);
Tokens.Add(t);
return t;
}
/// <summary>
/// Adds a Token to this Token's children.
/// </summary>
/// <param name="t">The Token to add.</param>
/// <returns>Returns the Token that was added.</returns>
public Token AddToken(Token t)
{
Tokens.Add(t);
return t;
}
/// <summary>
/// Finds the first token with the specified name and removes it.
/// </summary>
/// <param name="name">The name of the Token to remove.</param>
/// <returns>Returns the Token that was removed, or null if there was none.</returns>
public Token RemoveToken(string name)
{
var t = Tokens.Find(x => x.Name == name);
if (t != null)
Tokens.Remove(t);
return t;
}
/// <summary>
/// Finds the first token with the specified name and text and removes it.
/// </summary>
/// <param name="name">The name of the Token to remove.</param>
/// <param name="text">The text of the Token to remove.</param>
/// <returns>Returns the Token that was removed, or null if there was none.</returns>
public Token RemoveToken(string name, string text)
{
var t = Tokens.Find(x => x.Name == name && x.Text == text);
if (t != null)
Tokens.Remove(t);
return t;
}
/// <summary>
/// Removes the specified Token from this Token's children.
/// </summary>
/// <param name="token">The Token to remove.</param>
/// <returns>Returns the Token that was removed.</returns>
public Token RemoveToken(Token token)
{
Tokens.Remove(token);
return token;
}
/// <summary>
/// Removes the child Token at the specified index.
/// </summary>
/// <param name="index">The index of the Token to remove.</param>
/// <returns>Returns the Token that was removed.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">The requested index was out of range.</exception>
public Token RemoveToken(int index)
{
if (index < 0 || index >= Tokens.Count)
throw new ArgumentOutOfRangeException("i");
var t = Tokens[index];
Tokens.Remove(t);
return t;
}
/// <summary>
/// Removes all child Tokens by the specified name.
/// </summary>
/// <param name="name">The name of the Tokens to remove.</param>
public void RemoveAll(string name)
{
foreach (var t in Tokens.FindAll(x => x.Name == name))
Tokens.Remove(t);
}
/// <summary>
/// Finds a Token by a given path and returns it.
/// </summary>
/// <param name="pathSpec">The path to the Token to find.</param>
/// <returns>Returns the Token. If not found, returns null.</returns>
/// <example>"foo/bar" returns, the foo token's bar child. "bar[4]" returns the 4th bar token. "foo[=cake]" returns a foo token with text "cake".</example>
public Token Path(string pathSpec)
{
var parts = pathSpec.Split('/');
var point = this;
var final = parts.Last();
var indexRE = new Regex(@"\[(?<index>[0-9]+)\]");
var textRE = new Regex(@"\[=(?<text>\w+)\]");
if (indexRE.IsMatch(final))
final = final.Remove(final.IndexOf('['));
else if (textRE.IsMatch(final))
final = final.Remove(final.IndexOf('['));
foreach (var p in parts)
{
Token target = null;
if (indexRE.IsMatch(p))
{
var trueP = p.Remove(p.IndexOf('['));
var index = int.Parse(indexRE.Match(p).Groups["index"].ToString());
var targets = point.Tokens.FindAll(t => t.Name.Equals(trueP, StringComparison.OrdinalIgnoreCase));
if (targets == null || index >= targets.Count)
return null;
target = targets[index];
}
else if (textRE.IsMatch(p))
{
var trueP = p.Remove(p.IndexOf('['));
var text = textRE.Match(p).Groups["text"].ToString();
var possibilities = point.Tokens.Where(t => t.Name.Equals(trueP, StringComparison.OrdinalIgnoreCase));
target = possibilities.FirstOrDefault(t => !t.Text.IsBlank() && t.Text.Equals(text, StringComparison.OrdinalIgnoreCase));
}
else
target = point.Tokens.Find(t => t.Name.Equals(p, StringComparison.OrdinalIgnoreCase));
if (target == null)
return null;
if (target.Name.Equals(final, StringComparison.OrdinalIgnoreCase))
return target;
point = target;
}
return null;
}
public Token Item(int index)
{
if (index < 0 || index >= Tokens.Count)
throw new ArgumentOutOfRangeException("i");
return Tokens[index];
}
public int Count()
{
return Tokens.Count;
}
/// <summary>
/// Converts a TML string to a Token tree.
/// </summary>
/// <param name="source">The TML string to convert.</param>
public void Tokenize(string source)
{
var t = new List<Token>();
var lines = source.Split('\n');
var nodes = new List<Token>();
var prevTabs = 0;
var cdata = false;
var cdataText = new StringBuilder();
foreach (var line in lines.Where(x => !x.TrimStart().StartsWith("--")))
{
var l = line.TrimEnd();
if (cdata)
{
if (l.EndsWith("]]>"))
{
nodes.Last().AddToken("#text", 0, ParseEscapes(cdataText.ToString()));
cdata = false;
continue;
}
cdataText.AppendLine(l);
continue;
}
else
if (line.IsBlank())
continue;
//count number of tabs in front
var tabs = 0;
for (; tabs < l.Length - 1; tabs++)
if (l[tabs] != '\t')
break;
l = l.TrimStart();
var newOne = new Token();
var tokenName = l;
if (tokenName == "<[[")
{
//Start of a CDATA-style text block! Switch to CDATA mode, keep parsing until ]]> and place it in the last node as "#text".
cdata = true;
cdataText.Clear();
continue;
}
if (tokenName.StartsWith("- "))
{
//Token is an anonymous value -- transform from "- X" to "#a: X".
tokenName = string.Format("#a: {0}", tokenName.Substring(2));
l = tokenName;
}
if (l.Contains(": "))
{
//Token has a value
if (l.Contains(": \""))
{
var text = l.Substring(l.IndexOf('\"') + 1);
newOne.Text = text.Remove(text.LastIndexOf('\"'));
}
else if (l.Contains(": U+"))
{
var codePoint = l.Substring(l.LastIndexOf('+') + 1);
newOne.Value = int.Parse(codePoint, NumberStyles.HexNumber);
}
else if (l.Contains(": 0x"))
{
var codePoint = l.Substring(l.LastIndexOf('x') + 1);
newOne.Value = int.Parse(codePoint, NumberStyles.HexNumber);
}
else
{
float v;
var value = l.Substring(l.IndexOf(' ') + 1);
if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out v))
newOne.Value = v;
else
newOne.Text = value;
}
tokenName = tokenName.Remove(tokenName.IndexOf(':'));
if (!newOne.Text.IsBlank())
newOne.Text = ParseEscapes(newOne.Text);
#if DEBUG
if (tokenName.Contains(' '))
throw new Exception(string.Format("Found a token \"{0}\", probably a typo.", tokenName));
#endif
}
newOne.Name = tokenName;
if (tabs == 0)
{
//New one here
t.Add(newOne);
nodes.Clear();
nodes.Add(newOne);
}
else if (tabs == prevTabs + 1)
{
var hook = nodes[prevTabs];
hook.Tokens.Add(newOne);
nodes.Add(newOne);
}
else if (tabs < prevTabs)
{
var hook = nodes[tabs - 1];
hook.Tokens.Add(newOne);
nodes.RemoveRange(tabs, nodes.Count - tabs);
nodes.Add(newOne);
}
else if (tabs == prevTabs)
{
var hook = nodes[tabs - 1];
hook.Tokens.Add(newOne);
nodes[tabs] = newOne;
}
else
{
throw new Exception(string.Format("Token tree contains a line that's indented too far. Line is \"{0}\", indenting {1} level(s), but the previous line is only {2} level(s).", l.Trim(), tabs, prevTabs));
}
prevTabs = tabs;
}
Tokens = t;
//NoRolls = false;
}
/// <summary>
/// Recursively converts a Token list to a TML string. Available in debug builds only.
/// </summary>
/// <param name="list">The list of Tokens to convert.</param>
/// <param name="tabs">The amount of tab characters to indent.</param>
/// <returns>Returns a TML string representation of the specified Tokens. Returns the empty string in release builds.</returns>
#if DEBUG
public string DumpTokens(List<Token> list, int tabs)
{
var ret = new StringBuilder();
foreach (var item in list)
{
if (item.Name == "#text")
{
ret.AppendFormat("{0}<[[", new string('\t', tabs));
ret.AppendLine();
ret.Append(item.Text);
ret.AppendFormat("{0}]]>", new string('\t', tabs));
ret.AppendLine();
continue;
}
ret.AppendFormat("{0}{1}", new string('\t', tabs), item.Name);
if (item.IntValue != 0 || !item.Text.IsBlank())
{
ret.Append(": ");
if (item.IntValue != 0)
ret.Append(item.Value);
else
ret.AppendFormat("\"{0}\"", item.Text.Replace("\"", """));
ret.AppendLine();
}
else
ret.AppendLine();
if (item.Tokens.Count > 0)
ret.Append(DumpTokens(item.Tokens, tabs + 1));
}
return ret.ToString();
}
#else
public string DumpTokens(List<Token> list, int tabs)
{
return string.Empty;
}
#endif
private string ParseEscapes(string text)
{
var entity = new Regex("&#x([A-Za-z0-9]{2,4});");
var matches = entity.Matches(text);
foreach (Match match in matches)
{
var replacement = new string((char)int.Parse(match.Value.Substring(3, match.Value.Length - 4), NumberStyles.HexNumber), 1);
text = text.Replace(match.Value, replacement);
}
return text;
}
/// <summary>
/// Given a string of the format "1dX" or "1dX+Y", returns X and Y.
/// </summary>
/// <param name="text">A dice roll</param>
/// <param name="range">The total amount of dots on the die</param>
/// <param name="plus">The amount to be added to the die roll</param>
/// <returns>True if the string could be parsed, false otherwise.</returns>
public bool ParseRoll(string text, out int range, out int plus)
{
range = 0;
plus = 0;
var m = Regex.Match(text, @"1d(\d+)\+(\d+)");
if (!m.Success)
{
m = Regex.Match(text, @"1d(\d+)");
if (!m.Success)
return false;
}
range = int.Parse(m.Groups[1].Value);
if (m.Groups.Count == 3)
plus = int.Parse(m.Groups[2].Value);
return true;
}
/// <summary>
/// Recursively resolves roll and oneof values.
/// </summary>
public void ResolveRolls()
{
foreach (var token in Tokens)
{
if (token.Tokens.Count > 0)
token.ResolveRolls();
if (token.Text.IsBlank())
continue;
if (token.Text.StartsWith("roll "))
{
var xDyPz = token.Text.Substring(token.Text.LastIndexOf(' ') + 1);
int y = 0, z = 0;
ParseRoll(xDyPz, out y, out z);
var roll = Random.Next(y) + z;
token.Value = roll;
token.Text = null;
}
else if (token.Text.StartsWith("oneof "))
{
var options = token.Text.Substring(token.Text.IndexOf("of ") + 3).Split(',');
var choice = options.PickOne().Trim();
token.Text = choice;
}
}
}
/// <summary>
/// Applies a patch, given in TML format, to this Token tree.
/// </summary>
/// <param name="source">A TML representation of the patch to apply.</param>
/// <remarks>Patch syntax is somewhat based on JSON PATCH, RFC 6902.</remarks>
public void Patch(string source)
{
var patch = new Token();
patch.Tokenize(source);
foreach (var token in patch.Tokens)
{
var path = token.Text;
if (token.Name == "add")
{
if (path == "-")
{
Tokens.AddRange(token.Tokens);
}
else if (path.EndsWith("/-"))
{
var target = Path(path.Substring(0, path.Length - 2));
if (target != null)
{
target.Tokens.AddRange(token.Tokens);
}
}
{
var target = Path(path);
if (target != null)
{
Tokens.InsertRange(Tokens.IndexOf(target) + 1, token.Tokens);
}
}
}
else if (token.Name == "remove")
{
var target = Path(path);
if (target != null)
{
if (path.LastIndexOf('/') == -1)
{
Tokens.Remove(target);
}
else
{
var oneUp = Path(path.Substring(0, path.LastIndexOf('/')));
oneUp.Tokens.Remove(target);
}
}
}
else if (token.Name == "replace")
{
var target = Path(path);
if (target != null)
{
if (path.LastIndexOf('/') == -1)
{
Tokens[Tokens.IndexOf(target)] = token.Tokens[0];
}
else
{
var oneUp = Path(path.Substring(0, path.LastIndexOf('/')));
oneUp.Tokens[oneUp.Tokens.IndexOf(target)] = token.Tokens[0];
}
}
}
else if (token.Name == "set")
{
var target = Path(path);
if (target != null)
{
var val = token.GetToken("value");
var txt = token.GetToken("text");
if (val != null)
target.Value = val.Value;
if (txt != null)
target.Text = txt.Text;
}
}
}
}
}
/// <summary>
/// The backbone of Noxico data.
/// </summary>
public class Token : TokenCarrier
{
public string Name { get; set; }
public float Value { get; set; }
public string Text { get; set; }
public int IntValue { get { return (int)Value; } set { Value = (float)value; } }
public override string ToString()
{
return Text.IsBlank(string.Format("{0} ({1}, {2})", Name, Value, Tokens.Count),
string.Format("{0} ({1}, \"{2}\", {3})", Name, Value, Text, Tokens.Count));
}
/// <summary>
/// Initializes a new Token. Should not be used as-is unless directly followed by something to set the Name.
/// </summary>
public Token()
{
}
/// <summary>
/// Initializes a new Token.
/// </summary>
/// <param name="name">The name to give the new Token.</param>
public Token(string name)
{
Name = name;
}
/// <summary>
/// Initializes a new Token.
/// </summary>
/// <param name="name">The name to give the new Token.</param>
/// <param name="value">The value to give the new Token.</param>
public Token(string name, float value)
{
Name = name;
Value = value;
}
/// <summary>
/// Initializes a new Token.
/// </summary>
/// <param name="name">The name to give the new Token.</param>
/// <param name="text">The text to give the new Token.</param>
public Token(string name, string text)
{
Name = name;
Text = text;
}
/// <summary>
/// Initializes a new Token.
/// </summary>
/// <param name="name">The name to give the new Token.</param>
/// <param name="value">The value to give the new Token.</param>
/// <param name="text">The text to give the new Token.</param>
public Token(string name, float value, string text)
{
Name = name;
Value = value;
Text = text;
}
/// <summary>
/// Recursively serializes a Token to a stream for storage, along with its children.
/// </summary>
/// <param name="stream">The stream to write to.</param>
public void SaveToFile(BinaryWriter stream)
{
stream.Write(Name.OrEmpty());
var isInt = (Math.Abs(Value - IntValue) < float.Epsilon);
//Format: child count, reserved, has text, value is integral, has value.
var flags = 0;
if (IntValue != 0)
flags |= 1;
if (isInt)
flags |= 2;
if (!Text.IsBlank())
flags |= 4;
flags |= Tokens.Count << 4;
//We store this as an encoded value to allow any amount of child tokens.
stream.Write7BitEncodedInt(flags);
if (IntValue != 0)
{
if (isInt)
stream.Write7BitEncodedInt((int)Value);
else
stream.Write((Single)Value);
}
if (!Text.IsBlank())
stream.Write(Text);
if (Tokens.Count > 0)
Tokens.ForEach(x => x.SaveToFile(stream));
}
/// <summary>
/// Recursively deserializes a Token and its children from a stream.
/// </summary>
/// <param name="stream">The stream to read from.</param>
/// <returns>Returns the Token that was deserialized.</returns>
public static Token LoadFromFile(BinaryReader stream)
{
var newToken = new Token();
newToken.Name = stream.ReadString();
var flags = stream.Read7BitEncodedInt();
//Format: child count, reserved, has text, value is integral, has value.
var numTokens = flags >> 4;
if ((flags & 1) == 1)
{
var isInt = ((flags & 2) == 2);
if (isInt)
newToken.Value = stream.Read7BitEncodedInt();
else
newToken.Value = stream.ReadSingle();
}
if ((flags & 4) == 4)
newToken.Text = stream.ReadString();
for (var i = 0; i < numTokens; i++)
newToken.Tokens.Add(Token.LoadFromFile(stream));
return newToken;
}
/// <summary>
/// Recursively checks if this Token's children matches another list of Tokens, by name.
/// </summary>
/// <param name="otherSet">The list of Tokens to compare against.</param>
/// <returns>Returns true if the sets match.</returns>
public bool IsMatch(List<Token> otherSet)
{
foreach (var toFind in this.Tokens)
{
var f = otherSet.Find(x => x.Name == toFind.Name);
if (f == null)
return false;
if (toFind.Tokens.Count > 0)
{
var contentMatch = toFind.IsMatch(f.Tokens);
if (!contentMatch)
return false;
}
}
return true;
}
/// <summary>
/// Removes each Token in the specified set from this Token's children, by reference.
/// </summary>
/// <param name="otherSet">A list of Tokens to remove.</param>
public void RemoveSet(List<Token> otherSet)
{
//throw new NotImplementedException();
foreach (var t in otherSet)
this.Tokens.Remove(t);
}
/// <summary>
/// Recursively adds a list of Tokens to this Token's children, by cloning.
/// </summary>
/// <param name="otherSet">The list of Tokens to add.</param>
public void AddSet(List<Token> otherSet)
{
//this.Tokens.AddRange(otherSet);
foreach (var toAdd in otherSet)
{
var newToken = new Token(toAdd.Name, toAdd.Value, toAdd.Text);
if (toAdd.Tokens.Count > 0)
newToken.AddSet(toAdd.Tokens);
this.Tokens.Add(newToken);
//this.AddToken(toAdd.Name, toAdd.Value, toAdd.Text);
//if (toAdd.Tokens.Count > 0)
// this.GetToken(toAdd.Name).AddSet(toAdd.Tokens);
}
}
/// <summary>
/// Creates a new Token with the same name, value, text, and (recursively) children as this one.
/// </summary>
/// <param name="deep">If true, children are also cloned. If false, they are passed as references to the originals.</param>
/// <returns>Returns a copy of this Token.</returns>
public Token Clone(bool deep = true)
{
var t = new Token(Name, Value, Text);
foreach (var child in Tokens)
t.AddToken(deep ? child.Clone() : child);
return t;
}
/// <summary>
/// Compares this Token's name, value, and text to another Token's.
/// </summary>
/// <param name="other">The Token to compare to.</param>
/// <param name="deep">If true, also checks the Tokens' children.</param>
/// <returns>Returns true if the tokens match.</returns>
public bool Equals(Token other, bool deep = true)
{
if (other.Name == this.Name && (Math.Abs(other.Value - this.Value) < float.Epsilon) && other.Text == this.Text)
{
if (deep)
{
if (other.Tokens.Count != this.Tokens.Count)
return false; //quick cut-off!
for (var i = 0; i < this.Tokens.Count; i++)
if (!this.Tokens[i].Equals(other.Tokens[i]))
return false; //at least one of the child tokens doesn't match.
}
return true;
}
return false;
}
#if DEBUG
public void CheckSchema(string name, string id)
{
var schemas = Mix.GetTokenTree("schema.tml", true);
var thisSchema = schemas.FirstOrDefault(x => x.Name == name);
if (thisSchema == null)
return;
if (!string.IsNullOrEmpty(thisSchema.Text))
name = thisSchema.Text;
if (this.Name != name)
return;
while (thisSchema.HasToken("_copy"))
{
var copyFrom = thisSchema.GetToken("_copy");
thisSchema.RemoveToken(copyFrom);
var otherSchema = schemas.FirstOrDefault(x => x.Name == copyFrom.Text);
if (otherSchema == null)
break;
thisSchema.AddSet(otherSchema.Tokens);
foreach (var c in copyFrom.Tokens)
{
if (c.Name == "_remove")
{
var t = thisSchema.Path(c.Text);
var p = thisSchema.Path(c.Text.Substring(0, c.Text.LastIndexOf('/')));
p.RemoveToken(t);
}
}
}
var allowedMissing = new List<string>();
if (thisSchema.HasToken("_allowmissing"))
{
var allowMissing = thisSchema.GetToken("_allowmissing");
if (this.HasToken(allowMissing.Text))
allowedMissing.AddRange(allowMissing.Tokens.Select(t => t.Name));
}
var unknown = new List<string>();
Action<Token, Token> checkSchemaHelper = null;
checkSchemaHelper = new Action<Token, Token>((data, schema) =>
{
if (schema.HasToken("_reference"))
{
var reference = schema.GetToken("_reference");
var refSchema = schemas.FirstOrDefault(x => x.Name == reference.Text);
if (refSchema == null)
throw new Exception(string.Format("Schema check fail for {0} {1}: invalid reference to {2}.", name, id, reference.Text));
checkSchemaHelper(data, refSchema);
return;
}
if (schema.HasToken("_string") || schema.HasToken("_type") || schema.HasToken("_id") || schema.HasToken("_lua") || schema.HasToken("_color"))
{
if (string.IsNullOrEmpty(data.Text))
{
if (schema.HasToken("_candefault"))
return;
throw new Exception(string.Format("Schema check fail for {0} {1}: {2} should have a text value.", name, id, data.Name));
}
}
else if (schema.HasToken("_integer") || schema.HasToken("_measure") || schema.HasToken("_rating") || schema.HasToken("_character") || schema.HasToken("_factor") || schema.HasToken("_weight"))
{
if (!string.IsNullOrEmpty(data.Text))
{
if (data.Text.StartsWith("roll "))
return;
throw new Exception(string.Format("Schema check fail for {0} {1}: {2} should have a number value. String was given: {3}", name, id, data.Name, data.Text));
}
else if (schema.HasToken("_integer"))
{
if (data.IntValue != data.Value)
{
throw new Exception(string.Format("Schema check fail for {0} {1}: {2} should have an integer value. Float was given: {3}", name, id, data.Name, data.Value));
}
}
}
if (schema.HasToken("_cdata"))
{
if (data.Tokens.Count == 1 && data.Tokens[0].Name == "#text")
return;
throw new Exception(string.Format("Schema check fail for {0} {1}: {2} should have CDATA.", name, id, data.Name));
}
if (schema.HasToken("_tokens"))
{
var tokens = schema.GetToken("_tokens");
var arbitrary = tokens.HasToken("_arbitrary");
foreach (var token in tokens.Tokens)
{
if (token.Name == "_oneof")
{
var options = token.Tokens;
string found = null;
foreach (var option in options)
{
if (option.Name.StartsWith('_'))
continue;
if (data.HasToken(option.Name))
{
if (found != null)
throw new Exception(string.Format("Schema check fail for {0} {1}: {2} should have only one of {3} child tokens, {4} was found first, then {5}.", name, id, data.Name, token.Text, found, option.Name));
found = option.Name;
}
}
if (found == null && !token.HasToken("_optional"))
{
if (options.All(x => !x.Name.StartsWith('_') && allowedMissing.Contains(x.Name)))
return;
throw new Exception(string.Format("Schema check fail for {0} {1}: {2} should have only one of {3} child tokens, none were found.", name, id, data.Name, string.Join(" ", options.Select(x => x.Name))));
}
else
{
if (found != null)
checkSchemaHelper(data.GetToken(found), token.GetToken(found));
return;
}
}
if (token.Name.StartsWith('_'))
continue;
if (!data.HasToken(token.Name) && (!token.HasToken("_optional") && !allowedMissing.Contains(token.Name)))
{
throw new Exception(string.Format("Schema check fail for {0} {1}: {2} should have a {3} child token.", name, id, data.Name, token.Name));
}
else if (data.HasToken(token.Name))
{
foreach (var t in data.GetAll(token.Name))
checkSchemaHelper(t, token);
}
}
if (!arbitrary)
{
foreach (var token in data.Tokens)
if (!tokens.HasToken(token.Name) && !token.Name.StartsWith('_'))
unknown.Add(data.Name + "/" + token.Name);
}
}
});
checkSchemaHelper(this, thisSchema);
if (unknown.Count > 0)
{
Program.WriteLine("Schema check: {0} {1} has unrecognized tokens:", name, id);
foreach (var u in unknown)
Program.WriteLine("* {0}", u);
}
}
#endif
}
}