diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/spirit/example/karma/classify_char.cpp | |
parent | Initial commit. (diff) | |
download | ceph-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/example/karma/classify_char.cpp')
-rw-r--r-- | src/boost/libs/spirit/example/karma/classify_char.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/boost/libs/spirit/example/karma/classify_char.cpp b/src/boost/libs/spirit/example/karma/classify_char.cpp new file mode 100644 index 00000000..df113f33 --- /dev/null +++ b/src/boost/libs/spirit/example/karma/classify_char.cpp @@ -0,0 +1,103 @@ +// Copyright (c) 2001-2010 Hartmut Kaiser +// +// 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) + +/////////////////////////////////////////////////////////////////////////////// +// +// A character classification example +// +// [ HK August 12, 2009 ] spirit2 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/include/qi.hpp> +#include <boost/spirit/include/karma.hpp> +#include <boost/spirit/include/phoenix_core.hpp> +#include <boost/spirit/include/phoenix_operator.hpp> +#include <boost/fusion/include/std_pair.hpp> + +#include <iostream> +#include <string> +#include <complex> + +namespace client +{ + /////////////////////////////////////////////////////////////////////////// + // Our character classification generator + /////////////////////////////////////////////////////////////////////////// + //[tutorial_karma_complex_number + template <typename OutputIterator> + bool classify_character(OutputIterator sink, char c) + { + using boost::spirit::ascii::char_; + using boost::spirit::ascii::digit; + using boost::spirit::ascii::xdigit; + using boost::spirit::ascii::alpha; + using boost::spirit::ascii::punct; + using boost::spirit::ascii::space; + using boost::spirit::ascii::cntrl; + using boost::spirit::karma::omit; + using boost::spirit::karma::generate; + + if (!boost::spirit::char_encoding::ascii::isascii_(c)) + return false; + + return generate(sink, + // Begin grammar + ( + "The character '" << char_ << "' is " + << ( &digit << "a digit" + | &xdigit << "a xdigit" + | &alpha << "a alpha" + | &punct << "a punct" + | &space << "a space" + | &cntrl << "a cntrl" + | "of unknown type" + ) + ), + // End grammar + c, c + ); + } + //] +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "\t\tA character classification micro generator for Spirit...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + + std::cout << "Give me a character to classify\n"; + std::cout << "Type [q or Q] to quit\n\n"; + + std::string str; + while (getline(std::cin, str)) + { + if (str.empty()) + break; + + std::string generated; + std::back_insert_iterator<std::string> sink(generated); + if (!client::classify_character(sink, str[0])) + { + std::cout << "-------------------------\n"; + std::cout << "Generating failed\n"; + std::cout << "-------------------------\n"; + } + else + { + std::cout << generated << "\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} + + |