confix.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2009 Chris Hoeppler
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // The purpose of this example is to demonstrate different use cases for the
  6. // confix directive.
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. //[qi_confix_includes
  11. #include <boost/spirit/include/qi.hpp>
  12. #include <boost/spirit/repository/include/qi_confix.hpp>
  13. //]
  14. namespace client {
  15. //[qi_confix_using
  16. using boost::spirit::eol;
  17. using boost::spirit::lexeme;
  18. using boost::spirit::ascii::alnum;
  19. using boost::spirit::ascii::char_;
  20. using boost::spirit::ascii::space;
  21. using boost::spirit::qi::parse;
  22. using boost::spirit::qi::phrase_parse;
  23. using boost::spirit::repository::confix;
  24. //]
  25. //[qi_confix_cpp_comment
  26. template <typename Iterator>
  27. bool parse_cpp_comment(Iterator first, Iterator last, std::string& attr)
  28. {
  29. bool r = parse(first, last,
  30. confix("//", eol)[*(char_ - eol)], // grammar
  31. attr); // attribute
  32. if (!r || first != last) // fail if we did not get a full match
  33. return false;
  34. return r;
  35. }
  36. //]
  37. //[qi_confix_c_comment
  38. template <typename Iterator>
  39. bool parse_c_comment(Iterator first, Iterator last, std::string& attr)
  40. {
  41. bool r = parse(first, last,
  42. confix("/*", "*/")[*(char_ - "*/")], // grammar
  43. attr); // attribute
  44. if (!r || first != last) // fail if we did not get a full match
  45. return false;
  46. return r;
  47. }
  48. //]
  49. //[qi_confix_tagged_data
  50. template <typename Iterator>
  51. bool parse_tagged(Iterator first, Iterator last, std::string& attr)
  52. {
  53. bool r = phrase_parse(first, last,
  54. confix("<b>", "</b>")[lexeme[*(char_ - '<')]], // grammar
  55. space, // skip
  56. attr); // attribute
  57. if (!r || first != last) // fail if we did not get a full match
  58. return false;
  59. return r;
  60. }
  61. //]
  62. }
  63. int main()
  64. {
  65. // C++ comment
  66. std::string comment("// This is a comment\n");
  67. std::string attr;
  68. bool r = client::parse_cpp_comment(comment.begin(), comment.end(), attr);
  69. std::cout << "Parsing a C++ comment";
  70. if (r && attr == " This is a comment")
  71. std::cout << " succeeded." << std::endl;
  72. else
  73. std::cout << " failed" << std::endl;
  74. // C comment
  75. comment = "/* This is another comment */";
  76. attr.clear();
  77. r = client::parse_c_comment(comment.begin(), comment.end(), attr);
  78. std::cout << "Parsing a C comment";
  79. if (r && attr == " This is another comment ")
  80. std::cout << " succeeded." << std::endl;
  81. else
  82. std::cout << " failed" << std::endl;
  83. // Tagged data
  84. std::string data = "<b> This is the body. </b>";
  85. attr.clear();
  86. r = client::parse_tagged(data.begin(), data.end(), attr);
  87. std::cout << "Parsing tagged data";
  88. if (r && attr == "This is the body. ")
  89. std::cout << " succeeded." << std::endl;
  90. else
  91. std::cout << " failed" << std::endl;
  92. return 0;
  93. }