aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/fake/dep_parser.cc
blob: 613f03efde7efbf08062c043233dcbdf85ce4530 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2008, 2009, 2010, 2011 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/repositories/fake/dep_parser.hh>
#include <paludis/util/options.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/elike_dep_parser.hh>
#include <paludis/elike_conditional_dep_spec.hh>
#include <paludis/elike_package_dep_spec.hh>
#include <paludis/dep_spec.hh>
#include <paludis/environment.hh>
#include <paludis/repository.hh>
#include <paludis/package_id.hh>
#include <paludis/user_dep_spec.hh>
#include <list>

using namespace paludis;
using namespace paludis::fakerepository;

FakeDepParseError::FakeDepParseError(const std::string & s, const std::string & t) throw () :
    Exception("Error parsing '" + s + "': " + t)
{
}

namespace
{
    template <typename T_>
    struct ParseStackTypes
    {
        typedef std::list<std::shared_ptr<typename T_::BasicInnerNode> > Stack;
    };

    template <typename T_>
    void package_dep_spec_string_handler(const typename ParseStackTypes<T_>::Stack & h, const std::string & s)
    {
        PackageDepSpec p(parse_elike_package_dep_spec(s, ELikePackageDepSpecOptions() + epdso_allow_slot_deps
                    + epdso_allow_slot_star_deps + epdso_allow_slot_equal_deps + epdso_allow_repository_deps
                    + epdso_allow_use_deps + epdso_allow_ranged_deps + epdso_allow_tilde_greater_deps
                    + epdso_allow_slot_equal_deps_portage + epdso_allow_subslot_deps
                    + epdso_strict_parsing,
                    user_version_spec_options()));
        (*h.begin())->append(std::make_shared<PackageDepSpec>(p));
    }

    template <typename T_>
    void package_or_block_dep_spec_string_handler(const typename ParseStackTypes<T_>::Stack & h, const std::string & s)
    {
        if ((! s.empty()) && ('!' == s.at(0)))
        {
            (*h.begin())->append(std::make_shared<BlockDepSpec>(s,
                            parse_elike_package_dep_spec(s.substr(1),
                                ELikePackageDepSpecOptions() + epdso_allow_slot_deps
                                + epdso_allow_slot_star_deps + epdso_allow_slot_equal_deps + epdso_allow_repository_deps
                                + epdso_allow_use_deps + epdso_allow_ranged_deps + epdso_allow_tilde_greater_deps
                                + epdso_allow_slot_equal_deps_portage + epdso_allow_subslot_deps
                                + epdso_strict_parsing,
                                user_version_spec_options())));
        }
        else
            package_dep_spec_string_handler<T_>(h, s);
    }

    template <typename T_>
    void license_handler(const typename ParseStackTypes<T_>::Stack & h, const std::string & s)
    {
        (*h.begin())->append(std::make_shared<LicenseDepSpec>(s));
    }

    template <typename T_>
    void simple_uri_handler(const typename ParseStackTypes<T_>::Stack & h, const std::string & s)
    {
        (*h.begin())->append(std::make_shared<SimpleURIDepSpec>(s));
    }

    template <typename T_>
    void arrow_handler(const typename ParseStackTypes<T_>::Stack & h, const std::string & s, const std::string & t)
    {
        (*h.begin())->append(std::make_shared<FetchableURIDepSpec>(t.empty() ? s : s + " -> " + t));
    }

    void any_not_allowed_handler(const std::string & s) PALUDIS_ATTRIBUTE((noreturn));

    void any_not_allowed_handler(const std::string & s)
    {
        throw FakeDepParseError(s, "Any dep specs not allowed here");
    }

    void exactly_one_not_allowed_handler(const std::string & s) PALUDIS_ATTRIBUTE((noreturn));

    void exactly_one_not_allowed_handler(const std::string & s)
    {
        throw FakeDepParseError(s, "Exactly one dep specs not allowed here");
    }

    void at_most_one_not_allowed_handler(const std::string & s) PALUDIS_ATTRIBUTE((noreturn));

    void at_most_one_not_allowed_handler(const std::string & s)
    {
        throw FakeDepParseError(s, "At most one dep specs not allowed here");
    }

    void arrows_not_allowed_handler(const std::string & s, const std::string & f, const std::string & t) PALUDIS_ATTRIBUTE((noreturn));

    void arrows_not_allowed_handler(const std::string & s, const std::string & f, const std::string & t)
    {
        throw FakeDepParseError(s, "Arrow '" + f + " -> " + t + "' not allowed here");
    }

    void error_handler(const std::string & s, const std::string & t) PALUDIS_ATTRIBUTE((noreturn));

    void error_handler(const std::string & s, const std::string & t)
    {
        throw FakeDepParseError(s, t);
    }

    void labels_not_allowed_handler(const std::string & s, const std::string & f) PALUDIS_ATTRIBUTE((noreturn));

    void labels_not_allowed_handler(const std::string & s, const std::string & f)
    {
        throw FakeDepParseError(s, "Label '" + f + "' not allowed here");
    }

    template <typename T_, typename A_>
    void any_all_handler(typename ParseStackTypes<T_>::Stack & stack)
    {
        stack.push_front((*stack.begin())->append(std::make_shared<A_>()));
    }

    template <typename T_>
    void use_handler(typename ParseStackTypes<T_>::Stack & stack, const std::string & u)
    {
        stack.push_front((*stack.begin())->append(
                    std::make_shared<ConditionalDepSpec>(parse_elike_conditional_dep_spec(u, false))));
    }

    template <typename T_>
    void pop_handler(typename ParseStackTypes<T_>::Stack & stack, const std::string & s)
    {
        stack.pop_front();
        if (stack.empty())
            throw FakeDepParseError(s, "Too many ')'s");
    }

    template <typename T_>
    void should_be_empty_handler(typename ParseStackTypes<T_>::Stack & stack, const std::string & s)
    {
        if (1 != stack.size())
            throw FakeDepParseError(s, "Nesting error");
    }

    void do_nothing()
    {
    }

    void discard_annotations(const std::shared_ptr<const Map<std::string, std::string> > &)
    {
    }
}

std::shared_ptr<DependencySpecTree>
paludis::fakerepository::parse_depend(const std::string & s, const Environment * const)
{
    using namespace std::placeholders;

    ParseStackTypes<DependencySpecTree>::Stack stack;
    std::shared_ptr<DependencySpecTree> top(std::make_shared<DependencySpecTree>(std::make_shared<AllDepSpec>()));
    stack.push_front(top->top());

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<DependencySpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = &discard_annotations,
                n::on_any() = std::bind(&any_all_handler<DependencySpecTree, AnyDepSpec>, std::ref(stack)),
                n::on_arrow() = std::bind(&arrows_not_allowed_handler, s, _1, _2),
                n::on_at_most_one() = std::bind(&at_most_one_not_allowed_handler, s),
                n::on_error() = std::bind(&error_handler, s, _1),
                n::on_exactly_one() = std::bind(&exactly_one_not_allowed_handler, s),
                n::on_label() = std::bind(&labels_not_allowed_handler, s, _1),
                n::on_no_annotations() = &do_nothing,
                n::on_pop() = std::bind(&pop_handler<DependencySpecTree>, std::ref(stack), s),
                n::on_should_be_empty() = std::bind(&should_be_empty_handler<DependencySpecTree>, std::ref(stack), s),
                n::on_string() = std::bind(&package_or_block_dep_spec_string_handler<DependencySpecTree>, std::ref(stack), _1),
                n::on_use() = std::bind(&use_handler<DependencySpecTree>, std::ref(stack), _1),
                n::on_use_under_any() = &do_nothing
                ));

    parse_elike_dependencies(s, callbacks, { });

    return top;
}

std::shared_ptr<FetchableURISpecTree>
paludis::fakerepository::parse_fetchable_uri(const std::string & s, const Environment * const)
{
    using namespace std::placeholders;

    ParseStackTypes<FetchableURISpecTree>::Stack stack;
    std::shared_ptr<FetchableURISpecTree> top(std::make_shared<FetchableURISpecTree>(std::make_shared<AllDepSpec>()));
    stack.push_front(top->top());

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<FetchableURISpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = &discard_annotations,
                n::on_any() = std::bind(&any_not_allowed_handler, s),
                n::on_arrow() = std::bind(&arrow_handler<FetchableURISpecTree>, std::ref(stack), _1, _2),
                n::on_at_most_one() = std::bind(&at_most_one_not_allowed_handler, s),
                n::on_error() = std::bind(&error_handler, s, _1),
                n::on_exactly_one() = std::bind(&exactly_one_not_allowed_handler, s),
                n::on_label() = std::bind(&labels_not_allowed_handler, s, _1),
                n::on_no_annotations() = &do_nothing,
                n::on_pop() = std::bind(&pop_handler<FetchableURISpecTree>, std::ref(stack), s),
                n::on_should_be_empty() = std::bind(&should_be_empty_handler<FetchableURISpecTree>, std::ref(stack), s),
                n::on_string() = std::bind(&arrow_handler<FetchableURISpecTree>, std::ref(stack), _1, ""),
                n::on_use() = std::bind(&use_handler<FetchableURISpecTree>, std::ref(stack), _1),
                n::on_use_under_any() = &do_nothing
                ));

    parse_elike_dependencies(s, callbacks, { });

    return top;
}

std::shared_ptr<SimpleURISpecTree>
paludis::fakerepository::parse_simple_uri(const std::string & s, const Environment * const)
{
    using namespace std::placeholders;

    ParseStackTypes<SimpleURISpecTree>::Stack stack;
    std::shared_ptr<SimpleURISpecTree> top(std::make_shared<SimpleURISpecTree>(std::make_shared<AllDepSpec>()));
    stack.push_front(top->top());

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<SimpleURISpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = &discard_annotations,
                n::on_any() = std::bind(&any_not_allowed_handler, s),
                n::on_arrow() = std::bind(&arrows_not_allowed_handler, s, _1, _2),
                n::on_at_most_one() = std::bind(&at_most_one_not_allowed_handler, s),
                n::on_error() = std::bind(&error_handler, s, _1),
                n::on_exactly_one() = std::bind(&exactly_one_not_allowed_handler, s),
                n::on_label() = std::bind(&labels_not_allowed_handler, s, _1),
                n::on_no_annotations() = &do_nothing,
                n::on_pop() = std::bind(&pop_handler<SimpleURISpecTree>, std::ref(stack), s),
                n::on_should_be_empty() = std::bind(&should_be_empty_handler<SimpleURISpecTree>, std::ref(stack), s),
                n::on_string() = std::bind(&simple_uri_handler<SimpleURISpecTree>, std::ref(stack), _1),
                n::on_use() = std::bind(&use_handler<SimpleURISpecTree>, std::ref(stack), _1),
                n::on_use_under_any() = &do_nothing
                ));

    parse_elike_dependencies(s, callbacks, { });

    return top;
}

std::shared_ptr<LicenseSpecTree>
paludis::fakerepository::parse_license(const std::string & s, const Environment * const)
{
    using namespace std::placeholders;

    ParseStackTypes<LicenseSpecTree>::Stack stack;
    std::shared_ptr<LicenseSpecTree> top(std::make_shared<LicenseSpecTree>(std::make_shared<AllDepSpec>()));
    stack.push_front(top->top());

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<LicenseSpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = &discard_annotations,
                n::on_any() = std::bind(&any_all_handler<LicenseSpecTree, AnyDepSpec>, std::ref(stack)),
                n::on_arrow() = std::bind(&arrows_not_allowed_handler, s, _1, _2),
                n::on_at_most_one() = std::bind(&at_most_one_not_allowed_handler, s),
                n::on_error() = std::bind(&error_handler, s, _1),
                n::on_exactly_one() = std::bind(&exactly_one_not_allowed_handler, s),
                n::on_label() = std::bind(&labels_not_allowed_handler, s, _1),
                n::on_no_annotations() = &do_nothing,
                n::on_pop() = std::bind(&pop_handler<LicenseSpecTree>, std::ref(stack), s),
                n::on_should_be_empty() = std::bind(&should_be_empty_handler<LicenseSpecTree>, std::ref(stack), s),
                n::on_string() = std::bind(&license_handler<LicenseSpecTree>, std::ref(stack), _1),
                n::on_use() = std::bind(&use_handler<LicenseSpecTree>, std::ref(stack), _1),
                n::on_use_under_any() = &do_nothing
                ));

    parse_elike_dependencies(s, callbacks, { });

    return top;
}