]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2001-2010 Hartmut Kaiser |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | // The purpose of this example is to show how a simple custom primitive parser | |
7 | // component can be written. We develop a custom parser exposing the current | |
8 | // iterator position as its attribute. | |
9 | // | |
10 | // For more information see: http://spirit.sourceforge.net/home/?page_id=567 | |
11 | ||
12 | #include <boost/spirit/include/qi_parse_attr.hpp> | |
13 | #include <boost/spirit/include/qi_char.hpp> | |
14 | #include <boost/spirit/include/qi_operator.hpp> | |
15 | ||
1e59de90 | 16 | #include <iostream> |
7c673cae FG |
17 | #include <string> |
18 | #include "iter_pos.hpp" | |
19 | ||
20 | namespace qi = boost::spirit::qi; | |
21 | ||
22 | int main() | |
23 | { | |
24 | using custom_parser::iter_pos; | |
25 | ||
26 | std::string prefix, suffix; // attributes receiving the | |
27 | std::string::iterator position; // parsed values | |
28 | ||
29 | std::string input("prefix1234567"); | |
30 | std::string::iterator first = input.begin(); | |
31 | bool result = | |
32 | qi::parse(first, input.end() | |
33 | , +qi::alpha >> iter_pos >> +qi::digit | |
34 | , prefix, position, suffix); | |
35 | ||
36 | if (result) | |
37 | { | |
38 | std::cout << "-------------------------------- \n"; | |
39 | std::cout << "Parsing succeeded\n"; | |
40 | std::cout << "prefix is: " << prefix << "\n"; | |
41 | std::cout << "suffix is: " << suffix << "\n"; | |
42 | std::cout << "position is: " << std::distance(input.begin(), position) << "\n"; | |
43 | std::cout << "-------------------------------- \n"; | |
44 | } | |
45 | else | |
46 | { | |
47 | std::cout << "-------------------------------- \n"; | |
48 | std::cout << "Parsing failed\n"; | |
49 | std::cout << "-------------------------------- \n"; | |
50 | } | |
51 | return 0; | |
52 | } |