parse_sexpr.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*==============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Copyright (c) 2010-2011 Bryce Lelbach
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file BOOST_LICENSE_1_0.rst or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <boost/spirit/include/support_istream_iterator.hpp>
  9. #include <boost/spirit/include/support_line_pos_iterator.hpp>
  10. #include <boost/spirit/include/qi_parse.hpp>
  11. #include "sexpr_parser.hpp"
  12. int
  13. main()
  14. {
  15. using boost::spirit::qi::phrase_parse;
  16. std::cout << "/////////////////////////////////////////////////////////\n\n";
  17. std::cout << "sexpr parser...\n\n";
  18. std::cout << "/////////////////////////////////////////////////////////\n\n";
  19. std::cout << "Type an expression... or [q or Q] to quit\n\n";
  20. typedef std::string::const_iterator iterator_type;
  21. typedef sexpr::parser<iterator_type> parser;
  22. typedef sexpr::whitespace<iterator_type> space;
  23. parser p;
  24. space ws;
  25. std::string str;
  26. while (std::getline(std::cin, str))
  27. {
  28. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  29. break;
  30. std::string::const_iterator iter = str.begin();
  31. std::string::const_iterator end = str.end();
  32. bool r = phrase_parse(iter, end, p, ws);
  33. if (r && iter == end)
  34. {
  35. std::cout << "-------------------------\n";
  36. std::cout << "Parsing succeeded\n";
  37. std::cout << "-------------------------\n";
  38. }
  39. else
  40. {
  41. std::string rest(iter, end);
  42. std::cout << "-------------------------\n";
  43. std::cout << "Parsing failed\n";
  44. std::cout << "stopped at: \": " << rest << "\"\n";
  45. std::cout << "-------------------------\n";
  46. }
  47. }
  48. std::cout << "Bye... :-) \n\n";
  49. return 0;
  50. }