manual.qbk 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. [#manual]
  2. [section User manual]
  3. [section What is a parser]
  4. See the [link parser parser] section of the [link reference reference] for the
  5. explanation of what a parser is.
  6. [section The input of the parsers]
  7. Parsers take a [link string `string`] as input, which represents a string
  8. for template metaprograms. For example the string `"Hello World!"` can be
  9. defined the following way:
  10. string<'H','e','l','l','o',' ','W','o','r','l','d','!'>
  11. This syntax makes the input of the parsers difficult to read. Metaparse works
  12. with compilers using C++98, but the input of the parsers has to be defined the
  13. way it is described above.
  14. Based on `constexpr`, a feature provided by C++11, Metaparse provides a macro,
  15. [link BOOST_METAPARSE_STRING `BOOST_METAPARSE_STRING`] for defining strings:
  16. BOOST_METAPARSE_STRING("Hello World!")
  17. This defines a [link string `string`] as well, however, it is easier to
  18. read. The maximum length of the string that can be defined this way is limited,
  19. however, this limit is configurable. It is specified by the
  20. `BOOST_METAPARSE_LIMIT_STRING_SIZE` macro.
  21. [endsect]
  22. [section Source positions]
  23. A source position is described using a compile-time data structure. The
  24. following functions can be used to query it:
  25. * [link get_col `get_col`]
  26. * [link get_line `get_line`]
  27. The beginning of the input is [link start `start`] which requires
  28. `<boost/metaparse/start.hpp>` to be included.
  29. [endsect]
  30. [section Error handling]
  31. An error is described using a compile-time data structure. It contains
  32. information about the source position where the error was detected and some
  33. [link parsing_error_message description] about the error.
  34. [link debug_parsing_error `debug_parsing_error`] can be used to display the
  35. error message. Metaparse provides the
  36. [link BOOST_METAPARSE_DEFINE_ERROR `BOOST_METAPARSE_DEFINE_ERROR`] macro for
  37. defining simple [link parsing_error_message parsing error message]s.
  38. [endsect]
  39. [section Some examples of simple parsers]
  40. * A parser that parses nothing and always succeeds is
  41. [link return_ `return_`].
  42. * A parser that always fails is [link fail `fail`].
  43. * A parser that parses one character and returns the parsed character as the
  44. result is [link one_char `one_char`].
  45. [endsect]
  46. [section Combining parsers]
  47. Complex parsers can be built by combining simple parsers. The parser library
  48. contains a number of parser combinators that build new parsers from already
  49. existing ones.
  50. For example
  51. [link accept_when `accept_when`]`<Parser, Predicate, RejectErrorMsg>` is a
  52. parser. It uses `Parser` to parse the input. When `Parser` rejects the input,
  53. the combinator returns the error `Parser` failed with. When `Parser` is
  54. successful, the combinator validates the result using `Predicate`. If the
  55. predicate returns true, the combinator accepts the input, otherwise it generates
  56. an error with the message `RejectErrorMsg`.
  57. Having [link accept_when `accept_when`], [link one_char `one_char`] can be
  58. used to build parsers that accept only digit characters, only whitespaces, etc.
  59. For example [link digit `digit`] accepts only digit characters:
  60. typedef
  61. boost::metaparse::accept_when<
  62. boost::metaparse::one_char,
  63. boost::metaparse::util::is_digit,
  64. boost::metaparse::errors::digit_expected
  65. >
  66. digit;
  67. [endsect]
  68. [section Sequence]
  69. The result of a successful parsing is some value and the remaining string that
  70. was not parsed. The remaining string can be processed by another parser. The
  71. parser library provides a parser combinator, [link sequence `sequence`],
  72. that takes a number of parsers as arguments and builds a new parser from them
  73. that:
  74. * Parses the input using the first parser
  75. * If parsing succeeds, it parses the remaining string with the second parser
  76. * It continues applying the parsers in order as long as they succeed
  77. * If all of them succeed, it returns the list of results
  78. * If any of the parsers fails, the combinator fails as well and returns the
  79. error the first failing parser returned with
  80. [endsect]
  81. [#repetition]
  82. [section Repetition]
  83. It is a common thing to parse a list of things of unknown length. As an example
  84. let's start with something simple: the text is a list of numbers. For example:
  85. 11 13 3 21
  86. We want the result of parsing to be the sum of these values. Metaparse provides
  87. the [link int_ `int_`] parser we can use to parse one of these numbers.
  88. Metaparse provides the [link token `token`] combinator to consume the
  89. whitespaces after the number. So the following parser parses one number and the
  90. whitespaces after it:
  91. using int_token = token<int_>;
  92. The result of parsing is a boxed integer value: the value of the parsed number.
  93. For example parsing
  94. [link BOOST_METAPARSE_STRING `BOOST_METAPARSE_STRING`]`("13 ")` gives
  95. `boost::mpl::int_<13>` as the result.
  96. Our example input is a list of numbers. Each number can be parsed by
  97. `int_token`:
  98. [$images/metaparse/repeated_diag0.png [width 70%]]
  99. This diagram shows how the repeated application of `int_token` can parse the
  100. example input. Metaparse provides the [link repeated `repeated`] parser to
  101. easily implement this. The result of parsing is a typelist: the list of the
  102. individual numbers.
  103. [$images/metaparse/repeated_diag1.png [width 70%]]
  104. This diagram shows how [link repeated `repeated`]`<int_token>` works. It uses
  105. the `int_token` parser repeatedly and builds a `boost::mpl::vector` from the
  106. results it provides.
  107. But we need the sum of these, so we need to summarise the result. We can do this
  108. by wrapping our parser, [link repeated `repeated`]`<int_token>` with
  109. [link transform `transform`]. That gives us the opportunity to specify a
  110. function transforming this typelist to some other value - the sum of the
  111. elements in our case. Initially let's ignore how to summarise the elements in
  112. the vector. Let's assume that it can be implemented by a lambda expression and
  113. use `boost::mpl::lambda<...>::type` representing that lambda expression. Here is
  114. an example using [link transform `transform`] and this lambda expression:
  115. using sum_parser =
  116. transform<
  117. repeated<int_token>,
  118. boost::mpl::lambda<...>::type
  119. >;
  120. The [link transform `transform`]`<>` parser combinator wraps the
  121. [link repeated `repeated`]`<int_token>` to build the parser we need. Here is a
  122. diagram showing how it works:
  123. [$images/metaparse/repeated_diag2.png [width 70%]]
  124. As the diagram shows, the
  125. [link transform `transform`]`<`[link repeated `repeated`]`<int_token>, ...>`
  126. parser parses the input using [link repeated `repeated`]`<int_token>` and then
  127. does some processing on the result of parsing.
  128. Let's implement the missing lambda expression that tells
  129. [link transform `transform`] how to change the result coming from
  130. [link repeated `repeated`]`<int_token>`. We can summarise the numbers in a
  131. typelist by using Boost.MPL's `fold` or `accumulate`. Here is an example doing
  132. that:
  133. using sum_op = mpl::lambda<mpl::plus<mpl::_1, mpl::_2>>::type;
  134. using sum_parser =
  135. transform<
  136. repeated<int_token>,
  137. mpl::lambda<
  138. mpl::fold<mpl::_1, mpl::int_<0>, sum_op>
  139. >::type
  140. >;
  141. Here is an extended version of the above diagram showing what happens here:
  142. [$images/metaparse/repeated_diag3.png [width 70%]]
  143. This example parses the input, builds the list of numbers and then loops over it
  144. and summarises the values. It starts with the second argument of `fold`,
  145. `int_<0>` and adds every item of the list of numbers (which is the result of
  146. the parser [link repeated `repeated`]`<int_token>`) one by one.
  147. [note
  148. Note that [link transform `transform`] wraps another parser,
  149. [link repeated `repeated`]`<int_token>` here. It parses the input with that
  150. parser, gets the result of that parsing and changes that result.
  151. [link transform `transform`] itself will be a parser returning that updated
  152. result.
  153. ]
  154. [#introducing-foldl]
  155. [section Introducing foldl]
  156. It works, however, this is rather inefficient: it has a loop parsing the
  157. integers one by one, building a typelist and then it loops over this typelist to
  158. summarise the result. Using template metaprograms in your applications can have
  159. a serious impact on the compiler's memory usage and the speed of the
  160. compilation, therefore I recommend being careful with these things.
  161. Metaparse offers more efficient ways of achieving the same result. You don't
  162. need two loops: you can merge them together and add every number to your summary
  163. right after parsing it. Metaparse offers the [link foldl `foldl`] for this.
  164. With [link foldl `foldl`] you specify:
  165. * the parser to parse the individual elements of the list
  166. (which is `int_token` in our example)
  167. * the initial value used for folding (which is `int_<0>` in our example)
  168. * the forward operation merging the sub-result we have so far and the value
  169. coming from the last application of the parser (this was `sum_op` in our
  170. example)
  171. Our parser can be implemented this way:
  172. using better_sum_parser = foldl<int_token, mpl::int_<0>, sum_op>;
  173. As you can see the implementation of the parser is more compact.
  174. Here is a diagram showing what happens when you use this parser to parse some
  175. input:
  176. [$images/metaparse/foldl_diag1.png [width 70%]]
  177. As you can see, not only the implementation of the parser is more compact, but
  178. it achieves the same result by doing less as well. It parses the input by
  179. applying `int_token` repeatedly, just like the previous solution. But it
  180. produces the final result without building a typelist as an internal step. Here
  181. is how it works internally:
  182. [$images/metaparse/foldl_diag2.png [width 70%]]
  183. It summarises the results of the repeated `int_token` application using
  184. `sum_op`. This implementation is more efficient. It accepts an empty string as a
  185. valid input: the sum of it is `0`. It may be good for you, in which case you are
  186. done. If you don't wan to accept it, you can use [link foldl1 `foldl1`] instead
  187. of [link foldl `foldl`]. This is the same, but it rejects empty input.
  188. (Metaparse offers [link repeated1 `repeated1`] as well if you choose the first
  189. approach and would like to reject empty string)
  190. [endsect]
  191. [#introducing-foldr]
  192. [section Introducing foldr]
  193. [note
  194. Note that if you are reading this manual for the first time, you probably want
  195. to skip this section and proceed with
  196. [link introducing-foldl_start_with_parser Introducing foldl_start_with_parser]
  197. ]
  198. You might have noticed that Metaparse offers [link foldr `foldr`] as well. The
  199. difference between [link foldl `foldl`] and [link foldr `foldr`] is the
  200. direction in which the results are summarised. (`l` stands for ['from the Left]
  201. and `r` stands for ['from the Right]) Here is a diagram showing how
  202. `better_sum_parser` works if it is implemented using [link foldr `foldr`]:
  203. [$images/metaparse/foldr_diag1.png [width 70%]]
  204. As you can see this is very similar to using [link foldl `foldl`], but the
  205. results coming out of the individual applications of `int_token` are summarised
  206. in a right-to-left order. As `sum_op` is addition, it does not affect the end
  207. result, but in other cases it might.
  208. [note
  209. Note that the implementation of [link foldl `foldl`] is more efficient than
  210. [link foldr `foldr`]. Prefer [link foldl `foldl`] whenever possible.
  211. ]
  212. As you might expect it, Metaparse offers [link foldr1 `foldr1`] as well, which
  213. folds from the right and rejects empty input.
  214. [endsect]
  215. [#introducing-foldl_start_with_parser]
  216. [section Introducing foldl_start_with_parser]
  217. Let's change the grammar of our little language. Instead of a list of numbers,
  218. let's expect numbers separated by a `+` symbol. Our example input becomes the
  219. following:
  220. BOOST_METAPARSE_STRING("11 + 13 + 3 + 21")
  221. Parsing it with [link foldl `foldl`] or [link repeated `repeated`] is difficult:
  222. there has to be a `+` symbol before every element ['except] the first one. None
  223. of the already introduced repetition constructs offer a way of treating the
  224. first element in a different way.
  225. If we forget about the first number for a moment, the rest of the input is
  226. `"+ 13 + 3 + 21"`. This can easily be parsed by [link foldl `foldl`] (or
  227. [link repeated `repeated`]):
  228. using plus_token = token<lit_c<'+'>>;
  229. using plus_int = last_of<plus_token, int_token>;
  230. using sum_parser2 = foldl<plus_int, int_<0>, sum_op>;
  231. It uses `plus_int`, that is [link last_of `last_of`]`<plus_token, int_token>`
  232. as the parser that is used repeatedly to get the numbers. It does the following:
  233. * Uses `plus_token` to parse the `+` symbol and any whitespace that might follow
  234. it.
  235. * Uses then `int_token` to parse the number
  236. * Combines the above two with [link last_of `last_of`] to use both parsers in
  237. order and keep only the result of using the second one (the result of parsing
  238. the `+` symbol is thrown away - we don't care about it).
  239. This way [link last_of `last_of`]`<plus_token, int_token>` returns the value of
  240. the number as the result of parsing, just like our previous parser, `int_token`
  241. did. Because of this, it can be used as a drop-in replacement of `int_token` in
  242. the previous example and we get a parser for our updated language. Or at least
  243. for all number except the first one.
  244. This [link foldl `foldl`] can not parse the first element, because it expects a
  245. `+` symbol before every number. You might think of making the `+` symbol
  246. optional in the above approach - don't do that. It makes the parser accept
  247. `"11 + 13 3 21"` as well as the `+` symbol is now optional ['everywhere].
  248. What you could do is parsing the first element with `int_token`, the rest of
  249. the elements with the above [link foldl `foldl`]-based solution and add the
  250. result of the two. This is left as an exercise to the reader.
  251. Metaparse offers [link foldl_start_with_parser `foldl_start_with_parser`] to
  252. implement this. [link foldl_start_with_parser `foldl_start_with_parser`] is the
  253. same as [link foldl `foldl`]. The difference is that instead of an initial value
  254. to combine the list elements with it takes an ['initial parser]:
  255. using plus_token = token<lit_c<'+'>>;
  256. using plus_int = last_of<plus_token, int_token>;
  257. using sum_parser3 = foldl_start_with_parser<plus_int, int_token, sum_op>;
  258. [link foldl_start_with_parser `foldl_start_with_parser`] starts with applying
  259. that initial parser and uses the result it returns as the initial value for
  260. folding. It does the same as [link foldl `foldl`] after that. The following
  261. diagram shows how it can be used to parse a list of numbers separated by `+`
  262. symbols:
  263. [$images/metaparse/foldl_start_with_parser_diag1.png [width 70%]]
  264. As the diagram shows, it start parsing the list of numbers with `int_token`,
  265. uses its value as the starting value for folding (earlier approaches were using
  266. the value `int_<0>` as this starting value). Then it parses all elements of the
  267. list by using `plus_int` multiple times.
  268. [endsect]
  269. [#introducing-foldr_start_with_parser]
  270. [section Introducing foldr_start_with_parser]
  271. [note
  272. Note that if you are reading this manual for the first time, you probably want
  273. to skip this section and try creating some parsers using
  274. [link foldl_start_with_parser `foldl_start_with_parser`] instead.
  275. ]
  276. [@foldl_start_with_parser.hpp `foldl_start_with_parser`] has its
  277. ['from the right] pair,
  278. [link foldr_start_with_parser `foldr_start_with_parser`]. It uses the same
  279. elements as [link foldl_start_with_parser `foldl_start_with_parser`] but in a
  280. different order. Here is a parser for our example language implemented with
  281. [link foldr_start_with_parser `foldr_start_with_parser`]:
  282. using plus_token = token<lit_c<'+'>>;
  283. using int_plus = first_of<int_token, plus_token>;
  284. using sum_parser4 = foldr_start_with_parser<int_plus, int_token, sum_op>;
  285. Note that it uses `int_plus` instead of `plus_int`. This is because the parser
  286. the initial value for folding comes from is used after `int_plus` has parsed the
  287. input as many times as it could. It might sound strange for the first time, but
  288. the following diagram should help you understand how it works:
  289. [$images/metaparse/foldr_start_with_parser_diag1.png [width 70%]]
  290. As you can see, it starts with the parser that is applied repeatedly on the
  291. input, thus instead of parsing `plus_token int_token` repeatedly, we need to
  292. parse `int_token plus_token` repeatedly. The last number is not followed by `+`,
  293. thus `int_plus` fails to parse it and it stops the iteration.
  294. [link foldr_start_with_parser `foldr_start_with_parser`] then uses the other
  295. parser, `int_token` to parse the input. It succeeds and the result it returns is
  296. used as the starting value for folding from the right.
  297. [note
  298. Note that as the above description also suggests, the implementation of
  299. [link foldl_start_with_parser `foldl_start_with_parser`] is more efficient
  300. than [link foldr_start_with_parser `foldr_start_with_parser`]. Prefer
  301. [link foldl_start_with_parser `foldl_start_with_parser`] whenever possible.
  302. ]
  303. [endsect]
  304. [#introducing-foldl_reject_incomplete_start_with_parser]
  305. [section Introducing foldl_reject_incomplete_start_with_parser]
  306. Using a parser built with
  307. [link foldl_start_with_parser `foldl_start_with_parser`] we can parse the input
  308. when the input is correct. However, it is not always the case. Consider the
  309. following input for example:
  310. BOOST_METAPARSE_STRING("11 + 13 + 3 + 21 +")
  311. This is an invalid expression. However, if we parse it using the
  312. [link foldl_start_with_parser `foldl_start_with_parser`]-based parser presented
  313. earlier (`sum_parser3`), it accepts the input and the result is `48`. This is
  314. because [link foldl_start_with_parser `foldl_start_with_parser`] parses the
  315. input ['as long as it can]. It parses the first`int_token` (`11`) and then it
  316. starts parsing the `plus_int` elements (`+ 13`, `+ 3`, `+ 21`). After parsing
  317. all of these, it tries to parse the remaining `" +"` input using `plus_int`
  318. which fails and therefore
  319. [link foldl_start_with_parser `foldl_start_with_parser`] stops after `+ 21`.
  320. The problem is that the parser parses the longest sub-expression starting from
  321. the beginning, that represents a valid expression. The rest is ignored. The
  322. parser can be wrapped by [link entire_input `entire_input`] to make sure to
  323. reject expressions with invalid extra characters at the end, however, that
  324. won't make the error message useful. ([link entire_input `entire_input`] can
  325. only tell the author of the invalid expression that after `+ 21` is something
  326. wrong).
  327. Metaparse provides
  328. [link foldl_reject_incomplete_start_with_parser `foldl_reject_incomplete_start_with_parser`],
  329. which does the same as [link foldl_start_with_parser `foldl_start_with_parser`],
  330. except that once no further repetitions are found, it checks ['where] the
  331. repeated parser (in our example `plus_int`) fails. When it can make any progress
  332. (eg. it finds a `+` symbol), then
  333. [link foldl_reject_incomplete_start_with_parser `foldl_reject_incomplete_start_with_parser`]
  334. assumes, that the expression's author intended to make the repetition longer,
  335. but made a mistake and propagates the error message coming from that last broken
  336. expression.
  337. [$images/metaparse/foldl_reject_incomplete_start_with_parser_diag1.png [width 70%]]
  338. The above diagram shows how
  339. [link foldl_reject_incomplete_start_with_parser `foldl_reject_incomplete_start_with_parser`]
  340. parses the example invalid input and how it fails. This can be used for better
  341. error reporting from the parsers.
  342. Other folding parsers also have their `f` version. (eg.
  343. [link foldr_reject_incomplete `foldr_reject_incomplete`],
  344. [link foldl_reject_incomplete1 `foldl_reject_incomplete1`], etc).
  345. [endsect]
  346. [#finding-the-right-folding-parser-combinator]
  347. [section Finding the right folding parser combinator]
  348. As you might have noticed, there are a lot of different folding parser
  349. combinators. To help you find the right one, the following naming convention is
  350. used:
  351. [$images/metaparse/folds.png [width 70%]]
  352. [note
  353. Note that there is no `foldr_reject_incomplete_start_with_parser`. The `p`
  354. version of the right-folding parsers applies the special parser, whose result
  355. is the initial value, after the repeated elements. Therefore, when the parser
  356. parsing one repeated element fails, `foldr_start_with_parser` would apply that
  357. special final parser instead of checking how the repeated element's parser
  358. failed.
  359. ]
  360. [endsect]
  361. [endsect]
  362. [#result_types]
  363. [section What can be built from a compile-time string?]
  364. Parsers built using Metaparse are template metaprograms parsing text (or code)
  365. at compile-time. Here is a list of things that can be the "result" of parsing:
  366. * A ['type]. An example for this is a parser parsing a `printf` format string
  367. and returning the typelist (eg. `boost::mpl::vector`) of the expected
  368. arguments.
  369. * A ['constant value]. An example for this is the result of a calculator
  370. language. See the [link getting_started Getting Started] section for further
  371. details.
  372. * A ['runtime object]. A static runtime object can be generated that might be
  373. used at runtime. An example for this is parsing regular expressions at
  374. compile-time and building `boost::xpressive::sregex` objects. See the
  375. `regex` example of Metaparse for an example.
  376. * A C++ ['function], which might be called at runtime. A C++ function can be
  377. generated that can be called at runtime. It is good for generating native
  378. (and optimised) code from EDSLs. See the `compile_to_native_code` example of
  379. Metaparse as an example for this.
  380. * A [link metafunction_class ['template metafunction class]]. The result of
  381. parsing might be a type, which is a
  382. [link metafunction_class template metafunction class]. This is good for
  383. building an EDSL for template metaprogramming. See the `meta_hs` example of
  384. Metaparse as an example for this.
  385. [endsect]
  386. [section Grammars]
  387. Metaparse provides a way to define grammars in a syntax that resembles EBNF. The
  388. [link grammar `grammar`] template can be used to define a grammar. It can be
  389. used the following way:
  390. grammar<BOOST_METAPARSE_STRING("plus_exp")>
  391. ::import<BOOST_METAPARSE_STRING("int_token"), token<int_>>::type
  392. ::rule<BOOST_METAPARSE_STRING("ws ::= (' ' | '\n' | '\r' | '\t')*")>::type
  393. ::rule<BOOST_METAPARSE_STRING("plus_token ::= '+' ws"), front<_1>>::type
  394. ::rule<BOOST_METAPARSE_STRING("plus_exp ::= int_token (plus_token int_token)*"), plus_action>::type
  395. The code above defines a parser from a grammar definition. The start symbol of
  396. the grammar is `plus_exp`. The lines beginning with `::rule` define rules.
  397. Rules optionally have a semantic action, which is a metafunction class that
  398. transforms the result of parsing after the rule has been applied.
  399. Existing parsers can be bound to names and be used in the rules by importing
  400. them. Lines beginning with `::import` bind existing parsers to names.
  401. The result of a grammar definition is a parser which can be given to other
  402. parser combinators or be used directly. Given that grammars can import existing
  403. parsers and build new ones, they are parser combinators as well.
  404. [endsect]
  405. [endsect]
  406. [section Parsing based on `constexpr`]
  407. Metaparse is based on template metaprogramming, however, C++11 provides
  408. `constexpr`, which can be used for parsing at compile-time as well. While
  409. implementing parsers based on `constexpr` is easier for a C++ developer, since
  410. its syntax resembles the regular syntax of the language, the result of parsing
  411. has to be a `constexpr` value. Parsers based on template metaprogramming can
  412. build types as the result of parsing. These types may be boxed `constexpr`
  413. values but can be metafunction classes, classes with static functions which can
  414. be called at runtime, etc.
  415. When a parser built with Metaparse needs a sub-parser for processing a part of
  416. the input text and generating a `constexpr` value as the result of parsing, one
  417. can implement the sub-parser based on `constexpr` functions. Metaparse
  418. can be integrated with them and lift their results into C++ template
  419. metaprogramming. An example demonstrating this feature can be found among the
  420. examples (`constexpr_parser`). This capability makes it possible to integrate
  421. Metaparse with parsing libraries based on `constexpr`.
  422. [endsect]
  423. [section What types of grammars can be used?]
  424. It is possible to write parsers for ['context free grammars] using Metaparse.
  425. However, this is not the most general category of grammars that can be used. As
  426. Metaparse is a highly extendable framework, it is not clear what should be
  427. considered to be the limit of Metaparse itself. For example Metaparse provides
  428. the [link accept_when `accept_when`] [link parser_combinator parser combinator].
  429. It can be used to provide arbitrary predicates for enabled/disabling a specific
  430. rule. One can go as far as providing the Turing machine (as a
  431. [link metafunction metafunction]) of the entire grammar as a predicate, so one
  432. can build parsers for ['unrestricted grammars] that can be parsed using a Turing
  433. machine. Note that such a parser would not be considered to be a parser built
  434. with Metaparse, however, it is not clear how far a solution might go and still
  435. be considered using Metaparse.
  436. Metaparse assumes that the parsers are ['deterministic], as they have only "one"
  437. result. It is of course possible to write parsers and combinators that return a
  438. set (or list or some other container) of results as that "one" result, but that
  439. can be considered building a new parser library. There is no clear boundary for
  440. Metaparse.
  441. Metaparse supports building ['top-down parsers] and ['left-recursion] is not
  442. supported as it would lead to infinite recursion. ['Right-recursion] is
  443. supported, however, in most cases the
  444. [link repetition iterative parser combinators] provide better alternatives.
  445. [endsect]
  446. [endsect]