minimal.qbk 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. [/==============================================================================
  2. Copyright (C) 2001-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. I would like to thank Rainbowverse, llc (https://primeorbial.com/)
  6. for sponsoring this work and donating it to the community.
  7. ===============================================================================/]
  8. [section:minimal X3 Program Structure]
  9. As a prerequisite in understanding this tutorial, please review the previous
  10. [tutorial_employee employee example]. This example builds on top of that
  11. example.
  12. So far, to keep things simple, all of the tutorial programs are self
  13. contained in one cpp file. In reality, you will want to separate various
  14. logical modules of the parser into separate cpp and header files, decoupling
  15. the interface from the implememtation.
  16. There are many ways to structure an X3 parser, but the "minimal" example in
  17. this tutorial shows the preferred way. This example basically reuses the same
  18. parser as the [tutorial_employee employee example] for the sake of
  19. familiarity, but structured to allow separate compilation of the actual
  20. parser in its own definition file and cpp file. The cpp files, including main
  21. see only the header files --the interfaces. This is a good example on how X3
  22. parsers are structured in a C++ application.
  23. [heading Structure]
  24. The program is structured in a directory with the following header and cpp
  25. files:
  26. [table
  27. [[`File` ] [Description ]]
  28. [[[@../../../example/x3/minimal/ast.hpp ast.hpp]] [The AST ]]
  29. [[[@../../../example/x3/minimal/ast_adapted.hpp ast_adapted.hpp]] [Fusion adapters ]]
  30. [[[@../../../example/x3/minimal/config.hpp config.hpp]] [Configuration ]]
  31. [[[@../../../example/x3/minimal/employee.hpp employee.hpp]] [Main parser API ]]
  32. [[[@../../../example/x3/minimal/employee_def.hpp employee_def.hpp]] [Parser definitions ]]
  33. [[[@../../../example/x3/minimal/employee.cpp employee.cpp]] [Parser instantiation ]]
  34. [[[@../../../example/x3/minimal/main.cpp main.cpp]] [Main program ]]
  35. ]
  36. The contents of the files should already be familiar. It's essentially the
  37. same [tutorial_employee employee example]. So I will skip the details on how
  38. the parser works and focus only on the features needed for refactoring the
  39. program into a modular structure suitable for real-world deployment.
  40. [heading AST]
  41. We place the AST declaration here:
  42. namespace client { namespace ast
  43. {
  44. struct employee
  45. {
  46. int age;
  47. std::string forename;
  48. std::string surname;
  49. double salary;
  50. };
  51. using boost::fusion::operator<<;
  52. }}
  53. [heading Fusion adapters]
  54. Here, we adapt the AST for Fusion, making it a first-class fusion citizen:
  55. BOOST_FUSION_ADAPT_STRUCT(client::ast::employee,
  56. age, forename, surname, salary
  57. )
  58. [heading Main parser API]
  59. This is the main header file that all other cpp files need to include.
  60. [#__tutorial_spirit_declare__]
  61. [heading BOOST_SPIRIT_DECLARE]
  62. Remember [link __tutorial_spirit_define__ `BOOST_SPIRIT_DEFINE`]? If not,
  63. then you probably want to go back and review that section to get a better
  64. understanding of what's happening.
  65. Here in the header file, instead of `BOOST_SPIRIT_DEFINE`, we use
  66. `BOOST_SPIRIT_DECLARE` for the *top* rule. Behind the scenes, what's actually
  67. happening is that we are declaring a `parse_rule` function in the client
  68. namespace. For example, given a rule named `my_rule`,
  69. `BOOST_SPIRIT_DECLARE(my_rule)` expands to this code:
  70. template <typename Iterator, typename Context>
  71. bool parse_rule(
  72. decltype(my_rule)
  73. , Iterator& first, Iterator const& last
  74. , Context const& context, decltype(my_rule)::attribute_type& attr);
  75. If you went back and reviewed [link __tutorial_spirit_define__
  76. BOOST_SPIRIT_DEFINE], you'll see why it is exactly what we need to use for
  77. header files. `BOOST_SPIRIT_DECLARE` generates function declarations that are
  78. meant to be placed in hpp (header) files while `BOOST_SPIRIT_DEFINE`
  79. generates function definitions that are meant to be placed in cpp files.
  80. [note `BOOST_SPIRIT_DECLARE` is variadic and may be used for one or more rules.
  81. Example: `BOOST_SPIRIT_DECLARE(r1, r2, r3);`]
  82. In this example, the top rule is `employee`. We declare `employee` in this
  83. header file:
  84. namespace client
  85. {
  86. namespace parser
  87. {
  88. namespace x3 = boost::spirit::x3;
  89. using employee_type = x3::rule<class employee, ast::employee>;
  90. BOOST_SPIRIT_DECLARE(employee_type);
  91. }
  92. parser::employee_type employee();
  93. }
  94. We also provide a function that returns an `employee` object. This is the
  95. parser that we will use anywhere it is needed. X3 parser objects are very
  96. lightweight. They are basically simple tags with no data other than the name
  97. of the rule (e.g. "employee"). Notice that we are passing this by value.
  98. [heading Parser Definitions]
  99. Here is where we place the actual rules that make up our grammar:
  100. namespace parser
  101. {
  102. namespace x3 = boost::spirit::x3;
  103. namespace ascii = boost::spirit::x3::ascii;
  104. using x3::int_;
  105. using x3::lit;
  106. using x3::double_;
  107. using x3::lexeme;
  108. using ascii::char_;
  109. x3::rule<class employee, ast::employee> const employee = "employee";
  110. auto const quoted_string = lexeme['"' >> +(char_ - '"') >> '"'];
  111. auto const employee_def =
  112. lit("employee")
  113. >> '{'
  114. >> int_ >> ','
  115. >> quoted_string >> ','
  116. >> quoted_string >> ','
  117. >> double_
  118. >> '}'
  119. ;
  120. BOOST_SPIRIT_DEFINE(employee);
  121. }
  122. parser::employee_type employee()
  123. {
  124. return parser::employee;
  125. }
  126. In the parser definition, we use [link __tutorial_spirit_define__
  127. `BOOST_SPIRIT_DEFINE`] just like we did in the [tutorial_employee employee
  128. example].
  129. While this is another header file, it is not meant to be included by the
  130. client. Its purpose is to be included by an instantiations cpp file (see
  131. below). We place this in an `.hpp` file for flexibility, so we have the
  132. freedom to instantiate the parser with different iterator types.
  133. [#tutorial_configuration]
  134. [heading Configuration]
  135. Here, we declare some types for instatntaiting our X3 parser with. Rememeber
  136. that Spirit parsers can work with any __fwditer__. We'll also need to provide
  137. the initial context type. This is the context that X3 will use to initiate a
  138. parse. For calling `phrase_parse`, you will need the `phrase_parse_context`
  139. like we do below, passing in the skipper type.
  140. using iterator_type = std::string::const_iterator;
  141. using context_type = x3::phrase_parse_context<x3::ascii::space_type>::type;
  142. For plain `parse`, we simply use `x3::unused_type`.
  143. [heading Parser Instantiation]
  144. Now we instantiate our parser here, for our specific configuration:
  145. namespace client { namespace parser
  146. {
  147. BOOST_SPIRIT_INSTANTIATE(employee_type, iterator_type, context_type);
  148. }}
  149. For that, we use `BOOST_SPIRIT_INSTANTIATE`, passing in the parser type,
  150. the iterator type, and the context type.
  151. [heading BOOST_SPIRIT_INSTANTIATE]
  152. Go back and review [link __tutorial_spirit_define__ `BOOST_SPIRIT_DEFINE`]
  153. and [link __tutorial_spirit_declare__ `BOOST_SPIRIT_DECLARE`] to get a better
  154. grasp of what's happening with `BOOST_SPIRIT_INSTANTIATE` and why it is
  155. needed.
  156. So what the heck is `BOOST_SPIRIT_INSTANTIATE`? What we want is to isolate
  157. the instantiation of our parsers (rules and all that), into separate
  158. translation units (or cpp files, if you will). In this example, we want to
  159. place our x3 employee stuff in [@../../../example/x3/minimal/employee.cpp
  160. employee.cpp]. That way, we have separate compilation. Every time we update
  161. our employee parser source code, we only have to build the `employee.cpp`
  162. file. All the rest will not be affected. By compiling only once in one
  163. translation unit, we save on build times and avoid code bloat. There is no
  164. code duplication, which can happen otherwise if you simply include the
  165. employee parser ([@../../../example/x3/minimal/employee.hpp employee.hpp])
  166. everywhere.
  167. But how do you do that. Remember that our parser definitions are also placed
  168. in its own header file for flexibility, so we have the freedom to instantiate
  169. the parser with different iterator types.
  170. What we need to do is explicitly instantiate the `parse_rule` function we
  171. declared and defined via `BOOST_SPIRIT_DECLARE` and `BOOST_SPIRIT_DEFINE`
  172. respectively, using `BOOST_SPIRIT_INSTANTIATE`. For our particular example,
  173. `BOOST_SPIRIT_INSTANTIATE` expands to this code:
  174. template bool parse_rule<iterator_type, context_type>(
  175. employee_type rule_
  176. , iterator_type& first, iterator_type const& last
  177. , context_type const& context, employee_type::attribute_type& attr);
  178. [heading Main Program]
  179. Finally, we have our main program. The code is the same as single cpp file
  180. [tutorial_employee employee example], but here, we simply include three
  181. header files:
  182. #include "ast.hpp"
  183. #include "ast_adapted.hpp"
  184. #include "employee.hpp"
  185. # `ast.hpp` for the AST declaration
  186. # `ast_adapted.hpp` if you need to traverse the AST using fusion
  187. # `employee.hpp` the main parser API
  188. [endsect]