]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/spirit/test/x3/fusion_map.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / test / x3 / fusion_map.cpp
CommitLineData
7c673cae
FG
1/*=============================================================================
2 Copyright (c) 2001-2015 Joel de Guzman
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7c673cae 7#include <boost/spirit/home/x3.hpp>
11fdf7f2 8#include <boost/fusion/include/at_key.hpp>
7c673cae
FG
9#include <boost/fusion/include/make_map.hpp>
10#include <boost/fusion/adapted/struct.hpp>
11
12#include <string>
13#include <iostream>
14#include "test.hpp"
15
16struct AdaptedStruct {
17 std::string key1;
18 std::string key2;
19};
20
21class key1_attr {};
22class key2_attr {};
23
24BOOST_FUSION_ADAPT_ASSOC_STRUCT(
25 AdaptedStruct,
26 (std::string, key1, class key1_attr)
27 (std::string, key2, class key2_attr)
28 )
29
30template <class Parser, class Attribute>
31bool test_attr(const std::string in,Parser const& p, Attribute& attr) {
32 auto it = in.begin();
33 return boost::spirit::x3::parse(it,in.end(), p, attr);
34}
35
7c673cae
FG
36int
37main()
38{
39 using spirit_test::test;
40 using boost::spirit::x3::lit;
41 using boost::spirit::x3::attr;
42 using boost::spirit::x3::char_;
43 using boost::spirit::x3::eps;
44 namespace fusion = boost::fusion;
45
46 { // parsing sequence directly into fusion map
47
48 auto const key1 = lit("key1") >> attr(key1_attr());
49 auto const kv1 = key1 >> lit("=") >> +char_;
50
51 {
52 auto attr_ = fusion::make_map<key1_attr>(std::string());
53 BOOST_TEST(test_attr("key1=ABC", kv1, attr_));
54 BOOST_TEST(fusion::at_key<key1_attr>(attr_) == "ABC");
55 }
56 {
57 AdaptedStruct attr_;
58 BOOST_TEST(test_attr("key1=ABC", kv1, attr_));
59 BOOST_TEST(attr_.key1 == "ABC");
60 BOOST_TEST(attr_.key2 == "");
61 }
62 }
63 { // case when parser handling fusion assoc sequence
64 // is on one side of another sequence
65 auto const key1 = lit("key1") >> attr(key1_attr());
66 auto const kv1 = key1 >> lit("=") >> +~char_(';');
67
68 AdaptedStruct attr_;
69 BOOST_TEST(test_attr("key1=ABC", eps >> (kv1 % ';') , attr_));
70 BOOST_TEST(attr_.key1 == "ABC");
71 BOOST_TEST(attr_.key2 == "");
72 }
73 { // parsing repeated sequence directly into fusion map (overwrite)
74 auto const key1 = lit("key1") >> attr(key1_attr());
75 auto const kv1 = key1 >> lit("=") >> +~char_(';');
76
77 {
78 auto attr_ = fusion::make_map<key1_attr>(std::string());
79 BOOST_TEST(test_attr("key1=ABC;key1=XYZ", kv1 % ';', attr_));
80 BOOST_TEST(fusion::at_key<key1_attr>(attr_) == "XYZ");
81 }
82 {
83 AdaptedStruct attr_;
84 BOOST_TEST(test_attr("key1=ABC;key1=XYZ", kv1 % ';', attr_));
85 BOOST_TEST(attr_.key1 == "XYZ");
86 }
87 }
88
89 { // parsing repeated sequence directly into fusion map (append)
90
91 /* NOT IMPLEMENTED
92 auto const key1 = lit("key1") >> attr(key1_attr());
93 auto const kv1 = key1 >> lit("=") >> +char_;
94 auto attr_ = fusion::make_map<key1_attr>(std::vector<std::string>());
95
96 BOOST_TEST(test_attr("key1=ABC;key1=XYZ", kv1 % ";", attr_));
97 BOOST_TEST(fusion::at_key<key1_attr>(attr_) == {"ABC","XYZ"});
98 */
99 }
100
101 { // alternative over key-value pairs
102
103 auto const key1 = lit("key1") >> attr(key1_attr());
104 auto const key2 = lit("key2") >> attr(key2_attr());
105 auto const kv1 = key1 >> lit("=") >> +~char_(';');
106 auto const kv2 = key2 >> lit("=") >> +~char_(';');
107
108 auto attr_ = fusion::make_map<key1_attr, key2_attr>(std::string(),std::string());
109 BOOST_TEST(test_attr("key2=XYZ;key1=ABC", (kv1|kv2) % ';', attr_));
110 BOOST_TEST(fusion::at_key<key1_attr>(attr_) == "ABC");
111 BOOST_TEST(fusion::at_key<key2_attr>(attr_) == "XYZ");
112 }
113
114 { // parsing sequence where key is a variant
115
116 namespace x3 = boost::spirit::x3;
117 auto key1 = lit("key1") >> attr(key1_attr());
118 auto key2 = lit("key2") >> attr(key2_attr());
119 auto keys = key1 | key2;
120 auto pair = keys >> lit("=") >> +~char_(';');
121
122 {
123 auto attr_ = fusion::make_map<key1_attr,key2_attr>(std::string(),std::string());
124 BOOST_TEST(test_attr("key1=ABC;key2=XYZ", pair % ';', attr_));
125 BOOST_TEST(fusion::at_key<key1_attr>(attr_) == "ABC");
126 BOOST_TEST(fusion::at_key<key2_attr>(attr_) == "XYZ");
127 }
128 {
129 AdaptedStruct attr_;
130 BOOST_TEST(test_attr("key1=ABC;key2=XYZ", pair % ';', attr_));
131 BOOST_TEST(fusion::at_key<key1_attr>(attr_) == "ABC");
132 BOOST_TEST(fusion::at_key<key2_attr>(attr_) == "XYZ");
133 }
134 }
135
136 return boost::report_errors();
137}