]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/porting_guide_qi.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
3 http://spirit.sourceforge.net/
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 //[porting_guide_qi_includes
9 #include <boost/spirit/include/qi.hpp>
10 #include <boost/spirit/include/phoenix_operator.hpp>
16 //[porting_guide_qi_namespace
17 using namespace boost::spirit
;
20 //[porting_guide_qi_grammar
21 template <typename Iterator
>
22 struct roman
: qi::grammar
<Iterator
, unsigned()>
24 roman() : roman::base_type(first
)
27 ("C" , 100)("CC" , 200)("CCC" , 300)("CD" , 400)("D" , 500)
28 ("DC" , 600)("DCC" , 700)("DCCC" , 800)("CM" , 900) ;
31 ("X" , 10)("XX" , 20)("XXX" , 30)("XL" , 40)("L" , 50)
32 ("LX" , 60)("LXX" , 70)("LXXX" , 80)("XC" , 90) ;
35 ("I" , 1)("II" , 2)("III" , 3)("IV" , 4)("V" , 5)
36 ("VI" , 6)("VII" , 7)("VIII" , 8)("IX" , 9) ;
38 // qi::_val refers to the attribute of the rule on the left hand side
39 first
= eps
[qi::_val
= 0]
40 >> ( +lit('M') [qi::_val
+= 1000]
41 || hundreds
[qi::_val
+= qi::_1
]
42 || tens
[qi::_val
+= qi::_1
]
43 || ones
[qi::_val
+= qi::_1
]
47 qi::rule
<Iterator
, unsigned()> first
;
48 qi::symbols
<char, unsigned> hundreds
;
49 qi::symbols
<char, unsigned> tens
;
50 qi::symbols
<char, unsigned> ones
;
57 //[porting_guide_qi_parse
58 std::string
input("1,1");
59 std::string::iterator it
= input
.begin();
60 bool result
= qi::parse(it
, input
.end(), qi::int_
);
63 std::cout
<< "successful match!\n";
65 if (it
== input
.end())
66 std::cout
<< "full match!\n";
68 std::cout
<< "stopped at: " << std::string(it
, input
.end()) << "\n";
70 // seldomly needed: use std::distance to calculate the length of the match
71 std::cout
<< "matched length: " << std::distance(input
.begin(), it
) << "\n";
76 //[porting_guide_qi_phrase_parse
77 std::string
input(" 1, 1");
78 std::string::iterator it
= input
.begin();
79 bool result
= qi::phrase_parse(it
, input
.end(), qi::int_
, ascii::space
);
82 std::cout
<< "successful match!\n";
84 if (it
== input
.end())
85 std::cout
<< "full match!\n";
87 std::cout
<< "stopped at: " << std::string(it
, input
.end()) << "\n";
89 // seldomly needed: use std::distance to calculate the length of the match
90 std::cout
<< "matched length: " << std::distance(input
.begin(), it
) << "\n";
95 //[porting_guide_qi_use_grammar
96 std::string
input("MMIX"); // MMIX == 2009
97 std::string::iterator it
= input
.begin();
99 roman
<std::string::iterator
> r
;
100 if (qi::parse(it
, input
.end(), r
, value
))
101 std::cout
<< "successfully matched: " << value
<< "\n";