parse_api.qbk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2001-2011 Hartmut Kaiser
  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. [section:parse_api Parser API]
  8. [//////////////////////////////////////////////////////////////////////////////]
  9. [section:iterator_api Iterator Based Parser API]
  10. [heading Description]
  11. The library provides a couple of free functions to make parsing a snap.
  12. These parser functions have two forms. The first form `parse` works on
  13. the character level. The second `phrase_parse` works on the phrase level
  14. and requires skip parser. Both versions can take in attributes by
  15. reference that will hold the parsed values on a successful parse.
  16. [heading Header]
  17. // forwards to <boost/spirit/home/qi/parse.hpp>
  18. #include <boost/spirit/include/qi_parse.hpp>
  19. For variadic attributes:
  20. // forwards to <boost/spirit/home/qi/parse_attr.hpp>
  21. #include <boost/spirit/include/qi_parse_attr.hpp>
  22. The variadic attributes version of the API allows one or more
  23. attributes to be passed into the parse functions. The functions taking two
  24. or more are usable when the parser expression is a __qi_sequence__ only.
  25. In this case each of the attributes passed have to match the corresponding
  26. part of the sequence.
  27. For the API functions deducing the correct (matching) parser type from the
  28. supplied attribute type:
  29. // forwards to <boost/spirit/home/qi/detail/parse_auto.hpp>
  30. #include <boost/spirit/include/qi_parse_auto.hpp>
  31. Also, see __include_structure__.
  32. [heading Namespace]
  33. [table
  34. [[Name]]
  35. [[`boost::spirit::qi::parse` ]]
  36. [[`boost::spirit::qi::phrase_parse` ]]
  37. [[`boost::spirit::qi::skip_flag::postskip` ]]
  38. [[`boost::spirit::qi::skip_flag::dont_postskip` ]]
  39. ]
  40. [heading Synopsis]
  41. namespace boost { namespace spirit { namespace qi
  42. {
  43. template <typename Iterator, typename Expr>
  44. inline bool
  45. parse(
  46. Iterator& first
  47. , Iterator last
  48. , Expr const& expr);
  49. template <typename Iterator, typename Expr
  50. , typename Attr1, typename Attr2, ..., typename AttrN>
  51. inline bool
  52. parse(
  53. Iterator& first
  54. , Iterator last
  55. , Expr const& expr
  56. , Attr1& attr1, Attr2& attr2, ..., AttrN& attrN);
  57. template <typename Iterator, typename Expr, typename Skipper>
  58. inline bool
  59. phrase_parse(
  60. Iterator& first
  61. , Iterator last
  62. , Expr const& expr
  63. , Skipper const& skipper
  64. , BOOST_SCOPED_ENUM(skip_flag) post_skip = skip_flag::postskip);
  65. template <typename Iterator, typename Expr, typename Skipper
  66. , typename Attr1, typename Attr2, ..., typename AttrN>
  67. inline bool
  68. phrase_parse(
  69. Iterator& first
  70. , Iterator last
  71. , Expr const& expr
  72. , Skipper const& skipper
  73. , Attr1& attr1, Attr2& attr2, ..., AttrN& attrN);
  74. template <typename Iterator, typename Expr, typename Skipper
  75. , typename Attr1, typename Attr2, ..., typename AttrN>
  76. inline bool
  77. phrase_parse(
  78. Iterator& first
  79. , Iterator last
  80. , Expr const& expr
  81. , Skipper const& skipper
  82. , BOOST_SCOPED_ENUM(skip_flag) post_skip
  83. , Attr1& attr1, Attr2& attr2, ..., AttrN& attrN);
  84. }}}
  85. [note Starting with __spirit__ V2.5 (distributed with Boost V1.47) the
  86. placeholder `_val` can be used in semantic actions attached to top level
  87. parser components. In this case `_val` refers to the supplied attribute
  88. as a whole. For API functions taking more than one attribute argument
  89. `_val` will refer to a Fusion vector or references to the attributes.]
  90. __qi__ parser API functions based on the automatic creation of the matching
  91. parser type:
  92. namespace boost { namespace spirit { namespace qi
  93. {
  94. template <typename Iterator, typename Attr>
  95. inline bool
  96. parse(
  97. Iterator& first
  98. , Iterator last
  99. , Attr& attr);
  100. template <typename Iterator, typename Attr, typename Skipper>
  101. inline bool
  102. phrase_parse(
  103. Iterator& first
  104. , Iterator last
  105. , Attr& attr
  106. , Skipper const& skipper
  107. , BOOST_SCOPED_ENUM(skip_flag) post_skip = skip_flag::postskip);
  108. }}}
  109. All functions above return `true` if none of the involved parser components
  110. failed, and `false` otherwise.
  111. The maximum number of supported arguments is limited by the preprocessor
  112. constant `SPIRIT_ARGUMENTS_LIMIT`. This constant defaults to the value defined
  113. by the preprocessor constant `PHOENIX_LIMIT` (which in turn defaults to `10`).
  114. [note The variadic functions with two or more attributes internally combine
  115. references to all passed attributes into a `fusion::vector` and forward
  116. this as a combined attribute to the corresponding one attribute function.]
  117. The `phrase_parse` functions not taking an explicit `skip_flag` as one of their
  118. arguments invoke the passed skipper after a successful match of the parser
  119. expression. This can be inhibited by using the other versions of that function
  120. while passing `skip_flag::dont_postskip` to the corresponding argument.
  121. [table
  122. [[Parameter] [Description]]
  123. [[`Iterator`] [__fwditer__ pointing to the source to parse.]]
  124. [[`Expr`] [An expression that can be converted to a Qi parser.]]
  125. [[`Skipper`] [Parser used to skip white spaces.]]
  126. [[`Attr`] [An attribute type utilized to create the corresponding
  127. parser type from.]]
  128. [[`Attr1`, `Attr2`, ..., `AttrN`][One or more attributes.]]
  129. ]
  130. [endsect] [/ Iterator Based Parser API]
  131. [//////////////////////////////////////////////////////////////////////////////]
  132. [section:stream_api Stream Based Parser API]
  133. [heading Description]
  134. The library provides a couple of Standard IO __iomanip__ allowing to integrate
  135. __qi__ input parsing facilities with Standard input streams.
  136. These parser manipulators have two forms. The first form, `match`, works on
  137. the character level. The second `phrase_match` works on the phrase level
  138. and requires a skip parser. Both versions can take in attributes by reference
  139. that will hold the parsed values on a successful parse.
  140. [heading Header]
  141. // forwards to <boost/spirit/home/qi/stream/match_manip.hpp>
  142. #include <boost/spirit/include/qi_match.hpp>
  143. For variadic attributes:
  144. // forwards to <boost/spirit/home/qi/stream/match_manip_attr.hpp>
  145. #include <boost/spirit/include/qi_match_attr.hpp>
  146. The variadic attributes version of the API allows one or more
  147. attributes to be passed into the parse manipulators. The manipulators taking
  148. two or more attributes are usable when the parser expression is a
  149. __qi_sequence__ only. In this case each of the attributes passed have to
  150. match the corresponding part of the sequence.
  151. For the API functions deducing the correct (matching) parser type from the
  152. supplied attribute type:
  153. // forwards to <boost/spirit/home/qi/match_auto.hpp>
  154. #include <boost/spirit/include/qi_match_auto.hpp>
  155. Also, see __include_structure__.
  156. [heading Namespace]
  157. [table
  158. [[Name]]
  159. [[`boost::spirit::qi::match` ]]
  160. [[`boost::spirit::qi::phrase_match` ]]
  161. [[`boost::spirit::qi::skip_flag::postskip` ]]
  162. [[`boost::spirit::qi::skip_flag::dont_postskip` ]]
  163. ]
  164. [heading Synopsis]
  165. namespace boost { namespace spirit { namespace qi
  166. {
  167. template <typename Expr>
  168. inline <unspecified>
  169. match(
  170. Expr const& xpr);
  171. template <typename Expr
  172. , typename Attr1, typename Attr2, ..., typename AttrN>
  173. inline <unspecified>
  174. match(
  175. Expr const& xpr
  176. , Attr1& attr1, Attr2& attr2, ..., AttrN& attrN);
  177. template <typename Expr, typename Skipper>
  178. inline <unspecified>
  179. phrase_match(
  180. Expr const& expr
  181. , Skipper const& s
  182. , BOOST_SCOPED_ENUM(skip_flag) post_skip = skip_flag::postskip);
  183. template <typename Expr, typename Skipper
  184. , typename Attr1, typename Attr2, ..., typename AttrN>
  185. inline <unspecified>
  186. phrase_match(
  187. Expr const& expr
  188. , Skipper const& s
  189. , Attr1& attr1, Attr2& attr2, ..., AttrN& attrN);
  190. template <typename Expr, typename Skipper
  191. , typename Attr1, typename Attr2, ..., typename AttrN>
  192. inline <unspecified>
  193. phrase_match(
  194. Expr const& expr
  195. , Skipper const& s
  196. , BOOST_SCOPED_ENUM(skip_flag) post_skip
  197. , Attr1& attr1, Attr2& attr2, ..., AttrN& attrN);
  198. }}}
  199. __qi__ parser API functions based on the automatic creation of the matching
  200. parser type:
  201. namespace boost { namespace spirit { namespace qi
  202. {
  203. template <typename Attr>
  204. inline <unspecified>
  205. match(
  206. Attr& attr);
  207. template <typename Attr, typename Skipper>
  208. inline <unspecified>
  209. phrase_match(
  210. Attr& attr
  211. , Skipper const& s
  212. , BOOST_SCOPED_ENUM(skip_flag) post_skip = skip_flag::postskip);
  213. }}}
  214. All functions above return a standard IO stream manipulator instance (see
  215. __iomanip__), which when streamed from an input stream will result in parsing
  216. the input using the embedded __qi__ parser expression. Any error (or failed
  217. parse) occurring during the invocation of the __qi__ parsers will be reflected
  218. in the streams status flag (`std::ios_base::failbit` will be set).
  219. The maximum number of supported arguments is limited by the preprocessor
  220. constant `SPIRIT_ARGUMENTS_LIMIT`. This constant defaults to the value defined
  221. by the preprocessor constant `PHOENIX_LIMIT` (which in turn defaults to `10`).
  222. [note The variadic manipulators with two or more attributes internally combine
  223. references to all passed attributes into a `fusion::vector`
  224. and forward this as a combined attribute to the corresponding manipulator
  225. taking one attribute.]
  226. The `phrase_match` manipulators not taking an explicit `skip_flag` as one of their
  227. arguments invoke the passed skipper after a successful match of the parser
  228. expression. This can be inhibited by using the other versions of that manipulator
  229. while passing `skip_flag::dont_postskip` to the corresponding argument.
  230. [heading Template parameters]
  231. [table
  232. [[Parameter] [Description]]
  233. [[`Expr`] [An expression that can be converted to a Qi parser.]]
  234. [[`Skipper`] [Parser used to skip white spaces.]]
  235. [[`Attr`] [An attribute type utilized to create the corresponding
  236. parser type from.]]
  237. [[`Attr1`, `Attr2`, ..., `AttrN`][One or more attributes.]]
  238. ]
  239. [endsect] [/ Stream Based Parser API]
  240. [//////////////////////////////////////////////////////////////////////////////]
  241. [section:create_parser API for Automatic Parser Creation]
  242. [heading Description]
  243. The library implements a special API returning a parser instance for a
  244. supplied attribute type. This function finds the best matching parser type
  245. for the attribute based on a set of simple matching rules (as outlined in the
  246. table below) applied recursively to the attribute type. The returned parser
  247. can be utilized to match input for the provided attribute.
  248. [heading Header]
  249. // forwards to <boost/spirit/home/qi/auto.hpp>
  250. #include <boost/spirit/include/qi_auto.hpp>
  251. Also, see __include_structure__.
  252. [heading Namespace]
  253. [table
  254. [[Name]]
  255. [[`boost::spirit::qi::create_parser`]]
  256. [[`boost::spirit::traits::create_parser_exists`]]
  257. ]
  258. [heading Synopsis]
  259. namespace boost { namespace spirit { namespace qi
  260. {
  261. template <typename Attr>
  262. inline <unspecified>
  263. create_parser();
  264. }}}
  265. The returned instance can be directly passed as the parser (or the skipping
  266. parser) to any of the __qi__ API functions. Additionally it
  267. can be assigned to a rule as the rules right hand side expression. This
  268. function will return a valid parser type only if the meta function
  269. `traits::create_parser_exists` returns `mpl::true_`. Otherwise it will fail
  270. compiling.
  271. namespace boost { namespace spirit { namespace traits
  272. {
  273. template <typename Attr>
  274. struct create_parser_exists;
  275. }}}
  276. The meta function evaluates to `mpl::true_` if `create_parser` would return
  277. a valid parser for the given type `Attr`.
  278. The following table outlines the mapping rules from the attribute type to the
  279. parser type. These rules are applied recursively to create the parser
  280. type which can be used to match input for the given attribute type.
  281. [table
  282. [[Attribute type] [Generator type]]
  283. [[`char`, `wchar_t`] [`standard::char_`, `standard_wide::char_`]]
  284. [[`short`, `int`, `long`] [`short_`, `int_`, `long_`]]
  285. [[`unsigned short`, `unsigned int`, `unsigned long`]
  286. [`ushort_`, `uint_`, `ulong_`]]
  287. [[`float`, `double`, `long double`] [`float_`, `double_`, `long_double`]]
  288. [[`short`, `int`, `long`] [`short_`, `int_`, `long_`]]
  289. [[`long long`, `unsigned long long`]
  290. [`long_long`, `ulong_long`]]
  291. [[`bool`] [`bool_`]]
  292. [[Any (STL) container] [Kleene Star (unary `'*'`)]]
  293. [[Any Fusion sequence] [Sequence operator (`'>>'`)]]
  294. [[`boost::optional<>`] [Optional operator (unary `'-'`)]]
  295. [[`boost::variant<>`] [Alternative operator (`'|'`)]]
  296. ]
  297. [important The mapping for the parsers `long_long` and `ulong_long` are only
  298. available on platforms where the preprocessor constant
  299. `BOOST_HAS_LONG_LONG` is defined (i.e. on platforms having native
  300. support for `long long` and `unsigned long long` (64 bit) signed and
  301. unsigned integer types).]
  302. [heading Template parameters]
  303. [table
  304. [[Parameter] [Description]]
  305. [[`Attr`] [An attribute type utilized to create the corresponding
  306. parser type from.]]
  307. ]
  308. [endsect] [/ API for Automatic Parser Creation]
  309. [endsect]