summaryrefslogtreecommitdiff
path: root/support/pkgcheck/src/gparser.rs
blob: ac3e1e72893a625b1bbb1dcf7cc9bda576a0b2fb (plain)
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
use pest::Parser;

#[cfg(debug_assertions)]
const _GRAMMAR: &str = include_str!("generate.pest");

#[derive(Parser)]
#[grammar = "generate.pest"]
pub struct GenerateParser;

pub fn parse_generate(s: &str) -> Option<Vec<String>> {
    let pairs = GenerateParser::parse(Rule::file, s);

    if pairs.is_err() {
        return None;
    }

    let mut found = Vec::new();

    for pair in pairs.unwrap() {
        // A pair is a combination of the rule which matched and a span of input

        // A pair can be converted to an iterator of the tokens which make it up:
        for inner_pair in pair.into_inner() {
            let inner_span = inner_pair.clone().as_span();
            match inner_pair.as_rule() {
                Rule::filename => {
                    //println!("fname:  {}", inner_span.as_str());
                    // https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#clone_double_ref
                    let s: String = inner_span.as_str().to_string();
                    //println!("filename: {}", s);
                    found.push(s);
                }
                Rule::EOI => (),
                e => {
                    println!("unreachable: {:?}", e);
                    unreachable!()
                }
            };
        }
    }
    if found.is_empty() {
        None
    } else {
        Some(found)
    }
}