main.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*=============================================================================
  2. Copyright (c) 2002-2018 Joel de Guzman
  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. ///////////////////////////////////////////////////////////////////////////////
  7. //
  8. // This is the same employee parser (see employee.cpp) but structured to
  9. // allow separate compilation of the actual parser in its own definition
  10. // file (employee_def.hpp) and cpp file (employee.cpp). This main cpp file
  11. // sees only the header file (employee.hpp). This is a good example on how
  12. // parsers are structured in a C++ application.
  13. //
  14. // [ JDG May 9, 2007 ]
  15. // [ JDG May 13, 2015 ] spirit X3
  16. // [ JDG Feb 20, 2018 ] Minimal "best practice" example
  17. //
  18. // I would like to thank Rainbowverse, llc (https://primeorbial.com/)
  19. // for sponsoring this work and donating it to the community.
  20. //
  21. ///////////////////////////////////////////////////////////////////////////////
  22. #include "ast.hpp"
  23. #include "ast_adapted.hpp"
  24. #include "employee.hpp"
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // Main program
  27. ///////////////////////////////////////////////////////////////////////////////
  28. int
  29. main()
  30. {
  31. std::cout << "/////////////////////////////////////////////////////////\n\n";
  32. std::cout << "\t\tAn employee parser for Spirit...\n\n";
  33. std::cout << "/////////////////////////////////////////////////////////\n\n";
  34. std::cout
  35. << "Give me an employee of the form :"
  36. << "employee{age, \"forename\", \"surname\", salary } \n";
  37. std::cout << "Type [q or Q] to quit\n\n";
  38. using boost::spirit::x3::ascii::space;
  39. using iterator_type = std::string::const_iterator;
  40. using client::employee;
  41. std::string str;
  42. while (getline(std::cin, str))
  43. {
  44. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  45. break;
  46. client::ast::employee emp;
  47. iterator_type iter = str.begin();
  48. iterator_type const end = str.end();
  49. bool r = phrase_parse(iter, end, employee(), space, emp);
  50. if (r && iter == end)
  51. {
  52. std::cout << boost::fusion::tuple_open('[');
  53. std::cout << boost::fusion::tuple_close(']');
  54. std::cout << boost::fusion::tuple_delimiter(", ");
  55. std::cout << "-------------------------\n";
  56. std::cout << "Parsing succeeded\n";
  57. std::cout << "got: " << emp << std::endl;
  58. std::cout << "\n-------------------------\n";
  59. }
  60. else
  61. {
  62. std::cout << "-------------------------\n";
  63. std::cout << "Parsing failed\n";
  64. std::cout << "-------------------------\n";
  65. }
  66. }
  67. std::cout << "Bye... :-) \n\n";
  68. return 0;
  69. }