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
use colored::*;
use regex::RegexSet;
use nom::IResult;
use std::path::PathBuf;

/// Given a darcs boring file's contents, process it as a `RegexSet`. The second
/// argument is a file path, included so that we print nice errors.
pub fn darcs_contents_to_regex(file: &str, file_path: &PathBuf) -> RegexSet {

    let processed_vec: Vec<&str> = process_darcs_full(file, file_path);
    let processed_str: String = processed_vec.join("");
    let lines = processed_str.split_whitespace();

    let maybe_set = RegexSet::new(lines);
    if let Ok(s) = maybe_set {
        s
    } else {
        eprintln!(
            "{}: failed to parse darcs boring file at {:?}, ignoring",
            "Warning".yellow(),
            file_path
        );
        let empty: Vec<&str> = Vec::new();
        RegexSet::new(empty).expect("Error creating regex from empty vector")
    }

}

/// Given a `.gitignore` or `.ignore` file's contents, process it as a `RegexSet`. The second
/// argument is a file path, included so that we print nice errors.
pub fn file_contents_to_regex(file: &str, file_path: &PathBuf) -> RegexSet {
    let processed_vec: Vec<&str> = process_to_vector(file, file_path);
    let processed_str: String = processed_vec.join("");
    let lines = processed_str.split_whitespace();

    let maybe_set = RegexSet::new(lines.clone());
    if let Ok(s) = maybe_set {
        s
    } else {
        println!("{:?}", lines.collect::<Vec<&str>>());
        println!("{:?}", maybe_set);
        eprintln!(
            "{}: failed to parse .gitignore/.ignore at {:?}, ignoring",
            "Warning".yellow(),
            file_path
        );
        let empty: Vec<&str> = Vec::new();
        RegexSet::new(empty).expect("Error creating regex from empty vector")
    }
}

fn process_to_vector<'a>(input: &'a str, file_path: &PathBuf) -> Vec<&'a str> {
    match process(input) {
        IResult::Done(_, result) => result,
        _ => {
            eprintln!(
                "{}: Failed to parse gitignore at: {}",
                "Error".red(),
                file_path.display()
            );
            Vec::new()
        }
    }
}

fn process_darcs_full<'a>(input: &'a str, file_path: &PathBuf) -> Vec<&'a str> {
    match process_darcs(input) {
        IResult::Done(_, result) => result,
        _ => {
            eprintln!(
                "{}: Failed to parse darcs boring file at: {}",
                "Error".red(),
                file_path.display()
            );
            Vec::new()
        }
    }
}

named!(process_darcs<&str, Vec<&str>>,
    do_parse!(
        opt!(first_line) >>
        r: many0!(darcs) >>
        (r)
    )
);

named!(process<&str, Vec<&str>>,
    do_parse!(
        opt!(first_line) >>
        r: many0!(options) >>
        (r)
    )
);

named!(darcs<&str, &str>,
    alt!(
        tag!("\n") |
        gitignore_comment |
        is_not!("\\#") |
        parse_backslash |
        parse_not_comment
    )
);

named!(line<&str, &str>,
    do_parse!(
        tag!("\n") >>
        ("$\n")
    )
);

named!(options<&str, &str>,
    alt!(
        line |
        gitignore_comment |
        is_not!("*?+.#\n") |
        parse_asterix |
        parse_period |
        parse_questionmark |
        parse_plus |
        parse_not_comment
    )
);

named!(parse_not_comment<&str, &str>,
    do_parse!(
        is_not!("\n") >>
        ("#")
    )
);

named!(first_line<&str, &str>,
    do_parse!(
        tag!("#") >>
        is_not!("\n") >>
        tag!("\n") >>
        ("\n")
    )
);

named!(gitignore_comment<&str, &str>,
    do_parse!(
        tag!("\n#") >>
        is_not!("\n") >>
        ("\n")
    )
);

named!(parse_plus<&str, &str>,
   do_parse!(
       tag!("+") >>
       ("\\+")
   )
);

named!(parse_period<&str, &str>,
    do_parse!(
        tag!(".") >>
        ("\\.")
    )
);

named!(parse_backslash<&str, &str>,
    do_parse!(
        val: alt!(
            do_parse!(tag!("\\_") >> ("_")) |
            do_parse!(tag!("\\") >> ("\\"))
            ) >>
        (val)
    )
);

named!(parse_asterix<&str, &str>,
    do_parse!(
        tag!("*") >>
        opt!(do_parse!(tag!("\n") >> eof!() >> (""))) >>
        (".*")
    )
);

named!(parse_questionmark<&str, &str>,
    do_parse!(
        tag!("?") >>
        opt!(do_parse!(tag!("\n") >> eof!() >> (""))) >>
        (".")
    )
);