00001
00002
00003
00004
00005
00006
00007
00008
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <string>
00019 #include <iostream>
00020 #include <cassert>
00021 #include <vector>
00022
00023 #include <boost/spirit/include/classic_core.hpp>
00024 #include <boost/spirit/include/classic_confix.hpp>
00025 #include <boost/spirit/include/classic_lists.hpp>
00026 #include <boost/spirit/include/classic_escape_char.hpp>
00027
00029 using namespace std;
00030 using namespace BOOST_SPIRIT_CLASSIC_NS;
00031
00033
00034 class list_actor
00035 {
00036 public:
00037 list_actor (std::vector<std::string> &vec_) : vec(vec_) {}
00038
00039
00040
00041
00042 template <typename ActionIterT>
00043 void operator() (ActionIterT const &first, ActionIterT const &last) const
00044 {
00045 vec.push_back(std::string(first, last-first));
00046 }
00047
00048 private:
00049 std::vector<std::string> &vec;
00050 };
00051
00053
00054 int main ()
00055 {
00056
00057 char const* plist_wo_item = "element1,element2,element3";
00058 rule<> list_wo_item;
00059 std::vector<std::string> vec_list;
00060
00061 list_wo_item =
00062 list_p[push_back_a(vec_list)]
00063 ;
00064
00065 parse_info<> result = parse (plist_wo_item, list_wo_item);
00066
00067 cout << "-----------------------------------------------------------------"
00068 << endl;
00069
00070 if (result.hit)
00071 {
00072 cout
00073 << "Parsing simple list" << endl
00074 << "\t" << plist_wo_item << endl
00075 << "Parsed successfully!" << endl << endl;
00076
00077 cout
00078 << "Actor was called " << (int)vec_list.size()
00079 << " times: " << endl;
00080
00081 cout
00082 << "Results got from the list parser:" << endl;
00083 for (std::vector<std::string>::iterator it = vec_list.begin();
00084 it != vec_list.end(); ++it)
00085 {
00086 cout << *it << endl;
00087 }
00088 }
00089 else
00090 {
00091 cout << "Failed to parse simple list!" << endl;
00092 }
00093
00094 cout << endl;
00095
00096
00097
00098 char const *plist_csv = "\"string\",\"string with an embedded \\\"\","
00099 "12345,0.12345e4,,2";
00100 rule<> list_csv, list_csv_item;
00101 std::vector<std::string> vec_item;
00102
00103 vec_list.clear();
00104
00105 list_csv_item =
00106 !(
00107 confix_p('\"', *c_escape_ch_p, '\"')
00108 | longest_d[real_p | int_p]
00109 );
00110
00111 list_csv =
00112 list_p(
00113 list_csv_item[push_back_a(vec_item)],
00114 ','
00115 )[push_back_a(vec_list)]
00116 ;
00117
00118 result = parse (plist_csv, list_csv);
00119
00120 cout << "-----------------------------------------------------------------"
00121 << endl;
00122 if (result.hit)
00123 {
00124 cout
00125 << "Parsing CSV list (comma separated values) " << endl
00126 << "\t" << plist_csv << endl
00127 << "Parsed successfully!" << endl << endl;
00128
00129 if (result.full)
00130 {
00131 cout << "Matched " << (int)vec_list.size() <<
00132 " list elements (full list): " << endl;
00133 }
00134 else
00135 {
00136 cout << "Matched " << (int)vec_list.size() <<
00137 " list elements: " << endl;
00138 }
00139
00140 cout << "The list parser matched:" << endl;
00141 for (std::vector<std::string>::iterator itl = vec_list.begin();
00142 itl != vec_list.end(); ++itl)
00143 {
00144 cout << *itl << endl;
00145 }
00146
00147 cout << endl << "Item(s) got directly from the item parser:" << endl;
00148 for (std::vector<std::string>::iterator it = vec_item.begin();
00149 it != vec_item.end(); ++it)
00150 {
00151 cout << *it << endl;
00152 }
00153
00154 }
00155 else
00156 {
00157 cout << "Failed to parse CSV list!" << endl;
00158 }
00159
00160 cout << endl;
00161
00162
00163
00164
00165 char const *plist_csv_direct = "\"string\"<par>\"string with an embedded "
00166 "\\\"\"<par>12345<par>0.12345e4";
00167 rule<> list_csv_direct, list_csv_direct_item;
00168
00169 vec_list.clear();
00170 vec_item.clear();
00171
00172
00173
00174
00175 list_csv_direct_item =
00176 confix_p('\"', *c_escape_ch_p, '\"')
00177 | longest_d[real_p | int_p]
00178 ;
00179
00180 list_csv_direct =
00181 list_p.direct(
00182 (*list_csv_direct_item)[list_actor(vec_item)],
00183 "<par>"
00184 )[list_actor(vec_list)]
00185 ;
00186
00187 result = parse (plist_csv_direct, list_csv_direct);
00188
00189 cout << "-----------------------------------------------------------------"
00190 << endl;
00191 if (result.hit)
00192 {
00193 cout
00194 << "Parsing CSV list (comma separated values)" << endl
00195 << "The list parser was generated with 'list_p.direct()'" << endl
00196 << "\t" << plist_csv_direct << endl
00197 << "Parsed successfully!" << endl << endl;
00198
00199 if (result.full)
00200 {
00201 cout << "Matched " << vec_list.size() <<
00202 " list elements (full list): " << endl;
00203 }
00204 else
00205 {
00206 cout << "Matched " << vec_list.size() <<
00207 " list elements: " << endl;
00208 }
00209
00210 cout << "The list parser matched:" << endl;
00211 for (std::vector<std::string>::iterator itl = vec_list.begin();
00212 itl != vec_list.end(); ++itl)
00213 {
00214 cout << *itl << endl;
00215 }
00216
00217 cout << endl << "Items got directly from the item parser:" << endl;
00218 for (std::vector<std::string>::iterator it = vec_item.begin();
00219 it != vec_item.end(); ++it)
00220 {
00221 cout << *it << endl;
00222 }
00223
00224 }
00225 else
00226 {
00227 cout << "Failed to parse CSV list!" << endl;
00228 }
00229
00230 cout << endl;
00231
00232 return 0;
00233 }