1 /*============================================================================= 2 Copyright (c) 2002 Jeff Westfahl 3 http://spirit.sourceforge.net/ 4 5 Use, modification and distribution is subject to the Boost Software 6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 7 http://www.boost.org/LICENSE_1_0.txt) 8 =============================================================================*/ 9 /////////////////////////////////////////////////////////////////////////////// 10 // 11 // A parser that echoes a file 12 // See the "File Iterator" chapter in the User's Guide. 13 // 14 // [ JMW 8/05/2002 ] 15 // 16 /////////////////////////////////////////////////////////////////////////////// 17 18 #include <boost/spirit/include/classic_core.hpp> 19 #include <boost/spirit/include/classic_file_iterator.hpp> 20 #include <iostream> 21 22 /////////////////////////////////////////////////////////////////////////////// 23 using namespace BOOST_SPIRIT_CLASSIC_NS; 24 25 //////////////////////////////////////////////////////////////////////////// 26 // 27 // Types 28 // 29 //////////////////////////////////////////////////////////////////////////// 30 typedef char char_t; 31 typedef file_iterator<char_t> iterator_t; 32 typedef scanner<iterator_t> scanner_t; 33 typedef rule<scanner_t> rule_t; 34 35 //////////////////////////////////////////////////////////////////////////// 36 // 37 // Actions 38 // 39 //////////////////////////////////////////////////////////////////////////// echo(iterator_t first,iterator_t const & last)40void echo(iterator_t first, iterator_t const& last) 41 { 42 while (first != last) 43 std::cout << *first++; 44 } 45 46 //////////////////////////////////////////////////////////////////////////// 47 // 48 // Main program 49 // 50 //////////////////////////////////////////////////////////////////////////// 51 int main(int argc,char * argv[])52main(int argc, char* argv[]) 53 { 54 if (2 > argc) 55 { 56 std::cout << "Must specify a filename!\n"; 57 return -1; 58 } 59 60 // Create a file iterator for this file 61 iterator_t first(argv[1]); 62 63 if (!first) 64 { 65 std::cout << "Unable to open file!\n"; 66 return -1; 67 } 68 69 // Create an EOF iterator 70 iterator_t last = first.make_end(); 71 72 // A simple rule 73 rule_t r = *(anychar_p); 74 75 // Parse 76 parse_info <iterator_t> info = parse( 77 first, 78 last, 79 r[&echo] 80 ); 81 82 // This really shouldn't fail... 83 if (info.full) 84 std::cout << "Parse succeeded!\n"; 85 else 86 std::cout << "Parse failed!\n"; 87 88 return 0; 89 } 90