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
use super::*;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Nonprim {
    Tuple,
    Cntr,
    Seq,
    Map,
    None,
}

/// This only matches `Inline` and `Concise` variants. Looks ahead to guess type
/// based on opening delimiters and field naming. _Always_ returns the original
/// input.
pub fn pattern_match_delimited<'a, E: ParseError<&'a str>>(
    orig: &'a str,
) -> IResult<&'a str, Nonprim, E> {
    #[derive(PartialEq)]
    enum Ident {
        Prim,
        Nonprim,
        None,
    };

    let (i, ident_) = opt(ident(true))(orig)?;
    let (i, ident_) = if ident_.is_some() {
        (i, Ident::Prim)
    } else {
        let (i, ident__) = opt(ident(false))(orig)?;
        if ident__.is_some() {
            (i, Ident::Nonprim)
        } else {
            (orig, Ident::None)
        }
    };

    let (i, _) = multiline_whitespace(i)?;
    let next = i.chars().next().unwrap_or(' ');

    let mut r = if next == '[' {
        Nonprim::Seq
    } else if next == '{' {
        Nonprim::Map
    } else if next == '(' {
        // opening of tuple or container
        if (ident_ == Ident::None || ident_ == Ident::Prim) && unit_value::<E>(i).is_ok() {
            Nonprim::None // a unit value, not a tuple. Can't be named, or would be an empty tuple
        } else if recognise_field_assign(multiline_whitespace(&i[1..])?.0) {
            Nonprim::Cntr
        } else {
            Nonprim::Tuple
        }
    } else {
        Nonprim::None
    };

    if r != Nonprim::None && ident_ == Ident::Prim {
        r = Nonprim::None;
    }

    Ok((orig, r))
}

/// This only matches `Verbose` variants, returning true if it looks like a `Verbose` formatting.
/// _Always_ returns the original input.
pub fn pattern_match_verbose<'a, E: ParseError<&'a str>>(
    orig: &'a str,
) -> IResult<&'a str, bool, E> {
    let (i, _) = multiline_whitespace(orig)?; // ignore _all_ whitespace before hand

    // generally a Verbose format can be recognised by:
    // - A direct mapping (var_name = xxx)
    // - A nested mapping ([var_name])
    // - A sequence or map entry ([[var_name]])

    let is = recognise_field_assign(i);
    let is = is
        || preceded::<_, _, _, (), _, _>(
            char('['),
            terminated(
                ignore_inline_whitespace(valid_name),
                ignore_inline_whitespace(char(']')),
            ),
        )(i)
        .is_ok();
    let is = is
        || preceded::<_, _, _, (), _, _>(
            tag("[["),
            terminated(
                ignore_inline_whitespace(valid_name),
                ignore_inline_whitespace(tag("]]")),
            ),
        )(i)
        .is_ok();

    Ok((orig, is))
}

/// Looks at the next characters, ignoring spaces. If there is a a new line before
/// other characters then we say that this is in `Concise` format.
pub fn recognise_concise(i: &str) -> bool {
    const SKIP: &str = " \t\r";

    i.chars()
        .skip_while(|&c| SKIP.contains(c))
        .next()
        .map(|c| c == '\n')
        .unwrap_or(false)
}

/// Recognises the sequence: `field_name =`.
pub fn recognise_field_assign(i: &str) -> bool {
    preceded::<_, _, _, (), _, _>(field_name, terminated(inline_whitespace, char('=')))(i).is_ok()
}

#[cfg(test)]
mod tests {
    use super::*;
    use nom::error::VerboseError;

    macro_rules! test {
		( $ex:expr, $($x:literal ),* ) => {{
			$(
				let r = pattern_match_delimited::<VerboseError<_>>($x);
				assert_eq!(r, Ok(($x, $ex)));
			)*
		}};
	}

    #[test]
    fn test_pat_match_none() {
        test!(
            Nonprim::None,
            "123456",
            "-123456",
            "-3.14e-314",
            "()",
            "a = 123",
            r#""Hello, world!""#,
            "true",
            "false",
            "b91':C!WEH#L4R'"
        );
    }

    #[test]
    fn test_pat_match_seq() {
        test!(
            Nonprim::Seq,
            "[1,2,3]",
            "[\n1,2,3\n]",
            "Hello [1,2,3]",
            "Hello [\n1,2,3\n]",
            "Hello[1,2,3]",
            "Hello[\n1,2,3\n]"
        );
    }

    #[test]
    fn test_pat_match_map() {
        test!(
            Nonprim::Map,
            "{1:2, 3:4, 5:6}",
            "{\n1:2\n3:4\n5:6\n}",
            "Hello {1:2, 3:4, 5:6}",
            "Hello {\n1:2\n3:4\n5:6\n}",
            "Hello{1:2, 3:4, 5:6}",
            "Hello{\n1:2\n3:4\n5:6\n}"
        );
    }

    #[test]
    fn test_pat_match_tuple() {
        test!(
            Nonprim::Tuple,
            "(1,2,3)",
            "(\n1,2,3\n)",
            "Hello (1,2,3)",
            "Hello (\n1,2,3\n)",
            "Hello(1,2,3)",
            "Hello(\n1,2,3\n)"
        );
    }

    #[test]
    fn test_pat_match_cntr() {
        test!(
            Nonprim::Cntr,
            "(a=1, 2, 3)",
            "(\na  = 1,2,3\n)",
            "Hello (a = 1,2,3)",
            "Hello (\na=1,2,3\n)",
            "Hello(_a=1,2,3)",
            "Hello(\na \t\t\t =1,2,3\n)"
        );
    }
}