concepts.qbk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 Parser Concepts]
  8. Spirit.Qi parsers fall into a couple of generalized __concepts__. The
  9. /Parser/ is the most fundamental concept. All Spirit.Qi parsers are
  10. models of the /Parser/ concept. /PrimitiveParser/, /UnaryParser/,
  11. /BinaryParser/, /NaryParser/, and /Nonterminal/ are all refinements of the
  12. /Parser/ concept.
  13. The following sections provide details on these concepts.
  14. [/------------------------------------------------------------------------------]
  15. [section Parser]
  16. [heading Description]
  17. The /Parser/ is the most fundamental concept. A Parser has a member
  18. function, `parse`, that accepts a first-last __fwditer__ pair and returns
  19. bool as its result. The iterators delimit the data being parsed.
  20. The Parser's `parse` member function returns `true` if the parse
  21. succeeds, in which case the first iterator is advanced accordingly. Each
  22. Parser can represent a specific pattern or algorithm, or it can be a
  23. more complex parser formed as a composition of other Parsers.
  24. [variablelist Notation
  25. [[`p`] [A `Parser`.]]
  26. [[`P`] [A `Parser` type.]]
  27. [[`Iter`] [a __fwditer__ type.]]
  28. [[`f`, `l`] [__fwditer__. first/last iterator pair.]]
  29. [[`Context`] [The parser's __context__ type.]]
  30. [[`context`] [The parser's __context__, or __unused__.]]
  31. [[`skip`] [A skip Parser, or __unused__.]]
  32. [[`attrib`] [A __compatible_attribute__, or __unused__.]]
  33. ]
  34. [heading Valid Expressions]
  35. In the expressions below, the behavior of the parser, `p`, and how `skip`
  36. and `attrib` are handled by `p`, are left unspecified in the base `Parser`
  37. concept. These are specified in subsequent, more refined concepts and by
  38. the actual models thereof.
  39. For any Parser the following expressions must be valid:
  40. [table
  41. [[Expression] [Semantics] [Return type]]
  42. [[
  43. ``p.parse(f, l, context, skip, attr)``]
  44. [Match the input sequence
  45. starting from `f`. Return
  46. `true` if successful, otherwise
  47. return `false`.] [`bool`]]
  48. [[`p.what(context)`] [Get information about a Parser.] [__info__]]
  49. ]
  50. [heading Type Expressions]
  51. [table
  52. [[Expression] [Description]]
  53. [[`P::template attribute<Context, Iter>::type`] [The Parser's expected attribute.]]
  54. [[`traits::is_parser<P>::type`] [Metafunction that evaluates to `mpl::true_` if
  55. a certain type, `P` is a Parser, `mpl::false_`
  56. otherwise (See __mpl_boolean_constant__).]]
  57. ]
  58. [heading Postcondition]
  59. Upon return from `p.parse` the following post conditions should hold:
  60. * On a successful match, `f` is positioned one past the last
  61. matching character/token.
  62. * On a failed match, if a `skip` parser is __unused__,
  63. `f` is restored to its original position prior to entry.
  64. * On a failed match, if a `skip` parser is not __unused__,
  65. `f` is positioned one past the last character/token
  66. matching `skip`.
  67. * On a failed match, `attrib` state is undefined.
  68. * No post-skips: trailing `skip` characters/tokens will not be skipped.
  69. [heading Models]
  70. All parsers in Spirit.Qi are models of the /Parser/ concept.
  71. [endsect] [/ Parser Concept]
  72. [/------------------------------------------------------------------------------]
  73. [section PrimitiveParser]
  74. [heading Description]
  75. /PrimitiveParser/ is the most basic building block that the client uses
  76. to build more complex parsers.
  77. [heading Refinement of]
  78. [:__parser_concept__]
  79. [heading Pre-skip]
  80. Upon entry to the `parse` member function, a PrimitiveParser is required
  81. to do a pre-skip. Leading `skip` characters/tokens will be skipped prior
  82. to parsing. Only PrimitiveParsers are required to perform this pre-skip.
  83. This is typically carried out through a call to `qi::skip_over`:
  84. qi::skip_over(f, l, skip);
  85. [heading Type Expressions]
  86. [table
  87. [[Expression] [Description]]
  88. [[`traits::is_primitive_parser<P>::type`] [Metafunction that evaluates to `mpl::true_` if
  89. a certain type, `P`, is a PrimitiveParser, `mpl::false_`
  90. otherwise (See __mpl_boolean_constant__).]]
  91. ]
  92. [heading Models]
  93. * __qi_attr__
  94. * __qi_eoi__
  95. * __qi_eol__
  96. * __qi_eps__
  97. * __qi_symbols__
  98. [endsect] [/ PrimitiveParser Concept]
  99. [/------------------------------------------------------------------------------]
  100. [section UnaryParser]
  101. [heading Description]
  102. /UnaryParser/ is a composite parser that has a single subject. The
  103. UnaryParser may change the behavior of its subject following the
  104. __delegate_pattern__.
  105. [heading Refinement of]
  106. [:__parser_concept__]
  107. [variablelist Notation
  108. [[`p`] [A UnaryParser.]]
  109. [[`P`] [A UnaryParser type.]]
  110. ]
  111. [heading Valid Expressions]
  112. In addition to the requirements defined in __parser_concept__, for any
  113. UnaryParser the following must be met:
  114. [table
  115. [[Expression] [Semantics] [Return type]]
  116. [[`p.subject`] [Subject parser.] [__parser_concept__]]
  117. ]
  118. [heading Type Expressions]
  119. [table
  120. [[Expression] [Description]]
  121. [[`P::subject_type`] [The subject parser type.]]
  122. [[`traits::is_unary_parser<P>::type`] [Metafunction that evaluates to `mpl::true_` if
  123. a certain type, `P` is a UnaryParser, `mpl::false_`
  124. otherwise (See __mpl_boolean_constant__).]]
  125. ]
  126. [heading Invariants]
  127. For any UnaryParser, `P`, the following invariant always holds:
  128. * `traits::is_parser<P::subject_type>::type` evaluates to `mpl::true_`
  129. [heading Models]
  130. * __qi_and_predicate__
  131. * __qi_kleene__
  132. * __qi_lexeme__
  133. * __qi_not_predicate__
  134. * __qi_omit__
  135. * __qi_plus__
  136. * __qi_raw__
  137. * [qi_repeat `repeat`]
  138. * __qi_skip__
  139. [endsect] [/ UnaryParser Concept]
  140. [/------------------------------------------------------------------------------]
  141. [section BinaryParser]
  142. [heading Description]
  143. /BinaryParser/ is a composite parser that has a two subjects, `left` and
  144. `right`. The BinaryParser allows its subjects to be treated in the same
  145. way as a single instance of a __parser_concept__ following the
  146. __composite_pattern__.
  147. [heading Refinement of]
  148. [:__parser_concept__]
  149. [variablelist Notation
  150. [[`p`] [A BinaryParser.]]
  151. [[`P`] [A BinaryParser type.]]
  152. ]
  153. [heading Valid Expressions]
  154. In addition to the requirements defined in __parser_concept__, for any
  155. BinaryParser the following must be met:
  156. [table
  157. [[Expression] [Semantics] [Return type]]
  158. [[`p.left`] [Left parser.] [__parser_concept__]]
  159. [[`p.right`] [Right parser.] [__parser_concept__]]
  160. ]
  161. [heading Type Expressions]
  162. [table
  163. [[Expression] [Description]]
  164. [[`P::left_type`] [The left parser type.]]
  165. [[`P::right_type`] [The right parser type.]]
  166. [[`traits::is_binary_parser<P>::type`] [Metafunction that evaluates to `mpl::true_` if
  167. a certain type, `P` is a BinaryParser, `mpl::false_`
  168. otherwise (See __mpl_boolean_constant__).]]
  169. ]
  170. [heading Invariants]
  171. For any BinaryParser, `P`, the following invariants always hold:
  172. * `traits::is_parser<P::left_type>::type` evaluates to `mpl::true_`
  173. * `traits::is_parser<P::right_type>::type` evaluates to `mpl::true_`
  174. [heading Models]
  175. * __qi_difference__
  176. * __qi_list__
  177. [endsect] [/ BinaryParser Concept]
  178. [/------------------------------------------------------------------------------]
  179. [section NaryParser]
  180. [heading Description]
  181. /NaryParser/ is a composite parser that has one or more subjects. The
  182. NaryParser allows its subjects to be treated in the same way as a single
  183. instance of a __parser_concept__ following the __composite_pattern__.
  184. [heading Refinement of]
  185. [:__parser_concept__]
  186. [variablelist Notation
  187. [[`p`] [A NaryParser.]]
  188. [[`P`] [A NaryParser type.]]
  189. ]
  190. [heading Valid Expressions]
  191. In addition to the requirements defined in __parser_concept__, for any
  192. NaryParser the following must be met:
  193. [table
  194. [[Expression] [Semantics] [Return type]]
  195. [[`p.elements`] [The tuple of elements.] [A __fusion__ Sequence of __parser_concept__ types.]]
  196. ]
  197. [heading Type Expressions]
  198. [table
  199. [[Expression] [Description]]
  200. [[`p.elements_type`] [Elements tuple type.]]
  201. [[`traits::is_nary_parser<P>::type`] [Metafunction that evaluates to `mpl::true_` if
  202. a certain type, `P` is a NaryParser, `mpl::false_`
  203. otherwise (See __mpl_boolean_constant__).]]
  204. ]
  205. [heading Invariants]
  206. For each element, `E`, in any NaryParser, `P`, the following invariant
  207. always holds:
  208. * `traits::is_parser<E>::type` evaluates to `mpl::true_`
  209. [heading Models]
  210. * __qi_alternative__
  211. * __qi_expect__
  212. * __qi_permutation__
  213. * __qi_sequence__
  214. * __qi_sequential_or__
  215. [endsect] [/ NaryParser Concept]
  216. [/------------------------------------------------------------------------------]
  217. [section Nonterminal]
  218. [heading Description]
  219. A Nonterminal is a symbol in a __peg__ production that represents a
  220. grammar fragment. Nonterminals may self reference to specify recursion.
  221. This is one of the most important concepts and the reason behind the
  222. word "recursive" in recursive descent parsing.
  223. [heading Refinement of]
  224. [:__parser_concept__]
  225. [heading Signature]
  226. Nonterminals can have both synthesized and inherited attributes. The
  227. Nonterminal's /Signature/ specifies both the synthesized and inherited
  228. attributes. The specification uses the function declarator syntax:
  229. RT(A0, A1, A2, ..., AN)
  230. where `RT` is the Nonterminal's synthesized attribute and `A0` ... `AN`
  231. are the Nonterminal's inherited attributes.
  232. The default value is `void()` (no synthesized and inherited attributes).
  233. [heading Attributes]
  234. The Nonterminal models a C++ function. The Nonterminal's synthesized
  235. attribute is analogous to the function return value and its inherited
  236. attributes are analogous to function arguments. The inherited attributes
  237. (arguments) can be passed in just like any __qi_lazy_argument__, e.g.:
  238. r(expr) // Evaluate expr at parse time and pass the result to the Nonterminal r
  239. [heading `_val`]
  240. The `boost::spirit::qi::_val` placeholder can be used in __phoenix__
  241. semantic actions anywhere in the Nonterminal's definition. This
  242. __phoenix__ placeholder refers to the Nonterminal's (synthesized)
  243. attribute. The `_val` placeholder acts like a mutable reference to the
  244. Nonterminal's attribute.
  245. [note Starting with __spirit__ V2.5 (distributed with Boost V1.47) the
  246. placeholder `_val` can be used in semantic actions attached to top level
  247. parser components as well. See __parse_api__ for more information.]
  248. [heading `_r1` ... `r10`]
  249. The `boost::spirit::_r1` ... `boost::spirit::r10` placeholders can be used
  250. in __phoenix__ semantic actions anywhere in the Nonterminal's
  251. definition. These __phoenix__ placeholders refer to the Nonterminal's
  252. inherited attributes.
  253. [heading Locals]
  254. Nonterminals can have local variables that will be created on the stack
  255. at parse time. A locals descriptor added to the Nonterminal declaration
  256. will give the Nonterminal local variables:
  257. template <typename T0, typename T1, typename T2, ..., typename TN>
  258. struct locals;
  259. where `T0` ... `TN` are the types of local variables accessible in your
  260. __phoenix__ semantic actions using the placeholders:
  261. * `boost::spirit::_a`
  262. * `boost::spirit::_b`
  263. * `boost::spirit::_c`
  264. * `boost::spirit::_d`
  265. * `boost::spirit::_e`
  266. * `boost::spirit::_f`
  267. * `boost::spirit::_g`
  268. * `boost::spirit::_h`
  269. * `boost::spirit::_i`
  270. * `boost::spirit::_j`
  271. which correspond to the Nonterminal's local variables `T0` ... `T9`.
  272. [variablelist Notation
  273. [[`x`] [A Nonterminal]]
  274. [[`X`] [A Nonterminal type]]
  275. [[`arg1`, `arg2`, ..., `argN`] [__qi_lazy_arguments__ that evaluate to each of
  276. the Nonterminal's inherited attributes.]]
  277. ]
  278. [heading Valid Expressions]
  279. In addition to the requirements defined in __parser_concept__, for any
  280. Nonterminal the following must be met:
  281. [table
  282. [[Expression] [Semantics] [Return type]]
  283. [[`x`] [In a parser expression, invoke Nonterminal `x`] [`X`]]
  284. [[`x(arg1, arg2, ..., argN)`][In a parser expression, invoke Nonterminal `r`
  285. passing in inherited attributes
  286. `arg1` ... `argN`] [`X`]]
  287. [[`x.name(name)`] [Naming a Nonterminal.] [`void`]]
  288. [[`x.name()`] [Getting the name of a Nonterminal.] [`std::string`]]
  289. [[debug(x)] [Debug Nonterminal `x`.] [`void`]]
  290. ]
  291. [heading Type Expressions]
  292. [table
  293. [[Expression] [Description]]
  294. [[`X::sig_type`] [The Signature of `X`: In a function signature form
  295. as described above in the Signature paragraph.]]
  296. [[`X::locals_type`] [The local variables of `X`: An __mpl_fwd_sequence__.]]
  297. ]
  298. [heading Models]
  299. * __qi_rule__
  300. * __qi_grammar__
  301. [endsect] [/ Concept]
  302. [endsect]