1 [/==============================================================================
2 Copyright (C) 2001-2011 Joel de Guzman
3 Copyright (C) 2001-2011 Hartmut Kaiser
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ===============================================================================/]
8 [section:operator Parser Operators]
10 Operators are used as a means for object composition and embedding.
11 Simple parsers may be composed to form composites through operator
12 overloading, crafted to approximate the syntax of __peg__ (PEG). An
17 yields a new parser type which is a composite of its operands, `a` and
20 This module includes different parsers which get instantiated if one of
21 the overloaded operators is used with more primitive parser constructs.
22 It includes Alternative (`|`), And-predicate (unary `&`), Difference
23 (`-`), Expect (`>`), Kleene star (unary `*`), Lists (`%`), Not-predicate (`!`),
24 Optional (unary `-`), Permutation (`^`), Plus (unary
25 `+`), Sequence (`>>`), and Sequential-Or (`||`).
27 [heading Module Header]
29 // forwards to <boost/spirit/home/qi/operator.hpp>
30 #include <boost/spirit/include/qi_operator.hpp>
32 Also, see __include_structure__.
34 [/------------------------------------------------------------------------------]
35 [section:alternative Alternative Parser (`a | b`)]
39 The alternative operator, `a | b`, matches one of two or more operands
44 Alternative operands are tried one by one on a first-match-wins basis
45 starting from the leftmost operand. After a successfully matched
46 alternative is found, the parser concludes its search, essentially
47 short-circuiting the search for other potentially viable candidates.
48 This short-circuiting implicitly gives the highest priority to the
51 Short-circuiting is done in the same manner as C or C++'s logical
52 expressions; e.g. `if (x < 3 || y < 2)` where, if `x < 3`, the `y < 2`
53 test is not done at all. In addition to providing an implicit priority
54 rule for alternatives which is necessary, given its non-deterministic
55 nature, short-circuiting improves the execution time. If the order of
56 your alternatives is logically irrelevant, strive to put the (expected)
57 most common choice first for maximum efficiency.
61 // forwards to <boost/spirit/home/qi/operator/alternative.hpp>
62 #include <boost/spirit/include/qi_alternative.hpp>
64 Also, see __include_structure__.
68 [:__nary_parser_concept__]
70 [variablelist Notation
71 [[`a`, `b`] [A __parser_concept__]]
74 [heading Expression Semantics]
76 Semantics of an expression is defined only where it differs from, or is not
77 defined in __nary_parser_concept__.
80 [[Expression] [Semantics]]
81 [[`a | b`] [Match `a` or `b`.]]
86 See __qi_comp_attr_notation__.
89 [[Expression] [Attribute]]
91 [``a: A, b: B --> (a | b): variant<A, B>
92 a: A, b: Unused --> (a | b): optional<A>
93 a: A, b: B, c: Unused --> (a | b | c): optional<variant<A, B> >
94 a: Unused, b: B --> (a | b): optional<B>
95 a: Unused, b: Unused --> (a | b): Unused
96 a: A, b: A --> (a | b): A``]]
99 [note Alternative parsers do not roll back changes made to the outer attribute
100 because of a failed alternative. If you need to enforce that only the
101 succeeded alternative changes the outer attribute please utilize the
102 directive __qi_hold__`[]`.]
106 [:The overall complexity of the alternative parser is defined by the sum
107 of the complexities of its elements. The complexity of the alternative
108 parser itself is O(N), where N is the number of alternatives.]
112 [note The test harness for the example(s) below is presented in the
113 __qi_basics_examples__ section.]
115 Some using declarations:
117 [reference_using_declarations_alternative]
119 [reference_alternative]
121 [endsect] [/ Alternative]
123 [/------------------------------------------------------------------------------]
124 [section:and_predicate And-Predicate Parser (`&a`)]
126 [heading Description]
128 Syntactic predicates assert a certain conditional syntax to be satisfied
129 before evaluating another production. Similar to semantic predicates,
130 __qi_eps__, syntactic predicates do not consume any input. The /and-predicate/,
131 `&a`, is a positive syntactic predicate that returns a zero
132 length match only if its predicate matches.
136 // forwards to <boost/spirit/home/qi/operator/and_predicate.hpp>
137 #include <boost/spirit/include/qi_and_predicate.hpp>
139 Also, see __include_structure__.
143 [:__unary_parser_concept__]
145 [variablelist Notation
146 [[`a`] [A __parser_concept__]]
149 [heading Expression Semantics]
151 Semantics of an expression is defined only where it differs from, or is
152 not defined in __unary_parser_concept__.
155 [[Expression] [Semantics]]
156 [[`&a`] [If the predicate `a` matches, return a zero
157 length match. Otherwise, fail.]]
162 See __qi_comp_attr_notation__.
165 [[Expression] [Attribute]]
166 [[`&a`] [__unused_type__]]
171 [:The complexity is defined by the complexity of the predicate, `a`]
175 [note The test harness for the example(s) below is presented in the
176 __qi_basics_examples__ section.]
178 [reference_and_predicate]
180 [endsect] [/ And Predicate]
182 [/------------------------------------------------------------------------------]
183 [section:difference Difference Parser (`a - b`)]
185 [heading Description]
187 The difference operator, `a - b`, is a binary operator that matches the
188 first (LHS) operand but not the second (RHS). [footnote Unlike classic
189 Spirit, with Spirit2, the expression will always fail if the RHS is a
190 successful match regardless if the RHS matches less characters. For
191 example, the rule `lit("policeman") - "police"` will always fail to
192 match. Spirit2 does not count the matching chars while parsing and there
193 is no reliable and fast way to check if the LHS matches more than the
198 // forwards to <boost/spirit/home/qi/operator/difference.hpp>
199 #include <boost/spirit/include/qi_difference.hpp>
201 Also, see __include_structure__.
205 [:__binary_parser_concept__]
207 [variablelist Notation
208 [[`a`, `b`] [A __parser_concept__]]
211 [heading Expression Semantics]
213 Semantics of an expression is defined only where it differs from, or is
214 not defined in __binary_parser_concept__.
217 [[Expression] [Semantics]]
218 [[`a - b`] [Parse `a` but not `b`.]]
223 See __qi_comp_attr_notation__.
226 [[Expression] [Attribute]]
228 [``a: A, b: B --> (a - b): A
229 a: Unused, b: B --> (a - b): Unused``]]
234 [:The complexity of the difference parser is defined by the sum of the
235 complexities of both operands.]
239 [note The test harness for the example(s) below is presented in the
240 __qi_basics_examples__ section.]
242 [reference_difference]
244 [endsect] [/ Difference]
246 [/------------------------------------------------------------------------------]
247 [section:expect Expectation Parser (`a > b`)]
249 [heading Description]
251 There are occasions in which it is expected that the input must match a
252 particular parser or the input is invalid. Such cases generally arise
253 after matching a portion of a grammar, such that the context is fully
254 known. In such a situation, failure to match should result in an
255 exception. For example, when parsing an e-mail address, after matching a
256 name and "@" there must be a domain name or the address is invalid.
258 The expectation operator (>) requires that the following parser match
259 the input or an exception is emitted. Using on_error(), that exception
260 can be handled by calling a handler with the context at which the
261 parsing failed can be reported.
263 By contrast, the follows operator (>>) does not require that the
264 following parser match the input, which allows for backtracking or
265 simply returning false from the parse() function with no exceptions.
267 Like the __qi_sequence__, the expectation operator, `a > b`, parses two or
268 more operands (`a`, `b`, ... etc.), in sequence:
272 However, while the plain __qi_sequence__ simply returns a no-match
273 (returns `false`) when one of the elements fail, the expectation: `>`
274 operator throws an __qi_expectation_failure__`<Iter>` when the second or
275 succeeding operands (all operands except the first) fail to match.
279 // forwards to <boost/spirit/home/qi/operator/expect.hpp>
280 #include <boost/spirit/include/qi_expect.hpp>
282 Also, see __include_structure__.
286 [:__nary_parser_concept__]
288 [variablelist Notation
289 [[`a`, `b`] [A __parser_concept__]]
290 [[`Iter`] [A __fwditer__ type]]
293 [heading Expectation Failure]
295 When any operand, except the first, fail to match an
296 `expectation_failure<Iter>` is thrown:
298 template <typename Iter>
299 struct expectation_failure : std::runtime_error
301 Iter first; // [first, last) iterator pointing
302 Iter last; // to the error position in the input.
303 __info__ what_; // Information about the nature of the error.
306 [heading Expression Semantics]
308 Semantics of an expression is defined only where it differs from, or is not
309 defined in __nary_parser_concept__.
312 [[Expression] [Semantics]]
313 [[`a > b`] [Match `a` followed by `b`. If `a` fails, no-match.
314 If `b` fails, throw an `expectation_failure<Iter>`]]
319 See __qi_comp_attr_notation__.
322 [[Expression] [Attribute]]
324 [``a: A, b: B --> (a > b): tuple<A, B>
325 a: A, b: Unused --> (a > b): A
326 a: Unused, b: B --> (a > b): B
327 a: Unused, b: Unused --> (a > b): Unused
329 a: A, b: A --> (a > b): vector<A>
330 a: vector<A>, b: A --> (a > b): vector<A>
331 a: A, b: vector<A> --> (a > b): vector<A>
332 a: vector<A>, b: vector<A> --> (a > b): vector<A>``]]
337 [:The overall complexity of the expectation parser is defined by the sum
338 of the complexities of its elements. The complexity of the expectation
339 operator itself is O(N), where N is the number of elements in the
344 [note The test harness for the example(s) below is presented in the
345 __qi_basics_examples__ section.]
347 Some using declarations:
349 [reference_using_declarations_expect]
353 [endsect] [/ Expectation]
355 [/------------------------------------------------------------------------------]
356 [section:kleene Kleene Parser (`*a`)]
358 [heading Description]
360 The kleene operator, `*a`, is a unary operator that matches its operand
365 // forwards to <boost/spirit/home/qi/operator/kleene.hpp>
366 #include <boost/spirit/include/qi_kleene.hpp>
368 Also, see __include_structure__.
372 [:__unary_parser_concept__]
374 [variablelist Notation
375 [[`a`] [A __parser_concept__]]
378 [heading Expression Semantics]
380 Semantics of an expression is defined only where it differs from, or is not
381 defined in __unary_parser_concept__.
384 [[Expression] [Semantics]]
385 [[`*a`] [Match `a` zero or more times.]]
390 See __qi_comp_attr_notation__.
393 [[Expression] [Attribute]]
395 [``a: A --> *a: vector<A>
396 a: Unused --> *a: Unused``]]
401 [:The overall complexity of the Kleene star is defined by the complexity
402 of its subject, `a`, multiplied by the number of repetitions. The
403 complexity of the Kleene star itself is O(N), where N is the number
404 successful repetitions.]
408 [note The test harness for the example(s) below is presented in the
409 __qi_basics_examples__ section.]
415 [/------------------------------------------------------------------------------]
416 [section:list List Parser (`a % b`)]
418 [heading Description]
420 The list operator, `a % b`, is a binary operator that matches a list of
421 one or more repetitions of `a` separated by occurrences of `b`. This is
422 equivalent to `a >> *(b >> a)`.
426 // forwards to <boost/spirit/home/qi/operator/list.hpp>
427 #include <boost/spirit/include/qi_list.hpp>
429 Also, see __include_structure__.
433 [:__binary_parser_concept__]
435 [variablelist Notation
436 [[`a`, `b`] [A __parser_concept__]]
439 [heading Expression Semantics]
441 Semantics of an expression is defined only where it differs from, or is
442 not defined in __binary_parser_concept__.
445 [[Expression] [Semantics]]
446 [[`a % b`] [Match a list of one or more repetitions of `a`
447 separated by occurrences of `b`. This is equivalent
448 to `a >> *(b >> a)`.]]
453 See __qi_comp_attr_notation__.
456 [[Expression] [Attribute]]
458 [``a: A, b: B --> (a % b): vector<A>
459 a: Unused, b: B --> (a % b): Unused``]]
464 [:The overall complexity of the List is defined by the complexity of its
465 subject, `a`, multiplied by the number of repetitions. The complexity of
466 the List itself is O(N), where N is the number successful repetitions.]
470 [note The test harness for the example(s) below is presented in the
471 __qi_basics_examples__ section.]
477 [/------------------------------------------------------------------------------]
478 [section:not_predicate Not-Predicate Parser (`!a`)]
480 [heading Description]
482 Syntactic predicates assert a certain conditional syntax to be satisfied
483 before evaluating another production. Similar to semantic predicates,
484 __qi_eps__, syntactic predicates do not consume any input. The /not-predicate/,
485 `!a`, is a negative syntactic predicate that returns a zero
486 length match only if its predicate fails to match.
490 // forwards to <boost/spirit/home/qi/operator/not_predicate.hpp>
491 #include <boost/spirit/include/qi_not_predicate.hpp>
493 Also, see __include_structure__.
497 [:__unary_parser_concept__]
499 [variablelist Notation
500 [[`a`] [A __parser_concept__]]
503 [heading Expression Semantics]
505 Semantics of an expression is defined only where it differs from, or is
506 not defined in __unary_parser_concept__.
509 [[Expression] [Semantics]]
510 [[`!a`] [If the predicate `a` matches, fail. Otherwise,
511 return a zero length match.]]
516 See __qi_comp_attr_notation__.
519 [[Expression] [Attribute]]
520 [[`!a`] [__unused_type__]]
525 [:The complexity is defined by the complexity of the predicate, `a`]
529 [note The test harness for the example(s) below is presented in the
530 __qi_basics_examples__ section.]
532 [reference_not_predicate]
534 [endsect] [/ Not Predicate]
536 [/------------------------------------------------------------------------------]
537 [section:optional Optional Parser (`-a`)]
539 [heading Description]
541 The optional operator, `-a`, is a unary operator that matches its
542 operand zero or one time.
546 // forwards to <boost/spirit/home/qi/operator/optional.hpp>
547 #include <boost/spirit/include/qi_optional.hpp>
549 Also, see __include_structure__.
553 [:__unary_parser_concept__]
555 [variablelist Notation
556 [[`a`] [A __parser_concept__]]
559 [heading Expression Semantics]
561 Semantics of an expression is defined only where it differs from, or is not
562 defined in __unary_parser_concept__.
565 [[Expression] [Semantics]]
566 [[`-a`] [Match `a` zero or one time.]]
571 See __qi_comp_attr_notation__.
574 [[Expression] [Attribute]]
576 [``a: A --> -a: optional<A>
577 a: Unused --> -a: Unused``]]
582 [:The complexity is defined by the complexity of the operand, `a`]
586 [note The test harness for the example(s) below is presented in the
587 __qi_basics_examples__ section.]
591 [endsect] [/ Optional]
593 [/------------------------------------------------------------------------------]
594 [section:permutation Permutation Parser (`a ^ b`)]
596 [heading Description]
598 The permutation operator, `a ^ b`, matches one or more operands (`a`, `b`,
599 ... etc.) in any order:
603 The operands are the elements in the permutation set. Each element in
604 the permutation set may occur at most once, but not all elements of the
605 given set need to be present. Note that by this definition, the
606 permutation operator is not limited to strict permutations.
610 char_('a') ^ 'b' ^ 'c'
614 "a", "ab", "abc", "cba", "bca" ... etc.
618 // forwards to <boost/spirit/home/qi/operator/permutation.hpp>
619 #include <boost/spirit/include/qi_permutation.hpp>
621 Also, see __include_structure__.
625 [:__nary_parser_concept__]
627 [variablelist Notation
628 [[`a`, `b`] [A __parser_concept__]]
631 [heading Expression Semantics]
633 Semantics of an expression is defined only where it differs from, or is not
634 defined in __nary_parser_concept__.
637 [[Expression] [Semantics]]
638 [[`a ^ b`] [Match `a` or `b` in any order. Each operand
639 may match zero or one time as long as at least
640 one operand matches.]]
645 See __qi_comp_attr_notation__.
648 [[Expression] [Attribute]]
650 [``a: A, b: B --> (a ^ b): tuple<optional<A>, optional<B> >
651 a: A, b: Unused --> (a ^ b): optional<A>
652 a: Unused, b: B --> (a ^ b): optional<B>
653 a: Unused, b: Unused --> (a ^ b): Unused``]]
658 [:The overall complexity of the permutation parser is defined by the sum
659 of the complexities of its elements, s, multiplied by log s. The
660 complexity of the permutation parser itself is O(N log N), where N is
661 the number of elements.]
665 [note The test harness for the example(s) below is presented in the
666 __qi_basics_examples__ section.]
668 Some using declarations:
670 [reference_using_declarations_permutation]
672 [reference_permutation]
674 [endsect] [/ Permutation]
676 [/------------------------------------------------------------------------------]
677 [section:plus Plus Parser (`+a`)]
679 [heading Description]
681 The plus operator, `+a`, is a unary operator that matches its operand one
686 // forwards to <boost/spirit/home/qi/operator/plus.hpp>
687 #include <boost/spirit/include/qi_plus.hpp>
689 Also, see __include_structure__.
693 [:__unary_parser_concept__]
695 [variablelist Notation
696 [[`a`] [A __parser_concept__]]
699 [heading Expression Semantics]
701 Semantics of an expression is defined only where it differs from, or is not
702 defined in __unary_parser_concept__.
705 [[Expression] [Semantics]]
706 [[`+a`] [Match `a` one or more times.]]
711 See __qi_comp_attr_notation__.
714 [[Expression] [Attribute]]
716 [``a: A --> +a: vector<A>
717 a: Unused --> +a: Unused``]]
722 [:The overall complexity of the Plus is defined by the complexity of its
723 subject, `a`, multiplied by the number of repetitions. The complexity of
724 the Plus itself is O(N), where N is the number successful repetitions.]
728 [note The test harness for the example(s) below is presented in the
729 __qi_basics_examples__ section.]
735 [/------------------------------------------------------------------------------]
736 [section:sequence Sequence Parser (`a >> b`)]
738 [heading Description]
740 The sequence operator, `a >> b`, parses two or more operands (`a`,
741 `b`, ... etc.), in sequence:
747 // forwards to <boost/spirit/home/qi/operator/sequence.hpp>
748 #include <boost/spirit/include/qi_sequence.hpp>
750 Also, see __include_structure__.
754 [:__nary_parser_concept__]
756 [variablelist Notation
757 [[`a`, `b`] [A __parser_concept__]]
760 [heading Expression Semantics]
762 Semantics of an expression is defined only where it differs from, or is not
763 defined in __nary_parser_concept__.
766 [[Expression] [Semantics]]
767 [[`a >> b`] [Match `a` followed by `b`.]]
772 See __qi_comp_attr_notation__.
775 [[Expression] [Attribute]]
777 [``a: A, b: B --> (a >> b): tuple<A, B>
778 a: A, b: Unused --> (a >> b): A
779 a: Unused, b: B --> (a >> b): B
780 a: Unused, b: Unused --> (a >> b): Unused
782 a: A, b: A --> (a >> b): vector<A>
783 a: vector<A>, b: A --> (a >> b): vector<A>
784 a: A, b: vector<A> --> (a >> b): vector<A>
785 a: vector<A>, b: vector<A> --> (a >> b): vector<A>``]]
790 [:The overall complexity of the sequence parser is defined by the sum of
791 the complexities of its elements. The complexity of the sequence itself
792 is O(N), where N is the number of elements in the sequence.]
796 Some using declarations:
798 [reference_using_declarations_sequence]
800 [note The test harness for the example(s) below is presented in the
801 __qi_basics_examples__ section.]
805 [endsect] [/ Sequence]
807 [/------------------------------------------------------------------------------]
808 [section:sequential_or Sequential Or Parser (`a || b`)]
810 [heading Description]
812 The sequential-or operator, `a || b`, matches `a` or `b` or `a` followed
813 by `b`. That is, if both `a` and `b` match, it must be in sequence; this
814 is equivalent to `a >> -b | b`:
820 // forwards to <boost/spirit/home/qi/operator/sequential_or.hpp>
821 #include <boost/spirit/include/qi_sequential_or.hpp>
823 Also, see __include_structure__.
827 [:__nary_parser_concept__]
829 [variablelist Notation
830 [[`a`, `b`] [A __parser_concept__]]
833 [heading Expression Semantics]
835 Semantics of an expression is defined only where it differs from, or is not
836 defined in __nary_parser_concept__.
839 [[Expression] [Semantics]]
840 [[`a || b`] [Match `a` or `b` in sequence. equivalent to `a >> -b | b`]]
845 See __qi_comp_attr_notation__.
848 [[Expression] [Attribute]]
850 [``a: A, b: B --> (a || b): tuple<optional<A>, optional<B> >
851 a: A, b: Unused --> (a || b): optional<A>
852 a: Unused, b: B --> (a || b): optional<B>
853 a: Unused, b: Unused --> (a || b): Unused
855 a: A, b: A --> (a || b): vector<optional<A> >``]]
858 [note The sequential-or parser behaves attribute-wise very similar to the
859 plain sequence parser (`a >> b`) in the sense that it exposes the
860 attributes of its elements separately. For instance, if you attach a
861 semantic action to the whole sequential-or:
863 (int_ || int_)[print_pair(_1, _2)]
865 the function object `print_pair` would be invoked with the
866 attribute of the first `int_` (`boost::optional<int>`) as its first
867 parameter and the attribute of the second `int_` (`boost::optional<int>`
868 as well) as its second parameter.]
872 [:The overall complexity of the sequential-or parser is defined by the
873 sum of the complexities of its elements. The complexity of the
874 sequential-or itself is O(N), where N is the number of elements in the
879 [note The test harness for the example(s) below is presented in the
880 __qi_basics_examples__ section.]
882 Some using declarations:
884 [reference_using_declarations_sequential_or]
886 [reference_sequential_or]
888 [endsect] [/ Sequential Or]