• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 =============================================================================*/
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/spirit/home/x3.hpp>
9 
10 #include <string>
11 #include <iostream>
12 #include "test.hpp"
13 
14 int
main()15 main()
16 {
17     using boost::spirit::x3::ascii::char_;
18     using boost::spirit::x3::ascii::space;
19     using boost::spirit::x3::lit;
20     using spirit_test::test;
21     using spirit_test::test_attr;
22 
23     BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(char_ - 'a');
24 
25     // Basic tests
26     {
27         BOOST_TEST(test("b", char_ - 'a'));
28         BOOST_TEST(!test("a", char_ - 'a'));
29         BOOST_TEST(test("/* abcdefghijk */", "/*" >> *(char_ - "*/") >> "*/"));
30         BOOST_TEST(!test("switch", lit("switch") - "switch"));
31     }
32 
33     // Test attributes
34     {
35         char attr;
36         BOOST_TEST(test_attr("xg", (char_ - 'g') >> 'g', attr));
37         BOOST_TEST(attr == 'x');
38     }
39 
40     // Test handling of container attributes
41     {
42         std::string attr;
43         BOOST_TEST(test_attr("abcdefg", *(char_ - 'g') >> 'g', attr));
44         BOOST_TEST(attr == "abcdef");
45     }
46 
47     {
48         using boost::spirit::x3::_attr;
49 
50         std::string s;
51 
52         BOOST_TEST(test(
53             "/*abcdefghijk*/"
54           , "/*" >> *(char_ - "*/")[([&](auto& ctx){ s += _attr(ctx); })] >> "*/"
55         ));
56         BOOST_TEST(s == "abcdefghijk");
57         s.clear();
58 
59         BOOST_TEST(test(
60             "    /*abcdefghijk*/"
61           , "/*" >> *(char_ - "*/")[([&](auto& ctx){ s += _attr(ctx); })] >> "*/"
62           , space
63         ));
64         BOOST_TEST(s == "abcdefghijk");
65     }
66 
67     return boost::report_errors();
68 }
69