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
pub use directive::Directive;
pub use owner::Owner;
pub use per_file::PerFile;
pub use statement::Statement;

use error::ParseError;
use nom::types::CompleteStr;

pub mod error;

mod directive;
mod owner;
mod per_file;
mod statement;

pub fn parse<T: AsRef<str>>(input: T) -> Result<Vec<Statement>, ParseError> {
    let filtered = input
        .as_ref()
        .split("\n")
        // Attach line numbers to each line.
        .enumerate()
        // Remove any inline comments from each line.
        .map(|(line_num, content)| (line_num + 1, remove_inline_comments(content)))
        // Ignore empty lines and comments.
        .filter(|(_, content)| !content.is_empty() && !content.starts_with("#"));

    let mut parsed = vec![];
    for (line_num, content) in filtered {
        statement::statement(CompleteStr(content))
            .map(|(_, output)| parsed.push(output))
            .map_err(|e| ParseError::from_nom(line_num, e))?;
    }
    Ok(parsed)
}

fn is_whitespace(c: char) -> bool {
    c == ' ' || c == '\t' || c == '\n'
}

// TODO: I imagine it is more efficient to use nom to ignore inline comments rather than splitting
// the string. We should benchmark this and update the parser if that is the case.
fn remove_inline_comments(line: &str) -> &str {
    line.trim().split("#").nth(0).unwrap()
}

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

    #[test]
    fn empty() {
        assert!(parse("").unwrap().is_empty());
    }

    #[test]
    fn empty_ws() {
        assert!(parse(" ").unwrap().is_empty());
    }

    #[test]
    fn comment() {
        assert!(parse("# comment").unwrap().is_empty());
    }

    #[test]
    fn comment_ws() {
        assert!(parse("#comment").unwrap().is_empty());
    }

    #[test]
    fn inline_comment() {
        assert_eq!(
            parse("set noparent # comment").unwrap(),
            vec![Statement::Directive(Directive::NoParent)]
        );
    }

    #[test]
    fn inline_comment_ws() {
        assert_eq!(
            parse("set noparent# comment").unwrap(),
            vec![Statement::Directive(Directive::NoParent)]
        );
    }

    #[test]
    fn multiline() {
        assert_eq!(
            parse("set noparent\n*").unwrap(),
            vec![
                Statement::Directive(Directive::NoParent),
                Statement::Directive(Directive::StarGlob)
            ]
        );
    }

    #[test]
    fn multiline_trailing_newline() {
        assert_eq!(
            parse("set noparent\n*\n").unwrap(),
            vec![
                Statement::Directive(Directive::NoParent),
                Statement::Directive(Directive::StarGlob)
            ]
        );
    }

    #[test]
    fn multiline_leading_newline() {
        assert_eq!(
            parse("\nset noparent\n*").unwrap(),
            vec![
                Statement::Directive(Directive::NoParent),
                Statement::Directive(Directive::StarGlob)
            ]
        );
    }

    #[test]
    fn multiline_comments() {
        assert_eq!(
            parse("set noparent # comment\n* # comment").unwrap(),
            vec![
                Statement::Directive(Directive::NoParent),
                Statement::Directive(Directive::StarGlob)
            ]
        );
    }
}