aboutsummaryrefslogtreecommitdiff
path: root/paludis/elike_dep_parser.cc
blob: 54c0ba273c7a33fe720e61f0c536315559f2d0de (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2008, 2010 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/elike_dep_parser.hh>
#include <paludis/util/simple_parser.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/map.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/name.hh>

using namespace paludis;

namespace
{
    void error(const SimpleParser &, const ELikeDepParserCallbacks &, const std::string &) PALUDIS_ATTRIBUTE((noreturn));

    void error(const SimpleParser & parser, const ELikeDepParserCallbacks & callbacks, const std::string & msg)
    {
        callbacks.on_error()(parser.text(), parser.offset(), msg);
        throw InternalError(PALUDIS_HERE, "Got error '" + msg + "' parsing '" + parser.text() +
                "', but the error function returned");
    }

    void parse_annotations(SimpleParser & parser, const ELikeDepParserCallbacks & callbacks)
    {
        Context context("When parsing annotation block at offset '" + stringify(parser.offset()) + "':");

        if (! parser.consume(*simple_parser::any_of(" \t\r\n") & simple_parser::exact("[[")))
            return;

        if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
            error(parser, callbacks, "Expected space after '[['");

        std::shared_ptr<Map<std::string, std::string> > annotations(std::make_shared<Map<std::string, std::string>>());
        while (true)
        {
            std::string word;

            if (parser.eof())
                error(parser, callbacks, "Reached end of text but wanted ']]'");
            else if (parser.consume(+simple_parser::any_of(" \t\r\n")))
            {
            }
            else if (parser.consume(simple_parser::exact("]]")))
                break;
            else if (parser.consume(+simple_parser::any_except(" \t\r\n") >> word))
            {
                if ("=" == word)
                    error(parser, callbacks, "Equals not allowed here");
                else if ("[" == word || "]" == word)
                    error(parser, callbacks, "Brackets not allowed here");

                if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                    error(parser, callbacks, "Expected space after annotation key");

                if (! parser.consume(simple_parser::exact("=")))
                    error(parser, callbacks, "Expected equals after space after annotation key");

                if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                    error(parser, callbacks, "Expected space after equals");

                std::string value;

                if (parser.consume(simple_parser::exact("[")))
                {
                    if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                        error(parser, callbacks, "Expected space after annotation quote");

                    while (true)
                    {
                        std::string v;
                        if (parser.eof())
                            error(parser, callbacks, "Reached end of text but wanted ']'");
                        else if (parser.consume(+simple_parser::any_of(" \t\r\n")))
                        {
                        }
                        else if (parser.consume(simple_parser::exact("]")))
                            break;
                        else if (parser.consume(+simple_parser::any_except(" \t\r\n") >> v))
                        {
                            if (! value.empty())
                                value.append(" ");
                            value.append(v);
                        }
                        else
                            error(parser, callbacks, "Expected word or ']'");
                    }

                    if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                        error(parser, callbacks, "Expected space after ']'");
                }
                else if (parser.consume(+simple_parser::any_except(" \t\r\n") >> value))
                {
                }
                else
                    error(parser, callbacks, "Expected word or quoted string after equals");

                if (annotations->end() != annotations->find(word))
                    error(parser, callbacks, "Duplicate annotation key '" + word + "'");
                else
                    annotations->insert(word, value);
            }
            else
                error(parser, callbacks, "Couldn't find annotation key");
        }

        if (! parser.eof())
            if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                error(parser, callbacks, "Expected space or eof after ']]'");

        callbacks.on_annotations()(annotations);
    }

    void
    parse(SimpleParser & parser, const ELikeDepParserCallbacks & callbacks, const bool end_with_close_paren,
            const bool child_of_any)
    {
        while (true)
        {
            Context context("When parsing from offset '" + stringify(parser.offset()) + "':");
            std::string word;

            if (parser.eof())
            {
                if (end_with_close_paren)
                    error(parser, callbacks, "Reached end of text but wanted ')'");
                else
                    return;
            }
            else if (parser.consume(simple_parser::exact("(")))
            {
                if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                    error(parser, callbacks, "Expected space after '('");

                callbacks.on_all()();
                parse(parser, callbacks, true, false);
            }
            else if (parser.consume(+simple_parser::any_of(" \t\r\n")))
            {
                /* discard whitespace */
            }
            else if (parser.consume(simple_parser::exact(")")))
            {
                if (end_with_close_paren)
                {
                    if (! parser.eof())
                        if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                            error(parser, callbacks, "Expected space or end of text after ')'");
                    callbacks.on_pop()();
                    parse_annotations(parser, callbacks);
                    return;
                }
                else
                    error(parser, callbacks, "Got ')' but expected end of text");
            }
            else if (parser.consume(simple_parser::exact("->")))
            {
                error(parser, callbacks, "Can't have '->' here");
            }
            else if (parser.consume(simple_parser::exact("||")))
            {
                if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                    error(parser, callbacks, "Expected space after '||'");

                if (! parser.consume(simple_parser::exact("(")))
                    error(parser, callbacks, "Expected '(' after '||' then space");

                if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                    error(parser, callbacks, "Expected space after '|| ('");

                callbacks.on_any()();
                parse(parser, callbacks, true, true);
            }
            else if (parser.consume(simple_parser::exact("^^")))
            {
                if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                    error(parser, callbacks, "Expected space after '^^'");

                if (! parser.consume(simple_parser::exact("(")))
                    error(parser, callbacks, "Expected '(' after '^^' then space");

                if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                    error(parser, callbacks, "Expected space after '^^ ('");

                callbacks.on_exactly_one()();
                parse(parser, callbacks, true, true);
            }
            else if (parser.consume(+simple_parser::any_except(" \t\r\n") >> word))
            {
                if ('?' == word.at(word.length() - 1))
                {
                    if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                        error(parser, callbacks, "Expected space after 'use?'");

                    if (! parser.consume(simple_parser::exact("(")))
                        error(parser, callbacks, "Expected '(' after 'use?' then space");

                    if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                        error(parser, callbacks, "Expected space after 'use? ('");

                    if (child_of_any)
                        callbacks.on_use_under_any()();

                    callbacks.on_use()(word);
                    parse(parser, callbacks, true, false);
                }
                else if (':' == word.at(word.length() - 1))
                {
                    callbacks.on_label()(word);
                    parse_annotations(parser, callbacks);
                }
                else if (parser.consume(+simple_parser::any_of(" \t\r\n") & simple_parser::exact("->")))
                {
                    if (! parser.consume(+simple_parser::any_of(" \t\r\n")))
                        error(parser, callbacks, "Expected space after '->'");

                    std::string second;
                    if (! parser.consume(+simple_parser::any_except(" \t\r\n") >> second))
                        error(parser, callbacks, "Expected word after '->' then space");

                    if ("->" == second || "||" == second || "(" == second || ")" == second)
                        error(parser, callbacks, "Expected word after '->' then space");

                    callbacks.on_arrow()(word, second);
                    parse_annotations(parser, callbacks);
                }
                else
                {
                    callbacks.on_string()(word);
                    parse_annotations(parser, callbacks);
                }
            }
            else
                error(parser, callbacks, "Unexpected trailing text");
        }
    }
}

void
paludis::parse_elike_dependencies(const std::string & s, const ELikeDepParserCallbacks & callbacks)
{
    Context context("When parsing '" + s + "':");

    SimpleParser parser(s);
    parse(parser, callbacks, false, false);
    callbacks.on_should_be_empty()();
}