bug_fixes.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*=============================================================================
  2. Copyright (c) 2003 Giovanni Bajo
  3. Copyright (c) 2003 Joel de Guzman
  4. Copyright (c) 2003 Vaclav Vesely
  5. http://spirit.sourceforge.net/
  6. Use, modification and distribution is subject to the Boost Software
  7. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. http://www.boost.org/LICENSE_1_0.txt)
  9. =============================================================================*/
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/spirit/include/classic_core.hpp>
  12. #include <boost/spirit/include/classic_assign_actor.hpp>
  13. using namespace boost;
  14. using namespace BOOST_SPIRIT_CLASSIC_NS;
  15. ///////////////////////////////////////////////////////////////////////////////
  16. //
  17. // bug_001
  18. //
  19. // access_node_d[] and access_match_d[] iterator bug
  20. // http://sf.net/mailarchive/forum.php?thread_id=1963157&forum_id=1595
  21. // http://sf.net/mailarchive/forum.php?thread_id=1966224&forum_id=1595
  22. //
  23. ///////////////////////////////////////////////////////////////////////////////
  24. #include <boost/spirit/include/classic_ast.hpp>
  25. struct my_action
  26. {
  27. template <typename TreeT, typename IterT>
  28. void operator()(TreeT& /*t*/, IterT begin, IterT end) const
  29. {
  30. BOOST_TEST(*begin == '1');
  31. BOOST_TEST(*end == '2');
  32. }
  33. };
  34. void bug_001()
  35. {
  36. const char* text = "123";
  37. ast_parse(text, text+3, access_node_d[chlit<>('1')][my_action()]);
  38. ast_parse(text, text+3, access_match_d[chlit<>('1')][my_action()]);
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////
  41. //
  42. // bug_001
  43. //
  44. // mismatch closure return type bug
  45. // http://article.gmane.org/gmane.comp.parsers.spirit.general/3678
  46. //
  47. ///////////////////////////////////////////////////////////////////////////////
  48. #include <boost/spirit/include/classic_attribute.hpp>
  49. #include <string>
  50. typedef std::string member_type;
  51. struct my_closure: closure<my_closure, member_type>
  52. {
  53. member1 val;
  54. };
  55. void bug_002()
  56. {
  57. rule<scanner<char const*>, my_closure::context_t> my_rule = real_p;
  58. BOOST_TEST(parse("1", my_rule).full);
  59. }
  60. ///////////////////////////////////////////////////////////////////////////////
  61. //
  62. // bug_003
  63. //
  64. // impl::detach_clear bug
  65. // http://sourceforge.net/mailarchive/forum.php?thread_id=2008510&forum_id=25901
  66. //
  67. ///////////////////////////////////////////////////////////////////////////////
  68. #include <boost/spirit/include/classic_chset.hpp>
  69. void bug_003()
  70. {
  71. chset<> set;
  72. set = 'a';
  73. }
  74. ///////////////////////////////////////////////////////////////////////////////
  75. //
  76. // bug_004
  77. //
  78. // chset<>::operator~(range<>) bug
  79. // operator&(chset<>, range<>) bug
  80. // operator&(range<>, chset<>) bug
  81. //
  82. ///////////////////////////////////////////////////////////////////////////////
  83. #include <boost/limits.hpp>
  84. #include <boost/spirit/include/classic_chset.hpp>
  85. void bug_004()
  86. {
  87. const char min = (std::numeric_limits<char>::min)();
  88. const char max = (std::numeric_limits<char>::max)();
  89. {
  90. chset<> set(~range<>(min, max));
  91. BOOST_TEST(set.test(min) == false);
  92. BOOST_TEST(set.test(min) == false);
  93. }
  94. {
  95. chset<> set(chset<>(anychar_p) & range<>(min, max));
  96. BOOST_TEST(set.test(min) == true);
  97. BOOST_TEST(set.test(min) == true);
  98. }
  99. {
  100. chset<> set(range<>(min, max) & chset<>(anychar_p));
  101. BOOST_TEST(set.test(min) == true);
  102. BOOST_TEST(set.test(min) == true);
  103. }
  104. }
  105. ///////////////////////////////////////////////////////////////////////////////
  106. //
  107. // bug_005
  108. //
  109. // Most trailing space bug
  110. // http://article.gmane.org/gmane.comp.parsers.spirit.general/4029
  111. // JDG: Oct 18, 2005. We shall revert to the previous behavior where
  112. // Post skips are not allowed. The reason is that
  113. // there is a valid use case where input is obtained
  114. // from cin and multi_pass which results in an infinite
  115. // loop while the post skipper waits for a whitespace.
  116. // For examples like below, the grammar must explicitly
  117. // include the post whitespace. One possible way is to
  118. // place an end_p at the end of the grammar. The end_p
  119. // will trigger the post-skip.
  120. //
  121. ///////////////////////////////////////////////////////////////////////////////
  122. #include <boost/spirit/include/classic_core.hpp>
  123. using namespace boost;
  124. using namespace spirit;
  125. void bug_005()
  126. {
  127. BOOST_TEST(
  128. parse(" aaaaaaaaa ", *ch_p('a') >> end_p, space_p).full
  129. );
  130. BOOST_TEST(
  131. parse(" aaaaaaaaa ", lexeme_d[*ch_p('a')] >> end_p, space_p).full
  132. );
  133. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
  134. // not sure why Code Warrior 9.5 does not recognize ch_p(' ') as the
  135. // same as space_p (see above) when the inputs are spaces. The
  136. // tests below are redundant anyway.
  137. #else
  138. BOOST_TEST(
  139. parse(" aaaaaaaaa ", *ch_p('a') >> end_p, ch_p(' ')).full
  140. );
  141. BOOST_TEST(
  142. parse(" aaaaaaaaa ", lexeme_d[*ch_p('a')] >> end_p, ch_p(' ')).full
  143. );
  144. #endif
  145. }
  146. ///////////////////////////////////////////////////////////////////////////////
  147. //
  148. // bug_006
  149. //
  150. // confix bug
  151. //
  152. ///////////////////////////////////////////////////////////////////////////////
  153. #include <boost/limits.hpp>
  154. #include <boost/spirit/include/classic_confix.hpp>
  155. void bug_006()
  156. {
  157. BOOST_TEST(parse("#some comment", comment_p('#')).full);
  158. }
  159. ///////////////////////////////////////////////////////////////////////////////
  160. //
  161. // bug_007
  162. //
  163. // handling of trailing whitespace bug (ast_parse/pt_parse related)
  164. // JDG: Oct 18, 2005. We shall revert to the previous behavior where
  165. // Post skips are not allowed. The reason is that
  166. // there is a valid use case where input is obtained
  167. // from cin and multi_pass which results in an infinite
  168. // loop while the post skipper waits for a whitespace.
  169. // For examples like below, the grammar must explicitly
  170. // include the post whitespace. One possible way is to
  171. // place an end_p at the end of the grammar. The end_p
  172. // will trigger the post-skip.
  173. //
  174. ///////////////////////////////////////////////////////////////////////////////
  175. #include <boost/spirit/include/classic_ast.hpp>
  176. #include <boost/spirit/include/classic_parse_tree.hpp>
  177. void bug_007()
  178. {
  179. BOOST_TEST(parse("test ", str_p("test") >> end_p, space_p).full);
  180. BOOST_TEST(pt_parse("test ", str_p("test") >> end_p, space_p).full);
  181. BOOST_TEST(ast_parse("test ", str_p("test") >> end_p, space_p).full);
  182. }
  183. ///////////////////////////////////////////////////////////////////////////////
  184. //
  185. // sf_bug_718903
  186. //
  187. // see https://sourceforge.net/tracker/index.php
  188. // ?func=detail&aid=718903&group_id=28447&atid=393386
  189. //
  190. ///////////////////////////////////////////////////////////////////////////////
  191. #include <boost/cstdlib.hpp>
  192. #include <boost/spirit/include/classic_chset.hpp>
  193. void sf_bug_718903()
  194. {
  195. empty_match_parser<chset<char> >
  196. e(epsilon_p(chset_p("abc")));
  197. }
  198. ///////////////////////////////////////////////////////////////////////////////
  199. //
  200. // sf_bug_719322
  201. // range_run bug
  202. //
  203. // see http://sourceforge.net/tracker/index.php
  204. // ?func=detail&aid=719322&group_id=28447&atid=393386
  205. //
  206. ///////////////////////////////////////////////////////////////////////////////
  207. #include <boost/spirit/include/classic_basic_chset.hpp>
  208. void sf_bug_719322()
  209. {
  210. basic_chset<int> s;
  211. s.set(3, 3);
  212. s.set(1, 5);
  213. BOOST_TEST(s.test(5));
  214. }
  215. ///////////////////////////////////////////////////////////////////////////////
  216. //
  217. // sf_bug_742038
  218. //
  219. // see http://sf.net/tracker/
  220. // ?func=detail&atid=393386&aid=742038&group_id=28447
  221. //
  222. ///////////////////////////////////////////////////////////////////////////////
  223. #include <boost/spirit/include/classic_position_iterator.hpp>
  224. #include <boost/spirit/include/classic_file_iterator.hpp>
  225. #include <string>
  226. #include <fstream>
  227. #include <iostream>
  228. #include <boost/detail/lightweight_test.hpp>
  229. #include <stdio.h>
  230. template <typename IterT>
  231. void test_assign(IterT b, IterT e)
  232. {
  233. typedef scanner<IterT> scanner_t;
  234. #if (defined(__GNUC__) && defined(__MINGW32__)) \
  235. || (defined(__GNUC__) && (__GNUC_MINOR__ < 20))
  236. // There's a bug in g++3.x on MinGW that makes basic_string assert
  237. // when assigning from IterT [f, l) where IterT is a position_iterator.
  238. // This issue is discussed here:
  239. //
  240. // http://gcc.gnu.org/ml/libstdc++/2002-03/msg00196.html
  241. //
  242. // Aparently, this bug is only present on MinGW. I'm clueless as
  243. // to why this is so. Regressions on linux seem to be OK! :(
  244. //
  245. // With, g++3.1, assigning to basic_string from IterT [f, l) is a
  246. // compile error (a g++3.1 bug).
  247. //
  248. // In both cases above, we use a vector instead of a string.
  249. typedef std::vector<char> store;
  250. #else
  251. typedef std::string store;
  252. #endif
  253. store dst;
  254. rule<scanner_t> r = (*alpha_p)[assign_a(dst)];
  255. parse(b, e, r);
  256. store::iterator d = dst.begin();
  257. while (b != e)
  258. {
  259. if (*d != *b)
  260. BOOST_TEST(*d == *b);
  261. ++b;
  262. ++d;
  263. }
  264. }
  265. void sf_bug_742038()
  266. {
  267. std::string src = "abcdef";
  268. const char* tmpfilename = "sf_bug_742038.tmp";
  269. test_assign(src.begin(), src.end());
  270. position_iterator<std::string::iterator> b(src.begin(), src.end(), "");
  271. position_iterator<std::string::iterator> e;
  272. test_assign(b, e);
  273. {
  274. std::fstream f(tmpfilename, std::ios::out);
  275. f << src;
  276. f.close();
  277. file_iterator<> b1(tmpfilename);
  278. file_iterator<> e1(b1.make_end());
  279. test_assign(b1, e1);
  280. }
  281. std::remove(tmpfilename);
  282. }
  283. ///////////////////////////////////////////////////////////////////////////////
  284. //
  285. // bug_009
  286. //
  287. // limit_d bug
  288. // http://article.gmane.org/gmane.comp.parsers.spirit.devel/1891/
  289. //
  290. ///////////////////////////////////////////////////////////////////////////////
  291. void
  292. bug_009()
  293. {
  294. parse(
  295. "test"
  296. , limit_d(1U, 10U)[uint_p] | str_p("test"));
  297. }
  298. int
  299. main()
  300. {
  301. bug_001();
  302. bug_002();
  303. bug_003();
  304. bug_004();
  305. bug_005();
  306. bug_006();
  307. bug_007();
  308. bug_009();
  309. sf_bug_718903();
  310. sf_bug_719322();
  311. sf_bug_742038();
  312. return boost::report_errors();
  313. }