nextest_filtering/parsing/
unicode_string.rs

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
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

// Adapted from https://github.com/Geal/nom/blob/294ffb3d9e0ade2c3b7ddfff52484b6d643dcce1/examples/string.rs

use super::{expect_n, PResult, Span, SpanLength};
use crate::errors::ParseSingleError;
use std::fmt;
use winnow::{
    combinator::{alt, delimited, preceded, repeat, trace},
    token::{take_till, take_while},
    Parser,
};

fn parse_unicode(input: &mut Span<'_>) -> PResult<char> {
    trace("parse_unicode", |input: &mut _| {
        let parse_hex = take_while(1..=6, |c: char| c.is_ascii_hexdigit());
        let parse_delimited_hex = preceded('u', delimited('{', parse_hex, '}'));
        let parse_u32 = parse_delimited_hex.try_map(|hex| u32::from_str_radix(hex, 16));
        parse_u32.verify_map(std::char::from_u32).parse_next(input)
    })
    .parse_next(input)
}

fn parse_escaped_char(input: &mut Span<'_>) -> PResult<Option<char>> {
    trace("parse_escaped_char", |input: &mut _| {
        let valid = alt((
            parse_unicode,
            'n'.value('\n'),
            'r'.value('\r'),
            't'.value('\t'),
            'b'.value('\u{08}'),
            'f'.value('\u{0C}'),
            '\\'.value('\\'),
            '/'.value('/'),
            ')'.value(')'),
            ','.value(','),
        ));
        preceded(
            '\\',
            // If none of the valid characters are found, this will report an error.
            expect_n(
                valid,
                ParseSingleError::InvalidEscapeCharacter,
                // -1 to account for the preceding backslash.
                SpanLength::Offset(-1, 2),
            ),
        )
        .parse_next(input)
    })
    .parse_next(input)
}

// This should match parse_escaped_char above.
pub(crate) struct DisplayParsedString<'a>(pub(crate) &'a str);

impl fmt::Display for DisplayParsedString<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for c in self.0.chars() {
            match c {
                // These escapes are custom to nextest.
                '/' => f.write_str("\\/")?,
                ')' => f.write_str("\\)")?,
                ',' => f.write_str("\\,")?,
                // All the other escapes should be covered by this.
                c => write!(f, "{}", c.escape_default())?,
            }
        }
        Ok(())
    }
}
fn parse_literal<'i>(input: &mut Span<'i>) -> PResult<&'i str> {
    trace("parse_literal", |input: &mut _| {
        let not_quote_slash = take_till(1.., (',', ')', '\\'));

        not_quote_slash
            .verify(|s: &str| !s.is_empty())
            .parse_next(input)
    })
    .parse_next(input)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum StringFragment<'a> {
    Literal(&'a str),
    EscapedChar(char),
}

fn parse_fragment<'i>(input: &mut Span<'i>) -> PResult<Option<StringFragment<'i>>> {
    trace(
        "parse_fragment",
        alt((
            parse_literal.map(|span| Some(StringFragment::Literal(span))),
            parse_escaped_char.map(|res| res.map(StringFragment::EscapedChar)),
        )),
    )
    .parse_next(input)
}

/// Construct a string by consuming the input until the next unescaped ) or ,.
///
/// Returns None if the string isn't valid.
pub(super) fn parse_string(input: &mut Span<'_>) -> PResult<Option<String>> {
    trace(
        "parse_string",
        repeat(0.., parse_fragment).fold(
            || Some(String::new()),
            |string, fragment| {
                match (string, fragment) {
                    (Some(mut string), Some(StringFragment::Literal(s))) => {
                        string.push_str(s);
                        Some(string)
                    }
                    (Some(mut string), Some(StringFragment::EscapedChar(c))) => {
                        string.push(c);
                        Some(string)
                    }
                    (Some(_), None) => {
                        // We encountered a parsing error, and at this point we'll stop returning
                        // values.
                        None
                    }
                    (None, _) => None,
                }
            },
        ),
    )
    .parse_next(input)
}