[29] | 1 | /*============================================================================= |
---|
| 2 | Copyright (c) 2003 Hartmut Kaiser |
---|
| 3 | http://spirit.sourceforge.net/ |
---|
| 4 | |
---|
| 5 | Use, modification and distribution is subject to the Boost Software |
---|
| 6 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
| 7 | http://www.boost.org/LICENSE_1_0.txt) |
---|
| 8 | =============================================================================*/ |
---|
| 9 | #include <iostream> |
---|
| 10 | #include <boost/detail/lightweight_test.hpp> |
---|
| 11 | |
---|
| 12 | using namespace std; |
---|
| 13 | |
---|
| 14 | #define BOOST_SPIRIT_SWITCH_CASE_LIMIT 6 |
---|
| 15 | #define BOOST_SPIRIT_SELECT_LIMIT 6 |
---|
| 16 | #define PHOENIX_LIMIT 6 |
---|
| 17 | |
---|
| 18 | //#define BOOST_SPIRIT_DEBUG |
---|
| 19 | #include <boost/mpl/list.hpp> |
---|
| 20 | #include <boost/mpl/for_each.hpp> |
---|
| 21 | |
---|
| 22 | #include <boost/spirit/core/primitives/primitives.hpp> |
---|
| 23 | #include <boost/spirit/core/primitives/numerics.hpp> |
---|
| 24 | #include <boost/spirit/core/composite/actions.hpp> |
---|
| 25 | #include <boost/spirit/core/composite/operators.hpp> |
---|
| 26 | #include <boost/spirit/core/non_terminal/rule.hpp> |
---|
| 27 | #include <boost/spirit/core/non_terminal/grammar.hpp> |
---|
| 28 | #include <boost/spirit/dynamic/switch.hpp> |
---|
| 29 | #include <boost/spirit/dynamic/select.hpp> |
---|
| 30 | #include <boost/spirit/attribute/closure.hpp> |
---|
| 31 | |
---|
| 32 | using namespace boost::spirit; |
---|
| 33 | |
---|
| 34 | namespace test_grammars { |
---|
| 35 | |
---|
| 36 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 37 | // Test the direct switch_p usage (with default_p) |
---|
| 38 | struct switch_grammar_direct_default1 |
---|
| 39 | : public grammar<switch_grammar_direct_default1> |
---|
| 40 | { |
---|
| 41 | template <typename ScannerT> |
---|
| 42 | struct definition |
---|
| 43 | { |
---|
| 44 | definition(switch_grammar_direct_default1 const& /*self*/) |
---|
| 45 | { |
---|
| 46 | r = switch_p [ |
---|
| 47 | case_p<'a'>(int_p), |
---|
| 48 | case_p<'b'>(ch_p(',')), |
---|
| 49 | case_p<'c'>(str_p("bcd")), |
---|
| 50 | default_p(str_p("default")) |
---|
| 51 | ]; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | rule<ScannerT> r; |
---|
| 55 | rule<ScannerT> const& start() const { return r; } |
---|
| 56 | }; |
---|
| 57 | }; |
---|
| 58 | |
---|
| 59 | struct switch_grammar_direct_default2 |
---|
| 60 | : public grammar<switch_grammar_direct_default2> |
---|
| 61 | { |
---|
| 62 | template <typename ScannerT> |
---|
| 63 | struct definition |
---|
| 64 | { |
---|
| 65 | definition(switch_grammar_direct_default2 const& /*self*/) |
---|
| 66 | { |
---|
| 67 | r = switch_p [ |
---|
| 68 | case_p<'a'>(int_p), |
---|
| 69 | case_p<'b'>(ch_p(',')), |
---|
| 70 | default_p(str_p("default")), |
---|
| 71 | case_p<'c'>(str_p("bcd")) |
---|
| 72 | ]; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | rule<ScannerT> r; |
---|
| 76 | rule<ScannerT> const& start() const { return r; } |
---|
| 77 | }; |
---|
| 78 | }; |
---|
| 79 | |
---|
| 80 | struct switch_grammar_direct_default3 |
---|
| 81 | : public grammar<switch_grammar_direct_default3> |
---|
| 82 | { |
---|
| 83 | template <typename ScannerT> |
---|
| 84 | struct definition |
---|
| 85 | { |
---|
| 86 | definition(switch_grammar_direct_default3 const& /*self*/) |
---|
| 87 | { |
---|
| 88 | r = switch_p [ |
---|
| 89 | default_p(str_p("default")), |
---|
| 90 | case_p<'a'>(int_p), |
---|
| 91 | case_p<'b'>(ch_p(',')), |
---|
| 92 | case_p<'c'>(str_p("bcd")) |
---|
| 93 | ]; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | rule<ScannerT> r; |
---|
| 97 | rule<ScannerT> const& start() const { return r; } |
---|
| 98 | }; |
---|
| 99 | }; |
---|
| 100 | |
---|
| 101 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 102 | // Test the switch_p usage given a parser as the switch condition |
---|
| 103 | struct switch_grammar_parser_default1 |
---|
| 104 | : public grammar<switch_grammar_parser_default1> |
---|
| 105 | { |
---|
| 106 | template <typename ScannerT> |
---|
| 107 | struct definition |
---|
| 108 | { |
---|
| 109 | definition(switch_grammar_parser_default1 const& /*self*/) |
---|
| 110 | { |
---|
| 111 | r = switch_p(anychar_p) [ |
---|
| 112 | case_p<'a'>(int_p), |
---|
| 113 | case_p<'b'>(ch_p(',')), |
---|
| 114 | case_p<'c'>(str_p("bcd")), |
---|
| 115 | default_p(str_p("default")) |
---|
| 116 | ]; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | rule<ScannerT> r; |
---|
| 120 | rule<ScannerT> const& start() const { return r; } |
---|
| 121 | }; |
---|
| 122 | }; |
---|
| 123 | |
---|
| 124 | struct switch_grammar_parser_default2 |
---|
| 125 | : public grammar<switch_grammar_parser_default2> |
---|
| 126 | { |
---|
| 127 | template <typename ScannerT> |
---|
| 128 | struct definition |
---|
| 129 | { |
---|
| 130 | definition(switch_grammar_parser_default2 const& /*self*/) |
---|
| 131 | { |
---|
| 132 | r = switch_p(anychar_p) [ |
---|
| 133 | case_p<'a'>(int_p), |
---|
| 134 | case_p<'b'>(ch_p(',')), |
---|
| 135 | default_p(str_p("default")), |
---|
| 136 | case_p<'c'>(str_p("bcd")) |
---|
| 137 | ]; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | rule<ScannerT> r; |
---|
| 141 | rule<ScannerT> const& start() const { return r; } |
---|
| 142 | }; |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | struct switch_grammar_parser_default3 |
---|
| 146 | : public grammar<switch_grammar_parser_default3> |
---|
| 147 | { |
---|
| 148 | template <typename ScannerT> |
---|
| 149 | struct definition |
---|
| 150 | { |
---|
| 151 | definition(switch_grammar_parser_default3 const& /*self*/) |
---|
| 152 | { |
---|
| 153 | r = switch_p(anychar_p) [ |
---|
| 154 | default_p(str_p("default")), |
---|
| 155 | case_p<'a'>(int_p), |
---|
| 156 | case_p<'b'>(ch_p(',')), |
---|
| 157 | case_p<'c'>(str_p("bcd")) |
---|
| 158 | ]; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | rule<ScannerT> r; |
---|
| 162 | rule<ScannerT> const& start() const { return r; } |
---|
| 163 | }; |
---|
| 164 | }; |
---|
| 165 | |
---|
| 166 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 167 | // Test the switch_p usage given an actor as the switch condition |
---|
| 168 | struct select_result : public boost::spirit::closure<select_result, int> |
---|
| 169 | { |
---|
| 170 | member1 val; |
---|
| 171 | }; |
---|
| 172 | |
---|
| 173 | /////////////////////////////////////////////////////////////////////////// |
---|
| 174 | struct switch_grammar_actor_default1 |
---|
| 175 | : public grammar<switch_grammar_actor_default1> |
---|
| 176 | { |
---|
| 177 | template <typename ScannerT> |
---|
| 178 | struct definition |
---|
| 179 | { |
---|
| 180 | definition(switch_grammar_actor_default1 const& /*self*/) |
---|
| 181 | { |
---|
| 182 | using phoenix::arg1; |
---|
| 183 | r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >> |
---|
| 184 | switch_p(r.val) [ |
---|
| 185 | case_p<0>(int_p), |
---|
| 186 | case_p<1>(ch_p(',')), |
---|
| 187 | case_p<2>(str_p("bcd")), |
---|
| 188 | default_p(str_p("default")) |
---|
| 189 | ]; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | rule<ScannerT, select_result::context_t> r; |
---|
| 193 | rule<ScannerT, select_result::context_t> const& |
---|
| 194 | start() const { return r; } |
---|
| 195 | }; |
---|
| 196 | }; |
---|
| 197 | |
---|
| 198 | struct switch_grammar_actor_default2 |
---|
| 199 | : public grammar<switch_grammar_actor_default2> |
---|
| 200 | { |
---|
| 201 | template <typename ScannerT> |
---|
| 202 | struct definition |
---|
| 203 | { |
---|
| 204 | definition(switch_grammar_actor_default2 const& /*self*/) |
---|
| 205 | { |
---|
| 206 | using phoenix::arg1; |
---|
| 207 | r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >> |
---|
| 208 | switch_p(r.val) [ |
---|
| 209 | case_p<0>(int_p), |
---|
| 210 | case_p<1>(ch_p(',')), |
---|
| 211 | default_p(str_p("default")), |
---|
| 212 | case_p<2>(str_p("bcd")) |
---|
| 213 | ]; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | rule<ScannerT, select_result::context_t> r; |
---|
| 217 | rule<ScannerT, select_result::context_t> const& |
---|
| 218 | start() const { return r; } |
---|
| 219 | }; |
---|
| 220 | }; |
---|
| 221 | |
---|
| 222 | struct switch_grammar_actor_default3 |
---|
| 223 | : public grammar<switch_grammar_actor_default3> |
---|
| 224 | { |
---|
| 225 | template <typename ScannerT> |
---|
| 226 | struct definition |
---|
| 227 | { |
---|
| 228 | definition(switch_grammar_actor_default3 const& /*self*/) |
---|
| 229 | { |
---|
| 230 | using phoenix::arg1; |
---|
| 231 | r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >> |
---|
| 232 | switch_p(r.val) [ |
---|
| 233 | default_p(str_p("default")), |
---|
| 234 | case_p<0>(int_p), |
---|
| 235 | case_p<1>(ch_p(',')), |
---|
| 236 | case_p<2>(str_p("bcd")) |
---|
| 237 | ]; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | rule<ScannerT, select_result::context_t> r; |
---|
| 241 | rule<ScannerT, select_result::context_t> const& |
---|
| 242 | start() const { return r; } |
---|
| 243 | }; |
---|
| 244 | }; |
---|
| 245 | |
---|
| 246 | } // namespace test_grammars |
---|
| 247 | |
---|
| 248 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 249 | namespace tests { |
---|
| 250 | |
---|
| 251 | // Tests for known (to the grammars) sequences |
---|
| 252 | struct check_grammar_known { |
---|
| 253 | |
---|
| 254 | template <typename GrammarT> |
---|
| 255 | void operator()(GrammarT) |
---|
| 256 | { |
---|
| 257 | GrammarT g; |
---|
| 258 | |
---|
| 259 | BOOST_TEST(parse("a1", g).full); |
---|
| 260 | BOOST_TEST(!parse("a,", g).hit); |
---|
| 261 | BOOST_TEST(!parse("abcd", g).hit); |
---|
| 262 | |
---|
| 263 | BOOST_TEST(parse("a 1", g, space_p).full); |
---|
| 264 | BOOST_TEST(!parse("a ,", g, space_p).hit); |
---|
| 265 | BOOST_TEST(!parse("a bcd", g, space_p).hit); |
---|
| 266 | |
---|
| 267 | BOOST_TEST(!parse("b1", g).hit); |
---|
| 268 | BOOST_TEST(parse("b,", g).full); |
---|
| 269 | BOOST_TEST(!parse("bbcd", g).hit); |
---|
| 270 | |
---|
| 271 | BOOST_TEST(!parse("b 1", g, space_p).hit); |
---|
| 272 | BOOST_TEST(parse("b ,", g, space_p).full); |
---|
| 273 | BOOST_TEST(!parse("b bcd", g, space_p).hit); |
---|
| 274 | |
---|
| 275 | BOOST_TEST(!parse("c1", g).hit); |
---|
| 276 | BOOST_TEST(!parse("c,", g).hit); |
---|
| 277 | BOOST_TEST(parse("cbcd", g).full); |
---|
| 278 | |
---|
| 279 | BOOST_TEST(!parse("c 1", g, space_p).hit); |
---|
| 280 | BOOST_TEST(!parse("c ,", g, space_p).hit); |
---|
| 281 | BOOST_TEST(parse("c bcd", g, space_p).full); |
---|
| 282 | } |
---|
| 283 | }; |
---|
| 284 | |
---|
| 285 | // Tests for unknown (to the grammar) sequences |
---|
| 286 | struct check_grammar_unknown_default { |
---|
| 287 | |
---|
| 288 | template <typename GrammarT> |
---|
| 289 | void operator()(GrammarT) |
---|
| 290 | { |
---|
| 291 | GrammarT g; |
---|
| 292 | |
---|
| 293 | BOOST_TEST(!parse("d1", g).hit); |
---|
| 294 | BOOST_TEST(!parse("d,", g).hit); |
---|
| 295 | BOOST_TEST(!parse("dbcd", g).hit); |
---|
| 296 | |
---|
| 297 | BOOST_TEST(!parse("d 1", g, space_p).hit); |
---|
| 298 | BOOST_TEST(!parse("d ,", g, space_p).hit); |
---|
| 299 | BOOST_TEST(!parse("d bcd", g, space_p).hit); |
---|
| 300 | } |
---|
| 301 | }; |
---|
| 302 | |
---|
| 303 | // Tests for the default branches (with parsers) of the grammars |
---|
| 304 | struct check_grammar_default { |
---|
| 305 | |
---|
| 306 | template <typename GrammarT> |
---|
| 307 | void operator()(GrammarT) |
---|
| 308 | { |
---|
| 309 | GrammarT g; |
---|
| 310 | |
---|
| 311 | BOOST_TEST(parse("ddefault", g).full); |
---|
| 312 | BOOST_TEST(parse("d default", g, space_p).full); |
---|
| 313 | } |
---|
| 314 | }; |
---|
| 315 | |
---|
| 316 | } // namespace tests |
---|
| 317 | |
---|
| 318 | int |
---|
| 319 | main() |
---|
| 320 | { |
---|
| 321 | // Test switch_p parsers containing general default_p(...) case branches |
---|
| 322 | typedef boost::mpl::list< |
---|
| 323 | // switch_p syntax |
---|
| 324 | test_grammars::switch_grammar_direct_default1, |
---|
| 325 | test_grammars::switch_grammar_direct_default2, |
---|
| 326 | test_grammars::switch_grammar_direct_default3, |
---|
| 327 | |
---|
| 328 | // switch_p(parser) syntax |
---|
| 329 | test_grammars::switch_grammar_parser_default1, |
---|
| 330 | test_grammars::switch_grammar_parser_default2, |
---|
| 331 | test_grammars::switch_grammar_parser_default3, |
---|
| 332 | |
---|
| 333 | // switch_p(actor) syntax |
---|
| 334 | test_grammars::switch_grammar_actor_default1, |
---|
| 335 | test_grammars::switch_grammar_actor_default2, |
---|
| 336 | test_grammars::switch_grammar_actor_default3 |
---|
| 337 | > default_list_t; |
---|
| 338 | |
---|
| 339 | boost::mpl::for_each<default_list_t>(tests::check_grammar_known()); |
---|
| 340 | boost::mpl::for_each<default_list_t>(tests::check_grammar_unknown_default()); |
---|
| 341 | boost::mpl::for_each<default_list_t>(tests::check_grammar_default()); |
---|
| 342 | |
---|
| 343 | return boost::report_errors(); |
---|
| 344 | } |
---|