confix.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Hartmut Kaiser
  3. http://spirit.sourceforge.net/
  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. #ifndef BOOST_SPIRIT_CONFIX_HPP
  8. #define BOOST_SPIRIT_CONFIX_HPP
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include <boost/config.hpp>
  11. #include <boost/spirit/home/classic/namespace.hpp>
  12. #include <boost/spirit/home/classic/meta/as_parser.hpp>
  13. #include <boost/spirit/home/classic/core/composite/operators.hpp>
  14. #include <boost/spirit/home/classic/utility/confix_fwd.hpp>
  15. #include <boost/spirit/home/classic/utility/impl/confix.ipp>
  16. ///////////////////////////////////////////////////////////////////////////////
  17. namespace boost { namespace spirit {
  18. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  19. ///////////////////////////////////////////////////////////////////////////////
  20. //
  21. // confix_parser class
  22. //
  23. // Parses a sequence of 3 sub-matches. This class may
  24. // be used to parse structures, where the opening part is possibly
  25. // contained in the expression part and the whole sequence is only
  26. // parsed after seeing the closing part matching the first opening
  27. // subsequence. Example: C-comments:
  28. //
  29. // /* This is a C-comment */
  30. //
  31. ///////////////////////////////////////////////////////////////////////////////
  32. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  33. #pragma warning(push)
  34. #pragma warning(disable:4512) //assignment operator could not be generated
  35. #endif
  36. template<typename NestedT = non_nested, typename LexemeT = non_lexeme>
  37. struct confix_parser_gen;
  38. template <
  39. typename OpenT, typename ExprT, typename CloseT, typename CategoryT,
  40. typename NestedT, typename LexemeT
  41. >
  42. struct confix_parser :
  43. public parser<
  44. confix_parser<OpenT, ExprT, CloseT, CategoryT, NestedT, LexemeT>
  45. >
  46. {
  47. typedef
  48. confix_parser<OpenT, ExprT, CloseT, CategoryT, NestedT, LexemeT>
  49. self_t;
  50. confix_parser(OpenT const &open_, ExprT const &expr_, CloseT const &close_)
  51. : open(open_), expr(expr_), close(close_)
  52. {}
  53. template <typename ScannerT>
  54. typename parser_result<self_t, ScannerT>::type
  55. parse(ScannerT const& scan) const
  56. {
  57. return impl::confix_parser_type<CategoryT>::
  58. parse(NestedT(), LexemeT(), *this, scan, open, expr, close);
  59. }
  60. private:
  61. typename as_parser<OpenT>::type::embed_t open;
  62. typename as_parser<ExprT>::type::embed_t expr;
  63. typename as_parser<CloseT>::type::embed_t close;
  64. };
  65. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  66. #pragma warning(pop)
  67. #endif
  68. ///////////////////////////////////////////////////////////////////////////////
  69. //
  70. // Confix parser generator template
  71. //
  72. // This is a helper for generating a correct confix_parser<> from
  73. // auxiliary parameters. There are the following types supported as
  74. // parameters yet: parsers, single characters and strings (see
  75. // as_parser).
  76. //
  77. // If the body parser is an action_parser_category type parser (a parser
  78. // with an attached semantic action) we have to do something special. This
  79. // happens, if the user wrote something like:
  80. //
  81. // confix_p(open, body[f], close)
  82. //
  83. // where 'body' is the parser matching the body of the confix sequence
  84. // and 'f' is a functor to be called after matching the body. If we would
  85. // do nothing, the resulting code would parse the sequence as follows:
  86. //
  87. // start >> (body[f] - close) >> close
  88. //
  89. // what in most cases is not what the user expects.
  90. // (If this _is_ what you've expected, then please use the confix_p
  91. // generator function 'direct()', which will inhibit
  92. // re-attaching the actor to the body parser).
  93. //
  94. // To make the confix parser behave as expected:
  95. //
  96. // start >> (body - close)[f] >> close
  97. //
  98. // the actor attached to the 'body' parser has to be re-attached to the
  99. // (body - close) parser construct, which will make the resulting confix
  100. // parser 'do the right thing'. This refactoring is done by the help of
  101. // the refactoring parsers (see the files refactoring.[hi]pp).
  102. //
  103. // Additionally special care must be taken, if the body parser is a
  104. // unary_parser_category type parser as
  105. //
  106. // confix_p(open, *anychar_p, close)
  107. //
  108. // which without any refactoring would result in
  109. //
  110. // start >> (*anychar_p - close) >> close
  111. //
  112. // and will not give the expected result (*anychar_p will eat up all the
  113. // input up to the end of the input stream). So we have to refactor this
  114. // into:
  115. //
  116. // start >> *(anychar_p - close) >> close
  117. //
  118. // what will give the correct result.
  119. //
  120. // The case, where the body parser is a combination of the two mentioned
  121. // problems (i.e. the body parser is a unary parser with an attached
  122. // action), is handled accordingly too:
  123. //
  124. // confix_p(start, (*anychar_p)[f], end)
  125. //
  126. // will be parsed as expected:
  127. //
  128. // start >> (*(anychar_p - end))[f] >> end.
  129. //
  130. ///////////////////////////////////////////////////////////////////////////////
  131. template<typename NestedT, typename LexemeT>
  132. struct confix_parser_gen
  133. {
  134. // Generic generator function for creation of concrete confix parsers
  135. template<typename StartT, typename ExprT, typename EndT>
  136. struct paren_op_result_type
  137. {
  138. typedef confix_parser<
  139. typename as_parser<StartT>::type,
  140. typename as_parser<ExprT>::type,
  141. typename as_parser<EndT>::type,
  142. typename as_parser<ExprT>::type::parser_category_t,
  143. NestedT,
  144. LexemeT
  145. > type;
  146. };
  147. template<typename StartT, typename ExprT, typename EndT>
  148. typename paren_op_result_type<StartT, ExprT, EndT>::type
  149. operator()(StartT const &start_, ExprT const &expr_, EndT const &end_) const
  150. {
  151. typedef typename paren_op_result_type<StartT,ExprT,EndT>::type
  152. return_t;
  153. return return_t(
  154. as_parser<StartT>::convert(start_),
  155. as_parser<ExprT>::convert(expr_),
  156. as_parser<EndT>::convert(end_)
  157. );
  158. }
  159. // Generic generator function for creation of concrete confix parsers
  160. // which have an action directly attached to the ExprT part of the
  161. // parser (see comment above, no automatic refactoring)
  162. template<typename StartT, typename ExprT, typename EndT>
  163. struct direct_result_type
  164. {
  165. typedef confix_parser<
  166. typename as_parser<StartT>::type,
  167. typename as_parser<ExprT>::type,
  168. typename as_parser<EndT>::type,
  169. plain_parser_category, // do not re-attach action
  170. NestedT,
  171. LexemeT
  172. > type;
  173. };
  174. template<typename StartT, typename ExprT, typename EndT>
  175. typename direct_result_type<StartT,ExprT,EndT>::type
  176. direct(StartT const &start_, ExprT const &expr_, EndT const &end_) const
  177. {
  178. typedef typename direct_result_type<StartT,ExprT,EndT>::type
  179. return_t;
  180. return return_t(
  181. as_parser<StartT>::convert(start_),
  182. as_parser<ExprT>::convert(expr_),
  183. as_parser<EndT>::convert(end_)
  184. );
  185. }
  186. };
  187. ///////////////////////////////////////////////////////////////////////////////
  188. //
  189. // Predefined non_nested confix parser generators
  190. //
  191. ///////////////////////////////////////////////////////////////////////////////
  192. const confix_parser_gen<non_nested, non_lexeme> confix_p =
  193. confix_parser_gen<non_nested, non_lexeme>();
  194. ///////////////////////////////////////////////////////////////////////////////
  195. //
  196. // Comments are special types of confix parsers
  197. //
  198. // Comment parser generator template. This is a helper for generating a
  199. // correct confix_parser<> from auxiliary parameters, which is able to
  200. // parse comment constructs: (StartToken >> Comment text >> EndToken).
  201. //
  202. // There are the following types supported as parameters yet: parsers,
  203. // single characters and strings (see as_parser).
  204. //
  205. // There are two diffenerent predefined comment parser generators
  206. // (comment_p and comment_nest_p, see below), which may be used for
  207. // creating special comment parsers in two different ways.
  208. //
  209. // If these are used with one parameter, a comment starting with the given
  210. // first parser parameter up to the end of the line is matched. So for
  211. // instance the following parser matches C++ style comments:
  212. //
  213. // comment_p("//").
  214. //
  215. // If these are used with two parameters, a comment starting with the
  216. // first parser parameter up to the second parser parameter is matched.
  217. // For instance a C style comment parser should be constrcuted as:
  218. //
  219. // comment_p("/*", "*/").
  220. //
  221. // Please note, that a comment is parsed implicitly as if the whole
  222. // comment_p(...) statement were embedded into a lexeme_d[] directive.
  223. //
  224. ///////////////////////////////////////////////////////////////////////////////
  225. template<typename NestedT>
  226. struct comment_parser_gen
  227. {
  228. // Generic generator function for creation of concrete comment parsers
  229. // from an open token. The newline parser eol_p is used as the
  230. // closing token.
  231. template<typename StartT>
  232. struct paren_op1_result_type
  233. {
  234. typedef confix_parser<
  235. typename as_parser<StartT>::type,
  236. kleene_star<anychar_parser>,
  237. alternative<eol_parser, end_parser>,
  238. unary_parser_category, // there is no action to re-attach
  239. NestedT,
  240. is_lexeme // insert implicit lexeme_d[]
  241. >
  242. type;
  243. };
  244. template<typename StartT>
  245. typename paren_op1_result_type<StartT>::type
  246. operator() (StartT const &start_) const
  247. {
  248. typedef typename paren_op1_result_type<StartT>::type
  249. return_t;
  250. return return_t(
  251. as_parser<StartT>::convert(start_),
  252. *anychar_p,
  253. eol_p | end_p
  254. );
  255. }
  256. // Generic generator function for creation of concrete comment parsers
  257. // from an open and a close tokens.
  258. template<typename StartT, typename EndT>
  259. struct paren_op2_result_type
  260. {
  261. typedef confix_parser<
  262. typename as_parser<StartT>::type,
  263. kleene_star<anychar_parser>,
  264. typename as_parser<EndT>::type,
  265. unary_parser_category, // there is no action to re-attach
  266. NestedT,
  267. is_lexeme // insert implicit lexeme_d[]
  268. > type;
  269. };
  270. template<typename StartT, typename EndT>
  271. typename paren_op2_result_type<StartT,EndT>::type
  272. operator() (StartT const &start_, EndT const &end_) const
  273. {
  274. typedef typename paren_op2_result_type<StartT,EndT>::type
  275. return_t;
  276. return return_t(
  277. as_parser<StartT>::convert(start_),
  278. *anychar_p,
  279. as_parser<EndT>::convert(end_)
  280. );
  281. }
  282. };
  283. ///////////////////////////////////////////////////////////////////////////////
  284. //
  285. // Predefined non_nested comment parser generator
  286. //
  287. ///////////////////////////////////////////////////////////////////////////////
  288. const comment_parser_gen<non_nested> comment_p =
  289. comment_parser_gen<non_nested>();
  290. ///////////////////////////////////////////////////////////////////////////////
  291. //
  292. // comment_nest_parser class
  293. //
  294. // Parses a nested comments.
  295. // Example: nested PASCAL-comments:
  296. //
  297. // { This is a { nested } PASCAL-comment }
  298. //
  299. ///////////////////////////////////////////////////////////////////////////////
  300. template<typename OpenT, typename CloseT>
  301. struct comment_nest_parser:
  302. public parser<comment_nest_parser<OpenT, CloseT> >
  303. {
  304. typedef comment_nest_parser<OpenT, CloseT> self_t;
  305. comment_nest_parser(OpenT const &open_, CloseT const &close_):
  306. open(open_), close(close_)
  307. {}
  308. template<typename ScannerT>
  309. typename parser_result<self_t, ScannerT>::type
  310. parse(ScannerT const &scan) const
  311. {
  312. return do_parse(
  313. open >> *(*this | (anychar_p - close)) >> close,
  314. scan);
  315. }
  316. private:
  317. template<typename ParserT, typename ScannerT>
  318. typename parser_result<self_t, ScannerT>::type
  319. do_parse(ParserT const &p, ScannerT const &scan) const
  320. {
  321. return
  322. impl::contiguous_parser_parse<
  323. typename parser_result<ParserT, ScannerT>::type
  324. >(p, scan, scan);
  325. }
  326. typename as_parser<OpenT>::type::embed_t open;
  327. typename as_parser<CloseT>::type::embed_t close;
  328. };
  329. ///////////////////////////////////////////////////////////////////////////////
  330. //
  331. // Predefined nested comment parser generator
  332. //
  333. ///////////////////////////////////////////////////////////////////////////////
  334. template<typename OpenT, typename CloseT>
  335. struct comment_nest_p_result
  336. {
  337. typedef comment_nest_parser<
  338. typename as_parser<OpenT>::type,
  339. typename as_parser<CloseT>::type
  340. > type;
  341. };
  342. template<typename OpenT, typename CloseT>
  343. inline typename comment_nest_p_result<OpenT,CloseT>::type
  344. comment_nest_p(OpenT const &open, CloseT const &close)
  345. {
  346. typedef typename comment_nest_p_result<OpenT,CloseT>::type
  347. result_t;
  348. return result_t(
  349. as_parser<OpenT>::convert(open),
  350. as_parser<CloseT>::convert(close)
  351. );
  352. }
  353. ///////////////////////////////////////////////////////////////////////////////
  354. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  355. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  356. #endif