operator.qbk 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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:operator Parser Operators]
  8. Operators are used as a means for object composition and embedding.
  9. Simple parsers may be composed to form composites through operator
  10. overloading, crafted to approximate the syntax of __peg__ (PEG). An
  11. expression such as:
  12. a | b
  13. yields a new parser type which is a composite of its operands, `a` and
  14. `b`.
  15. This module includes different parsers which get instantiated if one of
  16. the overloaded operators is used with more primitive parser constructs.
  17. It includes Alternative (`|`), And-predicate (unary `&`), Difference
  18. (`-`), Expect (`>`), Kleene star (unary `*`), Lists (`%`), Not-predicate (`!`),
  19. Optional (unary `-`), Permutation (`^`), Plus (unary
  20. `+`), Sequence (`>>`), and Sequential-Or (`||`).
  21. [heading Module Header]
  22. // forwards to <boost/spirit/home/qi/operator.hpp>
  23. #include <boost/spirit/include/qi_operator.hpp>
  24. Also, see __include_structure__.
  25. [/------------------------------------------------------------------------------]
  26. [section:alternative Alternative Parser (`a | b`)]
  27. [heading Description]
  28. The alternative operator, `a | b`, matches one of two or more operands
  29. (`a`, `b`, ... etc.):
  30. a | b | ...
  31. Alternative operands are tried one by one on a first-match-wins basis
  32. starting from the leftmost operand. After a successfully matched
  33. alternative is found, the parser concludes its search, essentially
  34. short-circuiting the search for other potentially viable candidates.
  35. This short-circuiting implicitly gives the highest priority to the
  36. leftmost alternative.
  37. Short-circuiting is done in the same manner as C or C++'s logical
  38. expressions; e.g. `if (x < 3 || y < 2)` where, if `x < 3`, the `y < 2`
  39. test is not done at all. In addition to providing an implicit priority
  40. rule for alternatives which is necessary, given its non-deterministic
  41. nature, short-circuiting improves the execution time. If the order of
  42. your alternatives is logically irrelevant, strive to put the (expected)
  43. most common choice first for maximum efficiency.
  44. [heading Header]
  45. // forwards to <boost/spirit/home/qi/operator/alternative.hpp>
  46. #include <boost/spirit/include/qi_alternative.hpp>
  47. Also, see __include_structure__.
  48. [heading Model of]
  49. [:__nary_parser_concept__]
  50. [variablelist Notation
  51. [[`a`, `b`] [A __parser_concept__]]
  52. ]
  53. [heading Expression Semantics]
  54. Semantics of an expression is defined only where it differs from, or is not
  55. defined in __nary_parser_concept__.
  56. [table
  57. [[Expression] [Semantics]]
  58. [[`a | b`] [Match `a` or `b`.]]
  59. ]
  60. [heading Attributes]
  61. See __qi_comp_attr_notation__.
  62. [table
  63. [[Expression] [Attribute]]
  64. [[`a | b`]
  65. [``a: A, b: B --> (a | b): variant<A, B>
  66. a: A, b: Unused --> (a | b): optional<A>
  67. a: A, b: B, c: Unused --> (a | b | c): optional<variant<A, B> >
  68. a: Unused, b: B --> (a | b): optional<B>
  69. a: Unused, b: Unused --> (a | b): Unused
  70. a: A, b: A --> (a | b): A``]]
  71. ]
  72. [note Alternative parsers do not roll back changes made to the outer attribute
  73. because of a failed alternative. If you need to enforce that only the
  74. succeeded alternative changes the outer attribute please utilize the
  75. directive __qi_hold__`[]`.]
  76. [heading Complexity]
  77. [:The overall complexity of the alternative parser is defined by the sum
  78. of the complexities of its elements. The complexity of the alternative
  79. parser itself is O(N), where N is the number of alternatives.]
  80. [heading Example]
  81. [note The test harness for the example(s) below is presented in the
  82. __qi_basics_examples__ section.]
  83. Some using declarations:
  84. [reference_using_declarations_alternative]
  85. [reference_alternative]
  86. [endsect] [/ Alternative]
  87. [/------------------------------------------------------------------------------]
  88. [section:and_predicate And-Predicate Parser (`&a`)]
  89. [heading Description]
  90. Syntactic predicates assert a certain conditional syntax to be satisfied
  91. before evaluating another production. Similar to semantic predicates,
  92. __qi_eps__, syntactic predicates do not consume any input. The /and-predicate/,
  93. `&a`, is a positive syntactic predicate that returns a zero
  94. length match only if its predicate matches.
  95. [heading Header]
  96. // forwards to <boost/spirit/home/qi/operator/and_predicate.hpp>
  97. #include <boost/spirit/include/qi_and_predicate.hpp>
  98. Also, see __include_structure__.
  99. [heading Model of]
  100. [:__unary_parser_concept__]
  101. [variablelist Notation
  102. [[`a`] [A __parser_concept__]]
  103. ]
  104. [heading Expression Semantics]
  105. Semantics of an expression is defined only where it differs from, or is
  106. not defined in __unary_parser_concept__.
  107. [table
  108. [[Expression] [Semantics]]
  109. [[`&a`] [If the predicate `a` matches, return a zero
  110. length match. Otherwise, fail.]]
  111. ]
  112. [heading Attributes]
  113. See __qi_comp_attr_notation__.
  114. [table
  115. [[Expression] [Attribute]]
  116. [[`&a`] [__unused_type__]]
  117. ]
  118. [heading Complexity]
  119. [:The complexity is defined by the complexity of the predicate, `a`]
  120. [heading Example]
  121. [note The test harness for the example(s) below is presented in the
  122. __qi_basics_examples__ section.]
  123. [reference_and_predicate]
  124. [endsect] [/ And Predicate]
  125. [/------------------------------------------------------------------------------]
  126. [section:difference Difference Parser (`a - b`)]
  127. [heading Description]
  128. The difference operator, `a - b`, is a binary operator that matches the
  129. first (LHS) operand but not the second (RHS). [footnote Unlike classic
  130. Spirit, with Spirit2, the expression will always fail if the RHS is a
  131. successful match regardless if the RHS matches less characters. For
  132. example, the rule `lit("policeman") - "police"` will always fail to
  133. match. Spirit2 does not count the matching chars while parsing and there
  134. is no reliable and fast way to check if the LHS matches more than the
  135. RHS.]
  136. [heading Header]
  137. // forwards to <boost/spirit/home/qi/operator/difference.hpp>
  138. #include <boost/spirit/include/qi_difference.hpp>
  139. Also, see __include_structure__.
  140. [heading Model of]
  141. [:__binary_parser_concept__]
  142. [variablelist Notation
  143. [[`a`, `b`] [A __parser_concept__]]
  144. ]
  145. [heading Expression Semantics]
  146. Semantics of an expression is defined only where it differs from, or is
  147. not defined in __binary_parser_concept__.
  148. [table
  149. [[Expression] [Semantics]]
  150. [[`a - b`] [Parse `a` but not `b`.]]
  151. ]
  152. [heading Attributes]
  153. See __qi_comp_attr_notation__.
  154. [table
  155. [[Expression] [Attribute]]
  156. [[`a - b`]
  157. [``a: A, b: B --> (a - b): A
  158. a: Unused, b: B --> (a - b): Unused``]]
  159. ]
  160. [heading Complexity]
  161. [:The complexity of the difference parser is defined by the sum of the
  162. complexities of both operands.]
  163. [heading Example]
  164. [note The test harness for the example(s) below is presented in the
  165. __qi_basics_examples__ section.]
  166. [reference_difference]
  167. [endsect] [/ Difference]
  168. [/------------------------------------------------------------------------------]
  169. [section:expect Expectation Operator (`a > b`)]
  170. [heading Description]
  171. There are occasions in which it is expected that the input must match a
  172. particular parser or the input is invalid. Such cases generally arise
  173. after matching a portion of a grammar, such that the context is fully
  174. known. In such a situation, failure to match should result in an
  175. exception. For example, when parsing an e-mail address, after matching a
  176. name and "@" there must be a domain name or the address is invalid.
  177. The expectation operator (>) requires that the following parser match
  178. the input or an exception is emitted. Using on_error(), that exception
  179. can be handled by calling a handler with the context at which the
  180. parsing failed can be reported.
  181. By contrast, the follows operator (>>) does not require that the
  182. following parser match the input, which allows for backtracking or
  183. simply returning false from the parse() function with no exceptions.
  184. Like the __qi_sequence__, the expectation operator, `a > b`, parses two or
  185. more operands (`a`, `b`, ... etc.), in sequence:
  186. a > b > ...
  187. However, while the plain __qi_sequence__ simply returns a no-match
  188. (returns `false`) when one of the elements fail, the expectation: `>`
  189. operator throws an __qi_expectation_failure__`<Iter>` when the second or
  190. succeeding operands (all operands except the first) fail to match.
  191. [heading Header]
  192. // forwards to <boost/spirit/home/qi/operator/expect.hpp>
  193. #include <boost/spirit/include/qi_expect.hpp>
  194. Also, see __include_structure__.
  195. [heading Model of]
  196. [:__nary_parser_concept__]
  197. [variablelist Notation
  198. [[`a`, `b`] [A __parser_concept__]]
  199. [[`Iter`] [A __fwditer__ type]]
  200. ]
  201. [heading Expectation Failure]
  202. When any operand, except the first, fail to match an
  203. `expectation_failure<Iter>` is thrown:
  204. template <typename Iter>
  205. struct expectation_failure : std::runtime_error
  206. {
  207. Iter first; // [first, last) iterator pointing
  208. Iter last; // to the error position in the input.
  209. __info__ what_; // Information about the nature of the error.
  210. };
  211. [heading Expression Semantics]
  212. Semantics of an expression is defined only where it differs from, or is not
  213. defined in __nary_parser_concept__.
  214. [table
  215. [[Expression] [Semantics]]
  216. [[`a > b`] [Match `a` followed by `b`. If `a` fails, no-match.
  217. If `b` fails, throw an `expectation_failure<Iter>`]]
  218. ]
  219. [note If you need an exception to be thrown if `a` fails, use the __qi_expectd__ directive instead of the expectation operator.]
  220. [heading Attributes]
  221. See __qi_comp_attr_notation__.
  222. [table
  223. [[Expression] [Attribute]]
  224. [[`a > b`]
  225. [``a: A, b: B --> (a > b): tuple<A, B>
  226. a: A, b: Unused --> (a > b): A
  227. a: Unused, b: B --> (a > b): B
  228. a: Unused, b: Unused --> (a > b): Unused
  229. a: A, b: A --> (a > b): vector<A>
  230. a: vector<A>, b: A --> (a > b): vector<A>
  231. a: A, b: vector<A> --> (a > b): vector<A>
  232. a: vector<A>, b: vector<A> --> (a > b): vector<A>``]]
  233. ]
  234. [heading Complexity]
  235. [:The overall complexity of the expectation parser is defined by the sum
  236. of the complexities of its elements. The complexity of the expectation
  237. operator itself is O(N), where N is the number of elements in the
  238. sequence.]
  239. [heading Example]
  240. [note The test harness for the example(s) below is presented in the
  241. __qi_basics_examples__ section.]
  242. Some using declarations:
  243. [reference_using_declarations_expect]
  244. [reference_expect]
  245. [endsect] [/ Expectation]
  246. [/------------------------------------------------------------------------------]
  247. [section:kleene Kleene Parser (`*a`)]
  248. [heading Description]
  249. The kleene operator, `*a`, is a unary operator that matches its operand
  250. zero or more times.
  251. [heading Header]
  252. // forwards to <boost/spirit/home/qi/operator/kleene.hpp>
  253. #include <boost/spirit/include/qi_kleene.hpp>
  254. Also, see __include_structure__.
  255. [heading Model of]
  256. [:__unary_parser_concept__]
  257. [variablelist Notation
  258. [[`a`] [A __parser_concept__]]
  259. ]
  260. [heading Expression Semantics]
  261. Semantics of an expression is defined only where it differs from, or is not
  262. defined in __unary_parser_concept__.
  263. [table
  264. [[Expression] [Semantics]]
  265. [[`*a`] [Match `a` zero or more times.]]
  266. ]
  267. [heading Attributes]
  268. See __qi_comp_attr_notation__.
  269. [table
  270. [[Expression] [Attribute]]
  271. [[`*a`]
  272. [``a: A --> *a: vector<A>
  273. a: Unused --> *a: Unused``]]
  274. ]
  275. [heading Complexity]
  276. [:The overall complexity of the Kleene star is defined by the complexity
  277. of its subject, `a`, multiplied by the number of repetitions. The
  278. complexity of the Kleene star itself is O(N), where N is the number
  279. successful repetitions.]
  280. [heading Example]
  281. [note The test harness for the example(s) below is presented in the
  282. __qi_basics_examples__ section.]
  283. [reference_kleene]
  284. [endsect] [/ Kleene]
  285. [/------------------------------------------------------------------------------]
  286. [section:list List Parser (`a % b`)]
  287. [heading Description]
  288. The list operator, `a % b`, is a binary operator that matches a list of
  289. one or more repetitions of `a` separated by occurrences of `b`. This is
  290. equivalent to `a >> *(b >> a)`.
  291. [heading Header]
  292. // forwards to <boost/spirit/home/qi/operator/list.hpp>
  293. #include <boost/spirit/include/qi_list.hpp>
  294. Also, see __include_structure__.
  295. [heading Model of]
  296. [:__binary_parser_concept__]
  297. [variablelist Notation
  298. [[`a`, `b`] [A __parser_concept__]]
  299. ]
  300. [heading Expression Semantics]
  301. Semantics of an expression is defined only where it differs from, or is
  302. not defined in __binary_parser_concept__.
  303. [table
  304. [[Expression] [Semantics]]
  305. [[`a % b`] [Match a list of one or more repetitions of `a`
  306. separated by occurrences of `b`. This is
  307. equivalent to `a >> *(omit[b] >> a)`.]]
  308. ]
  309. [note The list operator `a % b` omits the attribute of parser `b`. If you need
  310. `b` to contribute to the result attribute choose `a >> *(b >> a)` over
  311. the list operator `a % b`.]
  312. [heading Attributes]
  313. See __qi_comp_attr_notation__.
  314. [table
  315. [[Expression] [Attribute]]
  316. [[`a % b`]
  317. [``a: A, b: B --> (a % b): vector<A>
  318. a: Unused, b: B --> (a % b): Unused``]]
  319. ]
  320. [heading Complexity]
  321. [:The overall complexity of the List is defined by the complexity of its
  322. subject, `a`, multiplied by the number of repetitions. The complexity of
  323. the List itself is O(N), where N is the number successful repetitions.]
  324. [heading Example]
  325. [note The test harness for the example(s) below is presented in the
  326. __qi_basics_examples__ section.]
  327. [reference_list]
  328. [endsect] [/ List]
  329. [/------------------------------------------------------------------------------]
  330. [section:not_predicate Not-Predicate Parser (`!a`)]
  331. [heading Description]
  332. Syntactic predicates assert a certain conditional syntax to be satisfied
  333. before evaluating another production. Similar to semantic predicates,
  334. __qi_eps__, syntactic predicates do not consume any input. The /not-predicate/,
  335. `!a`, is a negative syntactic predicate that returns a zero
  336. length match only if its predicate fails to match.
  337. [heading Header]
  338. // forwards to <boost/spirit/home/qi/operator/not_predicate.hpp>
  339. #include <boost/spirit/include/qi_not_predicate.hpp>
  340. Also, see __include_structure__.
  341. [heading Model of]
  342. [:__unary_parser_concept__]
  343. [variablelist Notation
  344. [[`a`] [A __parser_concept__]]
  345. ]
  346. [heading Expression Semantics]
  347. Semantics of an expression is defined only where it differs from, or is
  348. not defined in __unary_parser_concept__.
  349. [table
  350. [[Expression] [Semantics]]
  351. [[`!a`] [If the predicate `a` matches, fail. Otherwise,
  352. return a zero length match.]]
  353. ]
  354. [heading Attributes]
  355. See __qi_comp_attr_notation__.
  356. [table
  357. [[Expression] [Attribute]]
  358. [[`!a`] [__unused_type__]]
  359. ]
  360. [heading Complexity]
  361. [:The complexity is defined by the complexity of the predicate, `a`]
  362. [heading Example]
  363. [note The test harness for the example(s) below is presented in the
  364. __qi_basics_examples__ section.]
  365. [reference_not_predicate]
  366. [endsect] [/ Not Predicate]
  367. [/------------------------------------------------------------------------------]
  368. [section:optional Optional Parser (`-a`)]
  369. [heading Description]
  370. The optional operator, `-a`, is a unary operator that matches its
  371. operand zero or one time.
  372. [heading Header]
  373. // forwards to <boost/spirit/home/qi/operator/optional.hpp>
  374. #include <boost/spirit/include/qi_optional.hpp>
  375. Also, see __include_structure__.
  376. [heading Model of]
  377. [:__unary_parser_concept__]
  378. [variablelist Notation
  379. [[`a`] [A __parser_concept__]]
  380. ]
  381. [heading Expression Semantics]
  382. Semantics of an expression is defined only where it differs from, or is not
  383. defined in __unary_parser_concept__.
  384. [table
  385. [[Expression] [Semantics]]
  386. [[`-a`] [Match `a` zero or one time.]]
  387. ]
  388. [heading Attributes]
  389. See __qi_comp_attr_notation__.
  390. [table
  391. [[Expression] [Attribute]]
  392. [[`-a`]
  393. [``a: A --> -a: optional<A>
  394. a: Unused --> -a: Unused``]]
  395. ]
  396. [heading Complexity]
  397. [:The complexity is defined by the complexity of the operand, `a`]
  398. [heading Example]
  399. [note The test harness for the example(s) below is presented in the
  400. __qi_basics_examples__ section.]
  401. [reference_optional]
  402. [endsect] [/ Optional]
  403. [/------------------------------------------------------------------------------]
  404. [section:permutation Permutation Parser (`a ^ b`)]
  405. [heading Description]
  406. The permutation operator, `a ^ b`, matches one or more operands (`a`, `b`,
  407. ... etc.) in any order:
  408. a ^ b ^ ...
  409. The operands are the elements in the permutation set. Each element in
  410. the permutation set may occur at most once, but not all elements of the
  411. given set need to be present. Note that by this definition, the
  412. permutation operator is not limited to strict permutations.
  413. For example:
  414. char_('a') ^ 'b' ^ 'c'
  415. matches:
  416. "a", "ab", "abc", "cba", "bca" ... etc.
  417. [heading Header]
  418. // forwards to <boost/spirit/home/qi/operator/permutation.hpp>
  419. #include <boost/spirit/include/qi_permutation.hpp>
  420. Also, see __include_structure__.
  421. [heading Model of]
  422. [:__nary_parser_concept__]
  423. [variablelist Notation
  424. [[`a`, `b`] [A __parser_concept__]]
  425. ]
  426. [heading Expression Semantics]
  427. Semantics of an expression is defined only where it differs from, or is not
  428. defined in __nary_parser_concept__.
  429. [table
  430. [[Expression] [Semantics]]
  431. [[`a ^ b`] [Match `a` or `b` in any order. Each operand
  432. may match zero or one time as long as at least
  433. one operand matches.]]
  434. ]
  435. [heading Attributes]
  436. See __qi_comp_attr_notation__.
  437. [table
  438. [[Expression] [Attribute]]
  439. [[`a ^ b`]
  440. [``a: A, b: B --> (a ^ b): tuple<optional<A>, optional<B> >
  441. a: A, b: Unused --> (a ^ b): optional<A>
  442. a: Unused, b: B --> (a ^ b): optional<B>
  443. a: Unused, b: Unused --> (a ^ b): Unused``]]
  444. ]
  445. [heading Complexity]
  446. [:The overall complexity of the permutation parser is defined by the sum
  447. of the complexities of its elements, s, multiplied by log s. The
  448. complexity of the permutation parser itself is O(N log N), where N is
  449. the number of elements.]
  450. [heading Example]
  451. [note The test harness for the example(s) below is presented in the
  452. __qi_basics_examples__ section.]
  453. Some using declarations:
  454. [reference_using_declarations_permutation]
  455. [reference_permutation]
  456. [endsect] [/ Permutation]
  457. [/------------------------------------------------------------------------------]
  458. [section:plus Plus Parser (`+a`)]
  459. [heading Description]
  460. The plus operator, `+a`, is a unary operator that matches its operand one
  461. or more times.
  462. [heading Header]
  463. // forwards to <boost/spirit/home/qi/operator/plus.hpp>
  464. #include <boost/spirit/include/qi_plus.hpp>
  465. Also, see __include_structure__.
  466. [heading Model of]
  467. [:__unary_parser_concept__]
  468. [variablelist Notation
  469. [[`a`] [A __parser_concept__]]
  470. ]
  471. [heading Expression Semantics]
  472. Semantics of an expression is defined only where it differs from, or is not
  473. defined in __unary_parser_concept__.
  474. [table
  475. [[Expression] [Semantics]]
  476. [[`+a`] [Match `a` one or more times.]]
  477. ]
  478. [heading Attributes]
  479. See __qi_comp_attr_notation__.
  480. [table
  481. [[Expression] [Attribute]]
  482. [[`+a`]
  483. [``a: A --> +a: vector<A>
  484. a: Unused --> +a: Unused``]]
  485. ]
  486. [heading Complexity]
  487. [:The overall complexity of the Plus is defined by the complexity of its
  488. subject, `a`, multiplied by the number of repetitions. The complexity of
  489. the Plus itself is O(N), where N is the number successful repetitions.]
  490. [heading Example]
  491. [note The test harness for the example(s) below is presented in the
  492. __qi_basics_examples__ section.]
  493. [reference_plus]
  494. [endsect] [/ Plus]
  495. [/------------------------------------------------------------------------------]
  496. [section:sequence Sequence Parser (`a >> b`)]
  497. [heading Description]
  498. The sequence operator, `a >> b`, parses two or more operands (`a`,
  499. `b`, ... etc.), in sequence:
  500. a >> b >> ...
  501. [heading Header]
  502. // forwards to <boost/spirit/home/qi/operator/sequence.hpp>
  503. #include <boost/spirit/include/qi_sequence.hpp>
  504. Also, see __include_structure__.
  505. [heading Model of]
  506. [:__nary_parser_concept__]
  507. [variablelist Notation
  508. [[`a`, `b`] [A __parser_concept__]]
  509. ]
  510. [heading Expression Semantics]
  511. Semantics of an expression is defined only where it differs from, or is not
  512. defined in __nary_parser_concept__.
  513. [table
  514. [[Expression] [Semantics]]
  515. [[`a >> b`] [Match `a` followed by `b`.]]
  516. ]
  517. [heading Attributes]
  518. See __qi_comp_attr_notation__.
  519. [table
  520. [[Expression] [Attribute]]
  521. [[`a >> b`]
  522. [``a: A, b: B --> (a >> b): tuple<A, B>
  523. a: A, b: Unused --> (a >> b): A
  524. a: Unused, b: B --> (a >> b): B
  525. a: Unused, b: Unused --> (a >> b): Unused
  526. a: A, b: A --> (a >> b): vector<A>
  527. a: vector<A>, b: A --> (a >> b): vector<A>
  528. a: A, b: vector<A> --> (a >> b): vector<A>
  529. a: vector<A>, b: vector<A> --> (a >> b): vector<A>``]]
  530. ]
  531. [heading Complexity]
  532. [:The overall complexity of the sequence parser is defined by the sum of
  533. the complexities of its elements. The complexity of the sequence itself
  534. is O(N), where N is the number of elements in the sequence.]
  535. [heading Example]
  536. Some using declarations:
  537. [reference_using_declarations_sequence]
  538. [note The test harness for the example(s) below is presented in the
  539. __qi_basics_examples__ section.]
  540. [reference_sequence]
  541. [endsect] [/ Sequence]
  542. [/------------------------------------------------------------------------------]
  543. [section:sequential_or Sequential Or Parser (`a || b`)]
  544. [heading Description]
  545. The sequential-or operator, `a || b`, matches `a` or `b` or `a` followed
  546. by `b`. That is, if both `a` and `b` match, it must be in sequence; this
  547. is equivalent to `a >> -b | b`:
  548. a || b || ...
  549. [heading Header]
  550. // forwards to <boost/spirit/home/qi/operator/sequential_or.hpp>
  551. #include <boost/spirit/include/qi_sequential_or.hpp>
  552. Also, see __include_structure__.
  553. [heading Model of]
  554. [:__nary_parser_concept__]
  555. [variablelist Notation
  556. [[`a`, `b`] [A __parser_concept__]]
  557. ]
  558. [heading Expression Semantics]
  559. Semantics of an expression is defined only where it differs from, or is not
  560. defined in __nary_parser_concept__.
  561. [table
  562. [[Expression] [Semantics]]
  563. [[`a || b`] [Match `a` or `b` in sequence. equivalent to `a >> -b | b`]]
  564. ]
  565. [heading Attributes]
  566. See __qi_comp_attr_notation__.
  567. [table
  568. [[Expression] [Attribute]]
  569. [[`a || b`]
  570. [``a: A, b: B --> (a || b): tuple<optional<A>, optional<B> >
  571. a: A, b: Unused --> (a || b): optional<A>
  572. a: Unused, b: B --> (a || b): optional<B>
  573. a: Unused, b: Unused --> (a || b): Unused
  574. a: A, b: A --> (a || b): vector<optional<A> >``]]
  575. ]
  576. [note The sequential-or parser behaves attribute-wise very similar to the
  577. plain sequence parser (`a >> b`) in the sense that it exposes the
  578. attributes of its elements separately. For instance, if you attach a
  579. semantic action to the whole sequential-or:
  580. ``
  581. (int_ || int_)[print_pair(_1, _2)]
  582. ``
  583. the function object `print_pair` would be invoked with the
  584. attribute of the first `int_` (`boost::optional<int>`) as its first
  585. parameter and the attribute of the second `int_` (`boost::optional<int>`
  586. as well) as its second parameter.]
  587. [heading Complexity]
  588. [:The overall complexity of the sequential-or parser is defined by the
  589. sum of the complexities of its elements. The complexity of the
  590. sequential-or itself is O(N), where N is the number of elements in the
  591. sequence.]
  592. [heading Example]
  593. [note The test harness for the example(s) below is presented in the
  594. __qi_basics_examples__ section.]
  595. Some using declarations:
  596. [reference_using_declarations_sequential_or]
  597. [reference_sequential_or]
  598. [endsect] [/ Sequential Or]
  599. [endsect]