cookie_cutter_core 0.2.0

A feature-rich template engine with context aware escaping and both runtime and compiletime compilation
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
pub mod source_file;

use std::{
    collections::HashMap,
    fmt::{Debug, Display},
    num::{ParseFloatError, ParseIntError},
    ops::Range,
};

use ariadne::{Config, IndexType, Label, Report, ReportBuilder, ReportKind};
use chumsky::{
    extra,
    input::{Input, MappedSpan},
    primitive::{any, choice, just, map_ctx},
    text::{newline, TextExpected},
    util::Maybe,
    DefaultExpected, Parser,
};

use crate::{
    ast::{CurlyKind, EscapeSequence},
    AriadneCache, Span,
};

#[derive(PartialEq, Clone)]
/// Represents a failure to parse a source file
pub enum Error<'s> {
    /// In case there were multiple errors
    Multiple(Vec<Error<'s>>),
    /// An end of input was expected but the source file did not end
    ExpectedEndOfInput(Span<'s>),
    /// The source file ended unexpectedly
    UnexpectedEndOfInput(Span<'s>),
    /// One of the following characters were expected
    ExpectedOneOfMultipleChars {
        /// Where they were expected
        at: Span<'s>,
        /// Which character we got instead (if any in case of end of file)
        got: Option<char>,
        /// Which characters were expected
        expected: Vec<char>,
    },
    /// A specific character was expected
    ExpectedChar {
        /// Where it was expected
        at: Span<'s>,
        /// Which character we got instead (if any in case of end of file)
        got: Option<char>,
        /// Which character was expected
        expected: char,
    },
    /// Encountered an unexpected character
    UnexpectedChar(Span<'s>, Option<char>),
    /// Expected a digit
    ExpectedDigit {
        /// Where it was expected
        at: Span<'s>,
        /// Which character we got instead (if any in case of end of file)
        got: Option<char>,
        /// Which range of digits was expected
        expected: Range<u32>,
    },
    /// Expected a specific identifier (a name)
    ExpectedIdentifier {
        /// Where it was expected
        at: Span<'s>,
        /// Which character we got instead (if any in case of end of file)
        got: Option<char>,
        /// Which identifier we expected
        expected: &'s str,
    },
    /// Expected part of an identifier
    ExpectedIdentifierPart {
        /// Where it was expected
        at: Span<'s>,
        /// Which character we got instead (if any in case of end of file)
        got: Option<char>,
    },
    /// Expected inline whitespace (such as spaces and tabs but not line feeds)
    ExpectedInlineWhitespace {
        /// Where it was expected
        at: Span<'s>,
        /// Which character we got instead (if any in case of end of file)
        got: Option<char>,
    },
    /// Expected whitespace characters (such as spaces, tabs and line feeds)
    ExpectedWhitespace {
        /// Where it was expected
        at: Span<'s>,
        /// Which character we got instead (if any in case of end of file)
        got: Option<char>,
    },
    /// Expected a newline (such as LF or CRLF)
    ExpectedNewline {
        /// Where it was expected
        at: Span<'s>,
        /// Which character we got instead (if any in case of end of file)
        got: Option<char>,
    },
    /// A delimiter (such as a brace or bracket) did not get closed
    UnclosedDelimiter {
        /// Where the opening brace or backet is
        start: Span<'s>,
        /// Which kind of delimiter wasn't closed
        kind: DelimiterType,
    },
    /// Encountered an unknown escape sequence (character sequence starting with a backslash to use
    /// a certain sequence with a special meaning literally; such as \n)
    UnknownEscapeSequence(Span<'s>),
    /// Template bodies (templates and template literals) need to start with a {\n but we did not
    /// get that newline
    MissingTemplateBodyStartNewline {
        /// Where the template starts
        template_start: Span<'s>,
        /// Where we expected the newline
        expected_newline_span: Span<'s>,
    },
    /// Encountered an invalid int literal (such as 10); this could be caused by writing a number
    /// that is too large to fit in the signed 64 bit integer for example
    InvalidIntLiteral(ParseIntError, Span<'s>),
    /// Encountered an invalid float literal (such as 1.45); this could be caused by writing a
    /// number that does not fit in a 64 bit float for example
    InvalidFloatLiteral(ParseFloatError, Span<'s>),
    // false positive, since we mean the template language not rust
    #[allow(clippy::doc_markdown)]
    /// Encountered an invalid numeric member access (such as some_tuple.0); this could be caused
    /// by writing a number that is too large to fit in a 32 bit unsigned integer
    InvalidNumericMemberAccess(ParseIntError, Span<'s>),
    /// Encountered a temlate curly escape sequence (such as \{{) that had a wrong amount of curly
    /// braces. This can happen if you try to escape too few curly braces (in which case you can
    /// just them without the backslash).
    WrongCurlyCountTemplateCurlyEscapeSequence {
        /// Whether the escape sequence was of open or closed curly braces
        curly_kind: CurlyKind,

        /// Where the curly braces are that started the template or template literal
        template_curly_span: Span<'s>,
        /// The count of curly braces that started the template or template literal
        template_curly_count: usize,

        /// Where the escape sequence is
        escape_sequence_span: Span<'s>,
        /// How many curly braces the escape sequence used
        escape_sequence_curly_count: usize,
    },
}

impl<'s> Error<'s> {
    // hardly avoidable
    #[allow(clippy::too_many_lines)]
    fn write_to_buf(&self, cache: &mut AriadneCache<'s>, buf: &mut Vec<u8>, with_color: bool) {
        fn builder(at: Span<'_>, with_color: bool) -> ReportBuilder<'_, Span<'_>> {
            Report::build(ReportKind::Error, at).with_config(
                Config::new()
                    .with_color(with_color)
                    .with_index_type(IndexType::Byte),
            )
        }

        fn write<'s>(
            builder: ReportBuilder<'s, Span<'s>>,
            cache: &mut AriadneCache<'s>,
            buf: &mut Vec<u8>,
        ) {
            builder.finish().write(cache, buf).unwrap();
        }

        match self {
            Self::Multiple(inner) => {
                for err in inner {
                    err.write_to_buf(cache, buf, with_color);
                }
            }

            Self::ExpectedEndOfInput(at) => write(
                builder(*at, with_color).with_message("Expected end of input").with_label(Label::new(*at).with_message("Expected the end of input here")),
                cache,
                buf
            ),

            Self::UnexpectedEndOfInput(at) => write(
                builder(*at, with_color).with_message("Unexpected end of input"),
                cache,
                buf
            ),

            Self::ExpectedChar { at, expected, got: _ } => write(
                builder(*at, with_color).with_message("Expected character").with_label(Label::new(*at).with_message(format!("Expected {expected:?}"))),
                cache,
                buf
            ),

            Self::ExpectedOneOfMultipleChars { at, expected, got: _ } =>  write(
                builder(*at, with_color).with_message("Expected a different character").with_label(Label::new(*at).with_message(format!("Expected one of the following characters: {expected:?}"))),
                cache,
                buf
            ),

            Self::UnexpectedChar(at, _got) => write(
                builder(*at, with_color).with_message("Unexpected character").with_label(Label::new(*at).with_message("Unexpected character")),
                cache,
                buf
            ),

            Self::ExpectedDigit { at, expected, got: _ } => write(
                builder(*at, with_color).with_message("Expected a digit").with_label(Label::new(*at).with_message(format!("Expected a digit in the range of {expected:?}"))),
                cache,
                buf
            ),

            Self::ExpectedIdentifier { at, expected, got: _ } => write(
                builder(*at, with_color).with_message("Expected identifier").with_label(Label::new(*at).with_message(format!("Expected identifier {expected:?}"))),
                cache,
                buf
            ),

            Self::ExpectedIdentifierPart { at, got: _ } => write(
                builder(*at, with_color).with_message("Expected identifier").with_label(Label::new(*at).with_message("Expected an indentifier")),
                cache,
                buf
            ),

            Self::ExpectedInlineWhitespace { at, got: _ } => write(
                builder(*at, with_color).with_message("Expected inline whitespace").with_label(Label::new(*at).with_message("Expected inline whitespace here")),
                cache,
                buf
            ),

            Self::ExpectedWhitespace { at, got: _ } => write(
                builder(*at, with_color).with_message("Expected whitespace").with_label(Label::new(*at).with_message("Expected whitespace here")),
                cache,
                buf
            ),

            Self::ExpectedNewline { at, got: _ } => write(
                builder(*at, with_color).with_message("Expected a newline").with_label(Label::new(*at).with_message("Expected a newline here")),
                cache,
                buf
            ),

            Self::InvalidFloatLiteral(err, at) => write(
                builder(*at, with_color).with_message(format!("Invalid float literal: {err}")).with_label(Label::new(*at).with_message("This float literal is invalid")),
                cache,
                buf,
            ),

            Self::InvalidIntLiteral(err, at) => write(
                builder(*at, with_color).with_message(format!("Invalid int literal: {err}")).with_label(Label::new(*at).with_message("This int literal is invalid")),
                cache,
                buf,
            ),

            Self::InvalidNumericMemberAccess(err, at) => write(
                builder(*at, with_color).with_message(format!("Invalid tuple access: {err}")).with_label(Label::new(*at).with_message("This tuple access is invalid")),
                cache,
                buf,
            ),

            Self::MissingTemplateBodyStartNewline {
                template_start,
                expected_newline_span,
            } => write(
                builder(*expected_newline_span, with_color)
                    .with_message("Missing Template body newline")
                    .with_note("Template bodies need to start on a newline")
                    .with_label(
                        Label::new(*expected_newline_span)
                            .with_message("Expected a newline here")
                            .with_priority(1),
                    )
                    .with_label(
                        Label::new(*template_start)
                            .with_message("Template body starts here")
                            .with_priority(0),
                    ),
                cache,
                buf,
            ),

            Self::UnclosedDelimiter { start, kind } => write(
                builder(*start, with_color)
                    .with_message("Unclosed delimiter")
                    .with_label(
                        Label::new(*start).with_message(format!("This {kind} was never closed")),
                    ),
                cache,
                buf,
            ),

            Self::UnknownEscapeSequence(at) => write(
                builder(*at, with_color).with_message("Unknown escape sequence").with_label(Label::new(*at).with_message("This escape sequence is unknown")),
                cache,
                buf,
            ),

            Self::WrongCurlyCountTemplateCurlyEscapeSequence {
                curly_kind,
                template_curly_span,
                template_curly_count,
                escape_sequence_span,
                escape_sequence_curly_count,
            } => write(
                builder(*escape_sequence_span, with_color)
                    .with_message("Too few curly braces in escape sequence")
                    .with_label(Label::new(*template_curly_span).with_message(format!(
                        "Template body starts here with {template_curly_count} curly braces"
                    )))
                    .with_label(Label::new(*escape_sequence_span).with_message(format!("The template body uses {template_curly_count} curly braces but this {curly_kind} curly brace escape sequence escapes just {escape_sequence_curly_count}")))
                    .with_note(format!("Up to {template_curly_count} consecutive curly braces can be used without escaping them")),
                    cache,
                    buf
            ),
        }
    }
}

impl Debug for Error<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("\n")?;
        <Self as Display>::fmt(self, f)
    }
}

impl Display for Error<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut buf = Vec::new();
        let mut cache = AriadneCache(HashMap::new());

        self.write_to_buf(&mut cache, &mut buf, !f.alternate());

        f.write_str(std::str::from_utf8(&buf).unwrap())
    }
}

impl std::error::Error for Error<'_> {}

impl<'s, F: Fn(chumsky::span::SimpleSpan) -> Span<'s> + 's>
    chumsky::error::Error<'s, MappedSpan<Span<'s>, &'s str, F>> for Error<'s>
{
    fn merge(self, other: Self) -> Self {
        match (self, other) {
            (Error::Multiple(mut previous), Error::Multiple(next)) => {
                previous.extend(next);
                Error::Multiple(previous)
            }
            (Error::Multiple(mut previous), other) => {
                previous.push(other);
                Error::Multiple(previous)
            }
            (first, second) => Error::Multiple(vec![first, second]),
        }
    }
}

impl<'s, F: Fn(chumsky::span::SimpleSpan) -> Span<'s> + 's>
    chumsky::error::LabelError<'s, MappedSpan<Span<'s>, &'s str, F>, DefaultExpected<'s, char>>
    for Error<'s>
{
    fn expected_found<E: IntoIterator<Item = DefaultExpected<'s, char>>>(
        expected: E,
        found: Option<
            chumsky::util::MaybeRef<'s, <MappedSpan<Span<'s>, &'s str, F> as Input<'s>>::Token>,
        >,
        span: <MappedSpan<Span<'s>, &'s str, F> as Input<'s>>::Span,
    ) -> Self {
        Self::Multiple(
            expected
                .into_iter()
                .map(|expected| match expected {
                    DefaultExpected::Any => Self::UnexpectedEndOfInput(span),
                    DefaultExpected::EndOfInput => Self::ExpectedEndOfInput(span),
                    DefaultExpected::SomethingElse => {
                        Self::UnexpectedChar(span, found.map(Maybe::into_inner))
                    }
                    DefaultExpected::Token(t) => Self::ExpectedChar {
                        at: span,
                        expected: t.into_inner(),
                        got: found.map(Maybe::into_inner),
                    },
                    _ => Self::UnexpectedChar(span, found.map(Maybe::into_inner)),
                })
                .collect(),
        )
    }
}

impl<'s, F: Fn(chumsky::span::SimpleSpan) -> Span<'s> + 's>
    chumsky::error::LabelError<
        's,
        MappedSpan<Span<'s>, &'s str, F>,
        TextExpected<'s, MappedSpan<Span<'s>, &'s str, F>>,
    > for Error<'s>
{
    fn expected_found<
        E: IntoIterator<Item = TextExpected<'s, MappedSpan<Span<'s>, &'s str, F>>>,
    >(
        expected: E,
        found: Option<
            chumsky::util::MaybeRef<'s, <MappedSpan<Span<'s>, &'s str, F> as Input<'s>>::Token>,
        >,
        span: <MappedSpan<Span<'s>, &'s str, F> as Input<'s>>::Span,
    ) -> Self {
        Self::Multiple(
            expected
                .into_iter()
                .map(|expected| match expected {
                    TextExpected::Digit(range) => Error::ExpectedDigit {
                        at: span,
                        expected: range,
                        got: found.map(Maybe::into_inner),
                    },
                    TextExpected::Identifier(ident) => Error::ExpectedIdentifier {
                        at: span,
                        expected: ident,
                        got: found.map(Maybe::into_inner),
                    },
                    TextExpected::IdentifierPart => Error::ExpectedIdentifierPart {
                        at: span,
                        got: found.map(Maybe::into_inner),
                    },
                    TextExpected::InlineWhitespace => Error::ExpectedInlineWhitespace {
                        at: span,
                        got: found.map(Maybe::into_inner),
                    },
                    TextExpected::Newline => Error::ExpectedNewline {
                        at: span,
                        got: found.map(Maybe::into_inner),
                    },
                    TextExpected::Whitespace => Error::ExpectedWhitespace {
                        at: span,
                        got: found.map(Maybe::into_inner),
                    },
                    _ => Error::UnexpectedChar(span, found.map(Maybe::into_inner)),
                })
                .collect(),
        )
    }
}

impl<'s, F: Fn(chumsky::span::SimpleSpan) -> Span<'s> + 's>
    chumsky::error::LabelError<'s, MappedSpan<Span<'s>, &'s str, F>, Maybe<char, &'s char>>
    for Error<'s>
{
    fn expected_found<E: IntoIterator<Item = Maybe<char, &'s char>>>(
        expected: E,
        found: Option<
            chumsky::util::MaybeRef<'s, <MappedSpan<Span<'s>, &'s str, F> as Input<'s>>::Token>,
        >,
        span: <MappedSpan<Span<'s>, &'s str, F> as Input<'s>>::Span,
    ) -> Self {
        Self::ExpectedOneOfMultipleChars {
            at: span,
            got: found.map(Maybe::into_inner),
            expected: expected.into_iter().map(Maybe::into_inner).collect(),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
/// The kind of delimiter
pub enum DelimiterType {
    /// a multi line comment; /* and */
    MultiLineComment,
    /// a parameter list; ( and )
    ParameterList,
    /// an array type; [ and ]
    ArrayType,
    /// an array literal; [ and ]
    ArrayLiteral,
    /// a tuple type; ( and )
    TupleType,
    /// a tuple literal; ( and )
    TupleLiteral,
    /// a struct type; { and }
    StructType,
    /// a struct literal; { and }
    BracketedType,
    /// a string literal; " and "
    StructLiteral,
    /// a bracketed type; ( and )
    StringLiteral,
    /// a bracketed expression; ( and )
    BracketedExpression,
    /// a template body; {\n and \n}
    TemplateBody { curly_count: usize },
    /// select arms; { and }
    SelectArms,
    /// a command; {* and }*
    Command { curly_count: usize },
}

impl Display for DelimiterType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MultiLineComment => write!(f, "multiline comment"),
            Self::ParameterList => write!(f, "parameter list"),
            Self::ArrayType => write!(f, "array type"),
            Self::ArrayLiteral => write!(f, "array literal"),
            Self::TupleType => write!(f, "tuple type"),
            Self::TupleLiteral => write!(f, "tuple literal"),
            Self::StructType => write!(f, "struct type"),
            Self::BracketedType => write!(f, "bracketed type"),
            Self::StructLiteral => write!(f, "struct literal"),
            Self::StringLiteral => write!(f, "string literal"),
            Self::BracketedExpression => write!(f, "bracketed expression"),
            Self::TemplateBody { curly_count } if *curly_count == 1 => write!(f, "template body"),
            Self::TemplateBody { curly_count } => {
                write!(f, "template body ({curly_count} curly braces)")
            }
            Self::SelectArms => write!(f, "select arms"),
            Self::Command { curly_count } if *curly_count == 1 => write!(f, "command"),
            Self::Command { curly_count } => write!(f, "command ({curly_count} curly braces)"),
        }
    }
}

fn with_span<'a, I: Input<'a>, T, E: extra::ParserExtra<'a, I>>(
    original: impl Parser<'a, I, T, E> + Clone,
) -> impl Parser<'a, I, (T, I::Span), E> + Clone {
    original.map_with(|output, extra| (output, extra.span()))
}

fn map_err_missing_delimiter<
    's,
    I: Input<'s, Span = Span<'s>>,
    S,
    T,
    E,
    Extra: extra::ParserExtra<'s, I, Error = Error<'s>>,
>(
    start: impl Parser<'s, I, S, Extra> + Clone,
    output: impl Parser<'s, I, T, Extra> + Clone,
    end: impl Parser<'s, I, E, Extra> + Clone,
    delimiter_type: DelimiterType,
) -> impl Parser<'s, I, T, Extra> + Clone {
    start
        .to_span()
        .then(output)
        .then(end.or_not())
        .try_map(move |((start, output), end), _| match end {
            Some(_) => Ok(output),
            None => Err(Error::UnclosedDelimiter {
                start,
                kind: delimiter_type,
            }),
        })
}

fn ignore_ctx<'a, I: Input<'a>, T, E: extra::ParserExtra<'a, I>>(
    original: impl Parser<'a, I, T, extra::Full<E::Error, E::State, ()>> + Clone,
) -> impl Parser<'a, I, T, E> + Clone {
    map_ctx(|_| (), original)
}

#[macro_export]
#[allow(non_snake_case)]
/// Internal macro. Semver exempt. Do not use.
macro_rules! __internal__parse_with_path {
    ($p:expr, $path: expr, $source:expr) => {{
        use ::chumsky::{input::Input, Parser};

        $p.parse(
            $source.map_span(|span: ::chumsky::span::SimpleSpan| $crate::Span {
                start: span.start,
                end: span.end,
                path_and_source: ($path, $source),
            }),
        )
    }};
}

#[macro_export]
#[allow(non_snake_case)]
/// Internal macro. Semver exempt. Do not use.
macro_rules! __internal__parse_include_str {
    ($p:expr, $path:literal) => {{
        use ::chumsky::input::Input;

        $p.parse(
            include_str!($path).map_span(|span: ::chumsky::span::SimpleSpan| $crate::Span {
                start: span.start,
                end: span.end,
                path_and_source: ($path, include_str!($path)),
            }),
        )
    }};
}

#[macro_export]
#[allow(non_snake_case)]
/// Internal macro. Semver exempt. Do not use.
macro_rules! __internal__span_macro_with_path {
    ($span:path, $path:expr, $source:expr) => {
        macro_rules! s {
            ($expr:expr) => {{
                $span {
                    start: $expr.start,
                    end: $expr.end,
                    path_and_source: ($path, $source),
                }
            }};
        }
    };
}

#[macro_export]
#[allow(non_snake_case)]
/// Internal macro. Semver exempt. Do not use.
macro_rules! __internal__span_macro_include_str {
    ($span:path, $path:literal) => {
        macro_rules! s {
            ($expr:expr) => {{
                $span {
                    start: $expr.start,
                    end: $expr.end,
                    path_and_source: ($path, include_str!($path)),
                }
            }};
        }
    };
}

#[macro_export]
#[allow(non_snake_case)]
/// Internal macro. Semver exempt. Do not use.
macro_rules! __internal__parser {
    ($vis:vis $name:ident, $output:ty, $content:block) => {
        $vis fn $name<'s, F: Fn(::chumsky::span::SimpleSpan) -> $crate::Span<'s> + 's>() -> impl ::chumsky::Parser<'s, ::chumsky::input::MappedSpan<$crate::Span<'s>, &'s str, F>, $output, ::chumsky::extra::Err<$crate::parse::Error<'s>>> + Clone {
            $content/*.map_with(|m, ctx| {dbg!(ctx.span(), stringify!($name), &m); m})*/
        }
    };
    ($vis:vis $name:ident($($arg:ident: $arg_ty:ty),+), $output:ty, $content:block) => {
        $vis fn $name<'s, F: Fn(::chumsky::span::SimpleSpan) -> $crate::Span<'s> + 's>($($arg: $arg_ty),+) -> impl ::chumsky::Parser<'s, ::chumsky::input::MappedSpan<$crate::Span<'s>, &'s str, F>, $output, ::chumsky::extra::Err<$crate::parse::Error<'s>>> + Clone {
            $content/*.map_with(|m, ctx| {dbg!(ctx.span(), stringify!($name), &m); m})*/
        }
    };
    ($vis:vis $name:ident, $output:ty, $extra:ty, $content:block) => {
        $vis fn $name<'s, F: Fn(::chumsky::span::SimpleSpan) -> $crate::Span<'s> + 's>() -> impl ::chumsky::Parser<'s, ::chumsky::input::MappedSpan<$crate::Span<'s>, &'s str, F>, $output, $extra> + Clone {
            $content/*.map_with(|m, ctx| {dbg!(ctx.span(), stringify!($name), &m); m})*/
        }
    };
    ($vis:vis $name:ident($($arg:ident: $arg_ty:ty),+), $output:ty, $extra:ty, $content:block) => {
        $vis fn $name<'s, F: Fn(::chumsky::span::SimpleSpan) -> $crate::Span<'s> + 's>($($arg: $arg_ty),+) -> impl ::chumsky::Parser<'s, ::chumsky::input::MappedSpan<$crate::Span<'s>, &'s str, F>, $output, $extra> + Clone {
            $content/*.map_with(|m, ctx| {dbg!(ctx.span(), stringify!($name), &m); m})*/
        }
    };
}

__internal__parser! {capturing_newline, &'s str, {
   newline().to_slice()
}}

__internal__parser! {escape_sequence, EscapeSequence, {
    choice((
        just("\\n").to(EscapeSequence::Newline),
        just("\\t").to(EscapeSequence::Tab),
        just("\\r").to(EscapeSequence::CarriageReturn),
        just("\\\\").to(EscapeSequence::Backslash),
        just("\\").then(any()).try_map(|_, span| Err(Error::UnknownEscapeSequence(span)))
    ))
}}