summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/test/x3/raw.cpp
blob: 78f025ddd5c7ed4206ed713b87f3cfae8f94d204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*=============================================================================
    Copyright (c) 2001-2014 Joel de Guzman

    Distributed under 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 <boost/detail/lightweight_test.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/std_pair.hpp>

#include <iostream>
#include <string>
#include "test.hpp"

int main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    using namespace boost::spirit::x3::ascii;
    using boost::spirit::x3::raw;
    using boost::spirit::x3::eps;
    using boost::spirit::x3::lit;
    using boost::spirit::x3::_attr;
    using boost::spirit::x3::parse;
    using boost::spirit::x3::int_;
    using boost::spirit::x3::char_;

    {
        boost::iterator_range<char const*> range;
        std::string str;
        BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], range)));
        BOOST_TEST((std::string(range.begin(), range.end()) == "spirit_test_123"));
        BOOST_TEST((test_attr("  spirit", raw[*alpha], range, space)));
        BOOST_TEST((range.size() == 6));
    }

    {
        std::string str;
        BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], str)));
        BOOST_TEST((str == "spirit_test_123"));
    }

    {
        boost::iterator_range<char const*> range;
        BOOST_TEST((test("x", raw[alpha])));
        BOOST_TEST((test_attr("x", raw[alpha], range)));
    }

    {
        boost::iterator_range<char const*> range;
        BOOST_TEST((test("x", raw[alpha][ ([&](auto& ctx){ range = _attr(ctx); }) ])));
        BOOST_TEST(range.size() == 1 && *range.begin() == 'x');
    }

    {
        boost::iterator_range<char const*> range;
        BOOST_TEST((test("x123x", lit('x') >> raw[+digit] >> lit('x'))));
        BOOST_TEST((test_attr("x123x", lit('x') >> raw[+digit] >> lit('x'), range)));
        BOOST_TEST((std::string(range.begin(), range.end()) == "123"));
    }

    {
        using range = boost::iterator_range<std::string::iterator>;
        boost::variant<int, range> attr;

        std::string str("test");
        parse(str.begin(), str.end(),  (int_ | raw[*char_]), attr);

        auto rng = boost::get<range>(attr);
        BOOST_TEST(std::string(rng.begin(), rng.end()) == "test");
    }

    {
        std::vector<boost::iterator_range<std::string::iterator>> attr;
        std::string str("123abcd");
        parse(str.begin(), str.end()
          , (raw[int_] >> raw[*char_])
          , attr
        );
        BOOST_TEST(attr.size() == 2);
        BOOST_TEST(std::string(attr[0].begin(), attr[0].end()) == "123");
        BOOST_TEST(std::string(attr[1].begin(), attr[1].end()) == "abcd");
    }

    {
        std::pair<int, boost::iterator_range<std::string::iterator>> attr;
        std::string str("123abcd");
        parse(str.begin(), str.end()
          , (int_ >> raw[*char_])
          , attr
        );
        BOOST_TEST(attr.first == 123);
        BOOST_TEST(std::string(attr.second.begin(), attr.second.end()) == "abcd");
    }

    return boost::report_errors();
}