complex_number_easier.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. // Copyright (c) 2001-2010 Joel de Guzman
  3. //
  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. //
  8. // A complex number micro generator - take 2. Look'ma no semantic actions!
  9. //
  10. // [ HK July 26, 2009 ] spirit2
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #include <boost/config/warning_disable.hpp>
  14. #include <boost/spirit/include/qi.hpp>
  15. #include <boost/spirit/include/karma.hpp>
  16. #include <boost/spirit/include/phoenix_core.hpp>
  17. #include <boost/spirit/include/phoenix_operator.hpp>
  18. #include <boost/fusion/include/std_pair.hpp>
  19. #include <iostream>
  20. #include <string>
  21. #include <complex>
  22. namespace client
  23. {
  24. ///////////////////////////////////////////////////////////////////////////
  25. // Our complex number parser/compiler (that's just a copy of the complex
  26. // number example from Qi (see examples/qi/complex_number.cpp)
  27. ///////////////////////////////////////////////////////////////////////////
  28. template <typename Iterator>
  29. bool parse_complex(Iterator first, Iterator last, std::complex<double>& c)
  30. {
  31. using boost::spirit::qi::double_;
  32. using boost::spirit::qi::_1;
  33. using boost::spirit::qi::phrase_parse;
  34. using boost::spirit::ascii::space;
  35. using boost::phoenix::ref;
  36. double rN = 0.0;
  37. double iN = 0.0;
  38. bool r = phrase_parse(first, last,
  39. (
  40. '(' >> double_[ref(rN) = _1]
  41. >> -(',' >> double_[ref(iN) = _1]) >> ')'
  42. | double_[ref(rN) = _1]
  43. ),
  44. space);
  45. if (!r || first != last) // fail if we did not get a full match
  46. return false;
  47. c = std::complex<double>(rN, iN);
  48. return r;
  49. }
  50. ///////////////////////////////////////////////////////////////////////////
  51. // Our complex number generator
  52. ///////////////////////////////////////////////////////////////////////////
  53. //[tutorial_karma_complex_number_easier
  54. template <typename OutputIterator>
  55. bool generate_complex(OutputIterator sink, std::complex<double> const& c)
  56. {
  57. using boost::spirit::karma::double_;
  58. using boost::spirit::karma::omit;
  59. using boost::spirit::karma::generate;
  60. return generate(sink,
  61. // Begin grammar
  62. (
  63. !double_(0.0) << '(' << double_ << ", " << double_ << ')'
  64. | omit[double_] << double_
  65. ),
  66. // End grammar
  67. c.imag(), c.real(), c.imag() // Data to output
  68. );
  69. }
  70. //]
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////
  73. // Main program
  74. ///////////////////////////////////////////////////////////////////////////////
  75. int main()
  76. {
  77. std::cout << "/////////////////////////////////////////////////////////\n\n";
  78. std::cout << "\t\tA complex number micro generator for Spirit...\n\n";
  79. std::cout << "/////////////////////////////////////////////////////////\n\n";
  80. std::cout << "Give me a complex number of the form r or (r) or (r,i) \n";
  81. std::cout << "Type [q or Q] to quit\n\n";
  82. std::string str;
  83. while (getline(std::cin, str))
  84. {
  85. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  86. break;
  87. std::complex<double> c;
  88. if (client::parse_complex(str.begin(), str.end(), c))
  89. {
  90. std::cout << "-------------------------\n";
  91. std::string generated;
  92. std::back_insert_iterator<std::string> sink(generated);
  93. if (!client::generate_complex(sink, c))
  94. {
  95. std::cout << "-------------------------\n";
  96. std::cout << "Generating failed\n";
  97. std::cout << "-------------------------\n";
  98. }
  99. else
  100. {
  101. std::cout << "-------------------------\n";
  102. std::cout << "Generated: " << generated << "\n";
  103. std::cout << "-------------------------\n";
  104. }
  105. }
  106. else
  107. {
  108. std::cout << "-------------------------\n";
  109. std::cout << "Parsing failed\n";
  110. std::cout << "-------------------------\n";
  111. }
  112. }
  113. std::cout << "Bye... :-) \n\n";
  114. return 0;
  115. }