grammar_tests.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Joel de Guzman
  3. Copyright (c) 2003 Hartmut Kaiser
  4. http://spirit.sourceforge.net/
  5. Use, modification and distribution is subject to the Boost Software
  6. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #include <iostream>
  10. #include <boost/detail/lightweight_test.hpp>
  11. //#define BOOST_SPIRIT_DEBUG
  12. #define BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE
  13. #include <boost/spirit/include/classic_core.hpp>
  14. #include <boost/spirit/include/classic_grammar_def.hpp>
  15. using namespace BOOST_SPIRIT_CLASSIC_NS;
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // This feature is disabled on non compliant compilers (e.g. Borland 5.5.1
  18. // VC6 and VC7)
  19. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && \
  20. !BOOST_WORKAROUND(__BORLANDC__, <= 0x551) && \
  21. !BOOST_WORKAROUND(__GNUC__, < 3)
  22. # define BOOST_SPIRIT_USE_GRAMMARDEF
  23. #endif
  24. //////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Grammar tests
  27. //
  28. ///////////////////////////////////////////////////////////////////////////////
  29. struct num_list : public grammar<num_list>
  30. {
  31. enum {
  32. default_rule = 0,
  33. num_rule = 1
  34. };
  35. template <typename ScannerT>
  36. struct definition
  37. #if defined(BOOST_SPIRIT_USE_GRAMMARDEF)
  38. : public grammar_def<rule<ScannerT>, same>
  39. #endif
  40. {
  41. definition(num_list const& /*self*/)
  42. {
  43. num = int_p;
  44. r = num >> *(',' >> num);
  45. #if defined(BOOST_SPIRIT_USE_GRAMMARDEF)
  46. this->start_parsers(r, num);
  47. #endif
  48. BOOST_SPIRIT_DEBUG_RULE(num);
  49. BOOST_SPIRIT_DEBUG_RULE(r);
  50. }
  51. rule<ScannerT> r, num;
  52. #if !defined(BOOST_SPIRIT_USE_GRAMMARDEF)
  53. rule<ScannerT> const& start() const { return r; }
  54. #endif
  55. };
  56. };
  57. struct num_list_ex : public grammar<num_list_ex>
  58. {
  59. enum {
  60. default_rule = 0,
  61. num_rule = 1,
  62. integer_rule = 2
  63. };
  64. template <typename ScannerT>
  65. struct definition
  66. #if defined(BOOST_SPIRIT_USE_GRAMMARDEF)
  67. : public grammar_def<rule<ScannerT>, same, int_parser<int, 10, 1, -1> >
  68. #endif
  69. {
  70. definition(num_list_ex const& /*self*/)
  71. {
  72. num = integer;
  73. r = num >> *(',' >> num);
  74. #if defined(BOOST_SPIRIT_USE_GRAMMARDEF)
  75. this->start_parsers(r, num, integer);
  76. #endif
  77. BOOST_SPIRIT_DEBUG_RULE(num);
  78. BOOST_SPIRIT_DEBUG_RULE(r);
  79. }
  80. rule<ScannerT> r, num;
  81. int_parser<int, 10, 1, -1> integer;
  82. #if !defined(BOOST_SPIRIT_USE_GRAMMARDEF)
  83. rule<ScannerT> const& start() const { return r; }
  84. #endif
  85. };
  86. };
  87. void
  88. grammar_tests()
  89. {
  90. num_list nlist;
  91. BOOST_SPIRIT_DEBUG_GRAMMAR(nlist);
  92. parse_info<char const*> pi;
  93. pi = parse("123, 456, 789", nlist, space_p);
  94. BOOST_TEST(pi.hit);
  95. BOOST_TEST(pi.full);
  96. #if defined(BOOST_SPIRIT_USE_GRAMMARDEF)
  97. num_list_ex nlistex;
  98. BOOST_SPIRIT_DEBUG_GRAMMAR(nlistex);
  99. pi = parse("123, 456, 789", nlist.use_parser<num_list::default_rule>(),
  100. space_p);
  101. BOOST_TEST(pi.hit);
  102. BOOST_TEST(pi.full);
  103. pi = parse("123", nlist.use_parser<num_list::num_rule>(), space_p);
  104. BOOST_TEST(pi.hit);
  105. BOOST_TEST(pi.full);
  106. pi = parse("123, 456, 789", nlistex, space_p);
  107. BOOST_TEST(pi.hit);
  108. BOOST_TEST(pi.full);
  109. pi = parse("123, 456, 789",
  110. nlistex.use_parser<num_list_ex::default_rule>(), space_p);
  111. BOOST_TEST(pi.hit);
  112. BOOST_TEST(pi.full);
  113. pi = parse("123", nlistex.use_parser<num_list_ex::num_rule>(), space_p);
  114. BOOST_TEST(pi.hit);
  115. BOOST_TEST(pi.full);
  116. pi = parse("123", nlistex.use_parser<num_list_ex::integer_rule>(),
  117. space_p);
  118. BOOST_TEST(pi.hit);
  119. BOOST_TEST(pi.full);
  120. #endif // defined(BOOST_SPIRIT_USE_GRAMMARDEF)
  121. }
  122. ///////////////////////////////////////////////////////////////////////////////
  123. //
  124. // Main
  125. //
  126. ///////////////////////////////////////////////////////////////////////////////
  127. int
  128. main()
  129. {
  130. grammar_tests();
  131. return boost::report_errors();
  132. }