1 /*=============================================================================
2 Copyright (c) 2003 Sam Nabialek
3 Copyright (c) 2001-2010 Joel de Guzman
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 ///////////////////////////////////////////////////////////////////////////////
10 // The Nabialek trick.
12 // [ Sam Nabialek; Somewhere, sometime in 2003... ] spirit1
13 // [ JDG November 17, 2009 ] spirit2
14 // [ JDG January 10, 2010 ] Updated to use rule pointers
17 ///////////////////////////////////////////////////////////////////////////////
19 #include <boost/spirit/include/qi.hpp>
20 #include <boost/phoenix/operator.hpp>
26 namespace qi
= boost::spirit::qi
;
27 namespace ascii
= boost::spirit::ascii
;
29 ///////////////////////////////////////////////////////////////////////////////
30 // Our nabialek_trick grammar
31 ///////////////////////////////////////////////////////////////////////////////
32 template <typename Iterator
>
33 struct nabialek_trick
: qi::grammar
<
34 Iterator
, ascii::space_type
, qi::locals
<qi::rule
<Iterator
, ascii::space_type
>*> >
36 nabialek_trick() : nabialek_trick::base_type(start
)
44 id
= lexeme
[*(ascii::alnum
| '_')];
46 two
= id
>> ',' >> id
;
53 start
= *(keyword
[_a
= _1
] >> lazy(*_a
));
56 qi::rule
<Iterator
, ascii::space_type
> id
, one
, two
;
57 qi::rule
<Iterator
, ascii::space_type
, qi::locals
<qi::rule
<Iterator
, ascii::space_type
>*> > start
;
58 qi::symbols
<char, qi::rule
<Iterator
, ascii::space_type
>*> keyword
;
62 ///////////////////////////////////////////////////////////////////////////////
64 ///////////////////////////////////////////////////////////////////////////////
68 using boost::spirit::ascii::space
;
69 typedef std::string::const_iterator iterator_type
;
70 typedef client::nabialek_trick
<iterator_type
> nabialek_trick
;
72 nabialek_trick g
; // Our grammar
74 std::string str
= "one only\none again\ntwo first,second";
75 std::string::const_iterator iter
= str
.begin();
76 std::string::const_iterator end
= str
.end();
77 bool r
= phrase_parse(iter
, end
, g
, space
);
81 std::cout
<< "-------------------------\n";
82 std::cout
<< "Parsing succeeded\n";
83 std::cout
<< "-------------------------\n";
87 std::string
rest(iter
, end
);
88 std::cout
<< "-------------------------\n";
89 std::cout
<< "Parsing failed\n";
90 std::cout
<< "stopped at: \": " << rest
<< "\"\n";
91 std::cout
<< "-------------------------\n";