aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/fake/dep_parser.cc
blob: ed265bd54e738941b007bc9923b791ac21cdabdb (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
305
306
307
308
309
310
311
312
313
314
315
316
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2008, 2009 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/make_shared_ptr.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::tr1::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,
            const std::tr1::shared_ptr<const PackageID> & id)
    {
        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_strict_parsing,
                    user_version_spec_options(),
                    id));
        (*h.begin())->append(make_shared_ptr(new PackageDepSpec(p)));
    }

    template <typename T_>
    void package_or_block_dep_spec_string_handler(const typename ParseStackTypes<T_>::Stack & h, const std::string & s,
            const std::tr1::shared_ptr<const PackageID> & id)
    {
        if ((! s.empty()) && ('!' == s.at(0)))
        {
            (*h.begin())->append(make_shared_ptr(new 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_strict_parsing,
                                user_version_spec_options(),
                                id), false)));
        }
        else
            package_dep_spec_string_handler<T_>(h, s, id);
    }

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

    template <typename T_>
    void simple_uri_handler(const typename ParseStackTypes<T_>::Stack & h, const std::string & s)
    {
        (*h.begin())->append(make_shared_ptr(new 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(make_shared_ptr(new 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 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(make_shared_ptr(new A_)));
    }

    template <typename T_>
    void use_handler(typename ParseStackTypes<T_>::Stack & stack, const std::string & u,
            const Environment * const env, const std::tr1::shared_ptr<const PackageID> & id)
    {
        stack.push_front((*stack.begin())->append(
                    make_shared_ptr(new ConditionalDepSpec(parse_elike_conditional_dep_spec(u, env, id, 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::tr1::shared_ptr<const Map<std::string, std::string> > &)
    {
    }
}

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

    ParseStackTypes<DependencySpecTree>::Stack stack;
    std::tr1::shared_ptr<DependencySpecTree> top(make_shared_ptr(new DependencySpecTree(make_shared_ptr(new AllDepSpec))));
    stack.push_front(top->root());

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

    parse_elike_dependencies(s, callbacks);

    return top;
}

std::tr1::shared_ptr<ProvideSpecTree>
paludis::fakerepository::parse_provide(const std::string & s,
        const Environment * const env, const std::tr1::shared_ptr<const PackageID> & id)
{
    using namespace std::tr1::placeholders;

    ParseStackTypes<ProvideSpecTree>::Stack stack;
    std::tr1::shared_ptr<ProvideSpecTree> top(make_shared_ptr(new ProvideSpecTree(make_shared_ptr(new AllDepSpec))));
    stack.push_front(top->root());

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                value_for<n::on_all>(std::tr1::bind(&any_all_handler<ProvideSpecTree, AllDepSpec>, std::tr1::ref(stack))),
                value_for<n::on_annotations>(&discard_annotations),
                value_for<n::on_any>(std::tr1::bind(&any_not_allowed_handler, s)),
                value_for<n::on_arrow>(std::tr1::bind(&arrows_not_allowed_handler, s, _1, _2)),
                value_for<n::on_error>(std::tr1::bind(&error_handler, s, _1)),
                value_for<n::on_label>(std::tr1::bind(&labels_not_allowed_handler, s, _1)),
                value_for<n::on_pop>(std::tr1::bind(&pop_handler<ProvideSpecTree>, std::tr1::ref(stack), s)),
                value_for<n::on_should_be_empty>(std::tr1::bind(&should_be_empty_handler<ProvideSpecTree>, std::tr1::ref(stack), s)),
                value_for<n::on_string>(std::tr1::bind(&package_dep_spec_string_handler<ProvideSpecTree>, std::tr1::ref(stack), _1, id)),
                value_for<n::on_use>(std::tr1::bind(&use_handler<ProvideSpecTree>, std::tr1::ref(stack), _1, env, id)),
                value_for<n::on_use_under_any>(&do_nothing)
                ));

    parse_elike_dependencies(s, callbacks);

    return top;
}

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

    ParseStackTypes<FetchableURISpecTree>::Stack stack;
    std::tr1::shared_ptr<FetchableURISpecTree> top(make_shared_ptr(new FetchableURISpecTree(make_shared_ptr(new AllDepSpec))));
    stack.push_front(top->root());

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

    parse_elike_dependencies(s, callbacks);

    return top;
}

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

    ParseStackTypes<SimpleURISpecTree>::Stack stack;
    std::tr1::shared_ptr<SimpleURISpecTree> top(make_shared_ptr(new SimpleURISpecTree(make_shared_ptr(new AllDepSpec))));
    stack.push_front(top->root());

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

    parse_elike_dependencies(s, callbacks);

    return top;
}

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

    ParseStackTypes<LicenseSpecTree>::Stack stack;
    std::tr1::shared_ptr<LicenseSpecTree> top(make_shared_ptr(new LicenseSpecTree(make_shared_ptr(new AllDepSpec))));
    stack.push_front(top->root());

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

    parse_elike_dependencies(s, callbacks);

    return top;
}