directive.qbk 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2001-2011 Hartmut Kaiser
  4. Copyright (C) 2011 Bryce Lelbach
  5. Copyright (C) 2016 Frank Hein, maxence business consulting gmbh
  6. Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. ===============================================================================/]
  9. [section:directive Parser Directives]
  10. This module includes different directives usable to augment and parameterize
  11. other parsers. It includes the `no_case`, `lexeme`, `omit`, `raw`, `repeat`,
  12. `matches`, `no_skip`, `skip`, `hold`, `as<T>`, `as_string` and
  13. `as_wstring` directives.
  14. [heading Module Header]
  15. // forwards to <boost/spirit/home/qi/directive.hpp>
  16. #include <boost/spirit/include/qi_directive.hpp>
  17. Also, see __include_structure__.
  18. [/------------------------------------------------------------------------------]
  19. [section:lexeme Parser Directive Inhibiting Skipping (`lexeme[]`)]
  20. [heading Description]
  21. The `lexeme` directive makes its subject a primitive. In a logical point
  22. of view, lexemes (and primitives) are minimal atomic units (e.g. words,
  23. numbers, identifiers, etc). These are the things that you'd normally put
  24. in the lexer (hinting at the term "lexeme"), but in a lexer-less world,
  25. you put these in a lexeme. Seeing its subject as a primitive, the
  26. `lexeme` directive does an initial pre-skip (as all primitives do) and
  27. turns off white space skipping.
  28. At the phrase level, the parser ignores white spaces, possibly including
  29. comments. Use `lexeme` in situations where you want to work at the
  30. character level instead of the phrase level. Parsers can be made to work
  31. at the character level by enclosing the pertinent parts inside the
  32. `lexeme` directive. For example, here's a rule that parses integers:
  33. integer = lexeme[ -(lit('+') | '-') >> +digit ];
  34. The `lexeme` directive instructs its subject parser to work on the
  35. character level. Without it, the `integer` rule would have allowed
  36. erroneous embedded white spaces in inputs such as `"1 2 345"` which will
  37. be parsed as `"12345"`.
  38. [note Keep in mind that `lexeme[]` pre-skips spaces. If this is not
  39. desired, use the [qi_no_skip `no_skip`] directive instead.]
  40. [heading Header]
  41. // forwards to <boost/spirit/home/qi/directive/lexeme.hpp>
  42. #include <boost/spirit/include/qi_lexeme.hpp>
  43. Also, see __include_structure__.
  44. [heading Namespace]
  45. [table
  46. [[Name]]
  47. [[`boost::spirit::lexeme // alias: boost::spirit::qi::lexeme` ]]
  48. ]
  49. [heading Model of]
  50. [:__unary_parser_concept__]
  51. [variablelist Notation
  52. [[`a`] [A __parser_concept__.]]
  53. ]
  54. [heading Expression Semantics]
  55. Semantics of an expression is defined only where it differs from, or is
  56. not defined in __unary_parser_concept__.
  57. [table
  58. [[Expression] [Semantics]]
  59. [[`lexeme[a]`] [Pre-skip and turn off white space skipping for the
  60. subject parser, `a` (and all its children).]]
  61. ]
  62. [heading Attributes]
  63. See __qi_comp_attr_notation__.
  64. [table
  65. [[Expression] [Attribute]]
  66. [[`lexeme[a]`]
  67. [``a: A --> lexeme[a]: A
  68. a: Unused --> lexeme[a]: Unused``]]
  69. ]
  70. [heading Complexity]
  71. [:The complexity is defined by the complexity of the subject parser, `a`]
  72. [heading Example]
  73. [note The test harness for the example(s) below is presented in the
  74. __qi_basics_examples__ section.]
  75. Some using declarations:
  76. [reference_using_declarations_lexeme]
  77. Simple usage of `lexeme[]`:
  78. [reference_lexeme]
  79. [endsect]
  80. [/------------------------------------------------------------------------------]
  81. [section:no_skip Parser Directive Inhibiting Skipping Without Pre-skip (`no_skip[]`)]
  82. [heading Description]
  83. The `no_skip[]` directive turns off white space skipping. The difference to
  84. __qi_lexeme__ is that it does not do pre-skipping in any case. Otherwise it is
  85. completely equivalent to the __qi_lexeme__ directive.
  86. [heading Header]
  87. // forwards to <boost/spirit/home/qi/directive/no_skip.hpp>
  88. #include <boost/spirit/include/qi_no_skip.hpp>
  89. Also, see __include_structure__.
  90. [heading Namespace]
  91. [table
  92. [[Name]]
  93. [[`boost::spirit::no_skip // alias: boost::spirit::qi::no_skip` ]]
  94. ]
  95. [heading Model of]
  96. [:__unary_parser_concept__]
  97. [variablelist Notation
  98. [[`a`] [A __parser_concept__.]]
  99. ]
  100. [heading Expression Semantics]
  101. Semantics of an expression is defined only where it differs from, or is
  102. not defined in __unary_parser_concept__.
  103. [table
  104. [[Expression] [Semantics]]
  105. [[`no_skip[a]`] [Turns off white space skipping for the
  106. subject parser, `a` (and all its children). This
  107. directive does not pre-skips.]]
  108. ]
  109. [heading Attributes]
  110. See __qi_comp_attr_notation__.
  111. [table
  112. [[Expression] [Attribute]]
  113. [[`no_skip[a]`]
  114. [``a: A --> no_skip[a]: A
  115. a: Unused --> no_skip[a]: Unused``]]
  116. ]
  117. [heading Complexity]
  118. [:The complexity is defined by the complexity of the subject parser, `a`]
  119. [heading Example]
  120. [note The test harness for the example(s) below is presented in the
  121. __qi_basics_examples__ section.]
  122. Some using declarations:
  123. [reference_using_declarations_no_skip]
  124. Simple usage of `no_skip[]`:
  125. [reference_no_skip]
  126. [endsect]
  127. [/------------------------------------------------------------------------------]
  128. [section:no_case Parser Directive Inhibiting Case Sensitivity (`no_case[]`)]
  129. [heading Description]
  130. The `no_case[]` directive does not consume any input. The actual
  131. matching is done by its subject parser. It's purpose is to force
  132. matching of the subject parser (and all its children) to be case
  133. insensitive.
  134. [heading Header]
  135. // forwards to <boost/spirit/home/qi/directive/no_case.hpp>
  136. #include <boost/spirit/include/qi_no_case.hpp>
  137. Also, see __include_structure__.
  138. [heading Namespace]
  139. [table
  140. [[Name]]
  141. [[`ns::no_case`]]
  142. ]
  143. In the table above, `ns` represents a __char_encoding_namespace__.
  144. [heading Model of]
  145. The model of `no_case` is the model of its subject parser.
  146. [variablelist Notation
  147. [[`a`] [A __parser_concept__.]]
  148. [[`ns`] [A __char_encoding_namespace__.]]
  149. ]
  150. [heading Expression Semantics]
  151. Semantics of an expression is defined only where it differs from, or is
  152. not defined in the subject's concept.
  153. [table
  154. [[Expression] [Semantics]]
  155. [[`ns::no_case[a]`] [Force matching of the subject parser, `a`
  156. (and all its children) to be case insensitive]]
  157. ]
  158. [heading Attributes]
  159. See __qi_comp_attr_notation__.
  160. [table
  161. [[Expression] [Attribute]]
  162. [[`ns::no_case[a]`]
  163. [``a: A --> ns::no_case[a]: A
  164. a: Unused --> ns::no_case[a]: Unused``]]
  165. ]
  166. [heading Complexity]
  167. [:The complexity is defined by the complexity of the subject parser, `a`]
  168. [heading Example]
  169. [note The test harness for the example(s) below is presented in the
  170. __qi_basics_examples__ section.]
  171. Some using declarations:
  172. [reference_using_declarations_no_case]
  173. Simple usage of `no_case[]`:
  174. [reference_no_case]
  175. A more sophisticated use case of `no_case[]` in conjunction with a symbol
  176. table (see __qi_symbols__ for more details):
  177. [reference_symbols_with_no_case]
  178. [endsect]
  179. [/------------------------------------------------------------------------------]
  180. [section:omit Parser Directive Ignoring Attribute (`omit[]`)]
  181. [heading Description]
  182. The `omit[]` ignores the attribute of its subject parser replacing it
  183. with __unused__.
  184. [heading Header]
  185. // forwards to <boost/spirit/home/qi/directive/omit.hpp>
  186. #include <boost/spirit/include/qi_omit.hpp>
  187. Also, see __include_structure__.
  188. [heading Namespace]
  189. [table
  190. [[Name]]
  191. [[`boost::spirit::omit // alias: boost::spirit::qi::omit` ]]
  192. ]
  193. [heading Model of]
  194. [:__unary_parser_concept__]
  195. [variablelist Notation
  196. [[`a`] [A __parser_concept__.]]
  197. ]
  198. [heading Expression Semantics]
  199. Semantics of an expression is defined only where it differs from, or is
  200. not defined in __unary_parser_concept__.
  201. [table
  202. [[Expression] [Semantics]]
  203. [[`omit[a]`] [Ignore the attribute of the subject parser, `a`]]
  204. ]
  205. [heading Attributes]
  206. [table
  207. [[Expression] [Attribute]]
  208. [[`omit[a]`] [__unused_type__]]
  209. ]
  210. [heading Complexity]
  211. [:The complexity is defined by the complexity of the subject parser, `a`]
  212. [heading Example]
  213. [note The test harness for the example(s) below is presented in the
  214. __qi_basics_examples__ section.]
  215. Some using declarations:
  216. [reference_using_declarations_omit]
  217. [reference_omit]
  218. [endsect]
  219. [/------------------------------------------------------------------------------]
  220. [section:raw Directive for Transduction Parsing (`raw[]`)]
  221. [heading Description]
  222. The `raw[]` disregards the attribute of its subject parser, instead
  223. exposing the half-open range `[first, last)` pointing to the matched
  224. characters from the input stream. The `raw[]` directive brings back the
  225. classic Spirit transduction (un-attributed) behavior for a subject
  226. parser.
  227. [heading Header]
  228. // forwards to <boost/spirit/home/qi/directive/raw.hpp>
  229. #include <boost/spirit/include/qi_raw.hpp>
  230. Also, see __include_structure__.
  231. [heading Namespace]
  232. [table
  233. [[Name]]
  234. [[`boost::spirit::raw // alias: boost::spirit::qi::raw` ]]
  235. ]
  236. [heading Model of]
  237. [:__unary_parser_concept__]
  238. [variablelist Notation
  239. [[`a`] [A __parser_concept__.]]
  240. [[`Iter`] [A __fwditer__ type.]]
  241. ]
  242. [heading Expression Semantics]
  243. Semantics of an expression is defined only where it differs from, or is
  244. not defined in __unary_parser_concept__.
  245. [table
  246. [[Expression] [Semantics]]
  247. [[`raw[a]`] [Disregard the attribute of the subject parser, `a`.
  248. Expose instead the half-open range `[first, last)`
  249. pointing to the matched characters from the input stream.]]
  250. ]
  251. [heading Attributes]
  252. See __qi_comp_attr_notation__.
  253. [table
  254. [[Expression] [Attribute]]
  255. [[`raw[a]`]
  256. [``a: A --> raw[a]: boost::iterator_range<Iter>
  257. a: Unused --> raw[a]: Unused``]]
  258. ]
  259. [note See __boost_iterator_range__.]
  260. [heading Complexity]
  261. [:The complexity is defined by the complexity of the subject parser, `a`]
  262. [heading Example]
  263. [note The test harness for the example(s) below is presented in the
  264. __qi_basics_examples__ section.]
  265. Some using declarations:
  266. [reference_using_declarations_raw]
  267. [reference_raw]
  268. [endsect]
  269. [/------------------------------------------------------------------------------]
  270. [section:repeat Repetition Parser Directive (`repeat[]`)]
  271. [heading Description]
  272. The `repeat[]` provides a more powerful and flexible mechanism for
  273. repeating a parser. There are grammars that are impractical and
  274. cumbersome, if not impossible, for the basic EBNF iteration syntax
  275. (__qi_kleene__ and the __qi_plus__) to specify. Examples:
  276. * A file name may have a maximum of 255 characters only.
  277. * A specific bitmap file format has exactly 4096 RGB color information.
  278. * A 256 bit binary string (1..256 1s or 0s).
  279. [heading Header]
  280. // forwards to <boost/spirit/home/qi/directive/repeat.hpp>
  281. #include <boost/spirit/include/qi_repeat.hpp>
  282. Also, see __include_structure__.
  283. [heading Namespace]
  284. [table
  285. [[Name]]
  286. [[`boost::spirit::repeat // alias: boost::spirit::qi::repeat` ]]
  287. [[`boost::spirit::inf // alias: boost::spirit::qi::inf` ]]
  288. ]
  289. [heading Model of]
  290. [:__unary_parser_concept__]
  291. [variablelist Notation
  292. [[`a`] [A __parser_concept__.]]
  293. [[`n`, `min`, `max`] [An `int` anything that can be converted to an
  294. `int`, or a __qi_lazy_argument__ that evaluates to
  295. anything that can be converted to an `int`.]]
  296. ]
  297. [heading Expression Semantics]
  298. Semantics of an expression is defined only where it differs from, or is
  299. not defined in __unary_parser_concept__.
  300. [table
  301. [[Expression] [Semantics]]
  302. [[`repeat[a]`] [Repeat `a` zero or more times. Same as __qi_kleene__.]]
  303. [[`repeat(n)[a]`] [Repeat `a` exactly `n` times.]]
  304. [[`repeat(min, max)[a]`] [Repeat `a` at least `min` times and at most `max` times.]]
  305. [[`repeat(min, inf)[a]`] [Repeat `a` at least `min` or more (continuing until `a`
  306. fails or the input is consumed).]]
  307. ]
  308. [heading Attributes]
  309. See __qi_comp_attr_notation__.
  310. [table
  311. [[Expression] [Attribute]]
  312. [[`repeat[a]`]
  313. [``a: A --> repeat[a]: vector<A>
  314. a: Unused --> repeat[a]: Unused``]]
  315. [[`repeat(n)[a]`]
  316. [``a: A --> repeat(n)[a]: vector<A>
  317. a: Unused --> repeat(n)[a]: Unused``]]
  318. [[`repeat(min, max)[a]`]
  319. [``a: A --> repeat(min, max)[a]: vector<A>
  320. a: Unused --> repeat(min, max)[a]: Unused``]]
  321. [[`repeat(min, inf)[a]`]
  322. [``a: A --> repeat(min, inf)[a]: vector<A>
  323. a: Unused --> repeat(min, inf)[a]: Unused``]]
  324. ]
  325. [heading Complexity]
  326. [:The overall complexity is defined by the complexity of its subject
  327. parser. The complexity of `repeat` itself is O(N), where N is the number
  328. of repetitions to execute.]
  329. [heading Example]
  330. [note The test harness for the example(s) below is presented in the
  331. __qi_basics_examples__ section.]
  332. Using the repeat directive, we can now write our examples above.
  333. Some using declarations:
  334. [reference_using_declarations_repeat]
  335. [reference_repeat]
  336. The Loop parsers can be dynamic. Consider the parsing of a binary file
  337. of Pascal-style length prefixed string, where the first byte determines
  338. the length of the incoming string. Here's a sample input:
  339. [:__pascal_string__]
  340. [reference_repeat_pascal]
  341. [endsect]
  342. [/------------------------------------------------------------------------------]
  343. [section:matches Directive Testing if Parser Succeeded (`matches[]`)]
  344. [heading Description]
  345. The `matches[]` directive executes the embedded parser and returns whether it
  346. succeeded matching.
  347. [heading Header]
  348. // forwards to <boost/spirit/home/qi/directive/matches.hpp>
  349. #include <boost/spirit/include/qi_matches.hpp>
  350. Also, see __include_structure__.
  351. [heading Namespace]
  352. [table
  353. [[Name]]
  354. [[`boost::spirit::matches // alias: boost::spirit::qi::matches` ]]
  355. ]
  356. [heading Model of]
  357. [:__unary_parser_concept__]
  358. [variablelist Notation
  359. [[`a`] [A __parser_concept__.]]
  360. ]
  361. [heading Expression Semantics]
  362. Semantics of an expression is defined only where it differs from, or is
  363. not defined in __unary_parser_concept__.
  364. [table
  365. [[Expression] [Semantics]]
  366. [[`matches[a]`] [Execute the subject parser `a`, and return as its
  367. attribute whether it succeeded. The directive itself
  368. does always succeed.]]
  369. ]
  370. [heading Attributes]
  371. [table
  372. [[Expression] [Attribute]]
  373. [[`matches[a]`] [`bool`]]
  374. ]
  375. [heading Complexity]
  376. [:The complexity is defined by the complexity of the subject parser, `a`]
  377. [heading Example]
  378. [note The test harness for the example(s) below is presented in the
  379. __qi_basics_examples__ section.]
  380. Some using declarations:
  381. [reference_using_declarations_matches]
  382. [reference_matches]
  383. [endsect]
  384. [/------------------------------------------------------------------------------]
  385. [section:skip Parser Directive Re-Establishing Skipping (`skip[]`)]
  386. [heading Description]
  387. The `skip` directive is the inverse of __qi_lexeme__ or [qi_no_skip `no_skip`].
  388. While the __qi_lexeme__ directive turns off white space
  389. skipping, the `skip` directive turns it on again. This is simply done by
  390. wrapping the parts inside the `skip` directive:
  391. skip[a]
  392. It is also possible to supply a skip parser to the `skip` directive:
  393. skip(p)[a] // Use `p` as a skipper for parsing `a`
  394. This makes it possible to:
  395. * Perform localized phrase level parsing while doing character level parsing.
  396. * Replace the current skipper anywhere with an entirely different
  397. skipper while doing phrase level parsing.
  398. [heading Header]
  399. // forwards to <boost/spirit/home/qi/directive/skip.hpp>
  400. #include <boost/spirit/include/qi_skip.hpp>
  401. Also, see __include_structure__.
  402. [heading Namespace]
  403. [table
  404. [[Name]]
  405. [[`boost::spirit::skip // alias: boost::spirit::qi::skip` ]]
  406. ]
  407. [heading Model of]
  408. [:__unary_parser_concept__]
  409. [variablelist Notation
  410. [[`a`] [A __parser_concept__.]]
  411. ]
  412. [heading Expression Semantics]
  413. Semantics of an expression is defined only where it differs from, or is
  414. not defined in __unary_parser_concept__.
  415. [table
  416. [[Expression] [Semantics]]
  417. [[`skip[a]`] [Re-establish the skipper that got inhibited by lexeme or no_skip]]
  418. [[`skip(p)[a]`] [Use `p` as a skipper for parsing `a`]]
  419. ]
  420. [heading Attributes]
  421. See __qi_comp_attr_notation__.
  422. [table
  423. [[Expression] [Attribute]]
  424. [[`skip[a]`]
  425. [``a: A --> skip[a]: A
  426. a: Unused --> skip[a]: Unused``]]
  427. [[`skip(p)[a]`]
  428. [``a: A --> skip(p)[a]: A
  429. a: Unused --> skip(p)[a]: Unused``]]
  430. ]
  431. [heading Complexity]
  432. [:The complexity is defined by the complexity of the subject parser, `a`]
  433. [heading Example]
  434. [note The test harness for the example(s) below is presented in the
  435. __qi_basics_examples__ section.]
  436. Some using declarations:
  437. [reference_using_declarations_skip]
  438. Simple usage of `skip[]`:
  439. [reference_skip]
  440. [endsect]
  441. [/------------------------------------------------------------------------------]
  442. [section:hold Parser Directive for Attribute Commit/Rollback (`hold[]`)]
  443. [heading Description]
  444. The `hold[]` directive helps managing attributes, mainly for alternative
  445. parsers. It instantiates a new attribute instance for the embedded parser. The
  446. value of that attribute instance is copied to the outer attribute if the
  447. embedded parser succeeds and it is discarded otherwise. Alternative parsers
  448. normally do not rollback changes made to the outer attribute by an failed
  449. alternative. Wrapping those alternatives into a `hold[]` directive ensures that
  450. only the succeeding alternative gets to modify the attribute.
  451. [heading Header]
  452. // forwards to <boost/spirit/home/qi/directive/hold.hpp>
  453. #include <boost/spirit/include/qi_hold.hpp>
  454. Also, see __include_structure__.
  455. [heading Namespace]
  456. [table
  457. [[Name]]
  458. [[`boost::spirit::hold // alias: boost::spirit::qi::hold` ]]
  459. ]
  460. [heading Model of]
  461. [:__unary_parser_concept__]
  462. [variablelist Notation
  463. [[`a`] [A __parser_concept__.]]
  464. ]
  465. [heading Expression Semantics]
  466. Semantics of an expression is defined only where it differs from, or is
  467. not defined in __unary_parser_concept__.
  468. [table
  469. [[Expression] [Semantics]]
  470. [[`hold[a]`] [Create a new attribute instance while parsing `a`,
  471. copying the result to the outer attribute only after
  472. `a` succeeds.]]
  473. ]
  474. [heading Attributes]
  475. See __qi_comp_attr_notation__.
  476. [table
  477. [[Expression] [Attribute]]
  478. [[`hold[a]`]
  479. [``a: A --> hold[a]: A
  480. a: Unused --> hold[a]: Unused``]]
  481. ]
  482. [note The `hold[]` directive uses `swap()` to implement the rollback/commit
  483. semantics for the attribute. For this reason the attribute type needs to
  484. to be usable with `boost::swap` (needs to either define a proper overload
  485. for `swap(attribute_type&, attribute_type&)` or expose a member function
  486. `attribute_type::swap(attribute_type&)`.]
  487. [heading Complexity]
  488. [:The complexity is defined by the complexity of the subject parser, `a`]
  489. [heading Example]
  490. [note The test harness for the example(s) below is presented in the
  491. __qi_basics_examples__ section.]
  492. Some using declarations:
  493. [reference_using_declarations_hold]
  494. [reference_hold]
  495. [endsect]
  496. [/------------------------------------------------------------------------------]
  497. [section:as Parser Directives Forcing Atomic Assignment (`as<T>, as_string[], as_wstring[]`)]
  498. [heading Description]
  499. The `as<T>` class forces the atomic assignment of it's subject's synthesized
  500. attribute. Usually, repetitive parsers (such as __qi_kleene__, etc) or
  501. sequences exposing a `vector<A>` will assign elements to the container supplied
  502. as their synthesized attribute by calling __customize_push_back_container__
  503. repeatedly. In some cases, this may be undesirable. The `as<T>` class creates a
  504. directive that will pass a temporary object of type `T` to it's subject. If the
  505. subject parser passes, the temporary object will be assigned to the directive's
  506. supplied attribute with a single call to __customize_assign_to__. If the
  507. subject parser fails, the directive's attribute is not mutated.
  508. [note `T` is required to be a container type. If __customize_is_container__
  509. does not return true for `T`, a compile-time error will occur.]
  510. [note The `as<T>` implicitly causes commit/rollback semantics
  511. similar in nature to the __qi_hold__ directive.]
  512. [caution The __customize_assign_to__ customization point may end up
  513. using __customize_push_back_container__ to assign the temporary object to the
  514. supplied attribute by default, depending on the types involved. Use the
  515. interface described in __sec_customization_points__ to manipulate the semantics
  516. of this assignment operation.]
  517. [heading Header]
  518. // forwards to <boost/spirit/home/qi/directive/as.hpp>
  519. #include <boost/spirit/include/qi_as.hpp>
  520. Also, see __include_structure__.
  521. [heading Namespace]
  522. [table
  523. [[Name]]
  524. [[`boost::spirit::as // alias: boost::spirit::qi::as` ]]
  525. [[`boost::spirit::as_string // alias: boost::spirit::qi::as_string` ]]
  526. [[`boost::spirit::as_wstring // alias: boost::spirit::qi::as_wstring` ]]
  527. ]
  528. [heading Synopsis]
  529. template <typename T>
  530. struct as;
  531. [heading Template parameters]
  532. [table
  533. [[Parameter] [Description] [Default]]
  534. [[`T`] [A container type. [none]]
  535. ]
  536. [heading Model of]
  537. [:__unary_parser_concept__]
  538. [variablelist Notation
  539. [[`a`] [A __parser_concept__.]]
  540. [[`t`] [A container of type `T`.]]
  541. [[`attr`] [The attribute supplied to the directive.]]
  542. ]
  543. [heading Expression Semantics]
  544. Semantics of an expression is defined only where it differs from, or is
  545. not defined in __unary_parser_concept__.
  546. [table
  547. [[Expression] [Semantics]]
  548. [[`as<T>()[a]`] [Create a temporary object of `t` of type `T`,
  549. and invoke the subject parser `a`, supplying
  550. `t` as an attribute. If the subject parser
  551. passes, assign `t` to `attr`.]]
  552. [[`as_string[a]`] [Equivalent to `as<std::string>()[a]`]]
  553. [[`as_wstring[a]`] [Equivalent to `as<std::wstring>()[a]`]]
  554. ]
  555. [heading Attributes]
  556. See __qi_comp_attr_notation__.
  557. [table
  558. [[Expression] [Attribute]]
  559. [[`as<T>()[a]`] [`a: A --> as<T>()[a]: T`]]
  560. ]
  561. [heading Complexity]
  562. [:The complexity is defined by the complexity of the subject parser, `a`, and
  563. the complexity of the assignment of the container `t` to the supplied
  564. attribute `attr`.]
  565. [heading Example]
  566. [note The test harness for the example(s) below is presented in the
  567. __qi_basics_examples__ section.]
  568. Some using declarations:
  569. [reference_using_declarations_as]
  570. Simple usage of `as<T>`, `as_string` and `as_wstring`:
  571. [reference_as]
  572. [endsect]
  573. [/------------------------------------------------------------------------------]
  574. [section:expect Expectation Directive (`expect[]`)]
  575. [heading Description]
  576. There are occasions in which it is expected that the input must match a
  577. particular parser or the input is invalid. Such cases generally arise
  578. after matching a portion of a grammar, such that the context is fully
  579. known. In such a situation, failure to match should result in an
  580. exception. For example, when parsing an e-mail address, a name, an "@"
  581. and a domain name must be matched or the address is invalid.
  582. The expect directive requires that the argument parser matches
  583. the input or an exception is emitted. Using on_error(), that exception
  584. can be handled by calling a handler with the context at which the
  585. parsing failed can be reported.
  586. The expect directive parses an operand parser expression which may be a
  587. single parser or a complex parser expression like a sequence.
  588. Single parser:
  589. expect[a]
  590. Parser expression:
  591. expect[a >> b >> ...]
  592. In the latter case while the plain __qi_sequence__ simply returns a no-match
  593. (returns `false`) when one of the elements fail, the expect directive
  594. throws an __qi_expectation_failure__`<Iter>` if any of the parsers
  595. (even the first parser of a sequence) fails to match.
  596. [note Spirit provides two ways to handle expectation failures by throwing an expectation exception.
  597. Use the __qi_expect__ if you do not need an exception to be thrown when the first parser of a sequence fails. ]
  598. [heading Header]
  599. // forwards to <boost/spirit/home/qi/directive/expect.hpp>
  600. #include <boost/spirit/include/qi_expect.hpp>
  601. Also, see __include_structure__.
  602. [heading Model of]
  603. [:__unary_parser_concept__]
  604. [variablelist Notation
  605. [[`a`] [A __parser_concept__]]
  606. [[`Iter`] [A __fwditer__ type]]
  607. ]
  608. [heading Expectation Failure]
  609. When the operand parser fails to match an `expectation_failure<Iter>` is thrown:
  610. template <typename Iter>
  611. struct expectation_failure : std::runtime_error
  612. {
  613. Iter first; // [first, last) iterator pointing
  614. Iter last; // to the error position in the input.
  615. __info__ what_; // Information about the nature of the error.
  616. };
  617. [heading Expression Semantics]
  618. Semantics of an expression is defined only where it differs from, or is not
  619. defined in __unary_parser_concept__.
  620. [table
  621. [[Expression] [Semantics]]
  622. [[`expect[a]`] [Match `a`. If `a` fails, throw an `expectation_failure<Iter>`]]
  623. ]
  624. [heading Attributes]
  625. See __qi_comp_attr_notation__.
  626. [table
  627. [[Expression] [Attribute]]
  628. [[`expect[a]`]
  629. [``a: A --> expect[a]: A
  630. a: Unused --> expect[a] : Unused``]]
  631. ]
  632. [heading Complexity]
  633. [:The overall complexity of the expectation parser is defined by the
  634. complexity of it's argument parser. The complexity of the expect directive itself is O(1).]
  635. [heading Example]
  636. [note The test harness for the example(s) below is presented in the
  637. __qi_basics_examples__ section.]
  638. Some using declarations:
  639. [reference_using_declarations_expectd]
  640. [reference_expectd]
  641. [endsect]
  642. [endsect]