File: parser_attributes.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (69 lines) | stat: -rw-r--r-- 2,125 bytes parent folder | download | duplicates (5)
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
/**
 *   Copyright (C) 2024 T. Zachary Laine
 *
 *   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/parser/parser.hpp>

#include <boost/core/lightweight_test.hpp>


namespace bp = boost::parser;

bp::rule<struct seq1_tag, bp::tuple<int, char>> seq1 = "";
bp::rule<struct seq2_tag, bp::tuple<int, char>> seq2 = "";
auto const seq1_def = bp::int_ >> bp::char_('a');
auto const seq2_def = bp::int_ >> bp::char_('b');
BOOST_PARSER_DEFINE_RULES(seq1, seq2);

int main()
{
    // These are just some assorted cases that have, or seemed likely to,
    // cause problems when an internal failure in an alternative wipes out an
    // existing result.  This covers all the cases where "if (!success)"
    // causes the attribute to be overwritten with a default-constructed
    // value.

    {
        auto const parser =
            bp::string("FOO") >> -(bp::string("bar") | bp::string("foo"));

        auto result = bp::parse("FOOfoo", parser);
        BOOST_TEST(result);
        BOOST_TEST(bp::get(*result, bp::llong<0>{}) == std::string("FOO"));
        BOOST_TEST(bp::get(*result, bp::llong<1>{}) == std::string("foo"));
    }

    {
        auto const parser = bp::merge
            [bp::string("FOO") >> (bp::string("bar") | bp::string("foo"))];

        auto result = bp::parse("FOOfoo", parser);
        BOOST_TEST(result);
        BOOST_TEST(*result == std::string("FOOfoo"));
    }

    {
        auto const parser = bp::merge
            [(bp::attr(std::vector<std::string>({"FOO"})) | bp::eps) >>
             (bp::repeat(1)[bp::string("foo")] | bp::eps)];

        auto result = bp::parse("", parser);
        BOOST_TEST(result);
        BOOST_TEST(*result);
        BOOST_TEST(*result == std::vector<std::string>({"FOO"}));
    }

    {
        auto const parser = bp::merge[seq1 >> (seq2 | seq1)];

        auto result = bp::parse("7a9a", parser);
        BOOST_TEST(result);
        BOOST_TEST(*result == (bp::tuple<int, char>(9, 'a')));
    }

    return boost::report_errors();
}