num_list3.qbk 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 Redux - list syntax]
  8. So far, we've been using the syntax:
  9. double_ >> *(',' >> double_)
  10. to parse a comma-delimited list of numbers. Such lists are common in parsing and
  11. Spirit provides a simpler shortcut for them. The expression above can be
  12. simplified to:
  13. double_ % ','
  14. read as: a list of doubles separated by `','`.
  15. This sample, again a variation of our previous example, demonstrates just that:
  16. template <typename Iterator>
  17. bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
  18. {
  19. using x3::double_;
  20. using x3::phrase_parse;
  21. using x3::_attr;
  22. using ascii::space;
  23. auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };
  24. bool r = phrase_parse(first, last,
  25. // Begin grammar
  26. (
  27. double_[push_back] % ','
  28. )
  29. ,
  30. // End grammar
  31. space);
  32. if (first != last) // fail if we did not get a full match
  33. return false;
  34. return r;
  35. }
  36. The full cpp file for this example can be found here:
  37. [@../../../example/x3/num_list/num_list3.cpp num_list3.cpp]
  38. [endsect]