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 #include <boost/spirit/repository/include/qi_iter_pos.hpp> 16 17 #include <string> 18 19 namespace qi = boost::spirit::qi; 20 main()21int main() 22 { 23 using boost::spirit::repository::qi::iter_pos; 24 25 std::string prefix, suffix; // attributes receiving the 26 std::string::iterator position; // parsed values 27 28 std::string input("prefix1234567"); 29 std::string::iterator first = input.begin(); 30 bool result = 31 qi::parse(first, input.end() 32 , +qi::alpha >> iter_pos >> +qi::digit 33 , prefix, position, suffix); 34 35 if (result) 36 { 37 std::cout << "-------------------------------- \n"; 38 std::cout << "Parsing succeeded\n"; 39 std::cout << "prefix is: " << prefix << "\n"; 40 std::cout << "suffix is: " << suffix << "\n"; 41 std::cout << "position is: " << std::distance(input.begin(), position) << "\n"; 42 std::cout << "-------------------------------- \n"; 43 } 44 else 45 { 46 std::cout << "-------------------------------- \n"; 47 std::cout << "Parsing failed\n"; 48 std::cout << "-------------------------------- \n"; 49 } 50 return 0; 51 } 52