kamo 0.9.2

A library to assist in the creation of an interpreter or compiler and its associated runtime.
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
//! # Unicode character parsers
//!
//! This module contains parsers that match a single or multiple Unicode
//! characters. They return either the matched character or a slice containing
//! the matched characters.
//!
//! ## Example
//!
//! ```rust
//! # use kamo::parser::{prelude::{*, unicode::*}, Input};
//! let (output, input) = alpha0(Input::from("abc京123"))
//!     .expect("valid output");
//!
//! assert_eq!(output, "abc京");
//! assert_eq!(input, Input::from("123"));
//!
//! let (output, input) = alpha0(Input::from("123abc京"))
//!     .expect("valid output");
//!
//! assert_eq!(output, "");
//! assert_eq!(input, Input::from("123abc京"));
//! ```

use crate::parser::{code, predicate, Input, ParseError, ParseResult};

use super::CharacterError;

/// Matches zero or more Unicode alphabetic characters.
///
/// # Errors
///
/// This function does not return any errors.
///
/// # Example
///
/// ```rust
/// # use kamo::parser::{prelude::{*, unicode::*}, Input};
/// let (output, input) = alpha0(Input::from("abc京123"))
///     .expect("valid output");
///
/// assert_eq!(output, "abc京");
/// assert_eq!(input, Input::from("123"));
///
/// let (output, input) = alpha0(Input::from("123abc京"))
///     .expect("valid output");
///
/// assert_eq!(output, "");
/// assert_eq!(input, Input::from("123abc京"));
/// ```
pub fn alpha0(input: Input<'_>) -> ParseResult<&str> {
    let mut cursor = input;
    let output = cursor.advance_while(predicate::is_alpha);

    Ok((output, cursor))
}

/// Matches one or more Unicode alphabetic characters.
///
/// # Errors
///
/// If the input is empty or the first character is not an alphabetical
/// character, it returns a [`ParseError`] with the error code
/// [`ERR_ALPHA`](code::ERR_ALPHA) and the error variant
/// [`CharacterError::UnicodeAlpha`].
///
/// # Example
///
/// ```rust
/// # use kamo::{Position, parser::{prelude::{*, unicode::*}, CharacterError, code, Input}};
/// let (output, input) = alpha1(Input::from("abc京123"))
///     .expect("valid output");
///
/// assert_eq!(output, "abc京");
/// assert_eq!(input, Input::from("123"));
///
/// let error = alpha1(Input::from("123abc")).expect_err("error output");
///
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_ALPHA,
///     CharacterError::Alpha
/// ));
///
/// let error = alpha1(Input::from("")).expect_err("error output");
///
/// assert!(error.is_eof());
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_ALPHA,
///     CharacterError::Alpha
/// ));
/// ```
pub fn alpha1(input: Input<'_>) -> ParseResult<&str> {
    if let Some(ch) = input.current() {
        if !predicate::is_alpha(ch) {
            return Err(ParseError::new(
                input,
                code::ERR_ALPHA,
                CharacterError::UnicodeAlpha,
            ));
        }
        alpha0(input)
    } else {
        Err(ParseError::eof(input).and(input, code::ERR_ALPHA, CharacterError::UnicodeAlpha))
    }
}

/// Matches zero or more Unicode alphanumeric characters.
///
/// # Errors
///
/// This function does not return any errors.
///
/// # Example
///
/// ```rust
/// # use kamo::parser::{prelude::{*, unicode::*}, Input};
/// let (output, input) = alphanum0(Input::from("abc京123"))
///     .expect("valid output");
///
/// assert_eq!(output, "abc京123");
/// assert_eq!(input, Input::from(""));
///
/// let (output, input) = alphanum0(Input::from("123abc京"))
///     .expect("valid output");
///
/// assert_eq!(output, "123abc京");
/// assert_eq!(input, Input::from(""));
/// ```
pub fn alphanum0(input: Input<'_>) -> ParseResult<&str> {
    let mut cursor = input;
    let output = cursor.advance_while(predicate::is_alphanum);

    Ok((output, cursor))
}

/// Matches one or more Unicode alphanumeric characters.
///
/// # Errors
///
/// If the input is empty or the first character is not an alphanumeric
/// character, it returns a [`ParseError`] with the error code
/// [`ERR_ALPHANUM`](code::ERR_ALPHANUM) and the error variant
/// [`CharacterError::UnicodeAlphaNum`].
///
/// # Example
///
/// ```rust
/// # use kamo::{Position, parser::{prelude::{*, unicode::*}, CharacterError, code, Input}};
/// let (output, input) = alphanum1(Input::from("abc京123"))
///     .expect("valid output");
///
/// assert_eq!(output, "abc京123");
/// assert_eq!(input, Input::from(""));
///
/// let error = alphanum1(Input::from("_123abc")).expect_err("error output");
///
/// assert_eq!(error, ParseError::new(
///    Position::new(0, 1, 1),
///   code::ERR_ALPHANUM,
///   CharacterError::UnicodeAlphaNum
/// ));
///
/// let error = alphanum1(Input::from("")).expect_err("error output");
///
/// assert!(error.is_eof());
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_ALPHANUM,
///     CharacterError::UnicodeAlphaNum
/// ));
/// ```
pub fn alphanum1(input: Input<'_>) -> ParseResult<&str> {
    if let Some(ch) = input.current() {
        if !predicate::is_alphanum(ch) {
            return Err(ParseError::new(
                input,
                code::ERR_ALPHANUM,
                CharacterError::UnicodeAlphaNum,
            ));
        }
        alphanum0(input)
    } else {
        Err(ParseError::eof(input).and(input, code::ERR_ALPHANUM, CharacterError::UnicodeAlphaNum))
    }
}

/// Matches zero or more Unicode numeric characters.
///
/// # Errors
///
/// This function does not return any errors.
///
/// # Example
///
/// ```rust
/// # use kamo::parser::{prelude::{*, unicode::*}, Input};
/// let (output, input) = numeric0(Input::from("abc京123"))
///     .expect("valid output");
///
/// assert_eq!(output, "");
/// assert_eq!(input, Input::from("abc京123"));
///
/// let (output, input) = numeric0(Input::from("123abc京"))
///     .expect("valid output");
///
/// assert_eq!(output, "123");
/// assert_eq!(input, Input::from("abc京"));
/// ```
pub fn numeric0(input: Input<'_>) -> ParseResult<&str> {
    let mut cursor = input;
    let output = cursor.advance_while(predicate::is_numeric);

    Ok((output, cursor))
}

/// Matches one or more Unicode numeric characters.
///
/// # Errors
///
/// If the input is empty or the first character is not a numeric character,
/// it returns a [`ParseError`] with the error code
/// [`ERR_NUMERIC`](code::ERR_NUMERIC) and the error variant
/// [`CharacterError::UnicodeNumeric`].
///
/// # Example
///
/// ```rust
/// # use kamo::{Position, parser::{prelude::{*, unicode::*}, CharacterError, code, Input}};
/// let (output, input) = numeric1(Input::from("123abc京"))
///     .expect("valid output");
///
/// assert_eq!(output, "123");
/// assert_eq!(input, Input::from("abc京"));
///
/// let error = numeric1(Input::from("abc123京")).expect_err("error output");
///
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_NUMERIC,
///     CharacterError::UnicodeNumeric
/// ));
///
/// let error = numeric1(Input::from("")).expect_err("error output");
///
/// assert!(error.is_eof());
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_NUMERIC,
///     CharacterError::UnicodeNumeric
/// ));
/// ```
pub fn numeric1(input: Input<'_>) -> ParseResult<&str> {
    if let Some(ch) = input.current() {
        if !predicate::is_numeric(ch) {
            return Err(ParseError::new(
                input,
                code::ERR_NUMERIC,
                CharacterError::UnicodeNumeric,
            ));
        }
        numeric0(input)
    } else {
        Err(ParseError::eof(input).and(input, code::ERR_NUMERIC, CharacterError::UnicodeNumeric))
    }
}

/// Matches zero or more Unicode lowercase characters.
///
/// # Errors
///
/// This function does not return any errors.
///
/// # Example
///
/// ```rust
/// # use kamo::parser::{prelude::{*, unicode::*}, Input};
/// let (output, input) = lowercase0(Input::from("abc京123"))
///     .expect("valid output");
///
/// assert_eq!(output, "abc");
/// assert_eq!(input, Input::from("京123"));
///
/// let (output, input) = lowercase0(Input::from("Abc京"))
///     .expect("valid output");
///
/// assert_eq!(output, "");
/// assert_eq!(input, Input::from("Abc京"));
/// ```
pub fn lowercase0(input: Input<'_>) -> ParseResult<&str> {
    let mut cursor = input;
    let output = cursor.advance_while(predicate::is_lowercase);

    Ok((output, cursor))
}

/// Matches one or more Unicode lowercase characters.
///
/// # Errors
///
/// If the input is empty or the first character is not a lowercase character,
/// it returns a [`ParseError`] with the error code
/// [`ERR_LOWERCASE`](code::ERR_LOWERCASE) and the error variant
/// [`CharacterError::UnicodeLowercase`].
///
/// # Example
///
/// ```rust
/// # use kamo::{Position, parser::{prelude::{*, unicode::*}, CharacterError, code, Input}};
/// let (output, input) = lowercase1(Input::from("abc京123"))
///  .expect("valid output");
///
/// assert_eq!(output, "abc");
/// assert_eq!(input, Input::from("京123"));
///
/// let error = lowercase1(Input::from("Abc京")).expect_err("error output");
///
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_LOWERCASE,
///     CharacterError::UnicodeLowercase
/// ));
///
/// let error = lowercase1(Input::from("")).expect_err("error output");
///
/// assert!(error.is_eof());
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_LOWERCASE,
///     CharacterError::UnicodeLowercase
/// ));
/// ```
pub fn lowercase1(input: Input<'_>) -> ParseResult<&str> {
    if let Some(ch) = input.current() {
        if !predicate::is_lowercase(ch) {
            return Err(ParseError::new(
                input,
                code::ERR_LOWERCASE,
                CharacterError::UnicodeLowercase,
            ));
        }
        lowercase0(input)
    } else {
        Err(ParseError::eof(input).and(
            input,
            code::ERR_LOWERCASE,
            CharacterError::UnicodeLowercase,
        ))
    }
}

/// Matches zero or more Unicode uppercase characters.
///
/// # Errors
///
/// This function does not return any errors.
///
/// # Example
///
/// ```rust
/// # use kamo::parser::{prelude::{*, unicode::*}, Input};
/// let (output, input) = uppercase0(Input::from("ABC京123"))
///     .expect("valid output");
///
/// assert_eq!(output, "ABC");
/// assert_eq!(input, Input::from("京123"));
///
/// let (output, input) = uppercase0(Input::from("abc京"))
///     .expect("valid output");
///
/// assert_eq!(output, "");
/// assert_eq!(input, Input::from("abc京"));
/// ```
pub fn uppercase0(input: Input<'_>) -> ParseResult<&str> {
    let mut cursor = input;
    let output = cursor.advance_while(predicate::is_uppercase);

    Ok((output, cursor))
}

/// Matches one or more Unicode uppercase characters.
///
/// # Errors
///
/// If the input is empty or the first character is not an uppercase character,
/// it returns a [`ParseError`] with the error code
/// [`ERR_UPPERCASE`](code::ERR_UPPERCASE) and the error variant
/// [`CharacterError::UnicodeUppercase`].
///
/// # Example
///
/// ```rust
/// # use kamo::{Position, parser::{prelude::{*, unicode::*}, CharacterError, code, Input}};
/// let (output, input) = uppercase1(Input::from("ABC京123"))
///     .expect("valid output");
///
/// assert_eq!(output, "ABC");
/// assert_eq!(input, Input::from("京123"));
///
/// let error = uppercase1(Input::from("abc京")).expect_err("error output");
///
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_UPPERCASE,
///     CharacterError::UnicodeUppercase
/// ));
///
/// let error = uppercase1(Input::from("")).expect_err("error output");
///
/// assert!(error.is_eof());
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_UPPERCASE,
///     CharacterError::UnicodeUppercase
/// ));
/// ```
pub fn uppercase1(input: Input<'_>) -> ParseResult<&str> {
    if let Some(ch) = input.current() {
        if !predicate::is_uppercase(ch) {
            return Err(ParseError::new(
                input,
                code::ERR_UPPERCASE,
                CharacterError::UnicodeUppercase,
            ));
        }
        uppercase0(input)
    } else {
        Err(ParseError::eof(input).and(
            input,
            code::ERR_UPPERCASE,
            CharacterError::UnicodeUppercase,
        ))
    }
}

/// Matches zero or more Unicode whitespace characters.
///
/// # Errors
///
/// This function does not return any errors.
///
/// # Example
///
/// ```rust
/// # use kamo::parser::{prelude::{*, unicode::*}, Input};
/// let (output, input) = whitespace0(Input::from(" \t\n\u{a0}abc京123"))
///     .expect("valid output");
///
/// assert_eq!(output, " \t\n\u{a0}");
/// assert_eq!(input, Input::from("abc京123"));
///
/// let (output, input) = whitespace0(Input::from("abc京123"))
///     .expect("valid output");
///
/// assert_eq!(output, "");
/// assert_eq!(input, Input::from("abc京123"));
/// ```
pub fn whitespace0(input: Input<'_>) -> ParseResult<&str> {
    let mut cursor = input;
    let output = cursor.advance_while(predicate::is_whitespace);

    Ok((output, cursor))
}

/// Matches one or more Unicode whitespace characters.
///
/// # Errors
///
/// If the input is empty or the first character is not a whitespace character,
/// it returns a [`ParseError`] with the error code
/// [`ERR_WHITESPACE`](code::ERR_WHITESPACE) and the error variant
/// [`CharacterError::UnicodeWhitespace`].
///
/// # Example
///
/// ```rust
/// # use kamo::{Position, parser::{prelude::{*, unicode::*}, CharacterError, code, Input}};
/// let (output, input) = whitespace1(Input::from(" \t\n\u{a0}abc京123"))
///     .expect("valid output");
///
/// assert_eq!(output, " \t\n\u{a0}");
/// assert_eq!(input, Input::from("abc京123"));
///
/// let error = whitespace1(Input::from("abc京123")).expect_err("error output");
///
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_WHITESPACE,
///     CharacterError::UnicodeWhitespace
/// ));
///
/// let error = whitespace1(Input::from("")).expect_err("error output");
///
/// assert!(error.is_eof());
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_WHITESPACE,
///     CharacterError::UnicodeWhitespace
/// ));
/// ```
pub fn whitespace1(input: Input<'_>) -> ParseResult<&str> {
    if let Some(ch) = input.current() {
        if !predicate::is_whitespace(ch) {
            return Err(ParseError::new(
                input,
                code::ERR_WHITESPACE,
                CharacterError::UnicodeWhitespace,
            ));
        }
        whitespace0(input)
    } else {
        Err(ParseError::eof(input).and(
            input,
            code::ERR_WHITESPACE,
            CharacterError::UnicodeWhitespace,
        ))
    }
}

/// Matches a Unicode identifier start character.
///
/// Implements the [Unicode Standard Annex #31](https://www.unicode.org/reports/tr31/).
/// The standard states that the following characters are valid identifier start
/// characters:
///
/// > Start characters are derived from the Unicode `General_Category` of
/// > uppercase letters, lowercase letters, titlecase letters, modifier letters,
/// > other letters, letter numbers, plus `Other_ID_Start`, minus
/// > `Pattern_Syntax` and `Pattern_White_Space` code points.
///
/// This includes the ASCII characters `A-Z` and `a-z`.
///
/// # Errors
///
/// If the input is empty or the first character is not an identifier start
/// character, it returns a [`ParseError`] with the error code
/// [`ERR_IDENT_START`](code::ERR_IDENT_START) and the error variant
/// [`CharacterError::UnicodeIdentStart`].
///
/// # Example
///
/// ```rust
/// # use kamo::{Position, parser::{prelude::{*, unicode::*}, CharacterError, code, Input}};
/// let (output, input) = ident_start(Input::from("京abc123"))
///     .expect("valid output");
///
/// assert_eq!(output, '京');
/// assert_eq!(input, Input::from("abc123"));
///
/// let (output, input) = ident_start(Input::from("abc123"))
///     .expect("valid output");
///
/// assert_eq!(output, 'a');
/// assert_eq!(input, Input::from("bc123"));
///
/// let error = ident_start(Input::from("123abc")).expect_err("error output");
///
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_IDENT_START,
///     CharacterError::UnicodeIdentStart
/// ));
///
/// let error = ident_start(Input::from("")).expect_err("error output");
///
/// assert!(error.is_eof());
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_IDENT_START,
///     CharacterError::UnicodeIdentStart
/// ));
/// ```
pub fn ident_start(input: Input<'_>) -> ParseResult<char> {
    let mut cursor = input;

    cursor.current().map_or_else(
        || {
            Err(ParseError::eof(input).and(
                input,
                code::ERR_IDENT_START,
                CharacterError::UnicodeIdentStart,
            ))
        },
        |ch| {
            if predicate::is_ident_start(ch) {
                cursor.advance();
                Ok((ch, cursor))
            } else {
                Err(ParseError::new(
                    input,
                    code::ERR_IDENT_START,
                    CharacterError::UnicodeIdentStart,
                ))
            }
        },
    )
}

/// Matches a Unicode identifier start character or a character that satisfies
/// the given predicate.
///
/// The predicate is a closure or function that takes a character and returns a
/// boolean.
///
/// Implements the [Unicode Standard Annex #31](https://www.unicode.org/reports/tr31/).
/// The standard states that the following characters are valid identifier start
/// characters:
///
/// > Start characters are derived from the Unicode `General_Category` of
/// > uppercase letters, lowercase letters, titlecase letters, modifier letters,
/// > other letters, letter numbers, plus `Other_ID_Start`, minus
/// > `Pattern_Syntax` and `Pattern_White_Space` code points.
///
/// This includes the ASCII characters `A-Z` and `a-z`.
///
/// # Example
///
/// ```rust
/// # use kamo::{Position, parser::{prelude::{*, unicode::*}, CharacterError, code, Input}};
/// let (output, input) = ident_start_or(|ch| ch == '_')
///     (Input::from("_京abc123")).expect("valid output");
///
/// assert_eq!(output, '_');
/// assert_eq!(input, Input::from("京abc123"));
///
/// let (output, input) = ident_start_or(|ch| ch == '_')
///     (Input::from("abc123")).expect("valid output");
///
/// assert_eq!(output, 'a');
/// assert_eq!(input, Input::from("bc123"));
///
/// let error = ident_start_or(|ch| ch == '_')
///     (Input::from("123abc")).expect_err("error output");
///
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_IDENT_START,
///     CharacterError::UnicodeIdentStart
/// ));
///
/// let error = ident_start_or(|ch| ch == '_')(Input::from(""))
///     .expect_err("error output");
///
/// assert!(error.is_eof());
/// assert_eq!(error, ParseError::new(
///     Position::new(0, 1, 1),
///     code::ERR_IDENT_START,
///     CharacterError::UnicodeIdentStart
/// ));
/// ```
pub fn ident_start_or<F>(predicate: F) -> impl Fn(Input) -> ParseResult<char> + Copy
where
    F: Fn(char) -> bool + Copy,
{
    move |input| {
        let mut cursor = input;

        cursor.current().map_or_else(
            || {
                Err(ParseError::eof(input).and(
                    input,
                    code::ERR_IDENT_START,
                    CharacterError::UnicodeIdentStart,
                ))
            },
            |ch| {
                if predicate::is_ident_start(ch) || predicate(ch) {
                    cursor.advance();
                    Ok((ch, cursor))
                } else {
                    Err(ParseError::new(
                        input,
                        code::ERR_IDENT_START,
                        CharacterError::UnicodeIdentStart,
                    ))
                }
            },
        )
    }
}

/// Matches zero or more Unicode identifier continuation characters.
///
/// Implements the [Unicode Standard Annex #31](https://www.unicode.org/reports/tr31/).
/// The standard states that the following characters are valid identifier
/// continuation characters:
///
/// > Continue characters include [start characters](ident_start), plus
/// > characters having the Unicode `General_Category` of nonspacing marks,
/// > spacing combining marks, decimal number, connector punctuation, plus
/// > `Other_ID_Continue`, minus `Pattern_Syntax` and `Pattern_White_Space` code
/// > points.
///
/// This includes the ASCII characters `A-Z`, `a-z`, `0-9` and `_`.
///
/// # Errors
///
/// This function does not return any errors.
///
/// # Example
///
/// ```rust
/// # use kamo::{Position, parser::{prelude::{*, unicode::*}, CharacterError, code, Input}};
///
/// let (output, input) = ident_cont(Input::from("_abc123-"))
///     .expect("valid output");
///
/// assert_eq!(output, "_abc123");
/// assert_eq!(input, Input::from("-"));
///
/// let (output, input) = ident_cont(Input::from("京abc123"))
///     .expect("valid output");
///
/// assert_eq!(output, "京abc123");
/// assert_eq!(input, Input::from(""));
/// ```
pub fn ident_cont(input: Input<'_>) -> ParseResult<&str> {
    let mut cursor = input;
    let output = cursor.advance_while(predicate::is_ident_cont);

    Ok((output, cursor))
}

/// Matches zero or more Unicode identifier continuation character or a
/// character that satisfies the given predicate.
///
/// The predicate is a closure or function that takes a character and returns a
/// boolean.
///
/// Implements the [Unicode Standard Annex #31](https://www.unicode.org/reports/tr31/).
/// The standard states that the following characters are valid identifier
/// continuation characters:
///
/// > Continuation characters are derived from the Unicode `General_Category` of
/// > uppercase letters, lowercase letters, titlecase letters, modifier letters,
/// > other letters, letter numbers, plus `Other_ID_Continue`, minus
/// > `Pattern_Syntax` and `Pattern_White_Space` code points.
///
/// This includes the ASCII characters `A-Z`, `a-z` and `0-9`.
///
/// # Example
///
/// ```rust
/// # use kamo::{Position, parser::{prelude::{*, unicode::*}, CharacterError, code, Input}};
///
/// let (output, input) = ident_cont_or(|ch| ch == '$')
///     (Input::from("_abc123$-")).expect("valid output");
///
/// assert_eq!(output, "_abc123$");
/// assert_eq!(input, Input::from("-"));
///
/// let (output, input) = ident_cont_or(|ch| ch == '$')
///     (Input::from("京abc123$")).expect("valid output");
///
/// assert_eq!(output, "京abc123$");
/// assert_eq!(input, Input::from(""));
/// ```
pub fn ident_cont_or<F>(predicate: F) -> impl Fn(Input) -> ParseResult<&str> + Copy
where
    F: Fn(char) -> bool + Copy,
{
    move |input| {
        let mut cursor = input;
        let output = cursor.advance_while(|ch| predicate::is_ident_cont(ch) || predicate(ch));

        Ok((output, cursor))
    }
}