select_p_with_rule.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*=============================================================================
  2. Copyright (c) 2004 Vyacheslav E. Andrejev
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #include <iostream>
  9. #define PHOENIX_LIMIT 2
  10. #define BOOST_SPIRIT_SELECT_LIMIT 2
  11. #define BOOST_SPIRIT_RULE_SCANNERTYPE_LIMIT 2
  12. #include <boost/spirit/include/classic_core.hpp>
  13. #include <boost/spirit/include/classic_lists.hpp>
  14. #include <boost/spirit/include/classic_select.hpp>
  15. using namespace BOOST_SPIRIT_CLASSIC_NS;
  16. using namespace std;
  17. struct format_grammar : public grammar<format_grammar>
  18. {
  19. template <typename ScannerT>
  20. struct definition
  21. {
  22. definition(format_grammar const& /*self*/)
  23. {
  24. descriptor_list =
  25. list_p(format_descriptor, ch_p(','))
  26. ;
  27. format_descriptor =
  28. select_p(E_descriptor, EN_descriptor)
  29. ;
  30. E_descriptor = // E[w[.d][Ee]]
  31. lexeme_d
  32. [
  33. (ch_p('E') - (str_p("EN")))
  34. >> !(
  35. min_limit_d(1u)[uint_p]
  36. >> !(ch_p('.') >> uint_p)
  37. >> !(ch_p('E') >> min_limit_d(1u)[uint_p])
  38. )
  39. ]
  40. ;
  41. EN_descriptor = // EN[w[.d][Ee]]
  42. lexeme_d
  43. [
  44. str_p("EN")
  45. >> !(
  46. min_limit_d(1u)[uint_p]
  47. >> !(ch_p('.') >> uint_p)
  48. >> !(ch_p('E') >> min_limit_d(1u)[uint_p])
  49. )
  50. ]
  51. ;
  52. }
  53. rule<ScannerT> descriptor_list;
  54. rule<ScannerT> format_descriptor;
  55. rule<ScannerT> E_descriptor;
  56. rule<ScannerT> EN_descriptor;
  57. rule<ScannerT> const& start() const
  58. {
  59. return descriptor_list;
  60. }
  61. };
  62. };
  63. int main()
  64. {
  65. format_grammar grammar;
  66. const char* format = "E2, EN15.7, E20.10E3, E, EN";
  67. parse_info<> pi = parse(format, grammar, blank_p);
  68. if (pi.full) {
  69. cout << "Test concluded successful" << endl;
  70. return 0;
  71. }
  72. else {
  73. BOOST_SPIRIT_ASSERT(false); // Test fails
  74. return -1;
  75. }
  76. }