aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/dep_parser.cc
blob: 0cd6c85702c5ffbe3315f2112603575a5533d6e1 (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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006, 2007, 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/e/dep_parser.hh>
#include <paludis/repositories/e/parse_dependency_label.hh>
#include <paludis/repositories/e/parse_uri_label.hh>
#include <paludis/repositories/e/parse_plain_text_label.hh>
#include <paludis/repositories/e/parse_annotations.hh>
#include <paludis/repositories/e/eapi.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/options.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/map.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/return_literal_function.hh>
#include <paludis/util/log.hh>
#include <paludis/util/visitor_cast.hh>
#include <paludis/util/make_null_shared_ptr.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/singleton-impl.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/action.hh>
#include <paludis/choice.hh>
#include <paludis/dep_spec_annotations.hh>
#include <paludis/metadata_key.hh>
#include <paludis/always_enabled_dependency_label.hh>
#include <paludis/elike_blocker.hh>
#include <map>
#include <list>
#include <set>
#include <ostream>
#include <algorithm>
#include <functional>

using namespace paludis;
using namespace paludis::erepository;

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

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

namespace paludis
{
    namespace n
    {
        typedef Name<struct name_block_children> block_children;
        typedef Name<struct name_children> children;
        typedef Name<struct name_item> item;
        typedef Name<struct name_spec> spec;
    }
}

namespace
{
    enum BlockFixOp
    {
        bfo_explicit_strong,
        bfo_implicit_strong,
        bfo_implicit_weak
    };

    template <typename T_>
    struct ParseStackTypes
    {
        struct Item
        {
            NamedValue<n::block_children, std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> > > block_children;
            NamedValue<n::children, std::list<std::shared_ptr<DepSpec> > > children;
            NamedValue<n::item, std::shared_ptr<typename T_::BasicInnerNode> > item;
            NamedValue<n::spec, std::shared_ptr<DepSpec> > spec;
        };

        typedef std::list<Item> Stack;
        typedef std::function<void (const std::shared_ptr<DepSpec> &)> AnnotationsGoHere;
        typedef std::function<void (const std::list<std::shared_ptr<DepSpec> > &)> StarAnnotationsGoHere;
    };

    template <typename T_>
    void package_dep_spec_string_handler(
            typename ParseStackTypes<T_>::Stack & h,
            const typename ParseStackTypes<T_>::AnnotationsGoHere & annotations_go_here,
            const std::string & s,
            const EAPI & eapi)
    {
        std::shared_ptr<PackageDepSpec> spec(std::make_shared<PackageDepSpec>(
                    parse_elike_package_dep_spec(s, eapi.supported()->package_dep_spec_parse_options(),
                        eapi.supported()->version_spec_options())));
        h.begin()->item()->append(spec);
        h.begin()->children().push_back(spec);
        annotations_go_here(spec);
    }

    template <typename T_>
    void package_or_block_dep_spec_string_handler(
            typename ParseStackTypes<T_>::Stack & h,
            const typename ParseStackTypes<T_>::AnnotationsGoHere & annotations_go_here,
            const std::string & s,
            const EAPI & eapi)
    {
        auto p(split_elike_blocker(s));

        switch (std::get<0>(p))
        {
            case ebk_no_block:
                package_dep_spec_string_handler<T_>(h, annotations_go_here, s, eapi);
                break;

            case ebk_bang_question:
                throw EDepParseError(s, "!? blocks not allowed here");

            case ebk_single_bang:
            case ebk_double_bang:
                {
                    BlockFixOp op(bfo_implicit_weak);

                    if (std::get<0>(p) == ebk_double_bang)
                    {
                        if (! eapi.supported()->dependency_spec_tree_parse_options()[dstpo_double_bang_blocks])
                            throw EDepParseError(s, "Double-! blocks not allowed in this EAPI");
                        op = bfo_explicit_strong;
                    }
                    else
                    {
                        if (eapi.supported()->dependency_spec_tree_parse_options()[dstpo_single_bang_block_is_hard])
                            op = bfo_implicit_strong;
                    }

                    std::shared_ptr<BlockDepSpec> spec(std::make_shared<BlockDepSpec>(
                                s,
                                parse_elike_package_dep_spec(std::get<2>(p),
                                    eapi.supported()->package_dep_spec_parse_options(),
                                    eapi.supported()->version_spec_options())));
                    h.begin()->item()->append(spec);
                    h.begin()->block_children().push_back(std::make_pair(spec, op));
                    h.begin()->children().push_back(spec);

                    annotations_go_here(spec);
                }
                break;

            case last_ebk:
                throw InternalError(PALUDIS_HERE, "unhandled ebk");
        }
    }

    template <typename T_>
    void license_handler(
            typename ParseStackTypes<T_>::Stack & h,
            const typename ParseStackTypes<T_>::AnnotationsGoHere & annotations_go_here,
            const std::string & s)
    {
        std::shared_ptr<LicenseDepSpec> spec(std::make_shared<LicenseDepSpec>(s));
        h.begin()->item()->append(spec);
        h.begin()->children().push_back(spec);
        annotations_go_here(spec);
    }

    template <typename T_>
    void plain_text_handler(
            typename ParseStackTypes<T_>::Stack & h,
            const typename ParseStackTypes<T_>::AnnotationsGoHere & annotations_go_here,
            const std::string & s)
    {
        std::shared_ptr<PlainTextDepSpec> spec(std::make_shared<PlainTextDepSpec>(s));
        h.begin()->item()->append(spec);
        h.begin()->children().push_back(spec);
        annotations_go_here(spec);
    }

    template <typename T_>
    void simple_uri_handler(
            typename ParseStackTypes<T_>::Stack & h,
            const typename ParseStackTypes<T_>::AnnotationsGoHere & annotations_go_here,
            const std::string & s)
    {
        std::shared_ptr<SimpleURIDepSpec> spec(std::make_shared<SimpleURIDepSpec>(s));
        h.begin()->item()->append(spec);
        h.begin()->children().push_back(spec);
        annotations_go_here(spec);
    }

    template <typename T_>
    void arrow_handler(
            typename ParseStackTypes<T_>::Stack & h,
            const typename ParseStackTypes<T_>::AnnotationsGoHere & annotations_go_here,
            const std::string & s,
            const std::string & f,
            const std::string & t,
            const EAPI & eapi)
    {
        if (t.empty() || eapi.supported()->dependency_spec_tree_parse_options()[dstpo_uri_supports_arrow])
        {
            std::shared_ptr<FetchableURIDepSpec> spec(std::make_shared<FetchableURIDepSpec>(t.empty() ? f : f + " -> " + t));
            h.begin()->item()->append(spec);
            h.begin()->children().push_back(spec);
            annotations_go_here(spec);
        }
        else
            throw EDepParseError(s, "Arrows not allowed in this EAPI");
    }

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

    void any_not_allowed_handler(const std::string & s)
    {
        throw EDepParseError(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 EDepParseError(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 EDepParseError(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 EDepParseError(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 EDepParseError(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 EDepParseError(s, "Label '" + f + "' not allowed here");
    }

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

    void use_not_allowed_handler(const std::string & s, const std::string & f)
    {
        throw EDepParseError(s, "Conditional '" + f + "' not allowed here");
    }

    template <typename T_>
    void dependency_label_handler(
            const Environment * const env,
            typename ParseStackTypes<T_>::Stack & h,
            const typename ParseStackTypes<T_>::AnnotationsGoHere & annotations_go_here,
            const std::string & s,
            const EAPI & eapi)
    {
        std::shared_ptr<DependenciesLabelsDepSpec> spec(parse_dependency_label(env, s, eapi));
        h.begin()->item()->append(spec);
        h.begin()->children().push_back(spec);
        annotations_go_here(spec);
    }

    template <typename T_>
    void fetchable_label_handler(
            typename ParseStackTypes<T_>::Stack & h,
            const typename ParseStackTypes<T_>::AnnotationsGoHere & annotations_go_here,
            const std::string & s,
            const EAPI & eapi)
    {
        std::shared_ptr<URILabelsDepSpec> spec(parse_uri_label(s, eapi));
        h.begin()->item()->append(spec);
        h.begin()->children().push_back(spec);
        annotations_go_here(spec);
    }

    template <typename T_>
    void plain_text_label_handler(
            typename ParseStackTypes<T_>::Stack & h,
            const typename ParseStackTypes<T_>::AnnotationsGoHere & annotations_go_here,
            const std::string & s)
    {
        std::shared_ptr<PlainTextLabelDepSpec> spec(parse_plain_text_label(s));
        h.begin()->item()->append(spec);
        h.begin()->children().push_back(spec);
        annotations_go_here(spec);
    }

    template <typename T_, typename A_>
    void any_all_handler(typename ParseStackTypes<T_>::Stack & stack)
    {
        std::shared_ptr<A_> spec(std::make_shared<A_>());
        stack.push_front(make_named_values<typename ParseStackTypes<T_>::Item>(
                    n::block_children() = std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> >(),
                    n::children() = std::list<std::shared_ptr<DepSpec> >(),
                    n::item() = stack.begin()->item()->append(spec),
                    n::spec() = spec
                ));
    }

    template <typename T_>
    void use_handler(
            typename ParseStackTypes<T_>::Stack & stack,
            const std::string & u,
            const Environment * const,
            const EAPI & eapi,
            bool is_installed)
    {
        std::shared_ptr<ConditionalDepSpec> spec(std::make_shared<ConditionalDepSpec>(parse_elike_conditional_dep_spec(
                        u, is_installed || ! eapi.supported()->package_dep_spec_parse_options()[epdso_missing_use_deps_is_qa])));
        stack.push_front(make_named_values<typename ParseStackTypes<T_>::Item>(
                    n::block_children() = std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> >(),
                    n::children() = std::list<std::shared_ptr<DepSpec> >(),
                    n::item() = stack.begin()->item()->append(spec),
                    n::spec() = spec
                ));
    }

    template <typename T_>
    void pop_handler(
            typename ParseStackTypes<T_>::Stack & stack,
            const typename ParseStackTypes<T_>::AnnotationsGoHere & annotations_go_here,
            const typename ParseStackTypes<T_>::StarAnnotationsGoHere & star_annotations_go_here,
            const std::string & s)
    {
        annotations_go_here(stack.begin()->spec());
        star_annotations_go_here(stack.begin()->children());

        auto children(std::move(stack.begin()->children()));
        auto block_children(std::move(stack.begin()->block_children()));

        stack.pop_front();
        if (stack.empty())
            throw EDepParseError(s, "Too many ')'s");

        stack.begin()->children().insert(stack.begin()->children().end(), children.begin(), children.end());
        stack.begin()->block_children().insert(stack.begin()->block_children().end(), block_children.begin(), block_children.end());
    }

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

    void use_under_any_handler(const std::string & s, const EAPI & eapi)
    {
        if (eapi.supported()->dependency_spec_tree_parse_options()[dstpo_disallow_any_use])
            throw EDepParseError(s, "use? not allowed under || ( ) in this EAPI");
    }

    void do_nothing()
    {
    }

    void set_thing_to_annotate(std::shared_ptr<DepSpec> & spec, const std::shared_ptr<DepSpec> & s)
    {
        spec = s;
    }

    void set_thing_to_star_annotate(std::list<std::shared_ptr<DepSpec> > & spec_list, const std::list<std::shared_ptr<DepSpec> > & s)
    {
        spec_list = s;
    }

    void add_block_annotations(
            std::shared_ptr<DepSpecAnnotations> & annotations,
            const BlockFixOp & block_fix_op)
    {
        DepSpecAnnotationRole current_role(dsar_none);
        if (annotations)
            current_role = find_blocker_role_in_annotations(annotations);
        if (dsar_none != current_role)
            return;

        switch (block_fix_op)
        {
            case bfo_explicit_strong:
                annotations->add(make_named_values<DepSpecAnnotation>(
                            n::key() = "<resolution>",
                            n::kind() = dsak_synthetic,
                            n::role() = dsar_blocker_strong,
                            n::value() = "<explicit-strong>"
                            ));
                break;

            case bfo_implicit_strong:
                annotations->add(make_named_values<DepSpecAnnotation>(
                            n::key() = "<resolution>",
                            n::kind() = dsak_synthetic,
                            n::role() = dsar_blocker_strong,
                            n::value() = "<implicit-strong>"
                            ));
                break;

            case bfo_implicit_weak:
                annotations->add(make_named_values<DepSpecAnnotation>(
                            n::key() = "<resolution>",
                            n::kind() = dsak_synthetic,
                            n::role() = dsar_blocker_weak,
                            n::value() = "<implicit-weak>"
                            ));
                break;
        }
    }

    void add_synthetic_block_annotations(
            const EAPI & eapi,
            const std::shared_ptr<BlockDepSpec> & block_spec,
            const BlockFixOp & block_fix_op)
    {
        auto annotations(std::make_shared<DepSpecAnnotations>());
        if (block_spec->maybe_annotations())
            std::for_each(block_spec->maybe_annotations()->begin(), block_spec->maybe_annotations()->end(),
                    std::bind(&DepSpecAnnotations::add, annotations, std::placeholders::_1));

        add_block_annotations(annotations, block_fix_op);
        if (eapi.supported()->dependency_spec_tree_parse_options()[dstpo_no_self_block])
            annotations->add(make_named_values<DepSpecAnnotation>(
                        n::key() = "<match_package>",
                        n::kind() = dsak_synthetic,
                        n::role() = dsar_no_self_match,
                        n::value() = "<no_self_block>"
                        ));

        block_spec->set_annotations(annotations);
    }

    void add_expanded_annotation(
            const EAPI &,
            const std::shared_ptr<DepSpec> & spec,
            const DepSpecAnnotation & expandable)
    {
        auto annotations(std::make_shared<DepSpecAnnotations>());
        if (spec->maybe_annotations())
            std::for_each(spec->maybe_annotations()->begin(), spec->maybe_annotations()->end(),
                    std::bind(&DepSpecAnnotations::add, annotations, std::placeholders::_1));

        annotations->add(make_named_values<DepSpecAnnotation>(
                    n::key() = expandable.key().substr(1),
                    n::kind() = dsak_expanded,
                    n::role() = expandable.role(),
                    n::value() = expandable.value()));

        spec->set_annotations(annotations);
    }

    void apply_annotations(
            const EAPI & eapi,
            const std::shared_ptr<DepSpec> & spec,
            const std::list<std::shared_ptr<DepSpec> > & children,
            const std::shared_ptr<const Map<std::string, std::string> > & m)
    {
        spec->set_annotations(parse_annotations(eapi, m));

        for (auto a(spec->maybe_annotations()->begin()), a_end(spec->maybe_annotations()->end()) ;
                a != a_end ; ++a)
            switch (a->kind())
            {
                case dsak_synthetic:
                case dsak_literal:
                case dsak_expanded:
                    continue;

                case dsak_expandable:
                    for (auto c(children.begin()), c_end(children.end()) ;
                            c != c_end ; ++c)
                        add_expanded_annotation(eapi, *c, *a);
                    continue;

                case last_dsak:
                    break;
            }
    }
}

std::shared_ptr<DependencySpecTree>
paludis::erepository::parse_depend(const std::string & s, const Environment * const env, const EAPI & eapi, const bool is_installed)
{
    using namespace std::placeholders;

    ParseStackTypes<DependencySpecTree>::Stack stack;
    std::shared_ptr<AllDepSpec> spec(std::make_shared<AllDepSpec>());
    std::shared_ptr<DepSpec> thing_to_annotate(spec);
    std::list<std::shared_ptr<DepSpec> > thing_to_star_annotate;
    std::shared_ptr<DependencySpecTree> top(std::make_shared<DependencySpecTree>(spec));
    stack.push_front(make_named_values<ParseStackTypes<DependencySpecTree>::Item>(
                n::block_children() = std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> >(),
                n::children() = std::list<std::shared_ptr<DepSpec> >(),
                n::item() = top->top(),
                n::spec() = spec
            ));

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<DependencySpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = std::bind(&apply_annotations, std::cref(eapi), std::ref(thing_to_annotate), std::cref(thing_to_star_annotate), _1),
                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(&dependency_label_handler<DependencySpecTree>, env,
                    std::ref(stack),
                    ParseStackTypes<DependencySpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)),
                    _1, std::cref(eapi)),
                n::on_no_annotations() = &do_nothing,
                n::on_pop() = std::bind(&pop_handler<DependencySpecTree>, std::ref(stack),
                    ParseStackTypes<DependencySpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)),
                    ParseStackTypes<DependencySpecTree>::StarAnnotationsGoHere(std::bind(
                            &set_thing_to_star_annotate, std::ref(thing_to_star_annotate), _1)),
                    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),
                    ParseStackTypes<DependencySpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), _1, eapi),
                n::on_use() = std::bind(&use_handler<DependencySpecTree>, std::ref(stack), _1, env, std::cref(eapi), is_installed),
                n::on_use_under_any() = std::bind(&use_under_any_handler, s, std::cref(eapi))
            ));

    parse_elike_dependencies(s, callbacks, { });

    for (auto b(stack.begin()->block_children().begin()), b_end(stack.begin()->block_children().end()) ;
            b != b_end ; ++b)
        add_synthetic_block_annotations(eapi, b->first, b->second);

    return top;
}

std::shared_ptr<SetSpecTree>
paludis::erepository::parse_commented_set(const std::string & s, const Environment * const, const EAPI & eapi)
{
    using namespace std::placeholders;

    ParseStackTypes<SetSpecTree>::Stack stack;
    std::shared_ptr<AllDepSpec> spec(std::make_shared<AllDepSpec>());
    std::shared_ptr<DepSpec> thing_to_annotate(spec);
    std::list<std::shared_ptr<DepSpec> > thing_to_star_annotate;
    std::shared_ptr<SetSpecTree> top(std::make_shared<SetSpecTree>(spec));
    stack.push_front(make_named_values<ParseStackTypes<SetSpecTree>::Item>(
                n::block_children() = std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> >(),
                n::children() = std::list<std::shared_ptr<DepSpec> >(),
                n::item() = top->top(),
                n::spec() = spec
            ));

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<SetSpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = std::bind(&apply_annotations, std::cref(eapi), std::ref(thing_to_annotate), std::cref(thing_to_star_annotate), _1),
                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<SetSpecTree>, std::ref(stack),
                    ParseStackTypes<SetSpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)),
                    ParseStackTypes<SetSpecTree>::StarAnnotationsGoHere(std::bind(
                            &set_thing_to_star_annotate, std::ref(thing_to_star_annotate), _1)),
                    s),
                n::on_should_be_empty() = std::bind(&should_be_empty_handler<SetSpecTree>, std::ref(stack), s),
                n::on_string() = std::bind(&package_dep_spec_string_handler<SetSpecTree>, std::ref(stack),
                    ParseStackTypes<SetSpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), _1, eapi),
                n::on_use() = std::bind(&use_not_allowed_handler, s, _1),
                n::on_use_under_any() = std::bind(&use_under_any_handler, s, std::cref(eapi))
            ));

    parse_elike_dependencies(s, callbacks, { edpo_allow_embedded_comments });

    for (auto b(stack.begin()->block_children().begin()), b_end(stack.begin()->block_children().end()) ;
            b != b_end ; ++b)
        add_synthetic_block_annotations(eapi, b->first, b->second);

    return top;
}

std::shared_ptr<FetchableURISpecTree>
paludis::erepository::parse_fetchable_uri(const std::string & s, const Environment * const env, const EAPI & eapi, const bool is_installed)
{
    using namespace std::placeholders;

    ParseStackTypes<FetchableURISpecTree>::Stack stack;
    std::shared_ptr<AllDepSpec> spec(std::make_shared<AllDepSpec>());
    std::shared_ptr<DepSpec> thing_to_annotate(spec);
    std::list<std::shared_ptr<DepSpec> > thing_to_star_annotate;
    std::shared_ptr<FetchableURISpecTree> top(std::make_shared<FetchableURISpecTree>(spec));
    stack.push_front(make_named_values<ParseStackTypes<FetchableURISpecTree>::Item>(
                n::block_children() = std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> >(),
                n::children() = std::list<std::shared_ptr<DepSpec> >(),
                n::item() = top->top(),
                n::spec() = spec
            ));

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<FetchableURISpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = std::bind(&apply_annotations, std::cref(eapi), std::ref(thing_to_annotate), std::cref(thing_to_star_annotate), _1),
                n::on_any() = std::bind(&any_not_allowed_handler, s),
                n::on_arrow() = std::bind(&arrow_handler<FetchableURISpecTree>, std::ref(stack),
                    ParseStackTypes<FetchableURISpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), s, _1, _2, std::cref(eapi)),
                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(&fetchable_label_handler<FetchableURISpecTree>, std::ref(stack),
                    ParseStackTypes<FetchableURISpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), _1, std::cref(eapi)),
                n::on_no_annotations() = &do_nothing,
                n::on_pop() = std::bind(&pop_handler<FetchableURISpecTree>, std::ref(stack),
                    ParseStackTypes<FetchableURISpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)),
                    ParseStackTypes<FetchableURISpecTree>::StarAnnotationsGoHere(std::bind(
                            &set_thing_to_star_annotate, std::ref(thing_to_star_annotate), _1)),
                    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),
                    ParseStackTypes<FetchableURISpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), s, _1, "", std::cref(eapi)),
                n::on_use() = std::bind(&use_handler<FetchableURISpecTree>, std::ref(stack), _1, env, std::cref(eapi), is_installed),
                n::on_use_under_any() = std::bind(&use_under_any_handler, s, std::cref(eapi))
            ));

    parse_elike_dependencies(s, callbacks, { });

    return top;
}

std::shared_ptr<SimpleURISpecTree>
paludis::erepository::parse_simple_uri(const std::string & s, const Environment * const env, const EAPI & eapi, const bool is_installed)
{
    using namespace std::placeholders;

    ParseStackTypes<SimpleURISpecTree>::Stack stack;
    std::shared_ptr<AllDepSpec> spec(std::make_shared<AllDepSpec>());
    std::shared_ptr<DepSpec> thing_to_annotate(spec);
    std::list<std::shared_ptr<DepSpec> > thing_to_star_annotate;
    std::shared_ptr<SimpleURISpecTree> top(std::make_shared<SimpleURISpecTree>(spec));
    stack.push_front(make_named_values<ParseStackTypes<SimpleURISpecTree>::Item>(
                n::block_children() = std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> >(),
                n::children() = std::list<std::shared_ptr<DepSpec> >(),
                n::item() = top->top(),
                n::spec() = spec
            ));

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<SimpleURISpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = std::bind(&apply_annotations, std::cref(eapi), std::ref(thing_to_annotate), std::cref(thing_to_star_annotate), _1),
                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),
                    ParseStackTypes<SimpleURISpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)),
                    ParseStackTypes<SimpleURISpecTree>::StarAnnotationsGoHere(std::bind(
                            &set_thing_to_star_annotate, std::ref(thing_to_star_annotate), _1)),
                    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),
                    ParseStackTypes<SimpleURISpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), _1),
                n::on_use() = std::bind(&use_handler<SimpleURISpecTree>, std::ref(stack), _1, env, std::cref(eapi), is_installed),
                n::on_use_under_any() = &do_nothing
            ));

    parse_elike_dependencies(s, callbacks, { });

    return top;
}

std::shared_ptr<LicenseSpecTree>
paludis::erepository::parse_license(const std::string & s, const Environment * const env, const EAPI & eapi, const bool is_installed)
{
    using namespace std::placeholders;

    ParseStackTypes<LicenseSpecTree>::Stack stack;
    std::shared_ptr<AllDepSpec> spec(std::make_shared<AllDepSpec>());
    std::shared_ptr<DepSpec> thing_to_annotate(spec);
    std::list<std::shared_ptr<DepSpec> > thing_to_star_annotate;
    std::shared_ptr<LicenseSpecTree> top(std::make_shared<LicenseSpecTree>(spec));
    stack.push_front(make_named_values<ParseStackTypes<LicenseSpecTree>::Item>(
                n::block_children() = std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> >(),
                n::children() = std::list<std::shared_ptr<DepSpec> >(),
                n::item() = top->top(),
                n::spec() = spec
            ));

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<LicenseSpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = std::bind(&apply_annotations, std::cref(eapi), std::ref(thing_to_annotate), std::cref(thing_to_star_annotate), _1),
                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),
                    ParseStackTypes<LicenseSpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)),
                    ParseStackTypes<LicenseSpecTree>::StarAnnotationsGoHere(std::bind(
                            &set_thing_to_star_annotate, std::ref(thing_to_star_annotate), _1)),
                    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),
                    ParseStackTypes<LicenseSpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), _1),
                n::on_use() = std::bind(&use_handler<LicenseSpecTree>, std::ref(stack), _1, env, std::cref(eapi), is_installed),
                n::on_use_under_any() = std::bind(&use_under_any_handler, s, std::cref(eapi))
            ));

    parse_elike_dependencies(s, callbacks, { });

    return top;
}

std::shared_ptr<PlainTextSpecTree>
paludis::erepository::parse_plain_text(const std::string & s, const Environment * const env, const EAPI & eapi, const bool is_installed)
{
    using namespace std::placeholders;

    ParseStackTypes<PlainTextSpecTree>::Stack stack;
    std::shared_ptr<AllDepSpec> spec(std::make_shared<AllDepSpec>());
    std::shared_ptr<DepSpec> thing_to_annotate(spec);
    std::list<std::shared_ptr<DepSpec> > thing_to_star_annotate;
    std::shared_ptr<PlainTextSpecTree> top(std::make_shared<PlainTextSpecTree>(spec));
    stack.push_front(make_named_values<ParseStackTypes<PlainTextSpecTree>::Item>(
                n::block_children() = std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> >(),
                n::children() = std::list<std::shared_ptr<DepSpec> >(),
                n::item() = top->top(),
                n::spec() = spec
            ));

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<PlainTextSpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = std::bind(&apply_annotations, std::cref(eapi), std::ref(thing_to_annotate), std::cref(thing_to_star_annotate), _1),
                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<PlainTextSpecTree>, std::ref(stack),
                    ParseStackTypes<PlainTextSpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)),
                    ParseStackTypes<PlainTextSpecTree>::StarAnnotationsGoHere(std::bind(
                            &set_thing_to_star_annotate, std::ref(thing_to_star_annotate), _1)),
                    s),
                n::on_should_be_empty() = std::bind(&should_be_empty_handler<PlainTextSpecTree>, std::ref(stack), s),
                n::on_string() = std::bind(&plain_text_handler<PlainTextSpecTree>, std::ref(stack),
                        ParseStackTypes<PlainTextSpecTree>::AnnotationsGoHere(std::bind(
                                &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), _1),
                n::on_use() = std::bind(&use_handler<PlainTextSpecTree>, std::ref(stack), _1, env, std::cref(eapi), is_installed),
                n::on_use_under_any() = &do_nothing
            ));

    parse_elike_dependencies(s, callbacks, { });

    return top;
}

std::shared_ptr<PlainTextSpecTree>
paludis::erepository::parse_myoptions(const std::string & s, const Environment * const env, const EAPI & eapi, const bool is_installed)
{
    using namespace std::placeholders;

    ParseStackTypes<PlainTextSpecTree>::Stack stack;
    std::shared_ptr<AllDepSpec> spec(std::make_shared<AllDepSpec>());
    std::shared_ptr<DepSpec> thing_to_annotate(spec);
    std::list<std::shared_ptr<DepSpec> > thing_to_star_annotate;
    std::shared_ptr<PlainTextSpecTree> top(std::make_shared<PlainTextSpecTree>(spec));
    stack.push_front(make_named_values<ParseStackTypes<PlainTextSpecTree>::Item>(
                n::block_children() = std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> >(),
                n::children() = std::list<std::shared_ptr<DepSpec> >(),
                n::item() = top->top(),
                n::spec() = spec
            ));

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<PlainTextSpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = std::bind(&apply_annotations, std::cref(eapi), std::ref(thing_to_annotate), std::cref(thing_to_star_annotate), _1),
                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(&plain_text_label_handler<PlainTextSpecTree>, std::ref(stack),
                        ParseStackTypes<PlainTextSpecTree>::AnnotationsGoHere(std::bind(
                                &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), _1),
                n::on_no_annotations() = &do_nothing,
                n::on_pop() = std::bind(&pop_handler<PlainTextSpecTree>, std::ref(stack),
                    ParseStackTypes<PlainTextSpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)),
                    ParseStackTypes<PlainTextSpecTree>::StarAnnotationsGoHere(std::bind(
                            &set_thing_to_star_annotate, std::ref(thing_to_star_annotate), _1)),
                    s),
                n::on_should_be_empty() = std::bind(&should_be_empty_handler<PlainTextSpecTree>, std::ref(stack), s),
                n::on_string() = std::bind(&plain_text_handler<PlainTextSpecTree>, std::ref(stack),
                        ParseStackTypes<PlainTextSpecTree>::AnnotationsGoHere(std::bind(
                                &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), _1),
                n::on_use() = std::bind(&use_handler<PlainTextSpecTree>, std::ref(stack), _1, env, std::cref(eapi), is_installed),
                n::on_use_under_any() = &do_nothing
            ));

    parse_elike_dependencies(s, callbacks, { });

    return top;
}

std::shared_ptr<RequiredUseSpecTree>
paludis::erepository::parse_required_use(const std::string & s, const Environment * const env, const EAPI & eapi, const bool is_installed)
{
    using namespace std::placeholders;

    ParseStackTypes<RequiredUseSpecTree>::Stack stack;
    std::shared_ptr<AllDepSpec> spec(std::make_shared<AllDepSpec>());
    std::shared_ptr<DepSpec> thing_to_annotate(spec);
    std::list<std::shared_ptr<DepSpec> > thing_to_star_annotate;
    std::shared_ptr<RequiredUseSpecTree> top(std::make_shared<RequiredUseSpecTree>(spec));
    stack.push_front(make_named_values<ParseStackTypes<RequiredUseSpecTree>::Item>(
                n::block_children() = std::list<std::pair<std::shared_ptr<BlockDepSpec>, BlockFixOp> >(),
                n::children() = std::list<std::shared_ptr<DepSpec> >(),
                n::item() = top->top(),
                n::spec() = spec
            ));

    ELikeDepParserCallbacks callbacks(
            make_named_values<ELikeDepParserCallbacks>(
                n::on_all() = std::bind(&any_all_handler<RequiredUseSpecTree, AllDepSpec>, std::ref(stack)),
                n::on_annotations() = std::bind(&apply_annotations, std::cref(eapi), std::ref(thing_to_annotate), std::cref(thing_to_star_annotate), _1),
                n::on_any() = std::bind(&any_all_handler<RequiredUseSpecTree, 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(&any_all_handler<RequiredUseSpecTree, ExactlyOneDepSpec>, std::ref(stack)),
                n::on_label() = std::bind(&labels_not_allowed_handler, s, _1),
                n::on_no_annotations() = &do_nothing,
                n::on_pop() = std::bind(&pop_handler<RequiredUseSpecTree>, std::ref(stack),
                    ParseStackTypes<RequiredUseSpecTree>::AnnotationsGoHere(std::bind(
                            &set_thing_to_annotate, std::ref(thing_to_annotate), _1)),
                    ParseStackTypes<RequiredUseSpecTree>::StarAnnotationsGoHere(std::bind(
                            &set_thing_to_star_annotate, std::ref(thing_to_star_annotate), _1)),
                    s),
                n::on_should_be_empty() = std::bind(&should_be_empty_handler<RequiredUseSpecTree>, std::ref(stack), s),
                n::on_string() = std::bind(&plain_text_handler<RequiredUseSpecTree>, std::ref(stack),
                        ParseStackTypes<RequiredUseSpecTree>::AnnotationsGoHere(std::bind(
                                &set_thing_to_annotate, std::ref(thing_to_annotate), _1)), _1),
                n::on_use() = std::bind(&use_handler<RequiredUseSpecTree>, std::ref(stack), _1, env, std::cref(eapi), is_installed),
                n::on_use_under_any() = &do_nothing
            ));

    parse_elike_dependencies(s, callbacks, { });

    return top;
}