aboutsummaryrefslogtreecommitdiff
path: root/paludis/portage_dep_parser.cc
blob: 86f95ebaa5a8de8fdc2150751a1b85be8ee94229 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * 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/dep_spec.hh>
#include <paludis/portage_dep_lexer.hh>
#include <paludis/portage_dep_parser.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/stringify.hh>
#include <stack>

/** \file
 * Implementation for dep_parser.hh things.
 *
 * \ingroup grpdepparser
 */

using namespace paludis;

DepStringParseError::DepStringParseError(const std::string & d,
        const std::string & m) throw () :
    DepStringError(d, "in parse phase: " + m)
{
}

DepStringNestingError::DepStringNestingError(const std::string & dep_string) throw () :
    DepStringParseError(dep_string, "improperly balanced parentheses")
{
}

namespace
{
    /**
     * Our current state.
     *
     * \ingroup grpdepparser
     */
    enum PortageDepParserState
    {
        dps_initial,
        dps_had_double_bar,
        dps_had_double_bar_space,
        dps_had_paren,
        dps_had_use_flag,
        dps_had_use_flag_space
    };
}

std::tr1::shared_ptr<CompositeDepSpec>
PortageDepParser::parse(const std::string & s, const Policy & policy)
{
    Context context("When parsing dependency string '" + s + "':");

    std::stack<std::tr1::shared_ptr<CompositeDepSpec> > stack;
    stack.push(std::tr1::shared_ptr<CompositeDepSpec>(new AllDepSpec));

    PortageDepParserState state(dps_initial);
    PortageDepLexer lexer(s);
    PortageDepLexer::Iterator i(lexer.begin()), i_end(lexer.end());

    for ( ; i != i_end ; ++i)
    {
        Context local_context("When handling lexer token '" + i->second +
                "' (" + stringify(i->first) + "):");
        do
        {
            switch (state)
            {
                case dps_initial:
                    do
                    {
                        switch (i->first)
                        {
                            case dpl_whitespace:
                                 continue;

                            case dpl_text:
                                 {
                                     if (i->second.empty())
                                         throw DepStringParseError(i->second, "Empty text entry");
                                     stack.top()->add_child(policy.create(i->second));
                                 }
                                 continue;

                            case dpl_open_paren:
                                 {
                                     std::tr1::shared_ptr<CompositeDepSpec> a(new AllDepSpec);
                                     stack.top()->add_child(a);
                                     stack.push(a);
                                     state = dps_had_paren;
                                 }
                                 continue;

                            case dpl_close_paren:
                                 if (stack.empty())
                                     throw DepStringNestingError(s);
                                 stack.pop();
                                 if (stack.empty())
                                     throw DepStringNestingError(s);
                                 state = dps_had_paren;
                                 continue;

                            case dpl_double_bar:
                                 if (policy.permit_any_deps())
                                 {
                                     std::tr1::shared_ptr<CompositeDepSpec> a(new AnyDepSpec);
                                     stack.top()->add_child(a);
                                     stack.push(a);
                                     state = dps_had_double_bar;
                                 }
                                 else
                                     throw DepStringParseError(s, "|| is not allowed here");

                                 continue;

                            case dpl_use_flag:
                                 {
                                     std::string f(i->second);
                                     bool inv(f.length() && ('!' == f.at(0)));
                                     if (inv)
                                         f.erase(0, 1);

                                     if (f.empty())
                                         throw DepStringParseError(s,
                                                 "Bad use flag name '" + i->second + "'");
                                     if ('?' != f.at(f.length() - 1))
                                         throw DepStringParseError(s,
                                                 "Use flag name '" + i->second + "' missing '?'");

                                     f.erase(f.length() - 1);
                                     std::tr1::shared_ptr<CompositeDepSpec> a(
                                             new UseDepSpec(UseFlagName(f), inv));
                                     stack.top()->add_child(a);
                                     stack.push(a);
                                     state = dps_had_use_flag;
                                 }
                                 continue;

                        }
                        throw InternalError(PALUDIS_HERE,
                                "dps_initial: i->first is " + stringify(i->first));

                    } while (0);
                    continue;

                case dps_had_double_bar:
                    do
                    {
                        switch (i->first)
                        {
                            case dpl_whitespace:
                                state = dps_had_double_bar_space;
                                continue;

                            case dpl_text:
                            case dpl_use_flag:
                            case dpl_double_bar:
                            case dpl_open_paren:
                            case dpl_close_paren:
                                throw DepStringParseError(s, "Expected space after '||'");
                        }
                        throw InternalError(PALUDIS_HERE,
                                "dps_had_double_bar: i->first is " + stringify(i->first));

                    } while (0);
                    continue;

                case dps_had_double_bar_space:
                    do
                    {
                        switch (i->first)
                        {
                            case dpl_open_paren:
                                state = dps_initial;
                                continue;

                            case dpl_whitespace:
                            case dpl_text:
                            case dpl_use_flag:
                            case dpl_double_bar:
                            case dpl_close_paren:
                                throw DepStringParseError(s, "Expected '(' after '|| '");
                        }
                        throw InternalError(PALUDIS_HERE,
                                "dps_had_double_bar_space: i->first is " + stringify(i->first));
                    } while (0);
                    continue;

                case dps_had_paren:
                    do
                    {
                        switch (i->first)
                        {
                            case dpl_whitespace:
                                state = dps_initial;
                                continue;

                            case dpl_text:
                            case dpl_use_flag:
                            case dpl_double_bar:
                            case dpl_open_paren:
                            case dpl_close_paren:
                                throw DepStringParseError(s, "Expected space after '(' or ')'");
                        }
                        throw InternalError(PALUDIS_HERE,
                                "dps_had_paren: i->first is " + stringify(i->first));
                    } while (0);
                    continue;

                case dps_had_use_flag:
                    do
                    {
                        switch (i->first)
                        {
                            case dpl_whitespace:
                                state = dps_had_use_flag_space;
                                continue;

                            case dpl_text:
                            case dpl_use_flag:
                            case dpl_double_bar:
                            case dpl_open_paren:
                            case dpl_close_paren:
                                throw DepStringParseError(s, "Expected space after use flag");
                        }
                        throw InternalError(PALUDIS_HERE,
                                "dps_had_use_flag: i->first is " + stringify(i->first));
                    } while (0);
                    continue;

                case dps_had_use_flag_space:
                    do
                    {
                        switch (i->first)
                        {
                            case dpl_open_paren:
                                state = dps_had_paren;
                                continue;

                            case dpl_whitespace:
                            case dpl_text:
                            case dpl_use_flag:
                            case dpl_double_bar:
                            case dpl_close_paren:
                                throw DepStringParseError(s, "Expected '(' after use flag");
                        }
                        throw InternalError(PALUDIS_HERE,
                                "dps_had_use_flag_space: i->first is " + stringify(i->first));
                    } while (0);
                    continue;
            }
            throw InternalError(PALUDIS_HERE,
                    "state is " + stringify(state));

        } while (0);
    }

    if (stack.empty())
        throw DepStringNestingError(s);
    std::tr1::shared_ptr<CompositeDepSpec> result(stack.top());
    stack.pop();
    if (! stack.empty())
        throw DepStringNestingError(s);
    return result;
}

std::tr1::shared_ptr<const CompositeDepSpec>
PortageDepParser::parse_depend(const std::string & s, const PackageDepSpecParseMode mode)
{
    return PortageDepParser::parse(s, Policy::text_is_package_dep_spec(true, mode));
}

std::tr1::shared_ptr<const CompositeDepSpec>
PortageDepParser::parse_license(const std::string & s)
{
    return PortageDepParser::parse(s, Policy::text_is_text_dep_spec(true));
}

PortageDepParser::Policy::Policy(const bool p, const PackageDepSpecParseMode m,
        std::tr1::shared_ptr<StringDepSpec> (Policy::* const f) (const std::string &) const) :
    _permit_any_deps(p),
    _parse_mode(m),
    _create_func(f)
{
}

std::tr1::shared_ptr<StringDepSpec>
PortageDepParser::Policy::_create_text_dep_spec(const std::string & s) const
{
    return std::tr1::shared_ptr<StringDepSpec>(new PlainTextDepSpec(s));
}

std::tr1::shared_ptr<StringDepSpec>
PortageDepParser::Policy::_create_package_dep_spec(const std::string & s) const
{
    if (s.empty() || '!' != s.at(0))
        return std::tr1::shared_ptr<StringDepSpec>(new PackageDepSpec(s, _parse_mode));
    else
        return std::tr1::shared_ptr<StringDepSpec>(new BlockDepSpec(
                    std::tr1::shared_ptr<PackageDepSpec>(new PackageDepSpec(s.substr(1), _parse_mode))));
}

PortageDepParser::Policy
PortageDepParser::Policy::text_is_text_dep_spec(bool permit_any_deps)
{
    return Policy(permit_any_deps, pds_pm_permissive, &Policy::_create_text_dep_spec);
}

PortageDepParser::Policy
PortageDepParser::Policy::text_is_package_dep_spec(bool permit_any_deps, PackageDepSpecParseMode mode)
{
    return Policy(permit_any_deps, mode, &Policy::_create_package_dep_spec);
}

std::tr1::shared_ptr<StringDepSpec>
PortageDepParser::Policy::create(const std::string & s) const
{
    return (this->*_create_func)(s);
}

bool
PortageDepParser::Policy::permit_any_deps() const
{
    return _permit_any_deps;
}