Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

3.2. Dealing with input containing more than what is needed

[Note] Note

Note that you can find everything that has been included and defined so far here.

Let's try to give the parser two numbers instead of one:

> exp_parser1::apply<BOOST_METAPARSE_STRING("11 13")>::type
mpl_::integral_c<int, 11>

You might be surprised by this: the parser did not return an error. It parsed the first number, 11 and ignored 13. The way int_ works is that it parses the number at the beginning of the input text and ignores the rest of the input.

So exp_parser1 has a bug: our little language consists of one number, not a list of numbers. Let's fix our parser to treat more than one numbers as an invalid input:

> #include <boost/metaparse/entire_input.hpp>

This gives us the entire_input template class. We can wrap int_ with entire_input indicating that the number we parse with int_ should be the entire input. Anything that comes after that is an error. So our parser is entire_input<int_> now. Let's wrap it with build_parser:

> using exp_parser2 = build_parser<entire_input<int_>>;

Let's try this new parser out:

> exp_parser2::apply<BOOST_METAPARSE_STRING("13")>::type
mpl_::integral_c<int, 13>

It can still parse numbers. Let's try to give it two numbers:

> exp_parser2::apply<BOOST_METAPARSE_STRING("11 13")>::type
<< compilation error >>

This generates a compilation error, since the parser failed.


PrevUpHomeNext