Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > b9ba69a436161613d8fb030c8c726a8e > files > 643

spirit-1.5.1-2mdk.noarch.rpm

///////////////////////////////////////////////////////////////////////////////
//
//  A parser that echoes a file
//
//  [ JMW 8/05/2002 ]
//
///////////////////////////////////////////////////////////////////////////////

#include <boost/spirit/core.hpp>
#include <boost/spirit/iterator/file_iterator.hpp>
#include <iostream>

///////////////////////////////////////////////////////////////////////////////

using namespace spirit;


////////////////////////////////////////////////////////////////////////////
//
//  Types
//
////////////////////////////////////////////////////////////////////////////

typedef char                  CharT;
typedef file_iterator <CharT> IteratorT;
typedef scanner <IteratorT>   ScannerT;
typedef rule <ScannerT>       RuleT;


////////////////////////////////////////////////////////////////////////////
//
//  Actions
//
////////////////////////////////////////////////////////////////////////////

void Echo (IteratorT const & rBegin, IteratorT const & rEnd)
{
   while (rBegin < rEnd)
   {
      std::cout << *rBegin++;
   }
}


////////////////////////////////////////////////////////////////////////////
//
//  Main program
//
////////////////////////////////////////////////////////////////////////////

int main (int argc, char * argv [])
{
   if (2 > argc)
   {
      std::cout << "Must specify a filename!\n";

      return -1;
   }

   // Create a file iterator for this file
   IteratorT _Start (argv [1]);

   if (!_Start)
   {
      std::cout << "Unable to open file!\n";

      return -1;
   }

   // Create an EOF iterator
   IteratorT _End = _Start.make_end ();

   // A simple rule
   RuleT _Rule = *(anychar_p);

   // Parse
   parse_info <IteratorT> _Info = parse (
      _Start,
      _End,
      _Rule [&Echo]
      );

   // This really shouldn't fail...
   if (_Info.full)
   {
      std::cout << "Parse succeeded!\n";
   }
   else
   {
      std::cout << "Parse failed!\n";
   }

   return 0;
}