aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/dep_parser.cc
blob: 34649b0d8cabcb39180916f989ae52e82b0c5746 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/* 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/repositories/e/eapi.hh>
#include <paludis/repositories/e/dep_lexer.hh>
#include <paludis/repositories/e/dep_parser.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/tr1_functional.hh>
#include <paludis/util/visitor-impl.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <libwrapiter/libwrapiter_forward_iterator.hh>
#include <libwrapiter/libwrapiter_output_iterator.hh>
#include <stack>
#include <set>

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

using namespace paludis;
using namespace paludis::erepository;

#include <paludis/repositories/e/dep_parser-se.cc>

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
{
    struct LabelsAreURI;
    struct LabelsAreDependency;

    enum DepParserState
    {
        dps_initial,
        dps_had_double_bar,
        dps_had_double_bar_space,
        dps_had_paren,
        dps_had_use_flag,
        dps_had_use_flag_space,
        dps_had_text_arrow,
        dps_had_text_arrow_space,
        dps_had_text_arrow_text,
        dps_had_label
    };

    using namespace tr1::placeholders;

    struct ParsePackageDepSpec
    {
        PackageDepSpecParseMode _parse_mode;

        ParsePackageDepSpec(PackageDepSpecParseMode m) :
            _parse_mode(m)
        {
        }

        template <typename H_>
        void
        add(const std::string & s, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> & p) const
        {
            p(tr1::shared_ptr<TreeLeaf<H_, PackageDepSpec> >(new TreeLeaf<H_, PackageDepSpec>(
                            tr1::shared_ptr<PackageDepSpec>(new PackageDepSpec(s, _parse_mode)))));
        }

        template <typename H_>
        void
        add_arrow(const std::string & lhs, const std::string & rhs, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> &) const
        {
            throw DepStringParseError(lhs + " -> " + rhs, "Arrows not allowed in this context");
        }
    };

    struct ParsePackageOrBlockDepSpec
    {
        PackageDepSpecParseMode _parse_mode;

        ParsePackageOrBlockDepSpec(PackageDepSpecParseMode m) :
            _parse_mode(m)
        {
        }

        template <typename H_>
        void
        add(const std::string & s, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> & p) const
        {
            if (s.empty() || '!' != s.at(0))
                p(tr1::shared_ptr<TreeLeaf<H_, PackageDepSpec> >(new TreeLeaf<H_, PackageDepSpec>(
                                tr1::shared_ptr<PackageDepSpec>(new PackageDepSpec(s, _parse_mode)))));
            else
                p(tr1::shared_ptr<TreeLeaf<H_, BlockDepSpec> >(new TreeLeaf<H_, BlockDepSpec>(
                                tr1::shared_ptr<BlockDepSpec>(new BlockDepSpec(
                                        tr1::shared_ptr<PackageDepSpec>(new PackageDepSpec(s.substr(1), _parse_mode)))))));
        }

        template <typename H_>
        void
        add_arrow(const std::string & lhs, const std::string & rhs, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> &) const
        {
            throw DepStringParseError(lhs + " -> " + rhs, "Arrows not allowed in this context");
        }
    };

    struct ParseTextDepSpec
    {
        template <typename H_>
        void
        add(const std::string & s, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> & p) const
        {
            p(tr1::shared_ptr<TreeLeaf<H_, PlainTextDepSpec> >(new TreeLeaf<H_, PlainTextDepSpec>(
                            tr1::shared_ptr<PlainTextDepSpec>(new PlainTextDepSpec(s)))));
        }

        template <typename H_>
        void
        add_arrow(const std::string & lhs, const std::string & rhs, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> &) const
        {
            throw DepStringParseError(lhs + " -> " + rhs, "Arrows not allowed in this context");
        }
    };

    struct ParseURIDepSpec
    {
        const bool _supports_arrow;

        ParseURIDepSpec(bool a) :
            _supports_arrow(a)
        {
        }

        template <typename H_>
        void
        add(const std::string & s, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> & p) const
        {
            p(tr1::shared_ptr<TreeLeaf<H_, URIDepSpec> >(new TreeLeaf<H_, URIDepSpec>(
                            tr1::shared_ptr<URIDepSpec>(new URIDepSpec(s)))));
        }

        template <typename H_>
        void
        add_arrow(const std::string & lhs, const std::string & rhs, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> & p) const
        {
            if (_supports_arrow)
                p(tr1::shared_ptr<TreeLeaf<H_, URIDepSpec> >(new TreeLeaf<H_, URIDepSpec>(
                                tr1::shared_ptr<URIDepSpec>(new URIDepSpec(lhs + " -> " + rhs)))));
            else
                throw DepStringParseError(lhs + " -> " + rhs, "Arrows not allowed in this EAPI");
        }
    };

    template <typename H_, bool>
    struct HandleUse
    {
        static void handle(const std::string & s, const std::string & i,
                std::stack<std::pair<tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)>, bool> > & stack)
        {
            std::string f(i);
            bool inv(f.length() && ('!' == f.at(0)));
            if (inv)
                f.erase(0, 1);

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

            f.erase(f.length() - 1);
            tr1::shared_ptr<ConstTreeSequence<H_, UseDepSpec> > a(
                    new ConstTreeSequence<H_, UseDepSpec>(tr1::shared_ptr<UseDepSpec>(
                            new UseDepSpec(UseFlagName(f), inv))));
            stack.top().first(a);
            stack.push(std::make_pair(tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)>(
                    tr1::bind(&ConstTreeSequence<H_, UseDepSpec>::add, a, _1)), false));
        }
    };

    template <typename H_>
    struct HandleUse<H_, false>
    {
        static void handle(const std::string & s, const std::string &,
                std::stack<std::pair<tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)>, bool> > &)
        {
            throw DepStringParseError(s, "use? group is not allowed here");
        }
    };

    template <typename H_, bool>
    struct HandleAny
    {
        static void handle(const std::string &, std::stack<std::pair<tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)>, bool> > &
                stack)
        {
             tr1::shared_ptr<ConstTreeSequence<H_, AnyDepSpec> > a(new ConstTreeSequence<H_, AnyDepSpec>(
                         tr1::shared_ptr<AnyDepSpec>(new AnyDepSpec)));
             stack.top().first(a);
             stack.push(std::make_pair(tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)>(
                     tr1::bind(&ConstTreeSequence<H_, AnyDepSpec>::add, a, _1)), true));
        }
    };

    template <typename H_>
    struct HandleAny<H_, false>
    {
        static void handle(const std::string & s, std::stack<std::pair<tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)>, bool> > &)
        {
             throw DepStringParseError(s, "|| is not allowed here");
        }
    };

    template <typename H_, typename K_>
    struct HandleLabel
    {
        static void add(const std::string & s, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> &,
                const EAPI &)
        {
            throw DepStringParseError(s, "label is not allowed here");
        }
    };

    template <typename H_>
    struct HandleLabel<H_, LabelsAreURI>
    {
        static void add(const std::string & s, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> & p,
                const EAPI & e)
        {
            if (e.supported && e.supported->uri_labels)
                p(tr1::shared_ptr<TreeLeaf<H_, LabelsDepSpec<URILabelVisitorTypes> > >(
                            new TreeLeaf<H_, LabelsDepSpec<URILabelVisitorTypes> >(parse_uri_label(s, e))));
            else
                throw DepStringParseError(s, "URI labels not allowed in this EAPI");
        }
    };

    template <typename H_>
    struct HandleLabel<H_, LabelsAreDependency>
    {
        static void add(const std::string & s, tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)> & p,
                const EAPI & e)
        {
            if (e.supported && e.supported->dependency_labels)
                p(tr1::shared_ptr<TreeLeaf<H_, DependencyLabelDepSpec> >(
                            new TreeLeaf<H_, DependencyLabelDepSpec>(parse_dependency_label(s, e))));
            else
                throw DepStringParseError(s, "Dependency labels not allowed in this EAPI");
        }
    };

    bool disallow_any_use(const DependencySpecTreeParseMode tree_mode)
    {
        switch (tree_mode)
        {
            case dst_pm_eapi_0:
                return false;

            case dst_pm_paludis_1:
            case dst_pm_exheres_0:
                return true;

            case last_dst_pm:
                ;
        }

        throw InternalError(PALUDIS_HERE, "bad _tree_mode");
    }
}

namespace
{
    template <typename H_, typename I_, bool any_, bool use_, typename Label_>
    tr1::shared_ptr<typename H_::ConstItem>
    parse(const std::string & s, bool disallow_any_use, const I_ & p, const EAPI & e)
    {
        Context context("When parsing dependency string '" + s + "':");

        tr1::shared_ptr<ConstTreeSequence<H_, AllDepSpec> > result(
            new ConstTreeSequence<H_, AllDepSpec>(tr1::shared_ptr<AllDepSpec>(new AllDepSpec)));
        std::stack<std::pair<tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)>, bool> > stack;
        stack.push(std::make_pair(tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)>(
                tr1::bind(&ConstTreeSequence<H_, AllDepSpec>::add, result, _1)), false));

        std::string arrow_lhs;
        DepParserState state(dps_initial);
        DepLexer lexer(s);
        DepLexer::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_arrow:
                                     throw DepStringParseError(s, "Arrow not allowed here");

                                case dpl_text:
                                     {
                                         if (i->second.empty())
                                             throw DepStringParseError(i->second, "Empty text entry");

                                         DepLexer::Iterator i_fwd(next(i));
                                         if (i_fwd != i_end && i_fwd->first == dpl_whitespace && ++i_fwd != i_end
                                                 && i_fwd->first == dpl_arrow)
                                         {
                                             arrow_lhs = i->second;
                                             i = i_fwd;
                                             state = dps_had_text_arrow;
                                         }
                                         else
                                             p.template add<H_>(i->second, stack.top().first);
                                     }
                                     continue;

                                case dpl_open_paren:
                                     {
                                         tr1::shared_ptr<ConstTreeSequence<H_, AllDepSpec> > a(new ConstTreeSequence<H_, AllDepSpec>(
                                                     tr1::shared_ptr<AllDepSpec>(new AllDepSpec)));
                                         stack.top().first(a);
                                         stack.push(std::make_pair(tr1::function<void (tr1::shared_ptr<ConstAcceptInterface<H_> >)>(
                                                         tr1::bind(&ConstTreeSequence<H_, AllDepSpec>::add, a, _1)), false));
                                         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:
                                     HandleAny<H_, any_>::handle(s, stack);
                                     state = dps_had_double_bar;
                                     continue;

                                case dpl_use_flag:
                                     if (use_ && disallow_any_use && stack.top().second)
                                         throw DepStringParseError(s, "use? group is not allowed immediately under a || ( )");
                                     HandleUse<H_, use_>::handle(s, i->second, stack);
                                     state = dps_had_use_flag;
                                     continue;

                                case dpl_label:
                                     HandleLabel<H_, Label_>::add(i->second, stack.top().first, e);
                                     state = dps_had_label;
                                     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_arrow:
                                case dpl_use_flag:
                                case dpl_double_bar:
                                case dpl_open_paren:
                                case dpl_close_paren:
                                case dpl_label:
                                    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:
                                case dpl_arrow:
                                case dpl_label:
                                    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:
                                case dpl_arrow:
                                case dpl_label:
                                    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:
                                case dpl_arrow:
                                case dpl_label:
                                    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:
                                case dpl_arrow:
                                case dpl_label:
                                    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;

                    case dps_had_label:
                        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:
                                case dpl_arrow:
                                case dpl_label:
                                    throw DepStringParseError(s, "Expected space after label");
                            }
                            throw InternalError(PALUDIS_HERE,
                                    "dps_had_label: i->first is " + stringify(i->first));
                        } while (0);
                        continue;

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

                                case dpl_text:
                                case dpl_open_paren:
                                case dpl_use_flag:
                                case dpl_double_bar:
                                case dpl_close_paren:
                                case dpl_arrow:
                                case dpl_label:
                                    throw DepStringParseError(s, "Expected whitespace after arrow");
                            }
                            throw InternalError(PALUDIS_HERE,
                                    "dps_had_text_arrow: i->first is " + stringify(i->first));
                        } while (0);
                        continue;

                    case dps_had_text_arrow_space:
                        do
                        {
                            switch (i->first)
                            {
                                case dpl_whitespace:
                                    continue;

                                case dpl_text:
                                    state = dps_had_text_arrow_text;
                                    p.template add_arrow<H_>(arrow_lhs, i->second, stack.top().first);
                                    continue;

                                case dpl_open_paren:
                                case dpl_use_flag:
                                case dpl_double_bar:
                                case dpl_close_paren:
                                case dpl_arrow:
                                case dpl_label:
                                    throw DepStringParseError(s, "Expected text after whitespace after arrow");
                            }
                            throw InternalError(PALUDIS_HERE,
                                    "dps_had_text_arrow_space: i->first is " + stringify(i->first));
                        } while (0);
                        continue;

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

                                case dpl_text:
                                case dpl_open_paren:
                                case dpl_use_flag:
                                case dpl_close_paren:
                                case dpl_double_bar:
                                case dpl_arrow:
                                case dpl_label:
                                    throw DepStringParseError(s, "Expected whitespace after text after whitespace after arrow");
                            }
                            throw InternalError(PALUDIS_HERE,
                                    "dps_had_text_arrow_text: 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);

        switch (state)
        {
            case dps_initial:
            case dps_had_paren:
            case dps_had_text_arrow_text:
            case dps_had_text_arrow_space:
                break;

            case dps_had_double_bar_space:
            case dps_had_double_bar:
            case dps_had_use_flag:
            case dps_had_use_flag_space:
            case dps_had_text_arrow:
            case dps_had_label:
                throw DepStringParseError(s, "Unexpected end of string");
        }

        stack.pop();
        if (! stack.empty())
            throw DepStringNestingError(s);
        return result;
    }
}

tr1::shared_ptr<DependencySpecTree::ConstItem>
paludis::erepository::parse_depend(const std::string & s, const EAPI & e)
{
    Context c("When parsing dependency string '" + s + "' using EAPI '" + e.name + "':");

    if (! e.supported)
        throw DepStringParseError(s, "Don't know how to parse EAPI '" + e.name + "' dependencies");

    return parse<DependencySpecTree, ParsePackageOrBlockDepSpec, true, true, LabelsAreDependency>(s,
            disallow_any_use(e.supported->dependency_spec_tree_parse_mode),
            ParsePackageOrBlockDepSpec(e.supported->package_dep_spec_parse_mode), e);
}

tr1::shared_ptr<ProvideSpecTree::ConstItem>
paludis::erepository::parse_provide(const std::string & s, const EAPI & e)
{
    Context c("When parsing provide string '" + s + "' using EAPI '" + e.name + "':");

    if (! e.supported)
        throw DepStringParseError(s, "Don't know how to parse EAPI '" + e.name + "' provides");

    return parse<ProvideSpecTree, ParsePackageDepSpec, false, true, void>(s, false,
            ParsePackageDepSpec(pds_pm_eapi_0), e);
}

tr1::shared_ptr<RestrictSpecTree::ConstItem>
paludis::erepository::parse_restrict(const std::string & s, const EAPI & e)
{
    Context c("When parsing restrict string '" + s + "' using EAPI '" + e.name + "':");

    if (! e.supported)
        throw DepStringParseError(s, "Don't know how to parse EAPI '" + e.name + "' restrictions");

    return parse<RestrictSpecTree, ParseTextDepSpec, false, true, void>(s, false,
            ParseTextDepSpec(), e);
}

tr1::shared_ptr<URISpecTree::ConstItem>
paludis::erepository::parse_uri(const std::string & s, const EAPI & e)
{
    Context c("When parsing URI string '" + s + "' using EAPI '" + e.name + "':");

    if (! e.supported)
        throw DepStringParseError(s, "Don't know how to parse EAPI '" + e.name + "' URIs");

    return parse<URISpecTree, ParseURIDepSpec, false, true, LabelsAreURI>(s, false,
            ParseURIDepSpec(e.supported->uri_supports_arrow), e);
}

tr1::shared_ptr<LicenseSpecTree::ConstItem>
paludis::erepository::parse_license(const std::string & s, const EAPI & e)
{
    Context c("When parsing license string '" + s + "' using EAPI '" + e.name + "':");

    if (! e.supported)
        throw DepStringParseError(s, "Don't know how to parse EAPI '" + e.name + "' licenses");

    return parse<LicenseSpecTree, ParseTextDepSpec, true, true, void>(s,
            true, ParseTextDepSpec(), e);
}

tr1::shared_ptr<LabelsDepSpec<URILabelVisitorTypes> >
paludis::erepository::parse_uri_label(const std::string & s, const EAPI & e)
{
    Context context("When parsing label string '" + s + "' using EAPI '" + e.name + "':");

    if (s.empty())
        throw DepStringParseError(s, "Empty label");

    std::string c(e.supported->uri_labels->class_for_label(s.substr(0, s.length() - 1)));
    if (c.empty())
        throw DepStringParseError(s, "Unknown label");

    tr1::shared_ptr<LabelsDepSpec<URILabelVisitorTypes> > l(new LabelsDepSpec<URILabelVisitorTypes>);

    if (c == "URIMirrorsThenListedLabel")
        l->add_label(make_shared_ptr(new URIMirrorsThenListedLabel(s.substr(0, s.length() - 1))));
    else if (c == "URIMirrorsOnlyLabel")
        l->add_label(make_shared_ptr(new URIMirrorsOnlyLabel(s.substr(0, s.length() - 1))));
    else if (c == "URIListedOnlyLabel")
        l->add_label(make_shared_ptr(new URIListedOnlyLabel(s.substr(0, s.length() - 1))));
    else if (c == "URIListedThenMirrorsLabel")
        l->add_label(make_shared_ptr(new URIListedThenMirrorsLabel(s.substr(0, s.length() - 1))));
    else if (c == "URILocalMirrorsOnlyLabel")
        l->add_label(make_shared_ptr(new URILocalMirrorsOnlyLabel(s.substr(0, s.length() - 1))));
    else if (c == "URIManualOnlyLabel")
        l->add_label(make_shared_ptr(new URIManualOnlyLabel(s.substr(0, s.length() - 1))));
    else
        throw DepStringParseError(s, "Label '" + s + "' maps to unknown class '" + c + "'");

    return l;
}

tr1::shared_ptr<DependencyLabelDepSpec>
paludis::erepository::parse_dependency_label(const std::string & s, const EAPI & e)
{
    Context context("When parsing label string '" + s + "' using EAPI '" + e.name + "':");

    if (s.empty())
        throw DepStringParseError(s, "Empty label");

    std::set<std::string> labels;
    std::string label(s.substr(0, s.length() - 1));
    Tokeniser<delim_kind::AnyOfTag, delim_mode::DelimiterTag>(",+").tokenise(label, std::inserter(labels, labels.end()));

    tr1::shared_ptr<DependencyLabelDepSpec> l(new DependencyLabelDepSpec);

    for (std::set<std::string>::iterator it = labels.begin(), it_e = labels.end(); it != it_e; ++it)
    {
        std::string c(e.supported->dependency_labels->class_for_label(*it));
        if (c.empty())
            throw DepStringParseError(s, "Unknown label '" + *it + "'");

        if (c == "DependencyHostLabel")
            l->add_label(make_shared_ptr(new DependencyHostLabel(*it)));
        else if (c == "DependencyTargetLabel")
            l->add_label(make_shared_ptr(new DependencyTargetLabel(*it)));
        else if (c == "DependencyBuildLabel")
            l->add_label(make_shared_ptr(new DependencyBuildLabel(*it)));
        else if (c == "DependencyRunLabel")
            l->add_label(make_shared_ptr(new DependencyRunLabel(*it)));
        else if (c == "DependencyInstallLabel")
            l->add_label(make_shared_ptr(new DependencyInstallLabel(*it)));
        else if (c == "DependencyCompileLabel")
            l->add_label(make_shared_ptr(new DependencyCompileLabel(*it)));
        else if (c == "DependencySuggestedLabel")
            l->add_label(make_shared_ptr(new DependencySuggestedLabel(*it)));
        else if (c == "DependencyRecommendedLabel")
            l->add_label(make_shared_ptr(new DependencyRecommendedLabel(*it)));
        else if (c == "DependencyRequiredLabel")
            l->add_label(make_shared_ptr(new DependencyRequiredLabel(*it)));
        else if (c == "DependencyAnyLabel")
            l->add_label(make_shared_ptr(new DependencyAnyLabel(*it)));
        else if (c == "DependencyMineLabel")
            l->add_label(make_shared_ptr(new DependencyMineLabel(*it)));
        else if (c == "DependencyPrimaryLabel")
            l->add_label(make_shared_ptr(new DependencyPrimaryLabel(*it)));
        else if (c == "DependencyABILabel")
            l->add_label(make_shared_ptr(new DependencyABILabel(*it)));
        else
            throw DepStringParseError(s, "Label '" + *it + "' maps to unknown class '" + c + "'");
    }

    return l;
}