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
#![allow(non_snake_case)]
#![allow(dead_code)]

//!
//! Parsing of ABNF Core Rules
//!
//! See https://tools.ietf.org/html/rfc5234#appendix-B.1
//!

use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::{char, one_of};
use nom::combinator::recognize;
use nom::error::{ErrorKind, ParseError};
use nom::multi::many0;
use nom::sequence::tuple;
use nom::{Err, IResult};

pub fn one<'a, E: ParseError<&'a str>, F: Fn(char) -> bool>(
    input: &'a str,
    f: F,
) -> IResult<&'a str, char, E> {
    if input.is_empty() {
        return Err(Err::Error(ParseError::from_error_kind(
            input,
            ErrorKind::OneOf,
        )));
    }

    let mut chars = input.chars();
    let first = chars.nth(0).unwrap();

    if f(first) {
        Ok((chars.as_str(), first))
    } else {
        Err(Err::Error(ParseError::from_error_kind(
            input,
            ErrorKind::OneOf,
        )))
    }
}

/// ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
pub fn ALPHA<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    one(input, is_ALPHA)
}

pub fn is_ALPHA(c: char) -> bool {
    c.is_ascii_alphabetic()
}

/// BIT = "0" / "1"
pub fn BIT<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    one_of("01")(input)
}

/// CHAR = %x01-7F ; any 7-bit US-ASCII character, excluding NUL
pub fn CHAR<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    one(input, is_CHAR)
}

pub fn is_CHAR(c: char) -> bool {
    match c {
        '\x01'..='\x7F' => true,
        _ => false,
    }
}

/// CR = %x0D ; carriage return
pub fn CR<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    char('\r')(input)
}

/// CRLF = CR LF ; Internet standard newline
pub fn CRLF<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &str, E> {
    // This function accepts both, LF and CRLF
    // FIXME: ?
    alt((tag("\r\n"), tag("\n")))(input)
}

/// CTL = %x00-1F / %x7F ; controls
pub fn CTL<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    one(input, is_CTL)
}

pub fn is_CTL(c: char) -> bool {
    match c {
        '\x00'..='\x1F' | '\x7F' => true,
        _ => false,
    }
}

/// DIGIT = %x30-39 ; 0-9
pub fn DIGIT<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    one_of("0123456789")(input)
}

pub fn is_DIGIT(c: char) -> bool {
    c.is_ascii_digit()
}

/// DQUOTE = %x22 ; " (Double Quote)
pub fn DQUOTE<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    char('"')(input)
}

/// HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
pub fn HEXDIG<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    one(input, is_HEXDIG)
}

pub fn is_HEXDIG(c: char) -> bool {
    c.is_ascii_hexdigit()
}

/// HTAB = %x09 ; horizontal tab
pub fn HTAB<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    char('\t')(input)
}

/// LF = %x0A ; linefeed
pub fn LF<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    char('\n')(input)
}

/// LWSP = *(WSP / CRLF WSP)
///         ; Use of this linear-white-space rule
///         ;  permits lines containing only white
///         ;  space that are no longer legal in
///         ;  mail headers and have caused
///         ;  interoperability problems in other
///         ;  contexts.
///         ; Do not use when defining mail
///         ;  headers and use with caution in
///         ;  other contexts.
pub fn LWSP<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &str, E> {
    let parser = recognize(many0(alt((recognize(WSP), recognize(tuple((CRLF, WSP)))))));

    parser(input)
}

/// OCTET = %x00-FF ; 8 bits of data
pub fn OCTET(input: &[u8]) -> IResult<&[u8], &[u8]> {
    if input.is_empty() {
        Err(Err::Error((input, nom::error::ErrorKind::Char)))
    } else {
        Ok((&input[1..], &input[0..1]))
    }
}

/// SP = %x20
pub fn SP<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    char(' ')(input)
}

/// VCHAR = %x21-7E ; visible (printing) characters
pub fn VCHAR<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    one(input, is_VCHAR)
}

pub fn is_VCHAR(c: char) -> bool {
    match c {
        '\x21'..='\x7E' => true,
        _ => false,
    }
}

/// WSP = SP / HTAB ; white space
pub fn WSP<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
    alt((SP, HTAB))(input)
}

pub fn is_WSP(c: char) -> bool {
    match c {
        '\x20' | '\x09' => true,
        _ => false,
    }
}

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

    #[test]
    fn test_BIT() {
        assert_eq!(BIT::<VerboseError<&str>>("100"), Ok(("00", '1')));
        assert_eq!(BIT::<VerboseError<&str>>("010"), Ok(("10", '0')));
        assert!(BIT::<VerboseError<&str>>("").is_err());
        assert!(BIT::<VerboseError<&str>>("/").is_err());
        assert!(BIT::<VerboseError<&str>>("2").is_err());
    }

    #[test]
    fn test_HEXDIG() {
        assert_eq!(HEXDIG::<VerboseError<&str>>("FaA"), Ok(("aA", 'F')));
        assert_eq!(HEXDIG::<VerboseError<&str>>("0aA"), Ok(("aA", '0')));
        assert!(HEXDIG::<VerboseError<&str>>("").is_err());
        assert!(HEXDIG::<VerboseError<&str>>("/").is_err());
        assert!(HEXDIG::<VerboseError<&str>>(":").is_err());
        assert!(HEXDIG::<VerboseError<&str>>("`").is_err());
        assert!(HEXDIG::<VerboseError<&str>>("g").is_err());
        assert!(HEXDIG::<VerboseError<&str>>("@").is_err());
        assert!(HEXDIG::<VerboseError<&str>>("G").is_err());
    }
}