123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- /*=============================================================================
- Copyright (c) 2003 Hartmut Kaiser
- http://spirit.sourceforge.net/
- Use, modification and distribution is subject to 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)
- =============================================================================*/
- #include <iostream>
- #include <boost/detail/lightweight_test.hpp>
- #define BOOST_SPIRIT_SWITCH_CASE_LIMIT 6
- #define BOOST_SPIRIT_SELECT_LIMIT 6
- #define PHOENIX_LIMIT 6
- //#define BOOST_SPIRIT_DEBUG
- #include <boost/mpl/list.hpp>
- #include <boost/mpl/for_each.hpp>
- #include <boost/spirit/include/classic_primitives.hpp>
- #include <boost/spirit/include/classic_numerics.hpp>
- #include <boost/spirit/include/classic_actions.hpp>
- #include <boost/spirit/include/classic_operators.hpp>
- #include <boost/spirit/include/classic_rule.hpp>
- #include <boost/spirit/include/classic_grammar.hpp>
- #include <boost/spirit/include/classic_switch.hpp>
- #include <boost/spirit/include/classic_select.hpp>
- #include <boost/spirit/include/classic_closure.hpp>
- using namespace BOOST_SPIRIT_CLASSIC_NS;
- namespace test_grammars {
- ///////////////////////////////////////////////////////////////////////////////
- // Test the direct switch_p usage (with default_p)
- struct switch_grammar_direct_default1
- : public grammar<switch_grammar_direct_default1>
- {
- template <typename ScannerT>
- struct definition
- {
- definition(switch_grammar_direct_default1 const& /*self*/)
- {
- r = switch_p [
- case_p<'a'>(int_p),
- case_p<'b'>(ch_p(',')),
- case_p<'c'>(str_p("bcd")),
- default_p(str_p("default"))
- ];
- }
- rule<ScannerT> r;
- rule<ScannerT> const& start() const { return r; }
- };
- };
- struct switch_grammar_direct_default2
- : public grammar<switch_grammar_direct_default2>
- {
- template <typename ScannerT>
- struct definition
- {
- definition(switch_grammar_direct_default2 const& /*self*/)
- {
- r = switch_p [
- case_p<'a'>(int_p),
- case_p<'b'>(ch_p(',')),
- default_p(str_p("default")),
- case_p<'c'>(str_p("bcd"))
- ];
- }
- rule<ScannerT> r;
- rule<ScannerT> const& start() const { return r; }
- };
- };
- struct switch_grammar_direct_default3
- : public grammar<switch_grammar_direct_default3>
- {
- template <typename ScannerT>
- struct definition
- {
- definition(switch_grammar_direct_default3 const& /*self*/)
- {
- r = switch_p [
- default_p(str_p("default")),
- case_p<'a'>(int_p),
- case_p<'b'>(ch_p(',')),
- case_p<'c'>(str_p("bcd"))
- ];
- }
- rule<ScannerT> r;
- rule<ScannerT> const& start() const { return r; }
- };
- };
- ///////////////////////////////////////////////////////////////////////////////
- // Test the switch_p usage given a parser as the switch condition
- struct switch_grammar_parser_default1
- : public grammar<switch_grammar_parser_default1>
- {
- template <typename ScannerT>
- struct definition
- {
- definition(switch_grammar_parser_default1 const& /*self*/)
- {
- r = switch_p(anychar_p) [
- case_p<'a'>(int_p),
- case_p<'b'>(ch_p(',')),
- case_p<'c'>(str_p("bcd")),
- default_p(str_p("default"))
- ];
- }
- rule<ScannerT> r;
- rule<ScannerT> const& start() const { return r; }
- };
- };
- struct switch_grammar_parser_default2
- : public grammar<switch_grammar_parser_default2>
- {
- template <typename ScannerT>
- struct definition
- {
- definition(switch_grammar_parser_default2 const& /*self*/)
- {
- r = switch_p(anychar_p) [
- case_p<'a'>(int_p),
- case_p<'b'>(ch_p(',')),
- default_p(str_p("default")),
- case_p<'c'>(str_p("bcd"))
- ];
- }
- rule<ScannerT> r;
- rule<ScannerT> const& start() const { return r; }
- };
- };
- struct switch_grammar_parser_default3
- : public grammar<switch_grammar_parser_default3>
- {
- template <typename ScannerT>
- struct definition
- {
- definition(switch_grammar_parser_default3 const& /*self*/)
- {
- r = switch_p(anychar_p) [
- default_p(str_p("default")),
- case_p<'a'>(int_p),
- case_p<'b'>(ch_p(',')),
- case_p<'c'>(str_p("bcd"))
- ];
- }
- rule<ScannerT> r;
- rule<ScannerT> const& start() const { return r; }
- };
- };
- ///////////////////////////////////////////////////////////////////////////////
- // Test the switch_p usage given an actor as the switch condition
- struct select_result : public BOOST_SPIRIT_CLASSIC_NS::closure<select_result, int>
- {
- member1 val;
- };
- ///////////////////////////////////////////////////////////////////////////
- struct switch_grammar_actor_default1
- : public grammar<switch_grammar_actor_default1>
- {
- template <typename ScannerT>
- struct definition
- {
- definition(switch_grammar_actor_default1 const& /*self*/)
- {
- using phoenix::arg1;
- r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
- switch_p(r.val) [
- case_p<0>(int_p),
- case_p<1>(ch_p(',')),
- case_p<2>(str_p("bcd")),
- default_p(str_p("default"))
- ];
- }
- rule<ScannerT, select_result::context_t> r;
- rule<ScannerT, select_result::context_t> const&
- start() const { return r; }
- };
- };
- struct switch_grammar_actor_default2
- : public grammar<switch_grammar_actor_default2>
- {
- template <typename ScannerT>
- struct definition
- {
- definition(switch_grammar_actor_default2 const& /*self*/)
- {
- using phoenix::arg1;
- r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
- switch_p(r.val) [
- case_p<0>(int_p),
- case_p<1>(ch_p(',')),
- default_p(str_p("default")),
- case_p<2>(str_p("bcd"))
- ];
- }
- rule<ScannerT, select_result::context_t> r;
- rule<ScannerT, select_result::context_t> const&
- start() const { return r; }
- };
- };
- struct switch_grammar_actor_default3
- : public grammar<switch_grammar_actor_default3>
- {
- template <typename ScannerT>
- struct definition
- {
- definition(switch_grammar_actor_default3 const& /*self*/)
- {
- using phoenix::arg1;
- r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
- switch_p(r.val) [
- default_p(str_p("default")),
- case_p<0>(int_p),
- case_p<1>(ch_p(',')),
- case_p<2>(str_p("bcd"))
- ];
- }
- rule<ScannerT, select_result::context_t> r;
- rule<ScannerT, select_result::context_t> const&
- start() const { return r; }
- };
- };
-
- } // namespace test_grammars
- ///////////////////////////////////////////////////////////////////////////////
- namespace tests {
- // Tests for known (to the grammars) sequences
- struct check_grammar_known {
-
- template <typename GrammarT>
- void operator()(GrammarT)
- {
- GrammarT g;
-
- BOOST_TEST(parse("a1", g).full);
- BOOST_TEST(!parse("a,", g).hit);
- BOOST_TEST(!parse("abcd", g).hit);
-
- BOOST_TEST(parse("a 1", g, space_p).full);
- BOOST_TEST(!parse("a ,", g, space_p).hit);
- BOOST_TEST(!parse("a bcd", g, space_p).hit);
-
- BOOST_TEST(!parse("b1", g).hit);
- BOOST_TEST(parse("b,", g).full);
- BOOST_TEST(!parse("bbcd", g).hit);
-
- BOOST_TEST(!parse("b 1", g, space_p).hit);
- BOOST_TEST(parse("b ,", g, space_p).full);
- BOOST_TEST(!parse("b bcd", g, space_p).hit);
-
- BOOST_TEST(!parse("c1", g).hit);
- BOOST_TEST(!parse("c,", g).hit);
- BOOST_TEST(parse("cbcd", g).full);
-
- BOOST_TEST(!parse("c 1", g, space_p).hit);
- BOOST_TEST(!parse("c ,", g, space_p).hit);
- BOOST_TEST(parse("c bcd", g, space_p).full);
- }
- };
- // Tests for unknown (to the grammar) sequences
- struct check_grammar_unknown_default {
-
- template <typename GrammarT>
- void operator()(GrammarT)
- {
- GrammarT g;
-
- BOOST_TEST(!parse("d1", g).hit);
- BOOST_TEST(!parse("d,", g).hit);
- BOOST_TEST(!parse("dbcd", g).hit);
- BOOST_TEST(!parse("d 1", g, space_p).hit);
- BOOST_TEST(!parse("d ,", g, space_p).hit);
- BOOST_TEST(!parse("d bcd", g, space_p).hit);
- }
- };
-
- // Tests for the default branches (with parsers) of the grammars
- struct check_grammar_default {
-
- template <typename GrammarT>
- void operator()(GrammarT)
- {
- GrammarT g;
-
- BOOST_TEST(parse("ddefault", g).full);
- BOOST_TEST(parse("d default", g, space_p).full);
- }
- };
-
- } // namespace tests
- int
- main()
- {
- // Test switch_p parsers containing general default_p(...) case branches
- typedef boost::mpl::list<
- // switch_p syntax
- test_grammars::switch_grammar_direct_default1,
- test_grammars::switch_grammar_direct_default2,
- test_grammars::switch_grammar_direct_default3,
-
- // switch_p(parser) syntax
- test_grammars::switch_grammar_parser_default1,
- test_grammars::switch_grammar_parser_default2,
- test_grammars::switch_grammar_parser_default3,
-
- // switch_p(actor) syntax
- test_grammars::switch_grammar_actor_default1,
- test_grammars::switch_grammar_actor_default2,
- test_grammars::switch_grammar_actor_default3
- > default_list_t;
-
- boost::mpl::for_each<default_list_t>(tests::check_grammar_known());
- boost::mpl::for_each<default_list_t>(tests::check_grammar_unknown_default());
- boost::mpl::for_each<default_list_t>(tests::check_grammar_default());
- return boost::report_errors();
- }
|