repeat_ast_tests.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*=============================================================================
  2. Copyright (c) 2004 Chris Hoeppler
  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. // This test verifies, that reapeat_p et.al. work correctly while using AST's
  9. # include <map>
  10. # include <boost/detail/lightweight_test.hpp>
  11. # include <iostream>
  12. # include <string>
  13. # include <boost/spirit/include/classic_core.hpp>
  14. # include <boost/spirit/include/classic_loops.hpp>
  15. # include <boost/spirit/include/classic_ast.hpp>
  16. # include <boost/spirit/include/classic_tree_to_xml.hpp>
  17. using namespace BOOST_SPIRIT_CLASSIC_NS;
  18. static const int numID = 1;
  19. static const int funcID = 2;
  20. static const int expressionID = 3;
  21. struct grammar_fail : public grammar<grammar_fail>
  22. {
  23. template <typename ScannerT>
  24. struct definition
  25. {
  26. definition(grammar_fail const& /*self */)
  27. {
  28. num = leaf_node_d[real_p];
  29. func = root_node_d[ch_p('+') | '-']
  30. >> repeat_p(2)[expression];
  31. expression = func | num;
  32. }
  33. rule<ScannerT, parser_context<>, parser_tag<numID> > num;
  34. rule<ScannerT, parser_context<>, parser_tag<funcID> > func;
  35. typedef rule<ScannerT, parser_context<>, parser_tag<expressionID> > expr_t;
  36. expr_t expression;
  37. expr_t const& start() const { return expression; }
  38. };
  39. };
  40. struct grammar_success : public grammar<grammar_success>
  41. {
  42. template <typename ScannerT>
  43. struct definition
  44. {
  45. definition(grammar_success const& /*self */)
  46. {
  47. num = leaf_node_d[real_p];
  48. func = root_node_d[ch_p('+') | '-']
  49. >> expression >> expression; // line differing from grammar_fail
  50. expression = func | num;
  51. }
  52. rule<ScannerT, parser_context<>, parser_tag<numID> > num;
  53. rule<ScannerT, parser_context<>, parser_tag<funcID> > func;
  54. typedef rule<ScannerT, parser_context<>, parser_tag<expressionID> > expr_t;
  55. expr_t expression;
  56. expr_t const& start() const { return expression; }
  57. };
  58. };
  59. int main() {
  60. std::map<parser_id, std::string> rule_names;
  61. rule_names[expressionID] = "expression";
  62. rule_names[funcID] = "func";
  63. rule_names[numID] = "num";
  64. std::string test("+ 1 - 2 3");
  65. // case 1
  66. grammar_fail g_fail;
  67. tree_parse_info<> info1 = ast_parse(test.c_str(), g_fail, space_p);
  68. BOOST_TEST(info1.full);
  69. //std::cout << "...Case 1: Using repeat_p\n";
  70. //tree_to_xml(std::cerr, info1.trees, test, rule_names);
  71. // case 2
  72. grammar_success g_success;
  73. tree_parse_info<> info2 = ast_parse(test.c_str(), g_success, space_p);
  74. BOOST_TEST(info2.full);
  75. //std::cout << "...Case 2: No repeat_p\n";
  76. //tree_to_xml(std::cerr, info2.trees, test, rule_names);
  77. // could be used for test case instead of printing the xml stuff:
  78. BOOST_TEST(info2.trees.begin()->children.size() == 2);
  79. BOOST_TEST(info1.trees.begin()->children.size() == 2);
  80. return boost::report_errors();
  81. }