liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
/*=============================================================================
    Copyright (c) 2001-2011 Joel de Guzman
    Copyright (c) 2001-2011 Hartmut Kaiser
    Copyright (c) 2010-2011 Bryce Lelbach
 
    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(BOOST_SPIRIT_OUTPUT_UTREE_TRAITS_APR_16_2010_0655AM)
#define BOOST_SPIRIT_OUTPUT_UTREE_TRAITS_APR_16_2010_0655AM
 
#include <boost/spirit/home/support/attributes.hpp>
#include <boost/spirit/home/support/container.hpp>
#include <boost/spirit/home/support/utree.hpp>
#include <boost/spirit/home/qi/domain.hpp>
#include <boost/spirit/home/karma/domain.hpp>
#include <boost/spirit/home/qi/nonterminal/nonterminal_fwd.hpp>
#include <boost/spirit/home/karma/nonterminal/nonterminal_fwd.hpp>
 
#include <string>
 
#include <boost/cstdint.hpp>
#include <boost/variant.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/or.hpp>
#include <boost/range/iterator_range_core.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/utility/enable_if.hpp>
 
///////////////////////////////////////////////////////////////////////////////
namespace boost
{
    template <typename T>
    inline T get(boost::spirit::utree const& x)
    {
        return x.get<T>();
    }
}
 
///////////////////////////////////////////////////////////////////////////////
namespace boost { namespace spirit { namespace traits
{
    namespace detail
    {
        inline bool is_list(utree const& ut)
        {
            switch (traits::which(ut))
            {
            case utree_type::reference_type:
                return is_list(ut.deref());
 
            case utree_type::list_type:
            case utree_type::range_type:
                return true;
 
            default:
                break;
            }
            return false;
        }
 
        inline bool is_uninitialized(utree const& ut)
        {
            return traits::which(ut) == utree_type::invalid_type;
        }
    }
 
    // this specialization tells Spirit how to extract the type of the value
    // stored in the given utree node
    template <>
    struct variant_which<utree>
    {
        static int call(utree const& u) { return u.which(); }
    };
    
    template <>
    struct variant_which<utree::list_type>
    {
        static int call(utree::list_type const& u) { return u.which(); }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    // Make sure all components of an alternative expose utree, even if they
    // actually expose a utree::list_type
    template <typename Domain> 
    struct alternative_attribute_transform<utree::list_type, Domain>
      : mpl::identity<utree>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // Make sure all components of a sequence expose utree, even if they
    // actually expose a utree::list_type
    template <typename Domain> 
    struct sequence_attribute_transform<utree::list_type, Domain>
      : mpl::identity<utree>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // this specialization lets Spirit know that typed basic_strings
    // are strings
    template <typename Base, utree_type::info I>
    struct is_string<spirit::basic_string<Base, I> > 
      : mpl::true_ 
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // these specializations extract the character type of a utree typed string 
    template <typename T, utree_type::info I>
    struct char_type_of<spirit::basic_string<iterator_range<T>, I> >
      : char_type_of<T> 
    {};
 
    template <utree_type::info I>
    struct char_type_of<spirit::basic_string<std::string, I> > 
      : mpl::identity<char>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // these specializations extract a c string from a utree typed string
    template <typename String>
    struct extract_c_string;
 
    template <typename T, utree_type::info I>
    struct extract_c_string<
        spirit::basic_string<iterator_range<T const*>, I>
    > {
        typedef T char_type;
 
        typedef spirit::basic_string<iterator_range<T const*>, I> string;
 
        static T const* call (string& s)
        {
            return s.begin();
        }
 
        static T const* call (string const& s)
        {
            return s.begin();
        }
    };
    
    template <utree_type::info I>
    struct extract_c_string<spirit::basic_string<std::string, I> >
    {
        typedef char char_type;
 
        typedef spirit::basic_string<std::string, I> string;
 
        static char const* call (string& s)
        {
            return s.c_str();
        }
 
        static char const* call (string const& s)
        {
            return s.c_str();
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    // these specializations are needed because utree::value_type == utree
    template <> 
    struct is_substitute<utree, utree> 
      : mpl::true_ 
    {};
 
    template <> 
    struct is_weak_substitute<utree, utree> 
      : mpl::true_ 
    {};
 
    template <> 
    struct is_substitute<utree::list_type, utree::list_type> 
      : mpl::true_ 
    {};
 
    template <> 
    struct is_weak_substitute<utree::list_type, utree::list_type> 
      : mpl::true_ 
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // this specialization tells Spirit.Qi to allow assignment to an utree from
    // a variant
    namespace detail
    {
        struct assign_to_utree_visitor : static_visitor<>
        {
            assign_to_utree_visitor(utree& ut) : ut_(ut) {}
 
            template <typename T>
            void operator()(T& val) const
            {
                ut_ = val;
            }
 
            utree& ut_;
        };
    }
 
    template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
    struct assign_to_container_from_value<
        utree, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
    {
        static void
        call(variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& val, utree& attr)
        {
            apply_visitor(detail::assign_to_utree_visitor(attr), val);
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    // this specialization tells Spirit.Qi to allow assignment to an utree from
    // a STL container
    template <typename Attribute>
    struct assign_to_container_from_value<utree, Attribute>
    {
        // any non-container type will be either directly assigned or appended
        static void call(Attribute const& val, utree& attr, mpl::false_)
        {
            if (attr.empty())
                attr = val;
            else
                push_back(attr, val);
        }
 
        // any container type will be converted into a list_type utree
        static void call(Attribute const& val, utree& attr, mpl::true_)
        {
            typedef typename traits::container_iterator<Attribute const>::type 
                iterator_type;
 
            // make sure the attribute is a list, at least an empty one
            if (attr.empty())
                attr = empty_list;
 
            iterator_type end = traits::end(val);
            for (iterator_type i = traits::begin(val); i != end; traits::next(i))
                push_back(attr, traits::deref(i));
        }
 
        static void call(Attribute const& val, utree& attr)
        {
            call(val, attr, is_container<Attribute>());
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    // this specialization is required to disambiguate the specializations
    // related to utree
    template <>
    struct assign_to_container_from_value<utree, utree>
    {
        static void call(utree const& val, utree& attr)
        {
            if (attr.empty()) {
                attr = val;
            }
            else if (detail::is_list(val)) {
                typedef utree::const_iterator iterator_type;
 
                iterator_type end = traits::end(val);
                for (iterator_type i = traits::begin(val); i != end; traits::next(i))
                    push_back(attr, traits::deref(i));
            }
            else {
                push_back(attr, val);
            }
        }
    };
 
    template <>
    struct assign_to_container_from_value<utree, utree::list_type>
      : assign_to_container_from_value<utree, utree>
    {};
 
    // If the destination is a utree_list, we need to force the right hand side
    // value into a new sub-node, always, no questions asked.
    template <>
    struct assign_to_container_from_value<utree::list_type, utree>
    {
        static void call(utree const& val, utree& attr)
        {
            push_back(attr, val);
        }
    };
 
    // If both, the right hand side and the left hand side are utree_lists
    // we have a lhs rule which has a single rule exposing a utree_list as its
    // rhs (optionally wrapped into a directive or other unary parser). In this
    // case we do not create a new sub-node.
    template <>
    struct assign_to_container_from_value<utree::list_type, utree::list_type>
      : assign_to_container_from_value<utree, utree>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // this specialization makes sure strings get assigned as a whole and are 
    // not converted into a utree list
    template <>
    struct assign_to_container_from_value<utree, utf8_string_type>
    {
        static void call(utf8_string_type const& val, utree& attr)
        {
            if (attr.empty())
                attr = val;
            else
                push_back(attr, val);
        }
    };
 
    // this specialization keeps symbols from being transformed into strings  
    template<>
    struct assign_to_container_from_value<utree, utf8_symbol_type> 
    {
        static void call (utf8_symbol_type const& val, utree& attr) 
        {
            if (attr.empty())
                attr = val;
            else
                push_back(attr, val);
        }
    };
    
    template <>
    struct assign_to_container_from_value<utree, binary_string_type>
    {
        static void call(binary_string_type const& val, utree& attr)
        {
            if (attr.empty())
                attr = val;
            else
                push_back(attr, val);
        }
    };
 
    template<>
    struct assign_to_container_from_value<utree, utf8_symbol_range_type> 
    {
        static void call (utf8_symbol_range_type const& val, utree& attr) 
        {
            if (attr.empty())
                attr = val;
            else
                push_back(attr, val);
        }
    };
    
    template <>
    struct assign_to_container_from_value<utree, binary_range_type>
    {
        static void call(binary_range_type const& val, utree& attr)
        {
            if (attr.empty())
                attr = val;
            else
                push_back(attr, val);
        }
    };
 
    template <>
    struct assign_to_container_from_value<utree, std::string>
    {
        static void call(std::string const& val, utree& attr)
        {
            if (attr.empty())
                attr = val;
            else
                push_back(attr, val);
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    // this specialization tells Spirit.Qi to allow assignment to an utree from
    // generic iterators
    template <typename Iterator>
    struct assign_to_attribute_from_iterators<utree, Iterator>
    {
        static void
        call(Iterator const& first, Iterator const& last, utree& attr)
        {
            if (attr.empty())
                attr.assign(first, last);
            else {
                for (Iterator i = first; i != last; ++i)
                    push_back(attr, traits::deref(i));
            }
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    // Karma only: convert utree node to string
    namespace detail
    {
        struct attribute_as_string_type
        {
            typedef utf8_string_range_type type; 
 
            static type call(utree const& attr)
            {
                return boost::get<utf8_string_range_type>(attr);
            }
 
            static bool is_valid(utree const& attr)
            {
                switch (traits::which(attr))
                {
                case utree_type::reference_type:
                    return is_valid(attr.deref());
 
                case utree_type::string_range_type:
                case utree_type::string_type:
                    return true;
 
                default:
                    return false;
                }
            }
        };
    }
 
    template <>
    struct attribute_as<std::string, utree>
      : detail::attribute_as_string_type 
    {};
 
    template <>
    struct attribute_as<utf8_string_type, utree>
      : detail::attribute_as_string_type 
    {};
 
    template <>
    struct attribute_as<utf8_string_range_type, utree>
      : detail::attribute_as_string_type 
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    namespace detail
    {
        struct attribute_as_symbol_type
        {
            typedef utf8_symbol_range_type type; 
 
            static type call(utree const& attr)
            {
                return boost::get<utf8_symbol_range_type>(attr);
            }
 
            static bool is_valid(utree const& attr)
            {
                switch (traits::which(attr))
                {
                case utree_type::reference_type:
                    return is_valid(attr.deref());
 
                case utree_type::symbol_type:
                    return true;
 
                default:
                    return false;
                }
            }
        };
    }
 
    template <>
    struct attribute_as<utf8_symbol_type, utree>
      : detail::attribute_as_symbol_type 
    {};
 
    template <>
    struct attribute_as<utf8_symbol_range_type, utree>
      : detail::attribute_as_symbol_type 
    {};
    
    template <typename Attribute>
    struct attribute_as<Attribute, utree::list_type>
      : attribute_as<Attribute, utree>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    namespace detail
    {
        struct attribute_as_binary_string_type
        {
            typedef binary_range_type type; 
 
            static type call(utree const& attr)
            {
                return boost::get<binary_range_type>(attr);
            }
 
            static bool is_valid(utree const& attr)
            {
                switch (traits::which(attr))
                {
                case utree_type::reference_type:
                    return is_valid(attr.deref());
 
                case utree_type::binary_type:
                    return true;
 
                default:
                    return false;
                }
            }
        };
    }
 
    template <>
    struct attribute_as<binary_string_type, utree>
      : detail::attribute_as_binary_string_type 
    {};
 
    template <>
    struct attribute_as<binary_range_type, utree>
      : detail::attribute_as_binary_string_type 
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // push_back support for utree 
    template <typename T>
    struct push_back_container<utree, T>
    {
        static bool call(utree& c, T const& val)
        {
            switch (traits::which(c))
            {
                case utree_type::invalid_type:
                case utree_type::nil_type:
                case utree_type::list_type:
                    c.push_back(val);
                    break;
 
                default:
                {
                    utree ut;
                    ut.push_back(c);
                    ut.push_back(val);
                    c.swap(ut);
                }
                break;
            }
            return true;
        }
    };
 
    template <typename T>
    struct push_back_container<utree::list_type, T>
      : push_back_container<utree, T>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // ensure the utree attribute is an empty list
    template <>
    struct make_container_attribute<utree>
    {
        static void call(utree& ut)
        {
            if (!detail::is_list(ut)) {
                if (detail::is_uninitialized(ut))
                    ut = empty_list;
                else {
                    utree retval (empty_list);
                    retval.push_back(ut);
                    ut.swap(retval);
                }
            }
        }
    };
 
    template <>
    struct make_container_attribute<utree::list_type>
      : make_container_attribute<utree>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // an utree is a container on its own
    template <>
    struct build_std_vector<utree>
    {
        typedef utree type;
    };
 
    template <>
    struct build_std_vector<utree::list_type>
    {
        typedef utree::list_type type;
    };
 
    ///////////////////////////////////////////////////////////////////////////
    // debug support for utree
    template <typename Out>
    struct print_attribute_debug<Out, utree>
    {
        static void call(Out& out, utree const& val)
        {
            out << val;
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    // force utree list attribute in a sequence to be dereferenced if a rule
    // or a grammar exposes an utree as it's attribute
    namespace detail
    {
        // Checks whether the exposed Attribute allows to handle utree or 
        // utree::list_type directly. Returning mpl::false_ from this meta 
        // function will force a new utree instance to be created for each
        // invocation of the embedded parser.
 
        // The purpose of using utree::list_type as an attribute is to force a 
        // new sub-node in the result.
        template <typename Attribute, typename Enable = void>
        struct handles_utree_list_container 
          : mpl::and_<
                mpl::not_<is_same<utree::list_type, Attribute> >,
                traits::is_container<Attribute> >
        {};
 
        // The following specializations make sure that the actual handling of
        // an utree (or utree::list_type) attribute is deferred to the embedded
        // parsers of a sequence, alternative or optional component.
        template <typename Attribute>
        struct handles_utree_list_container<Attribute
              , typename enable_if<fusion::traits::is_sequence<Attribute> >::type>
          : mpl::true_
        {};
 
        template <typename Attribute>
        struct handles_utree_list_container<boost::optional<Attribute> >
          : mpl::true_
        {};
 
        template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
        struct handles_utree_list_container<
                boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
          : mpl::true_
        {};
    }
 
    template <
        typename IteratorA, typename IteratorB, typename Context
      , typename T1, typename T2, typename T3, typename T4>
    struct handles_container<qi::rule<IteratorA, T1, T2, T3, T4>
          , utree, Context, IteratorB>
      : detail::handles_utree_list_container<typename attribute_of<
            qi::rule<IteratorA, T1, T2, T3, T4>, Context, IteratorB
        >::type>
    {};
 
    template <
        typename IteratorA, typename IteratorB, typename Context
      , typename T1, typename T2, typename T3, typename T4>
    struct handles_container<qi::grammar<IteratorA, T1, T2, T3, T4>
          , utree, Context, IteratorB>
      : detail::handles_utree_list_container<typename attribute_of<
            qi::grammar<IteratorA, T1, T2, T3, T4>, Context, IteratorB
        >::type>
    {};
 
    template <
        typename IteratorA, typename IteratorB, typename Context
      , typename T1, typename T2, typename T3, typename T4>
    struct handles_container<qi::rule<IteratorA, T1, T2, T3, T4>
          , utree::list_type, Context, IteratorB>
      : detail::handles_utree_list_container<typename attribute_of<
            qi::rule<IteratorA, T1, T2, T3, T4>, Context, IteratorB
        >::type>
    {};
 
    template <
        typename IteratorA, typename IteratorB, typename Context
      , typename T1, typename T2, typename T3, typename T4>
    struct handles_container<qi::grammar<IteratorA, T1, T2, T3, T4>
          , utree::list_type, Context, IteratorB>
      : detail::handles_utree_list_container<typename attribute_of<
            qi::grammar<IteratorA, T1, T2, T3, T4>, Context, IteratorB
        >::type>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    template <typename Attribute, typename Sequence>
    struct pass_through_container<
            utree, utree, Attribute, Sequence, qi::domain>
      : detail::handles_utree_list_container<Attribute>
    {};
 
    template <typename Attribute, typename Sequence>
    struct pass_through_container<
            utree::list_type, utree, Attribute, Sequence, qi::domain>
      : detail::handles_utree_list_container<Attribute>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    namespace detail
    {
        // Checks whether the exposed Attribute allows to handle utree or 
        // utree::list_type directly. Returning mpl::false_ from this meta 
        // function will force a new utree instance to be created for each
        // invocation of the embedded parser.
 
        // The purpose of using utree::list_type as an attribute is to force a 
        // new sub-node in the result.
        template <typename Attribute, typename Enable = void>
        struct handles_utree_container 
          : mpl::and_<
                mpl::not_<is_same<utree, Attribute> >,
                traits::is_container<Attribute> >
        {};
 
        // The following specializations make sure that the actual handling of
        // an utree (or utree::list_type) attribute is deferred to the embedded
        // parsers of a sequence, alternative or optional component.
        template <typename Attribute>
        struct handles_utree_container<Attribute
              , typename enable_if<fusion::traits::is_sequence<Attribute> >::type>
          : mpl::true_
        {};
 
        template <typename Attribute>
        struct handles_utree_container<boost::optional<Attribute> >
          : mpl::true_
        {};
 
        template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
        struct handles_utree_container<
                boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
          : mpl::true_
        {};
    }
 
    template <
        typename IteratorA, typename IteratorB, typename Context
      , typename T1, typename T2, typename T3, typename T4>
    struct handles_container<karma::rule<IteratorA, T1, T2, T3, T4>
          , utree, Context, IteratorB>
      : detail::handles_utree_container<typename attribute_of<
            karma::rule<IteratorA, T1, T2, T3, T4>, Context, IteratorB
        >::type>
    {};
 
    template <
        typename IteratorA, typename IteratorB, typename Context
      , typename T1, typename T2, typename T3, typename T4>
    struct handles_container<karma::grammar<IteratorA, T1, T2, T3, T4>
          , utree, Context, IteratorB>
      : detail::handles_utree_container<typename attribute_of<
            karma::grammar<IteratorA, T1, T2, T3, T4>, Context, IteratorB
        >::type>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    template <typename Attribute, typename Sequence>
    struct pass_through_container<
            utree, utree, Attribute, Sequence, karma::domain>
      : detail::handles_utree_container<Attribute>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // the specialization below tells Spirit how to handle utree if it is used
    // with an optional component
    template <>
    struct optional_attribute<utree>
    {
        typedef utree const& type;
 
        static type call(utree const& val)
        {
            return val;
        }
 
        // only 'invalid_type' utree nodes are not valid
        static bool is_valid(utree const& val)
        {
          return !detail::is_uninitialized(val);
        }
    };
 
    template <>
    struct build_optional<utree>
    {
        typedef utree type;
    };
 
    template <>
    struct build_optional<utree::list_type>
    {
        typedef utree::list_type type;
    };
 
    // an utree is an optional (in any domain)
    template <>
    struct not_is_optional<utree, qi::domain>
      : mpl::false_
    {};
 
    template <>
    struct not_is_optional<utree::list_type, qi::domain>
      : mpl::false_
    {};
 
    template <>
    struct not_is_optional<utree, karma::domain>
      : mpl::false_
    {};
 
    template <>
    struct not_is_optional<utree::list_type, karma::domain>
      : mpl::false_
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    // the specialization below tells Spirit to handle utree as if it
    // where a 'real' variant (in the context of karma)
    template <>
    struct not_is_variant<utree, karma::domain>
      : mpl::false_ 
    {};
 
    template <>
    struct not_is_variant<utree::list_type, karma::domain>
      : mpl::false_ 
    {};
 
    // The specializations below tell Spirit to verify whether an attribute
    // type is compatible with a given variant type
    template <>
    struct compute_compatible_component_variant<
            utree, iterator_range<utree::iterator> >
      : mpl::true_
    {
        typedef iterator_range<utree::iterator> compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::list_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<
            utree, iterator_range<utree::const_iterator> >
      : mpl::true_
    {
        typedef iterator_range<utree::const_iterator> compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::list_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<utree, utree::invalid_type>
      : mpl::true_
    {
        typedef utree::invalid_type compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::invalid_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<utree, utree::nil_type>
      : mpl::true_
    {
        typedef utree::nil_type compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::nil_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<utree, bool>
      : mpl::true_
    {
        typedef bool compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::bool_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<utree, int>
      : mpl::true_
    {
        typedef int compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::int_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<utree, double>
      : mpl::true_
    {
        typedef double compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::double_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<
            utree, utf8_string_range_type>
      : mpl::true_
    {
        typedef utf8_string_range_type compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::string_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<
            utree, utf8_string_type>
      : mpl::true_
    {
        typedef utf8_string_type compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::string_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<
            utree, utf8_symbol_range_type>
      : mpl::true_
    {
        typedef utf8_symbol_range_type compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::symbol_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<
            utree, utf8_symbol_type>
      : mpl::true_
    {
        typedef utf8_symbol_type compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::symbol_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<
            utree, binary_range_type>
      : mpl::true_
    {
        typedef binary_range_type compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::binary_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<
            utree, binary_string_type>
      : mpl::true_
    {
        typedef binary_string_type compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::binary_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<utree, utree>
      : mpl::true_
    {
        typedef utree compatible_type;
 
        static bool is_compatible(int d)
        {
            return d >= utree_type::invalid_type &&
                   d <= utree_type::reference_type;
        }
    };
 
    template <>
    struct compute_compatible_component_variant<
            utree, std::vector<utree> >
      : mpl::true_
    {
        typedef utree compatible_type;
 
        static bool is_compatible(int d)
        {
            return d >= utree_type::invalid_type &&
                   d <= utree_type::reference_type;
        }
    };
 
    template <typename Sequence>
    struct compute_compatible_component_variant<utree, Sequence
          , mpl::false_
          , typename enable_if<fusion::traits::is_sequence<Sequence> >::type>
      : mpl::true_
    {
        typedef iterator_range<utree::const_iterator> compatible_type;
 
        static bool is_compatible(int d)
        {
            return d == utree_type::list_type;
        }
    };
 
    template <typename Attribute>
    struct compute_compatible_component_variant<utree::list_type, Attribute>
      : compute_compatible_component_variant<utree, Attribute>
    {};
 
    ///////////////////////////////////////////////////////////////////////////
    template <>
    struct symbols_lookup<utree, utf8_symbol_type>
    {
        typedef std::string type;
 
        static type call(utree const& t)
        {
            utf8_symbol_range_type r = boost::get<utf8_symbol_range_type>(t);
            return std::string(traits::begin(r), traits::end(r));
        }
    };
 
    template <>
    struct symbols_lookup<utf8_symbol_type, utf8_symbol_type>
    {
        typedef std::string type;
 
        static type call(utf8_symbol_type const& t)
        {
            return t;
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    namespace detail
    {
        template <typename T>
        inline T get_or_deref(utree const& t)
        {
            if (detail::is_list(t))
                return boost::get<T>(t.front());
            return boost::get<T>(t);
        }
    }
 
    template <>
    struct extract_from_container<utree, utree::nil_type>
    {
        typedef utree::nil_type type;
 
        template <typename Context>
        static type call(utree const&, Context&)
        {
            return nil;
        }
    };
 
    template <>
    struct extract_from_container<utree, char>
    {
        typedef char type;
 
        template <typename Context>
        static type call(utree const& t, Context&)
        {
            utf8_symbol_range_type r = detail::get_or_deref<utf8_symbol_range_type>(t);
            return r.front();
        }
    };
 
    template <>
    struct extract_from_container<utree, bool>
    {
        typedef bool type;
 
        template <typename Context>
        static type call(utree const& t, Context&)
        {
            return detail::get_or_deref<bool>(t);
        }
    };
 
    template <>
    struct extract_from_container<utree, int>
    {
        typedef int type;
 
        template <typename Context>
        static type call(utree const& t, Context&)
        {
            return detail::get_or_deref<int>(t);
        }
    };
 
    template <>
    struct extract_from_container<utree, double>
    {
        typedef double type;
 
        template <typename Context>
        static type call(utree const& t, Context&)
        {
            return detail::get_or_deref<double>(t);
        }
    };
 
    template <typename Traits, typename Alloc>
    struct extract_from_container<utree, std::basic_string<char, Traits, Alloc> >
    {
        typedef std::basic_string<char, Traits, Alloc> type;
 
        template <typename Context>
        static type call(utree const& t, Context&)
        {
            utf8_string_range_type r = detail::get_or_deref<utf8_string_range_type>(t);
            return type(traits::begin(r), traits::end(r));
        }
    };
 
    template <>
    struct extract_from_container<utree, utf8_symbol_type>
    {
        typedef utf8_symbol_type type;
 
        template <typename Context>
        static type call(utree const& t, Context&)
        {
            utf8_symbol_range_type r = detail::get_or_deref<utf8_symbol_range_type>(t);
            return type(traits::begin(r), traits::end(r));
        }
    };
 
    template <>
    struct extract_from_container<utree, utf8_string_type>
    {
        typedef utf8_string_type type;
 
        template <typename Context>
        static type call(utree const& t, Context&)
        {
            utf8_string_range_type r = detail::get_or_deref<utf8_string_range_type>(t);
            return type(traits::begin(r), traits::end(r));
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    template <>
    struct transform_attribute<utree const, utree::nil_type, karma::domain>
    {
        typedef utree::nil_type type;
 
        static type pre(utree const&)
        {
            return nil;
        }
    };
 
    template <>
    struct transform_attribute<utree const, char, karma::domain>
    {
        typedef char type;
 
        static type pre(utree const& t)
        {
            utf8_string_range_type r = detail::get_or_deref<utf8_string_range_type>(t);
            return r.front();
        }
    };
 
    template <>
    struct transform_attribute<utree const, bool, karma::domain>
    {
        typedef bool type;
 
        static type pre(utree const& t)
        {
            return detail::get_or_deref<bool>(t);
        }
    };
 
    template <>
    struct transform_attribute<utree const, int, karma::domain>
    {
        typedef int type;
 
        static type pre(utree const& t)
        {
            return detail::get_or_deref<int>(t);
        }
    };
 
    template <>
    struct transform_attribute<utree const, double, karma::domain>
    {
        typedef double type;
 
        static type pre(utree const& t)
        {
            return detail::get_or_deref<double>(t);
        }
    };
 
    template <typename Traits, typename Alloc>
    struct transform_attribute<
        utree const, std::basic_string<char, Traits, Alloc>, karma::domain>
    {
        typedef std::basic_string<char, Traits, Alloc> type;
 
        static type pre(utree const& t)
        {
            utf8_string_range_type r = detail::get_or_deref<utf8_string_range_type>(t);
            return type(traits::begin(r), traits::end(r));
        }
    };
 
    // this specialization is used whenever a utree is passed to a rule as part
    // of a sequence
    template <typename Iterator>
    struct transform_attribute<
        iterator_range<Iterator> const, utree, karma::domain>
    {
        typedef utree type;
 
        static type pre(iterator_range<Iterator> const& t)
        {
            // return utree the begin iterator points to
            Iterator it = t.begin();
            utree result(boost::ref(*it));
            ++it;
            return result;
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    template <>
    struct transform_attribute<utree const, utf8_string_type, karma::domain>
    {
        typedef utf8_string_type type;
 
        static type pre(utree const& t)
        {
            utf8_string_range_type r = detail::get_or_deref<utf8_string_range_type>(t);
            return type(traits::begin(r), traits::end(r));
        }
    };
 
    template <>
    struct transform_attribute<utree const, utf8_symbol_type, karma::domain>
    {
        typedef utf8_symbol_type type;
 
        static type pre(utree const& t)
        {
            utf8_symbol_range_type r = detail::get_or_deref<utf8_symbol_range_type>(t);
            return type(traits::begin(r), traits::end(r));
        }
    };
 
    template <typename Attribute>
    struct transform_attribute<utree::list_type const, Attribute, karma::domain>
      : transform_attribute<utree const, Attribute, karma::domain>
    {};
}}}
 
#endif