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