summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/example/qi/nabialek.cpp
blob: 4795578c067503dd6b70479521e4209d9918c35d (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
/*=============================================================================
    Copyright (c) 2003 Sam Nabialek
    Copyright (c) 2001-2010 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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
//  The Nabialek trick.
//
//  [ Sam Nabialek; Somewhere, sometime in 2003... ]    spirit1
//  [ JDG November 17, 2009 ]                           spirit2
//  [ JDG January 10, 2010 ]                            Updated to use rule pointers
//                                                      for efficiency.
//
///////////////////////////////////////////////////////////////////////////////

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <string>

namespace client
{
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    ///////////////////////////////////////////////////////////////////////////////
    //  Our nabialek_trick grammar
    ///////////////////////////////////////////////////////////////////////////////
    template <typename Iterator>
    struct nabialek_trick : qi::grammar<
        Iterator, ascii::space_type, qi::locals<qi::rule<Iterator, ascii::space_type>*> >
    {
        nabialek_trick() : nabialek_trick::base_type(start)
        {
            using ascii::alnum;
            using qi::lexeme;
            using qi::lazy;
            using qi::_a;
            using qi::_1;

            id = lexeme[*(ascii::alnum | '_')];
            one = id;
            two = id >> ',' >> id;

            keyword.add
                ("one", &one)
                ("two", &two)
                ;

            start = *(keyword[_a = _1] >> lazy(*_a));
        }

        qi::rule<Iterator, ascii::space_type> id, one, two;
        qi::rule<Iterator, ascii::space_type, qi::locals<qi::rule<Iterator, ascii::space_type>*> > start;
        qi::symbols<char, qi::rule<Iterator, ascii::space_type>*> keyword;
    };
}

///////////////////////////////////////////////////////////////////////////////
//  Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
    using boost::spirit::ascii::space;
    typedef std::string::const_iterator iterator_type;
    typedef client::nabialek_trick<iterator_type> nabialek_trick;

    nabialek_trick g; // Our grammar

    std::string str = "one only\none again\ntwo first,second";
    std::string::const_iterator iter = str.begin();
    std::string::const_iterator end = str.end();
    bool r = phrase_parse(iter, end, g, space);

    if (r && iter == end)
    {
        std::cout << "-------------------------\n";
        std::cout << "Parsing succeeded\n";
        std::cout << "-------------------------\n";
    }
    else
    {
        std::string rest(iter, end);
        std::cout << "-------------------------\n";
        std::cout << "Parsing failed\n";
        std::cout << "stopped at: \": " << rest << "\"\n";
        std::cout << "-------------------------\n";
    }

    return 0;
}