-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathArgumentReader.cs
1050 lines (904 loc) · 25.6 KB
/
ArgumentReader.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
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
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
/**
* Author: David Miller
**/
// pulled from the ClojureCLR codebase and adapted for Nostrand
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using clojure.lang;
namespace Nostrand
{
public static class ArgumentReader
{
#region Symbol definitions
static readonly Keyword EOF = Keyword.intern(null, "eof");
#endregion
#region Macro characters & #-dispatch
static IFn _taggedReader = new TaggedReader();
static IFn[] _macros = new IFn[256];
static IFn[] _dispatchMacros = new IFn[256];
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
static ArgumentReader()
{
_macros['"'] = new StringReader();
_macros[';'] = new CommentReader();
_macros['^'] = new MetaReader();
_macros['('] = new ListReader();
_macros[')'] = new UnmatchedDelimiterReader();
_macros['['] = new VectorReader();
_macros[']'] = new UnmatchedDelimiterReader();
_macros['{'] = new MapReader();
_macros['}'] = new UnmatchedDelimiterReader();
_macros['\\'] = new CharacterReader();
_macros['#'] = new DispatchReader();
_dispatchMacros['^'] = new MetaReader();
//_dispatchMacros['"'] = new RegexReader();
_dispatchMacros['{'] = new SetReader();
_dispatchMacros['<'] = new UnreadableReader();
_dispatchMacros['_'] = new DiscardReader();
}
static bool isMacro(int ch)
{
return ch < _macros.Length && _macros[ch] != null;
}
static IFn getMacro(int ch)
{
return ch < _macros.Length ? _macros[ch] : null;
}
static bool isTerminatingMacro(int ch)
{
return (ch != '#' && ch != '\'' && ch < _macros.Length && _macros[ch] != null);
}
#endregion
#region main entry points - readString, read
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "read")]
static public Object readString(String s, IPersistentMap opts)
{
PushbackTextReader r = new PushbackTextReader(new System.IO.StringReader(s));
return read(r, opts);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "read")]
public static Object read(PushbackTextReader r, IPersistentMap opts)
{
return read(r, !opts.containsKey(EOF), opts.valAt(EOF), false, opts);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly")]
public static object read(PushbackTextReader r,
bool eofIsError,
object eofValue,
bool isRecursive,
Object opts)
{
try
{
for (;;)
{
int ch = r.Read();
while (isWhitespace(ch))
ch = r.Read();
if (ch == -1)
{
if (eofIsError)
throw new EndOfStreamException("EOF while reading");
return eofValue;
}
if (Char.IsDigit((char)ch))
{
object n = readNumber(r, (char)ch);
return RT.suppressRead() ? null : n;
}
IFn macroFn = getMacro(ch);
if (macroFn != null)
{
object ret = macroFn.invoke(r, (char)ch, opts);
if (RT.suppressRead())
return null;
// no op macros return the reader
if (ret == r)
continue;
return ret;
}
if (ch == '+' || ch == '-')
{
int ch2 = r.Read();
if (Char.IsDigit((char)ch2))
{
Unread(r, ch2);
object n = readNumber(r, (char)ch);
return RT.suppressRead() ? null : n;
}
Unread(r, ch2);
}
//string token = readToken(r, (char)ch);
//return RT.suppressRead() ? null : interpretToken(token);
string rawToken;
string token;
string mask;
bool eofSeen;
readToken(r, (char)ch, true, out rawToken, out token, out mask, out eofSeen);
if (eofSeen)
{
if (eofIsError)
throw new EndOfStreamException("EOF while reading symbol");
return eofValue;
}
return RT.suppressRead() ? null : InterpretToken(rawToken, token, mask);
}
}
catch (Exception e)
{
if (isRecursive)
throw;
LineNumberingTextReader lntr = r as LineNumberingTextReader;
if (lntr == null)
throw;
throw new ReaderException(lntr.LineNumber, lntr.ColumnNumber, e);
}
}
private static object ReadAux(PushbackTextReader r, object opts)
{
return read(r, true, null, true, opts);
}
#endregion
#region Character hacking
static void Unread(PushbackTextReader r, int ch)
{
if (ch != -1)
r.Unread(ch);
}
static bool isWhitespace(int ch)
{
return Char.IsWhiteSpace((char)ch) || ch == ',';
}
static bool NonConstituent(int ch)
{
// HACK allow all characters to support ~/homedir/paths
return false; // return ch == '@' || ch == '`' || ch == '~';
}
// Roughly a match to Java Character.digit(char,int),
// though I don't handle all unicode digits.
static int CharValueInRadix(int c, int radix)
{
if (char.IsDigit((char)c))
return c - '0' < radix ? c - '0' : -1;
if ('A' <= c && c <= 'Z')
return c - 'A' < radix - 10 ? c - 'A' + 10 : -1;
if ('a' <= c && c <= 'z')
return c - 'a' < radix - 10 ? c - 'a' + 10 : -1;
return -1;
}
static int readUnicodeChar(string token, int offset, int length, int radix)
{
if (token.Length != offset + length)
throw new ArgumentException("Invalid unicode character: \\" + token);
int uc = 0;
for (int i = offset; i < offset + length; ++i)
{
int d = CharValueInRadix(token[i], radix);
if (d == -1)
throw new ArgumentException("Invalid digit: " + token[i]);
uc = uc * radix + d;
}
return (char)uc;
}
static int readUnicodeChar(PushbackTextReader r, int initch, int radix, int length, bool exact)
{
int uc = CharValueInRadix(initch, radix);
if (uc == -1)
throw new ArgumentException("Invalid digit: " + (char)initch);
int i = 1;
for (; i < length; ++i)
{
int ch = r.Read();
if (ch == -1 || isWhitespace(ch) || isMacro(ch))
{
Unread(r, ch);
break;
}
int d = CharValueInRadix(ch, radix);
if (d == -1)
throw new ArgumentException("Invalid digit: " + (char)ch);
uc = uc * radix + d;
}
if (i != length && exact)
throw new ArgumentException("Invalid character length: " + i + ", should be: " + length);
return uc;
}
#endregion
#region Other
static List<Object> ReadDelimitedList(char delim, PushbackTextReader r, bool isRecursive, object opts)
{
LineNumberingTextReader lntr = r as LineNumberingTextReader;
int firstLine = lntr != null ? lntr.LineNumber : -1;
List<Object> a = new List<object>();
for (;;)
{
int ch = r.Read();
while (isWhitespace(ch))
ch = r.Read();
if (ch == -1)
{
if (firstLine < 0)
throw new EndOfStreamException("EOF while reading");
else
throw new EndOfStreamException("EOF while reading, starting at line " + firstLine);
}
if (ch == delim)
{
break;
}
IFn macroFn = getMacro(ch);
if (macroFn != null)
{
Object mret = macroFn.invoke(r, (char)ch, opts);
//no op macros return the reader
if (mret != r)
a.Add(mret);
}
else
{
Unread(r, ch);
object o = read(r, true, null, isRecursive, opts);
if (o != r)
a.Add(o);
}
}
return a;
}
#endregion
#region Reading tokens
static string readSimpleToken(PushbackTextReader r, char initch, bool leadConstituent)
{
if (leadConstituent && NonConstituent(initch))
throw new InvalidOperationException("Invalid leading characters: " + (char)initch);
StringBuilder sb = new StringBuilder();
sb.Append(initch);
for (;;)
{
int ch = r.Read();
if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
{
Unread(r, ch);
return sb.ToString();
}
sb.Append((char)ch);
}
}
static void readToken(PushbackTextReader r, char initch, bool leadConstituent, out String rawToken, out String token, out String mask, out bool eofSeen)
{
if (leadConstituent && NonConstituent(initch))
throw new InvalidOperationException("Invalid leading characters: " + (char)initch);
bool allowSymEscape = RT.booleanCast(RT.AllowSymbolEscapeVar.deref());
bool rawMode = false;
StringBuilder sbRaw = new StringBuilder();
StringBuilder sbToken = new StringBuilder();
StringBuilder sbMask = new StringBuilder();
if (allowSymEscape && initch == '|')
{
rawMode = true;
sbRaw.Append(initch);
}
else
{
sbRaw.Append(initch);
sbToken.Append(initch);
sbMask.Append(initch);
}
for (;;)
{
int ch = r.Read();
if (rawMode)
{
if (ch == -1)
{
rawToken = sbRaw.ToString();
token = sbToken.ToString();
mask = sbMask.ToString();
eofSeen = true;
return;
}
if (ch == '|')
{
int ch2 = r.Read();
if (ch2 == '|')
{
sbRaw.Append('|');
sbToken.Append('|');
sbMask.Append('a');
}
else
{
r.Unread(ch2);
rawMode = false;
sbRaw.Append(ch);
}
}
else
{
sbRaw.Append((char)ch);
sbToken.Append((char)ch);
sbMask.Append('a');
}
}
else
{
if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
{
Unread(r, ch);
rawToken = sbRaw.ToString();
token = sbToken.ToString();
mask = sbMask.ToString();
eofSeen = false;
return;
}
else if (NonConstituent(ch))
throw new InvalidOperationException("Invalid constituent character: " + (char)ch);
else if (ch == '|' && allowSymEscape)
{
rawMode = true;
sbRaw.Append((char)ch);
}
else
{
sbRaw.Append((char)ch);
sbToken.Append((char)ch);
sbMask.Append((char)ch);
}
}
}
}
public static object InterpretToken(string token)
{
return InterpretToken(token, token, token);
}
public static object InterpretToken(string rawToken, string token, string mask)
{
if (token.Equals("nil"))
{
return null;
}
else if (token.Equals("true"))
{
//return RT.T;
return true;
}
else if (token.Equals("false"))
{
//return RT.F;
return false;
}
object ret = matchSymbol(token, mask);
if (ret != null)
return ret;
// HACK return rawToken symbol when invalid to support /absolute/paths
return Symbol.intern(rawToken); // throw new ArgumentException("Invalid token: " + rawToken);
}
static Regex symbolPat = new Regex("^[:]?([^\\p{Nd}/].*/)?(/|[^\\p{Nd}/][^/]*)$");
private static void ExtractNamesUsingMask(string token, string maskNS, string maskName, out string ns, out string name)
{
if (String.IsNullOrEmpty(maskNS))
{
ns = null;
name = token;
}
else
{
ns = token.Substring(0, maskNS.Length - 1);
name = token.Substring(maskNS.Length);
}
}
static object matchSymbol(string token, string mask)
{
Match m = symbolPat.Match(mask);
if (m.Success)
{
string maskNS = m.Groups[1].Value;
string maskName = m.Groups[2].Value;
if (maskNS != null && maskNS.EndsWith(":/")
|| maskName.EndsWith(":")
|| mask.IndexOf("::", 1) != -1)
return null;
if (mask.StartsWith("::"))
return null;
bool isKeyword = mask[0] == ':';
if (isKeyword)
{
Match m2 = symbolPat.Match(mask.Substring(1));
if (!m2.Success)
return null;
string ns;
string name;
ExtractNamesUsingMask(token.Substring(1), m2.Groups[1].Value, m2.Groups[2].Value, out ns, out name);
return Keyword.intern(ns, name);
}
else
{
string ns;
string name;
ExtractNamesUsingMask(token, maskNS, maskName, out ns, out name);
return Symbol.intern(ns, name);
}
}
return null;
}
#endregion
#region Reading numbers
static Regex intRE = new Regex("^([-+]?)(?:(0)|([1-9][0-9]*)|0[xX]([0-9A-Fa-f]+)|0([0-7]+)|([1-9][0-9]?)[rR]([0-9A-Za-z]+)|0[0-9]+)(N)?$");
static Regex ratioRE = new Regex("^([-+]?[0-9]+)/([0-9]+)$");
static Regex floatRE = new Regex("^([-+]?[0-9]+(\\.[0-9]*)?([eE][-+]?[0-9]+)?)(M)?$");
static object readNumber(PushbackTextReader r, char initch)
{
StringBuilder sb = new StringBuilder();
sb.Append(initch);
for (;;)
{
int ch = r.Read();
if (ch == -1 || isWhitespace(ch) || isMacro(ch))
{
Unread(r, ch);
break;
}
sb.Append((char)ch);
}
string s = sb.ToString();
object n = MatchNumber(s);
if (n == null)
throw new FormatException("Invalid number: " + s);
return n;
}
public static object MatchNumber(string s)
{
Match m = intRE.Match(s);
if (m.Success)
{
if (m.Groups[2].Success)
{
// matched 0 or 0N only
if (m.Groups[8].Success)
return BigInt.ZERO;
return 0L;
}
bool isNeg = m.Groups[1].Value == "-";
string n = null;
int radix = 10;
if (m.Groups[3].Success)
{
n = m.Groups[3].Value;
radix = 10;
}
else if (m.Groups[4].Success)
{
n = m.Groups[4].Value;
radix = 16;
}
else if (m.Groups[5].Success)
{
n = m.Groups[5].Value;
radix = 8;
}
else if (m.Groups[7].Success)
{
n = m.Groups[7].Value;
radix = Int32.Parse(m.Groups[6].Value, System.Globalization.CultureInfo.InvariantCulture);
}
if (n == null)
return null;
BigInteger bn = BigInteger.Parse(n, radix);
if (isNeg)
bn = bn.Negate();
if (m.Groups[8].Success) // N suffix
return BigInt.fromBigInteger(bn);
long ln;
if (bn.AsInt64(out ln))
return Numbers.num(ln);
return BigInt.fromBigInteger(bn);
}
m = floatRE.Match(s);
if (m.Success)
{
if (m.Groups[4].Success)
{
string val = m.Groups[1].Value;
// MS implementation of java.util.BigDecimal has a bug when the string has a leading+
//if ( val[0] == '+' )
// val = val.Substring(1);
return BigDecimal.Parse(val);
}
return (object)Double.Parse(s, System.Globalization.CultureInfo.InvariantCulture);
}
m = ratioRE.Match(s);
if (m.Success)
{
// There is a bug in the BigInteger c-tor that causes it barf on a leading +.
string numerString = m.Groups[1].Value;
string denomString = m.Groups[2].Value;
if (numerString[0] == '+')
numerString = numerString.Substring(1);
//return Numbers.BIDivide(new BigInteger(numerString), new BigInteger(denomString));
return Numbers.divide(
Numbers.ReduceBigInt(BigInt.fromBigInteger(BigInteger.Parse(numerString))),
Numbers.ReduceBigInt(BigInt.fromBigInteger(BigInteger.Parse(denomString))));
}
return null;
}
#endregion
#region Readers
public abstract class ReaderBase : AFn
{
public override object invoke(object arg1, object arg2, object arg3)
{
return Read((PushbackTextReader)arg1, (Char)arg2, arg3);
}
protected abstract object Read(PushbackTextReader r, char c, object opts);
}
#region CharacterReader
public sealed class CharacterReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char backslash, object opts)
{
int ch = r.Read();
if (ch == -1)
throw new EndOfStreamException("EOF while reading character");
String token = readSimpleToken(r, (char)ch, false);
if (token.Length == 1)
return token[0];
else if (token.Equals("newline"))
return '\n';
else if (token.Equals("space"))
return ' ';
else if (token.Equals("tab"))
return '\t';
else if (token.Equals("backspace"))
return '\b';
else if (token.Equals("formfeed"))
return '\f';
else if (token.Equals("return"))
return '\r';
else if (token.StartsWith("u"))
{
char c = (char)readUnicodeChar(token, 1, 4, 16);
if (c >= '\uD800' && c <= '\uDFFF') // surrogate code unit?
throw new InvalidOperationException("Invalid character constant: \\u" + ((int)c).ToString("x"));
return c;
}
else if (token.StartsWith("o"))
{
int len = token.Length - 1;
if (len > 3)
throw new InvalidOperationException("Invalid octal escape sequence length: " + len);
int uc = readUnicodeChar(token, 1, len, 8);
if (uc > 255) //octal377
throw new InvalidOperationException("Octal escape sequence must be in range [0, 377].");
return (char)uc;
}
throw new InvalidOperationException("Unsupported character: \\" + token);
}
}
#endregion
#region String Reader
public sealed class StringReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char doublequote, object opts)
{
StringBuilder sb = new StringBuilder();
for (int ch = r.Read(); ch != '"'; ch = r.Read())
{
if (ch == -1)
throw new EndOfStreamException("EOF while reading string");
if (ch == '\\') //escape
{
ch = r.Read();
if (ch == -1)
throw new EndOfStreamException("EOF while reading string");
switch (ch)
{
case 't':
ch = '\t';
break;
case 'r':
ch = '\r';
break;
case 'n':
ch = '\n';
break;
case '\\':
break;
case '"':
break;
case 'b':
ch = '\b';
break;
case 'f':
ch = '\f';
break;
case 'u':
ch = r.Read();
if (CharValueInRadix(ch, 16) == -1)
throw new InvalidOperationException("Invalid unicode escape: \\u" + (char)ch);
ch = readUnicodeChar((PushbackTextReader)r, ch, 16, 4, true);
break;
default:
{
//if (CharValueInRadix(ch, 8) != -1) -- this is correct, but we end up with different error message for 8,9 than JVM, so do the following to match:
if (Char.IsDigit((char)ch))
{
ch = readUnicodeChar((PushbackTextReader)r, ch, 8, 3, false);
if (ch > 255) //octal377
throw new InvalidOperationException("Octal escape sequence must be in range [0, 377].");
}
else
throw new InvalidOperationException("Unsupported escape character: \\" + (char)ch);
}
break;
}
}
sb.Append((char)ch);
}
return sb.ToString();
}
}
#endregion
#region Discard/Comment readers
public sealed class CommentReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char semicolon, object opts)
{
int ch;
do
{
ch = r.Read();
} while (ch != -1 && ch != '\n' && ch != '\r');
return r;
}
}
public sealed class DiscardReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char underscore, object opts)
{
ReadAux(r, opts);
return r;
}
}
#endregion
#region DispatchReader
public sealed class DispatchReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char hash, object opts)
{
int ch = r.Read();
if (ch == -1)
throw new EndOfStreamException("EOF while reading character");
IFn fn = _dispatchMacros[ch];
if (fn == null)
{
// try tagged reader
if (Char.IsLetter((char)ch))
{
Unread(r, ch);
return _taggedReader.invoke(r, (char)ch, opts);
}
throw new InvalidOperationException(String.Format("No dispatch macro for: {0}", (char)ch));
}
return fn.invoke(r, (char)ch, opts);
}
}
#endregion
#region MetaReader
public sealed class MetaReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char caret, object opts)
{
int startLine = -1;
int startCol = -1;
LineNumberingTextReader lntr = r as LineNumberingTextReader;
if (lntr != null)
{
startLine = lntr.LineNumber;
startCol = lntr.ColumnNumber;
}
IPersistentMap metaAsMap;
{
object meta = ReadAux(r, opts);
if (meta is Symbol || meta is String)
metaAsMap = RT.map(RT.TagKey, meta);
else if (meta is Keyword)
metaAsMap = RT.map(meta, true);
else if ((metaAsMap = meta as IPersistentMap) == null)
throw new ArgumentException("Metadata must be Symbol,Keyword,String or Map");
}
object o = ReadAux(r, opts);
if (o is IMeta)
{
if (startLine != -1 && o is ISeq)
metaAsMap = metaAsMap.assoc(RT.LineKey, startLine)
.assoc(RT.ColumnKey, startCol)
.assoc(RT.SourceSpanKey, RT.map(
RT.StartLineKey, startLine,
RT.StartColumnKey, startCol,
RT.EndLineKey, lntr.LineNumber,
RT.EndColumnKey, lntr.ColumnNumber));
IReference iref = o as IReference;
if (iref != null)
{
iref.resetMeta(metaAsMap);
return o;
}
object ometa = RT.meta(o);
for (ISeq s = RT.seq(metaAsMap); s != null; s = s.next())
{
IMapEntry kv = (IMapEntry)s.first();
ometa = RT.assoc(ometa, kv.key(), kv.val());
}
return ((IObj)o).withMeta((IPersistentMap)ometa);
}
else
throw new ArgumentException("Metadata can only be applied to IMetas");
}
}
#endregion
#region Collection readers
public sealed class ListReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char leftparen, object opts)
{
//int startLine = -1;
//int startCol = -1;
//LineNumberingTextReader lntr = r as LineNumberingTextReader;
//if (lntr != null)
//{
// startLine = lntr.LineNumber;
// startCol = lntr.ColumnNumber;
//}
IList<Object> list = ReadDelimitedList(')', r, true, opts);
if (list.Count == 0)
return PersistentList.EMPTY;
IObj s = (IObj)PersistentList.create((IList)list);
return s;
}
}
public sealed class VectorReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char leftparen, object opts)
{
return LazilyPersistentVector.create(ReadDelimitedList(']', r, true, opts));
}
}
public sealed class MapReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char leftbrace, object opts)
{
Object[] a = ReadDelimitedList('}', r, true, opts).ToArray();
if ((a.Length & 1) == 1)
throw new ArgumentException("Map literal must contain an even number of forms");
return RT.map(a);
}
}
public sealed class SetReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char leftbracket, object opts)
{
return PersistentHashSet.createWithCheck(ReadDelimitedList('}', r, true, opts));
}
}
public sealed class UnmatchedDelimiterReader : ReaderBase
{
protected override object Read(PushbackTextReader reader, char rightdelim, object opts)
{
throw new ArgumentException("Unmatched delimiter: " + rightdelim);
}
}
#endregion
#region UnreadableReader
public sealed class UnreadableReader : ReaderBase
{
protected override object Read(PushbackTextReader reader, char leftangle, object opts)
{
throw new ArgumentException("Unreadable form");
}
}
#endregion
#region TaggedReader
public sealed class TaggedReader : ReaderBase
{
protected override object Read(PushbackTextReader r, char leftparen, object opts)
{
object name = read(r, true, null, false, opts);
Symbol sym = name as Symbol;
if (sym == null)
throw new InvalidOperationException("Reader tag must be a symbol");
return ReadTagged(r, sym, (IPersistentMap)opts);
}
static readonly Keyword READERS = Keyword.intern(null, "readers");
static readonly Keyword DEFAULT = Keyword.intern(null, "default");
private static object ReadTagged(PushbackTextReader r, Symbol tag, IPersistentMap opts)
{
object o = ReadAux(r, opts);
ILookup readers = (ILookup)RT.get(opts, READERS);
IFn dataReader = (IFn)RT.get(readers, tag);
if (dataReader == null)
dataReader = (IFn)RT.get(RT.DefaultDataReadersVar.deref(), tag);
if (dataReader == null)
{
IFn defaultReader = (IFn)RT.get(opts, DEFAULT);
if (defaultReader != null)
return defaultReader.invoke(tag, o);
else
throw new InvalidOperationException("No reader function for tag " + tag.ToString());
}
else
return dataReader.invoke(o);
}
}
#endregion
#endregion
#region ReaderException
[Serializable]
public sealed class ReaderException : Exception
{
readonly int _line;
public int Line
{
get { return _line; }
}
readonly int _column;
public int Column
{
get { return _column; }
}
public ReaderException(int line, int column, Exception e)