summaryrefslogtreecommitdiff
path: root/src/ebuild/parsers.rs
blob: 73603a2eaf34c01f63e6f3699c4444e1e72035c9 (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
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 std::path::PathBuf;

use mon::{Parser, ParserIter, alpha1, alphanumeric, r#if, one_of, tag, whitespace1};

use crate::{
    Parseable,
    ebuild::{Conditional, Depend, Eapi, Eclass, License, SrcUri, Uri, UriPrefix},
    useflag::UseFlag,
};

impl<'a> Parseable<'a, &'a str> for UriPrefix {
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        tag("+mirror")
            .map(|_| UriPrefix::Mirror)
            .or(tag("+fetch").map(|_| UriPrefix::Fetch))
    }
}

impl<'a> Parseable<'a, &'a str> for Uri {
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        let protocol = alpha1::<&str>()
            .followed_by(tag("://"))
            .map(|output: &str| output.to_string());
        let path = r#if(|c: &char| !c.is_ascii_whitespace())
            .repeated()
            .at_least(1)
            .recognize()
            .map(|output: &str| output.to_string());

        protocol
            .and(path)
            .map(|(protocol, path)| Uri { protocol, path })
    }
}

impl<'a> Parseable<'a, &'a str> for SrcUri {
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        let filename = || {
            r#if(|c: &char| !c.is_ascii_whitespace())
                .repeated()
                .at_least(1)
                .recognize()
                .map(|output: &str| PathBuf::from(output))
        };

        let uri = UriPrefix::parser()
            .opt()
            .and(Uri::parser())
            .and(filename().preceded_by(tag(" -> ")).opt())
            .map(|((prefix, uri), filename)| SrcUri::Uri {
                prefix,
                uri,
                filename,
            });

        uri.or(filename().map(|path: PathBuf| SrcUri::Filename(path)))
    }
}

impl<'a> Parseable<'a, &'a str> for License {
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        let start = alphanumeric().or(one_of("_".chars()));
        let rest = alphanumeric().or(one_of("+_.-".chars())).repeated().many();

        start
            .and(rest)
            .recognize()
            .map(|output: &str| License(output.to_string()))
    }
}

impl<'a> Parseable<'a, &'a str> for Eapi {
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        let start = alphanumeric().or(one_of("_".chars()));
        let rest = alphanumeric().or(one_of("+_.-".chars())).repeated().many();

        start
            .and(rest)
            .recognize()
            .map(|output: &str| Eapi(output.to_string()))
    }
}

// TODO:
// Cant find information about eclass names in pms so we allow anything except
// for whitespace.
impl<'a> Parseable<'a, &'a str> for Eclass {
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        r#if(|c: &char| !c.is_ascii_whitespace())
            .repeated()
            .at_least(1)
            .recognize()
            .map(|output: &str| Eclass(output.to_string()))
    }
}

impl<'a, T> Parseable<'a, &'a str> for Depend<T>
where
    T: Parseable<'a, &'a str>,
{
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        |it| {
            let exprs = || {
                Depend::parser()
                    .separated_by_with_trailing(whitespace1())
                    .at_least(1)
                    .delimited_by(tag("(").followed_by(whitespace1()), tag(")"))
            };

            let all_of_group = exprs().map(|exprs| Depend::AllOf(exprs));

            let any_of_group = exprs()
                .preceded_by(tag("||").followed_by(whitespace1()))
                .map(|exprs| Depend::AnyOf(exprs));

            let one_of_group = exprs()
                .preceded_by(tag("^^").followed_by(whitespace1()))
                .map(|exprs| Depend::OneOf(exprs));

            let conditional_group = Conditional::parser()
                .followed_by(whitespace1())
                .and(exprs())
                .map(|(conditional, exprs)| Depend::ConditionalGroup(conditional, exprs));

            T::parser()
                .map(|e| Depend::Element(e))
                .or(conditional_group)
                .or(any_of_group)
                .or(all_of_group)
                .or(one_of_group)
                .parse(it)
        }
    }
}

impl<'a> Parseable<'a, &'a str> for Conditional {
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        UseFlag::parser()
            .preceded_by(tag("!"))
            .followed_by(tag("?"))
            .map(Conditional::Negative)
            .or(UseFlag::parser()
                .followed_by(tag("?"))
                .map(Conditional::Positive))
    }
}

#[cfg(test)]
mod test {

    use mon::{ParserIter, input::InputIter};

    use crate::{atom::Atom, ebuild::Depend};

    use super::*;

    #[test]
    fn test_src_uri() {
        let tests = [
            "https://example.com/foo/bar.tar.gz",
            "https://example.com/foo/bar.tar.gz -> bar.tar.gz",
        ];

        for test in tests {
            SrcUri::parser()
                .check_finished(InputIter::new(test))
                .unwrap();
        }
    }

    #[test]
    fn test_expr() {
        let it = InputIter::new("flag? ( || ( foo/bar foo/bar ) )");

        Depend::<Atom>::parser()
            .separated_by(whitespace1())
            .many()
            .check_finished(it)
            .unwrap();
    }
}