num_list2.qbk 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. [/==============================================================================
  2. Copyright (C) 2001-2015 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 Number List - stuffing numbers into a std::vector]
  8. This sample demonstrates a parser for a comma separated list of numbers and
  9. using a semantic action to collect the numbers into a `std::vector`.
  10. template <typename Iterator>
  11. bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
  12. {
  13. using x3::double_;
  14. using x3::phrase_parse;
  15. using x3::_attr;
  16. using ascii::space;
  17. auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };
  18. bool r = phrase_parse(first, last,
  19. // Begin grammar
  20. (
  21. double_[push_back]
  22. >> *(',' >> double_[push_back])
  23. )
  24. ,
  25. // End grammar
  26. space);
  27. if (first != last) // fail if we did not get a full match
  28. return false;
  29. return r;
  30. }
  31. The full cpp file for this example can be found here:
  32. [@../../../example/x3/num_list/num_list2.cpp num_list2.cpp]
  33. This, again, is the same parser as before. This time, instead of summing up the
  34. numbers, we stuff them in a `std::vector`.
  35. [&](auto& ctx){ v.push_back(_attr(ctx)); }
  36. [endsect]