summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/test/x3/rule2.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/spirit/test/x3/rule2.cpp
parentInitial commit. (diff)
downloadceph-upstream.tar.xz
ceph-upstream.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/spirit/test/x3/rule2.cpp')
-rw-r--r--src/boost/libs/spirit/test/x3/rule2.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/boost/libs/spirit/test/x3/rule2.cpp b/src/boost/libs/spirit/test/x3/rule2.cpp
new file mode 100644
index 00000000..fd6c07fa
--- /dev/null
+++ b/src/boost/libs/spirit/test/x3/rule2.cpp
@@ -0,0 +1,108 @@
+/*=============================================================================
+ Copyright (c) 2001-2015 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 <string>
+#include <cstring>
+#include <iostream>
+#include "test.hpp"
+
+int
+main()
+{
+ using spirit_test::test_attr;
+ using spirit_test::test;
+
+ using namespace boost::spirit::x3::ascii;
+ using boost::spirit::x3::rule;
+ using boost::spirit::x3::lit;
+ using boost::spirit::x3::unused_type;
+ using boost::spirit::x3::_attr;
+
+ { // context tests
+
+ char ch;
+ auto a = rule<class a, char>() = alpha;
+
+ // this semantic action requires the context
+ auto f = [&](auto& ctx){ ch = _attr(ctx); };
+ BOOST_TEST(test("x", a[f]));
+ BOOST_TEST(ch == 'x');
+
+ // this semantic action requires the (unused) context
+ auto f2 = [&](auto&){ ch = 'y'; };
+ BOOST_TEST(test("x", a[f2]));
+ BOOST_TEST(ch == 'y');
+
+ // the semantic action may optionally not have any arguments at all
+ auto f3 = [&]{ ch = 'z'; };
+ BOOST_TEST(test("x", a[f3]));
+ BOOST_TEST(ch == 'z');
+
+ BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
+ BOOST_TEST(ch == 'z');
+ }
+
+ { // auto rules tests
+
+ char ch = '\0';
+ auto a = rule<class a, char>() = alpha;
+ auto f = [&](auto& ctx){ ch = _attr(ctx); };
+
+ BOOST_TEST(test("x", a[f]));
+ BOOST_TEST(ch == 'x');
+ ch = '\0';
+ BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
+ BOOST_TEST(ch == 'z');
+
+ ch = '\0';
+ BOOST_TEST(test("x", a[f]));
+ BOOST_TEST(ch == 'x');
+ ch = '\0';
+ BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
+ BOOST_TEST(ch == 'z');
+ }
+
+ { // auto rules tests: allow stl containers as attributes to
+ // sequences (in cases where attributes of the elements
+ // are convertible to the value_type of the container or if
+ // the element itself is an stl container with value_type
+ // that is convertible to the value_type of the attribute).
+
+ std::string s;
+ auto f = [&](auto& ctx){ s = _attr(ctx); };
+
+ {
+ auto r = rule<class r, std::string>()
+ = char_ >> *(',' >> char_)
+ ;
+
+ BOOST_TEST(test("a,b,c,d,e,f", r[f]));
+ BOOST_TEST(s == "abcdef");
+ }
+
+ {
+ auto r = rule<class r, std::string>()
+ = char_ >> *(',' >> char_);
+ s.clear();
+ BOOST_TEST(test("a,b,c,d,e,f", r[f]));
+ BOOST_TEST(s == "abcdef");
+ }
+
+ {
+ auto r = rule<class r, std::string>()
+ = char_ >> char_ >> char_ >> char_ >> char_ >> char_;
+ s.clear();
+ BOOST_TEST(test("abcdef", r[f]));
+ BOOST_TEST(s == "abcdef");
+ }
+ }
+
+ return boost::report_errors();
+}