scan-rules 0.1.1

This crate provides some macros for quickly parsing values out of text. Roughly speaking, it does the inverse of the print!/format! macros; or, in other words, a similar job to scanf from C.
Documentation
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
/*
Copyright ⓒ 2016 Daniel Keep.

Licensed under the MIT license (see LICENSE or <http://opensource.org
/licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
<http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
files in the project carrying such notice may not be copied, modified,
or distributed except according to those terms.
*/
/*!

Miscellaneous, abstract scanners.
*/
use std::marker::PhantomData;
use strcursor::StrCursor;
use ::ScanError;
use ::input::ScanInput;
use ::util::StrUtil;
use super::{
    ScanFromStr, ScanSelfFromStr,
    ScanFromBinary, ScanFromOctal, ScanFromHex,
};

/**

Scans the given `Output` type from its binary representation.
*/
pub struct Binary<Output>(PhantomData<Output>);

impl<'a, Output> ScanFromStr<'a> for Binary<Output>
where Output: ScanFromBinary<'a> {
    type Output = Output;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        Output::scan_from_binary(s)
    }
}

#[cfg(test)]
#[test]
fn test_binary() {
    assert_match!(Binary::<i32>::scan_from("0 1 2 x"), Ok((0b0, 1)));
    assert_match!(Binary::<i32>::scan_from("012x"), Ok((0b1, 2)));
    assert_match!(Binary::<i32>::scan_from("0b012x"), Ok((0b0, 1)));
    assert_match!(Binary::<i32>::scan_from("110010101110000b"), Ok((0x6570, 15)));
}

/**

Scans all remaining input into a string.

In most cases, you should use the `.. name` tail capture term to perform this task.  This scanner is provided as a way to do this in contexts where tail capture is not valid (because it normally wouldn't make any sense).
*/
pub struct Everything<'a, Output=&'a str>(PhantomData<(&'a (), Output)>);

#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Everything<'a, &'a str> {
    type Output = &'a str;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        Ok((s.into(), s.len()))
    }
}

#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Everything<'a, String> {
    type Output = String;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        Ok((s.into(), s.len()))
    }
}

#[cfg(not(str_into_output_extra_broken))]
impl<'a, Output> ScanFromStr<'a> for Everything<'a, Output>
where &'a str: Into<Output> {
    type Output = Output;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        Ok((s.into(), s.len()))
    }
}

#[cfg(test)]
#[test]
fn test_everything() {
    // That's the scanner named `Everything`, not literally everything.
    assert_match!(Everything::<&str>::scan_from(""), Ok(("", 0)));
    assert_match!(Everything::<&str>::scan_from(""), Ok(("", 3)));
    assert_match!(Everything::<&str>::scan_from("うまいー うまいー ぼうぼうぼうぼう"), Ok(("うまいー うまいー ぼうぼうぼうぼう", 54)));
}

/**

Scans the given `Output` type from its hexadecimal representation.
*/
pub struct Hex<Output>(PhantomData<Output>);

impl<'a, Output> ScanFromStr<'a> for Hex<Output>
where Output: ScanFromHex<'a> {
    type Output = Output;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        Output::scan_from_hex(s)
    }
}

#[cfg(test)]
#[test]
fn test_hex() {
    assert_match!(Hex::<i32>::scan_from("0 1 2 x"), Ok((0x0, 1)));
    assert_match!(Hex::<i32>::scan_from("012x"), Ok((0x12, 3)));
    assert_match!(Hex::<i32>::scan_from("0x012x"), Ok((0x0, 1)));
    assert_match!(Hex::<i32>::scan_from("BadCafé"), Ok((0xbadcaf, 6)));
}

/**

Scans a single identifier into a string.

Specifically, this will match a single `XID_Start` character (or underscore) followed by zero or more `XID_Continue` characters.
*/
pub struct Ident<'a, Output=&'a str>(PhantomData<(&'a (), Output)>);

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Ident<'a, &'a str> {
    type Output = &'a str;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_ident(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            None => {
                // Err(ScanError::syntax("expected identifier"))
                Err(ScanError::syntax_no_message())
            },
        }
    }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Ident<'a, String> {
    type Output = String;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_ident(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            None => {
                // Err(ScanError::syntax("expected identifier"))
                Err(ScanError::syntax_no_message())
            },
        }
    }
}

#[cfg(not(str_into_output_extra_broken))]
// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
impl<'a, Output> ScanFromStr<'a> for Ident<'a, Output>
where &'a str: Into<Output> {
    type Output = Output;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_ident(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            None => {
                // Err(ScanError::syntax("expected identifier"))
                Err(ScanError::syntax_no_message())
            },
        }
    }
}

fn match_ident(s: &str) -> Option<usize> {
    use ::util::TableUtil;
    use ::unicode::derived_property::{XID_Continue_table, XID_Start_table};

    let mut ics = s.char_indices();

    let first_len = match ics.next() {
        Some((_, '_')) => 1,
        Some((_, c)) if XID_Start_table.span_table_contains(&c) => c.len_utf8(),
        _ => return None,
    };

    let len = ics
        .take_while(|&(_, c)| XID_Continue_table.span_table_contains(&c))
        .map(|(i, c)| i + c.len_utf8())
        .last()
        .unwrap_or(first_len);

    Some(len)
}

#[cfg(test)]
#[test]
fn test_ident() {
    use ::ScanError as SE;
    use ::ScanErrorKind as SEK;

    assert_eq!(match_ident("a"), Some(1));

    assert_match!(Ident::<&str>::scan_from(""), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(Ident::<&str>::scan_from("a"), Ok(("a", 1)));
    assert_match!(Ident::<&str>::scan_from("two words "), Ok(("two", 3)));
    assert_match!(Ident::<&str>::scan_from("two_words "), Ok(("two_words", 9)));
    assert_match!(Ident::<&str>::scan_from("0123abc456 "), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(Ident::<&str>::scan_from("_0123abc456 "), Ok(("_0123abc456", 11)));
    assert_match!(Ident::<&str>::scan_from("f(blah)"), Ok(("f", 1)));
}

/**

Explicitly infer the type of a scanner.

This is useful in cases where you want to only *partially* specify a scanner type, but the partial type cannot be inferred under normal circumstances.

For example, tuples allow their element types to scan to be abstract scanners; *e.g.* `(Word<String>, Hex<i32>)` will scan to `(String, i32)`.  However, this interferes with inferring the scanner type when you *partially* specify a tuple type.  If you attempt to store the result of scanning `(_, _)` into a `(String, i32)`, Rust cannot determine whether the *scanner* type should be `(String, Hex<i32>)`, or `(Word<String>, i32)`, or something else entirely.

This scanner, then, *requires* that the inner type scan to itself and *only* to itself.
*/
pub struct Inferred<T>(PhantomData<T>);

impl<'a, T> ScanFromStr<'a> for Inferred<T>
where T: ScanSelfFromStr<'a> {
    type Output = T;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        T::scan_from(s)
    }
}

/**

Scans everything up to the end of the current line, *or* the end of the input, whichever comes first.  The scanned result *does not* include the line terminator.

Note that this is effectively equivalent to the `Everything` matcher when used with `readln!`.
*/
pub struct Line<'a, Output=&'a str>(PhantomData<(&'a (), Output)>);

#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Line<'a, &'a str> {
    type Output = &'a str;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        let (a, b) = match_line(s);
        Ok((s[..a].into(), b))
    }
}

#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Line<'a, String> {
    type Output = String;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        let (a, b) = match_line(s);
        Ok((s[..a].into(), b))
    }
}

#[cfg(not(str_into_output_extra_broken))]
impl<'a, Output> ScanFromStr<'a> for Line<'a, Output> where &'a str: Into<Output> {
    type Output = Output;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        let (a, b) = match_line(s);
        Ok((s[..a].into(), b))
    }
}

fn match_line(s: &str) -> (usize, usize) {
    let mut ibs = s.bytes().enumerate();

    let line_end;

    loop {
        match ibs.next() {
            Some((i, b'\r')) => {
                line_end = i;
                break;
            },
            Some((i, b'\n')) => return (i, i+1),
            Some(_) => (),
            None => return (s.len(), s.len()),
        }
    }

    // If we get here, it's because we found an `\r` and need to look for an `\n`.
    if let Some((_, b'\n')) = ibs.next() {
        (line_end, line_end + 2)
    } else {
        (line_end, line_end + 1)
    }
}

#[cfg(test)]
#[test]
fn test_line() {
    assert_match!(Line::<&str>::scan_from(""), Ok(("", 0)));
    assert_match!(Line::<&str>::scan_from("abc def"), Ok(("abc def", 7)));
    assert_match!(Line::<&str>::scan_from("abc\ndef"), Ok(("abc", 4)));
    assert_match!(Line::<&str>::scan_from("abc\r\ndef"), Ok(("abc", 5)));
    assert_match!(Line::<&str>::scan_from("abc\rdef"), Ok(("abc", 4)));
}

/**

Scans a sequence of non-space characters into a string.

This *will not* match an empty sequence; there must be at least one non-space character for the scan to succeed.
*/
pub struct NonSpace<'a, Output=&'a str>(PhantomData<(&'a (), Output)>);

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for NonSpace<'a, &'a str> {
    type Output = &'a str;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_non_space(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected at least one non-space character")),
            None => Err(ScanError::syntax_no_message())
        }
    }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for NonSpace<'a, String> {
    type Output = String;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_non_space(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected at least one non-space character")),
            None => Err(ScanError::syntax_no_message())
        }
    }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(not(str_into_output_extra_broken))]
impl<'a, Output> ScanFromStr<'a> for NonSpace<'a, Output>
where &'a str: Into<Output> {
    type Output = Output;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_non_space(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected at least one non-space character")),
            None => Err(ScanError::syntax_no_message())
        }
    }
}

fn match_non_space(s: &str) -> Option<usize> {
    use ::util::TableUtil;
    use ::unicode::property::White_Space_table as WS;

    s.char_indices()
        .take_while(|&(_, c)| !WS.span_table_contains(&c))
        .map(|(i, c)| i + c.len_utf8())
        .last()
}

#[cfg(test)]
#[test]
fn test_non_space() {
    use ::ScanError as SE;
    use ::ScanErrorKind as SEK;

    assert_match!(NonSpace::<&str>::scan_from(""), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(NonSpace::<&str>::scan_from(" abc "), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(NonSpace::<&str>::scan_from("abc "), Ok(("abc", 3)));
    assert_match!(NonSpace::<&str>::scan_from("abc\t"), Ok(("abc", 3)));
    assert_match!(NonSpace::<&str>::scan_from("abc\r"), Ok(("abc", 3)));
    assert_match!(NonSpace::<&str>::scan_from("abc\n"), Ok(("abc", 3)));
    assert_match!(NonSpace::<&str>::scan_from("abc\u{a0}"), Ok(("abc", 3)));
    assert_match!(NonSpace::<&str>::scan_from("abc\u{2003}"), Ok(("abc", 3)));
    assert_match!(NonSpace::<&str>::scan_from("abc\u{200B}"), Ok(("abc\u{200b}", 6)));
    assert_match!(NonSpace::<&str>::scan_from("abc\u{3000}"), Ok(("abc", 3)));
}

/**

Scans a single number into a string.

Specifically, this will match a continuous run of decimal characters (*i.e.* /`\d+`/).

Note that this *includes* non-ASCII decimal characters, meaning it will scan numbers such as "42", "1701", and "𐒩0꘠᧑".
*/
pub struct Number<'a, Output=&'a str>(PhantomData<(&'a (), Output)>);

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Number<'a, &'a str> {
    type Output = &'a str;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_number(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a number")),
            None => Err(ScanError::syntax_no_message()),
        }
    }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Number<'a, String> {
    type Output = String;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_number(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a number")),
            None => Err(ScanError::syntax_no_message()),
        }
    }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(not(str_into_output_extra_broken))]
impl<'a, Output> ScanFromStr<'a> for Number<'a, Output>
where &'a str: Into<Output> {
    type Output = Output;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_number(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a number")),
            None => Err(ScanError::syntax_no_message()),
        }
    }
}

fn match_number(s: &str) -> Option<usize> {
    use ::util::TableUtil;
    use ::unicode::general_category::Nd_table as Nd;

    s.char_indices()
        .take_while(|&(_, c)| Nd.span_table_contains(&c))
        .map(|(i, c)| i + c.len_utf8())
        .last()
}

#[cfg(test)]
#[test]
fn test_number() {
    use ::ScanError as SE;
    use ::ScanErrorKind as SEK;

    assert_match!(Number::<&str>::scan_from(""), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(Number::<&str>::scan_from("a"), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(Number::<&str>::scan_from("0"), Ok(("0", 1)));
    assert_match!(Number::<&str>::scan_from("0x"), Ok(("0", 1)));
    assert_match!(Number::<&str>::scan_from("x0"), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(Number::<&str>::scan_from("123 456 xyz"), Ok(("123", 3)));
    assert_match!(Number::<&str>::scan_from("123 456 xyz"), Ok(("123", 3)));
    assert_match!(Number::<&str>::scan_from("123456789 "), Ok(("123456789", 15)));
    assert_match!(Number::<&str>::scan_from("𐒩0꘠᧑ "), Ok(("𐒩0꘠᧑", 13)));
}

/**

Scans the given `Output` type from its octal representation.
*/
pub struct Octal<Output>(PhantomData<Output>);

impl<'a, Output> ScanFromStr<'a> for Octal<Output>
where Output: ScanFromOctal<'a> {
    type Output = Output;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        Output::scan_from_octal(s)
    }
}

#[cfg(test)]
#[test]
fn test_octal() {
    assert_match!(Octal::<i32>::scan_from("0 1 2 x"), Ok((0o0, 1)));
    assert_match!(Octal::<i32>::scan_from("012x"), Ok((0o12, 3)));
    assert_match!(Octal::<i32>::scan_from("0o012x"), Ok((0o0, 1)));
    assert_match!(Octal::<i32>::scan_from("7558"), Ok((0o755, 3)));
}

/**

An abstract scanner that scans a `(K, V)` value using the syntax `K: V`.

This scanner is designed to take advantage of three things:

1. Maps (*i.e.* associative containers) typically print themselves with the syntax `{key_0: value_0, key_1: value_1, ...}`.

2. Maps typically implement `Extend<(K, V)>`; that is, you can add new items by extending the map with a `(K, V)` tuple.

3. Repeating bindings can be scanned into any container that implements `Default` and `Extend`.

As such, this scanner allows one to parse a `Map` type like so:

```ignore
scan!(input; "{", [let kvs: KeyValuePair<K, V>],*: Map<_, _>, "}" => kvs)
```
*/
pub struct KeyValuePair<K, V>(PhantomData<(K, V)>);

impl<'a, K, V> ScanFromStr<'a> for KeyValuePair<K, V>
where K: ScanSelfFromStr<'a>, V: ScanSelfFromStr<'a> {
    type Output = (K, V);
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        scan!(s;
            (let k: K, ":", let v: V, ..tail) => ((k, v), s.subslice_offset_stable(tail).unwrap())
        )
    }
}

/**

Scans a quoted string.

Specifically, it scans the quoting format used by the `Debug` formatter for strings.

The scanned string has all escape sequences expanded to their values, and the surrounding quotes removed.
*/
pub enum QuotedString {}

impl<'a> ScanFromStr<'a> for QuotedString {
    type Output = String;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        let syn = |s| ScanError::syntax(s);

        let cur = StrCursor::new_at_start(s);
        let (cp, cur) = try!(cur.next_cp().ok_or(syn("expected quoted string")));
        match cp {
            '"' => (),
            _ => return Err(syn("expected `\"` for quoted string"))
        }

        let mut s = String::new();
        let mut cur = cur;
        loop {
            match cur.next_cp() {
                None => return Err(syn("unterminated quoted string")),
                Some(('\\', after)) => {
                    match after.slice_after().split_escape_default() {
                        Err(err) => return Err(ScanError::other(err).add_offset(after.byte_pos())),
                        Ok((cp, tail)) => {
                            // TODO: replace this
                            unsafe { cur.unsafe_set_at(tail); }
                            s.push(cp);
                        },
                    }
                },
                Some(('"', after)) => {
                    cur = after;
                    break;
                },
                Some((cp, after)) => {
                    cur = after;
                    s.push(cp);
                },
            }
        }

        Ok((s, cur.byte_pos()))
    }
}

#[cfg(test)]
#[test]
fn test_quoted_string() {
    use ::ScanError as SE;
    use ::ScanErrorKind as SEK;
    use self::QuotedString as QS;

    assert_match!(QS::scan_from(""), Err(SE { kind: SEK::Syntax(_), .. }));
    assert_match!(QS::scan_from("dummy xyz"), Err(SE { kind: SEK::Syntax(_), .. }));
    assert_match!(QS::scan_from("'dummy' xyz"), Err(SE { kind: SEK::Syntax(_), .. }));
    assert_match!(QS::scan_from("\"dummy\" xyz"),
        Ok((ref s, 7)) if s == "dummy");
    assert_match!(QS::scan_from("\"ab\\\"cd\" xyz"),
        Ok((ref s, 8)) if s == "ab\"cd");
    assert_match!(QS::scan_from("\"ab\\x41cd\" xyz"),
        Ok((ref s, 10)) if s == "abAcd");
    assert_match!(QS::scan_from("\"a\\'b\\u{5B57}c\\0d\" xyz"),
        Ok((ref s, 18)) if s == "a'b字c\0d");
}

/**

Scans a sequence of space characters into a string.

This *will not* match an empty sequence; there must be at least one space character for the scan to succeed.
*/
pub struct Space<'a, Output=&'a str>(PhantomData<(&'a (), Output)>);

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Space<'a, &'a str> {
    type Output = &'a str;

    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_space(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a space")),
            None => Err(ScanError::syntax_no_message()),
        }
    }

    fn wants_leading_junk_stripped() -> bool { false }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Space<'a, String> {
    type Output = String;

    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_space(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a space")),
            None => Err(ScanError::syntax_no_message()),
        }
    }

    fn wants_leading_junk_stripped() -> bool { false }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(not(str_into_output_extra_broken))]
impl<'a, Output> ScanFromStr<'a> for Space<'a, Output>
where &'a str: Into<Output> {
    type Output = Output;

    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_space(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a space")),
            None => Err(ScanError::syntax_no_message()),
        }
    }

    fn wants_leading_junk_stripped() -> bool { false }
}

fn match_space(s: &str) -> Option<usize> {
    use ::util::TableUtil;
    use ::unicode::property::White_Space_table as WS;

    s.char_indices()
        .take_while(|&(_, c)| WS.span_table_contains(&c))
        .map(|(i, c)| i + c.len_utf8())
        .last()
}

#[cfg(test)]
#[test]
fn test_space() {
    use ::ScanError as SE;
    use ::ScanErrorKind as SEK;

    assert_match!(Space::<&str>::scan_from(""), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(Space::<&str>::scan_from("a"), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(Space::<&str>::scan_from("0"), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(Space::<&str>::scan_from(" "), Ok((" ", 1)));
    assert_match!(Space::<&str>::scan_from("\t"), Ok(("\t", 1)));
    assert_match!(Space::<&str>::scan_from("\r"), Ok(("\r", 1)));
    assert_match!(Space::<&str>::scan_from("\n"), Ok(("\n", 1)));
    assert_match!(Space::<&str>::scan_from("\r\n"), Ok(("\r\n", 2)));
    assert_match!(Space::<&str>::scan_from("  \t \n \t\t "), Ok(("  \t \n \t\t ", 9)));
    assert_match!(Space::<&str>::scan_from("  \t \nx \t\t "), Ok(("  \t \n", 5)));
}

/**

Scans a single word into a string.

Specifically, this will match a continuous run of alphabetic, digit, punctuation, mark, and joining characters (*i.e.* /`\w+`/).
*/
pub struct Word<'a, Output=&'a str>(PhantomData<(&'a (), Output)>);

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Word<'a, &'a str> {
    type Output = &'a str;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_word(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a word")),
            None => Err(ScanError::syntax_no_message()),
        }
    }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Word<'a, String> {
    type Output = String;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_word(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a word")),
            None => Err(ScanError::syntax_no_message()),
        }
    }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(not(str_into_output_extra_broken))]
impl<'a, Output> ScanFromStr<'a> for Word<'a, Output>
where &'a str: Into<Output> {
    type Output = Output;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        match match_word(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a word")),
            None => Err(ScanError::syntax_no_message()),
        }
    }
}

fn match_word(s: &str) -> Option<usize> {
    use ::util::TableUtil;
    use ::unicode::regex::PERLW as W;

    s.char_indices()
        .take_while(|&(_, c)| W.span_table_contains(&c))
        .map(|(i, c)| i + c.len_utf8())
        .last()
}

#[cfg(test)]
#[test]
fn test_word() {
    use ::ScanError as SE;
    use ::ScanErrorKind as SEK;

    assert_match!(Word::<&str>::scan_from(""), Err(SE { kind: SEK::SyntaxNoMessage, .. }));
    assert_match!(Word::<&str>::scan_from("a"), Ok(("a", 1)));
    assert_match!(Word::<&str>::scan_from("0"), Ok(("0", 1)));
    assert_match!(Word::<&str>::scan_from("0x"), Ok(("0x", 2)));
    assert_match!(Word::<&str>::scan_from("x0"), Ok(("x0", 2)));
    assert_match!(Word::<&str>::scan_from("123 456 xyz"), Ok(("123", 3)));
    assert_match!(Word::<&str>::scan_from("123 456 xyz"), Ok(("123", 3)));
    assert_match!(Word::<&str>::scan_from("123456789 "), Ok(("123456789", 15)));
    assert_match!(Word::<&str>::scan_from("𐒩0꘠᧑ "), Ok(("𐒩0꘠᧑", 13)));
    assert_match!(Word::<&str>::scan_from("kumquat,bingo"), Ok(("kumquat", 7)));
    assert_match!(Word::<&str>::scan_from("mixed言葉كتابة "), Ok(("mixed言葉كتابة", 21)));
}

/**

Scans a single word-ish thing into a string.

Specifically, this will match a word (a continuous run of alphabetic, digit, punctuation, mark, and joining characters), a number (a continuous run of digits), or a single other non-whitespace character  (*i.e.* /`\w+|\d+|\S`/).
*/
pub struct Wordish<'a, Output=&'a str>(PhantomData<(&'a (), Output)>);

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Wordish<'a, &'a str> {
    type Output = &'a str;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        // TODO: This should be modified to grab an entire *grapheme cluster* in the event it can't find a word or number.
        match match_wordish(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a word, number or some other character")),
            None => Err(ScanError::syntax_no_message()),
        }
    }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(str_into_output_extra_broken)]
impl<'a> ScanFromStr<'a> for Wordish<'a, String> {
    type Output = String;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        // TODO: This should be modified to grab an entire *grapheme cluster* in the event it can't find a word or number.
        match match_wordish(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a word, number or some other character")),
            None => Err(ScanError::syntax_no_message()),
        }
    }
}

// FIXME: Error message omitted due to https://github.com/rust-lang/rust/issues/26448.
#[cfg(not(str_into_output_extra_broken))]
impl<'a, Output> ScanFromStr<'a> for Wordish<'a, Output>
where &'a str: Into<Output> {
    type Output = Output;
    fn scan_from<I: ScanInput<'a>>(s: I) -> Result<(Self::Output, usize), ScanError> {
        let s = s.as_str();
        // TODO: This should be modified to grab an entire *grapheme cluster* in the event it can't find a word or number.
        match match_wordish(s) {
            Some(b) => {
                let word = &s[..b];
                let tail = &s[b..];
                Ok((word.into(), s.subslice_offset_stable(tail).unwrap()))
            },
            // None => Err(ScanError::syntax("expected a word, number or some other character")),
            None => Err(ScanError::syntax_no_message()),
        }
    }
}

fn match_wordish(s: &str) -> Option<usize> {
    use ::util::TableUtil;
    use ::unicode::regex::PERLW;

    let word_len = s.char_indices()
        .take_while(|&(_, c)| PERLW.span_table_contains(&c))
        .map(|(i, c)| i + c.len_utf8())
        .last();

    match word_len {
        Some(n) => Some(n),
        None => s.chars().next().map(|c| c.len_utf8()),
    }
}