summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/example/karma/classify_char.cpp
blob: df113f3341946ca84752920eda8185ea45ebdda4 (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
98
99
100
101
102
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;
}