]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/spirit/doc/qi/warming_up.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / spirit / doc / qi / warming_up.qbk
CommitLineData
7c673cae
FG
1[/==============================================================================
2 Copyright (C) 2001-2011 Joel de Guzman
3 Copyright (C) 2001-2011 Hartmut Kaiser
4
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
9[section Warming up]
10
11We'll start by showing examples of parser expressions to give you a feel on how
12to build parsers from the simplest parser, building up as we go. When comparing
13EBNF to __spirit__, the expressions may seem awkward at first. __spirit__ heavily
14uses operator overloading to accomplish its magic.
15
16[heading Trivial Example #1 Parsing a number]
17
18Create a parser that will parse a floating-point number.
19
20 double_
21
22(You've got to admit, that's trivial!) The above code actually generates a
23Spirit floating point parser (a built-in parser). Spirit has many pre-defined
24parsers and consistent naming conventions help you keep from going insane!
25
26[heading Trivial Example #2 Parsing two numbers]
27
28Create a parser that will accept a line consisting of two floating-point numbers.
29
30 double_ >> double_
31
32Here you see the familiar floating-point numeric parser `double_` used twice,
33once for each number. What's that `>>` operator doing in there? Well, they had
34to be separated by something, and this was chosen as the "followed by" sequence
35operator. The above program creates a parser from two simpler parsers, glueing
36them together with the sequence operator. The result is a parser that is a
37composition of smaller parsers. Whitespace between numbers can implicitly be
38consumed depending on how the parser is invoked (see below).
39
40[note When we combine parsers, we end up with a "bigger" parser, but
41 it's still a parser. Parsers can get bigger and bigger, nesting more and more,
42 but whenever you glue two parsers together, you end up with one bigger parser.
43 This is an important concept.
44]
45
46[heading Trivial Example #3 Parsing zero or more numbers]
47
48Create a parser that will accept zero or more floating-point numbers.
49
50 *double_
51
52This is like a regular-expression Kleene Star, though the syntax might look a
53bit odd for a C++ programmer not used to seeing the `*` operator overloaded like
54this. Actually, if you know regular expressions it may look odd too since the
55star is before the expression it modifies. C'est la vie. Blame it on the fact
56that we must work with the syntax rules of C++.
57
58Any expression that evaluates to a parser may be used with the Kleene Star.
59Keep in mind that C++ operator precedence rules may require you to put
60expressions in parentheses for complex expressions. The Kleene Star
61is also known as a Kleene Closure, but we call it the Star in most places.
62
63[heading Trivial Example #4 Parsing a comma-delimited list of numbers]
64
65This example will create a parser that accepts a comma-delimited list of
66numbers.
67
68 double_ >> *(char_(',') >> double_)
69
70Notice `char_(',')`. It is a literal character parser that can recognize the
71comma `','`. In this case, the Kleene Star is modifying a more complex parser,
72namely, the one generated by the expression:
73
74 (char_(',') >> double_)
75
76Note that this is a case where the parentheses are necessary. The Kleene star
77encloses the complete expression above.
78
79[heading Let's Parse!]
80
81We're done with defining the parser. So the next step is now invoking this
82parser to do its work. There are a couple of ways to do this. For now, we will
83use the `phrase_parse` function. One overload of this function accepts four
84arguments:
85
86# An iterator pointing to the start of the input
87# An iterator pointing to one past the end of the input
88# The parser object
89# Another parser called the skip parser
90
91In our example, we wish to skip spaces and tabs. Another parser named `space`
92is included in Spirit's repertoire of predefined parsers. It is a very simple
93parser that simply recognizes whitespace. We will use `space` as our skip
94parser. The skip parser is the one responsible for skipping characters in
95between parser elements such as the `double_` and `char_`.
96
97Ok, so now let's parse!
98
99[import ../../example/qi/num_list1.cpp]
100[tutorial_numlist1]
101
102The parse function returns `true` or `false` depending on the result of
103the parse. The first iterator is passed by reference. On a successful
104parse, this iterator is repositioned to the rightmost position consumed
105by the parser. If this becomes equal to `last`, then we have a full
106match. If not, then we have a partial match. A partial match happens
107when the parser is only able to parse a portion of the input.
108
109Note that we inlined the parser directly in the call to parse. Upon calling
110parse, the expression evaluates into a temporary, unnamed parser which is passed
111into the parse() function, used, and then destroyed.
112
113Here, we opted to make the parser generic by making it a template, parameterized
114by the iterator type. By doing so, it can take in data coming from any STL
115conforming sequence as long as the iterators conform to a forward iterator.
116
117You can find the full cpp file here: [@../../example/qi/num_list1.cpp]
118
119[note `char` and `wchar_t` operands
120
121The careful reader may notice that the parser expression has `','` instead of
122`char_(',')` as the previous examples did. This is ok due to C++ syntax rules of
123conversion. There are `>>` operators that are overloaded to accept a `char` or
124`wchar_t` argument on its left or right (but not both). An operator may be
125overloaded if at least one of its parameters is a user-defined type. In this
126case, the `double_` is the 2nd argument to `operator>>`, and so the proper
127overload of `>>` is used, converting `','` into a character literal parser.
128
129The problem with omitting the `char_` should be obvious: `'a' >> 'b'` is not a
130spirit parser, it is a numeric expression, right-shifting the ASCII (or another
131encoding) value of `'a'` by the ASCII value of `'b'`. However, both
132`char_('a') >> 'b'` and `'a' >> char_('b')` are Spirit sequence parsers
133for the letter `'a'` followed by `'b'`. You'll get used to it, sooner or later.
134]
135
136Finally, take note that we test for a full match (i.e. the parser fully parsed
137the input) by checking if the first iterator, after parsing, is equal to the end
138iterator. You may strike out this part if partial matches are to be allowed.
139
140[endsect] [/ Warming up]