date_generator_parser.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #ifndef DATE_TIME_DATE_GENERATOR_PARSER_HPP__
  2. #define DATE_TIME_DATE_GENERATOR_PARSER_HPP__
  3. /* Copyright (c) 2005 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. #include <string>
  11. #include <vector>
  12. #include <iterator> // istreambuf_iterator
  13. #include <boost/throw_exception.hpp>
  14. #include <boost/date_time/compiler_config.hpp>
  15. #include <boost/date_time/string_parse_tree.hpp>
  16. #include <boost/date_time/date_generators.hpp>
  17. #include <boost/date_time/format_date_parser.hpp>
  18. namespace boost { namespace date_time {
  19. //! Class for date_generator parsing
  20. /*! The elements of a date_generator "phrase" are parsed from the input stream in a
  21. * particular order. All elements are required and the order in which they appear
  22. * cannot change, however, the elements themselves can be changed. The default
  23. * elements and their order are as follows:
  24. *
  25. * - partial_date => "dd Month"
  26. * - nth_day_of_the_week_in_month => "nth weekday of month"
  27. * - first_day_of_the_week_in_month => "first weekday of month"
  28. * - last_day_of_the_week_in_month => "last weekday of month"
  29. * - first_day_of_the_week_after => "weekday after"
  30. * - first_day_of_the_week_before => "weekday before"
  31. *
  32. * Weekday and Month names and formats are handled via the date_input_facet.
  33. *
  34. */
  35. template<class date_type, typename charT>
  36. class date_generator_parser
  37. {
  38. public:
  39. typedef std::basic_string<charT> string_type;
  40. typedef std::istreambuf_iterator<charT> stream_itr_type;
  41. typedef typename date_type::month_type month_type;
  42. typedef typename date_type::day_of_week_type day_of_week_type;
  43. typedef typename date_type::day_type day_type;
  44. typedef string_parse_tree<charT> parse_tree_type;
  45. typedef typename parse_tree_type::parse_match_result_type match_results;
  46. typedef std::vector<std::basic_string<charT> > collection_type;
  47. typedef partial_date<date_type> partial_date_type;
  48. typedef nth_kday_of_month<date_type> nth_kday_type;
  49. typedef first_kday_of_month<date_type> first_kday_type;
  50. typedef last_kday_of_month<date_type> last_kday_type;
  51. typedef first_kday_after<date_type> kday_after_type;
  52. typedef first_kday_before<date_type> kday_before_type;
  53. typedef charT char_type;
  54. static const char_type first_string[6];
  55. static const char_type second_string[7];
  56. static const char_type third_string[6];
  57. static const char_type fourth_string[7];
  58. static const char_type fifth_string[6];
  59. static const char_type last_string[5];
  60. static const char_type before_string[8];
  61. static const char_type after_string[6];
  62. static const char_type of_string[3];
  63. enum phrase_elements {first=0, second, third, fourth, fifth, last,
  64. before, after, of, number_of_phrase_elements};
  65. //! Creates a date_generator_parser with the default set of "element_strings"
  66. date_generator_parser()
  67. {
  68. element_strings(string_type(first_string),
  69. string_type(second_string),
  70. string_type(third_string),
  71. string_type(fourth_string),
  72. string_type(fifth_string),
  73. string_type(last_string),
  74. string_type(before_string),
  75. string_type(after_string),
  76. string_type(of_string));
  77. }
  78. //! Creates a date_generator_parser using a user defined set of element strings
  79. date_generator_parser(const string_type& first_str,
  80. const string_type& second_str,
  81. const string_type& third_str,
  82. const string_type& fourth_str,
  83. const string_type& fifth_str,
  84. const string_type& last_str,
  85. const string_type& before_str,
  86. const string_type& after_str,
  87. const string_type& of_str)
  88. {
  89. element_strings(first_str, second_str, third_str, fourth_str, fifth_str,
  90. last_str, before_str, after_str, of_str);
  91. }
  92. //! Replace strings that determine nth week for generator
  93. void element_strings(const string_type& first_str,
  94. const string_type& second_str,
  95. const string_type& third_str,
  96. const string_type& fourth_str,
  97. const string_type& fifth_str,
  98. const string_type& last_str,
  99. const string_type& before_str,
  100. const string_type& after_str,
  101. const string_type& of_str)
  102. {
  103. collection_type phrases;
  104. phrases.push_back(first_str);
  105. phrases.push_back(second_str);
  106. phrases.push_back(third_str);
  107. phrases.push_back(fourth_str);
  108. phrases.push_back(fifth_str);
  109. phrases.push_back(last_str);
  110. phrases.push_back(before_str);
  111. phrases.push_back(after_str);
  112. phrases.push_back(of_str);
  113. m_element_strings = parse_tree_type(phrases, this->first); // enum first
  114. }
  115. void element_strings(const collection_type& col)
  116. {
  117. m_element_strings = parse_tree_type(col, this->first); // enum first
  118. }
  119. //! returns partial_date parsed from stream
  120. template<class facet_type>
  121. partial_date_type
  122. get_partial_date_type(stream_itr_type& sitr,
  123. stream_itr_type& stream_end,
  124. std::ios_base& a_ios,
  125. const facet_type& facet) const
  126. {
  127. // skip leading whitespace
  128. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  129. day_type d(1);
  130. month_type m(1);
  131. facet.get(sitr, stream_end, a_ios, d);
  132. facet.get(sitr, stream_end, a_ios, m);
  133. return partial_date_type(d,m);
  134. }
  135. //! returns nth_kday_of_week parsed from stream
  136. template<class facet_type>
  137. nth_kday_type
  138. get_nth_kday_type(stream_itr_type& sitr,
  139. stream_itr_type& stream_end,
  140. std::ios_base& a_ios,
  141. const facet_type& facet) const
  142. {
  143. // skip leading whitespace
  144. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  145. typename nth_kday_type::week_num wn;
  146. day_of_week_type wd(0); // no default constructor
  147. month_type m(1); // no default constructor
  148. match_results mr = m_element_strings.match(sitr, stream_end);
  149. switch(mr.current_match) {
  150. case first : { wn = nth_kday_type::first; break; }
  151. case second : { wn = nth_kday_type::second; break; }
  152. case third : { wn = nth_kday_type::third; break; }
  153. case fourth : { wn = nth_kday_type::fourth; break; }
  154. case fifth : { wn = nth_kday_type::fifth; break; }
  155. default:
  156. {
  157. boost::throw_exception(std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'"));
  158. BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(wn = nth_kday_type::first);
  159. }
  160. } // week num
  161. facet.get(sitr, stream_end, a_ios, wd); // day_of_week
  162. extract_element(sitr, stream_end, of); // "of" element
  163. facet.get(sitr, stream_end, a_ios, m); // month
  164. return nth_kday_type(wn, wd, m);
  165. }
  166. //! returns first_kday_of_week parsed from stream
  167. template<class facet_type>
  168. first_kday_type
  169. get_first_kday_type(stream_itr_type& sitr,
  170. stream_itr_type& stream_end,
  171. std::ios_base& a_ios,
  172. const facet_type& facet) const
  173. {
  174. // skip leading whitespace
  175. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  176. day_of_week_type wd(0); // no default constructor
  177. month_type m(1); // no default constructor
  178. extract_element(sitr, stream_end, first); // "first" element
  179. facet.get(sitr, stream_end, a_ios, wd); // day_of_week
  180. extract_element(sitr, stream_end, of); // "of" element
  181. facet.get(sitr, stream_end, a_ios, m); // month
  182. return first_kday_type(wd, m);
  183. }
  184. //! returns last_kday_of_week parsed from stream
  185. template<class facet_type>
  186. last_kday_type
  187. get_last_kday_type(stream_itr_type& sitr,
  188. stream_itr_type& stream_end,
  189. std::ios_base& a_ios,
  190. const facet_type& facet) const
  191. {
  192. // skip leading whitespace
  193. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  194. day_of_week_type wd(0); // no default constructor
  195. month_type m(1); // no default constructor
  196. extract_element(sitr, stream_end, last); // "last" element
  197. facet.get(sitr, stream_end, a_ios, wd); // day_of_week
  198. extract_element(sitr, stream_end, of); // "of" element
  199. facet.get(sitr, stream_end, a_ios, m); // month
  200. return last_kday_type(wd, m);
  201. }
  202. //! returns first_kday_of_week parsed from stream
  203. template<class facet_type>
  204. kday_before_type
  205. get_kday_before_type(stream_itr_type& sitr,
  206. stream_itr_type& stream_end,
  207. std::ios_base& a_ios,
  208. const facet_type& facet) const
  209. {
  210. // skip leading whitespace
  211. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  212. day_of_week_type wd(0); // no default constructor
  213. facet.get(sitr, stream_end, a_ios, wd); // day_of_week
  214. extract_element(sitr, stream_end, before);// "before" element
  215. return kday_before_type(wd);
  216. }
  217. //! returns first_kday_of_week parsed from stream
  218. template<class facet_type>
  219. kday_after_type
  220. get_kday_after_type(stream_itr_type& sitr,
  221. stream_itr_type& stream_end,
  222. std::ios_base& a_ios,
  223. const facet_type& facet) const
  224. {
  225. // skip leading whitespace
  226. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  227. day_of_week_type wd(0); // no default constructor
  228. facet.get(sitr, stream_end, a_ios, wd); // day_of_week
  229. extract_element(sitr, stream_end, after); // "after" element
  230. return kday_after_type(wd);
  231. }
  232. private:
  233. parse_tree_type m_element_strings;
  234. //! Extracts phrase element from input. Throws ios_base::failure on error.
  235. void extract_element(stream_itr_type& sitr,
  236. stream_itr_type& stream_end,
  237. typename date_generator_parser::phrase_elements ele) const
  238. {
  239. // skip leading whitespace
  240. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  241. match_results mr = m_element_strings.match(sitr, stream_end);
  242. if(mr.current_match != ele) {
  243. boost::throw_exception(std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'"));
  244. }
  245. }
  246. };
  247. template<class date_type, class CharT>
  248. const typename date_generator_parser<date_type, CharT>::char_type
  249. date_generator_parser<date_type, CharT>::first_string[6] =
  250. {'f','i','r','s','t'};
  251. template<class date_type, class CharT>
  252. const typename date_generator_parser<date_type, CharT>::char_type
  253. date_generator_parser<date_type, CharT>::second_string[7] =
  254. {'s','e','c','o','n','d'};
  255. template<class date_type, class CharT>
  256. const typename date_generator_parser<date_type, CharT>::char_type
  257. date_generator_parser<date_type, CharT>::third_string[6] =
  258. {'t','h','i','r','d'};
  259. template<class date_type, class CharT>
  260. const typename date_generator_parser<date_type, CharT>::char_type
  261. date_generator_parser<date_type, CharT>::fourth_string[7] =
  262. {'f','o','u','r','t','h'};
  263. template<class date_type, class CharT>
  264. const typename date_generator_parser<date_type, CharT>::char_type
  265. date_generator_parser<date_type, CharT>::fifth_string[6] =
  266. {'f','i','f','t','h'};
  267. template<class date_type, class CharT>
  268. const typename date_generator_parser<date_type, CharT>::char_type
  269. date_generator_parser<date_type, CharT>::last_string[5] =
  270. {'l','a','s','t'};
  271. template<class date_type, class CharT>
  272. const typename date_generator_parser<date_type, CharT>::char_type
  273. date_generator_parser<date_type, CharT>::before_string[8] =
  274. {'b','e','f','o','r','e'};
  275. template<class date_type, class CharT>
  276. const typename date_generator_parser<date_type, CharT>::char_type
  277. date_generator_parser<date_type, CharT>::after_string[6] =
  278. {'a','f','t','e','r'};
  279. template<class date_type, class CharT>
  280. const typename date_generator_parser<date_type, CharT>::char_type
  281. date_generator_parser<date_type, CharT>::of_string[3] =
  282. {'o','f'};
  283. } } //namespace
  284. #endif // DATE_TIME_DATE_GENERATOR_PARSER_HPP__