Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext
11.3.2. Using foldl_reject_incomplete_start_with_parser at other places as well

We have replaced one foldl_start_with_parser with foldl_reject_incomplete_start_with_parser. Other layers (mult_exp, unary_exp, etc) use folding as well. Let's use it at all layers:

> struct plus_exp6;
> using paren_exp5 = middle_of<lparen_token, plus_exp6, rparen_token>;
> using primary_exp4 = one_of<int_token, paren_exp5, fail<missing_primary_expression>>;
> using unary_exp4 = \
...> foldr_start_with_parser< \
...>   minus_token, \
...>   primary_exp4, \
...>   boost::mpl::lambda<boost::mpl::negate<boost::mpl::_1>>::type \
...> >;
> using mult_exp7 = \
...> foldl_reject_incomplete_start_with_parser< \
...>   sequence<one_of<times_token, divides_token>, unary_exp4>, \
...>   unary_exp4, \
...>   boost::mpl::quote2<binary_op> \
...> >;
> struct plus_exp6 : \
...> foldl_reject_incomplete_start_with_parser< \
...>   sequence<one_of<plus_token, minus_token>, mult_exp7>, \
...>   mult_exp7, \
...>   boost::mpl::quote2<binary_op> \
...> > {};
> using exp_parser23 = build_parser<plus_exp6>;

copy-paste friendly version

[Note] Note

Note that unary_exp4 uses foldr_start_with_parser instead of foldr_reject_incomplete_start_with_parser. The reason behind it is that there is no foldr_reject_incomplete_start_with_parser. foldr_start_with_parser applies the primary_exp4 parser when minus_token does not accept the input any more. Therefore, it is supposed to catch the errors of incomplete expressions after the repetition.

Let's try different invalid expressions:

> exp_parser23::apply<BOOST_METAPARSE_STRING("1+(2*")>::type
<< compilation error >>
..... x__________________PARSING_FAILED__________________x<1, 6, missing_primary_expression> ....
<< compilation error >>

> exp_parser23::apply<BOOST_METAPARSE_STRING("1+(2*3")>::type
<< compilation error >>
..... x__________________PARSING_FAILED__________________x<1, 7, unpaired<1, 3, literal_expected<')'>>> ....
<< compilation error >>

PrevUpHomeNext