file_parser.cpp 2.4 KB

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