string_parse_tree.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #ifndef BOOST_DATE_TIME_STRING_PARSE_TREE___HPP__
  2. #define BOOST_DATE_TIME_STRING_PARSE_TREE___HPP__
  3. /* Copyright (c) 2004-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 "boost/lexical_cast.hpp" //error without?
  11. #include "boost/algorithm/string/case_conv.hpp"
  12. #include <map>
  13. #include <string>
  14. #include <vector>
  15. #include <algorithm>
  16. namespace boost { namespace date_time {
  17. template<typename charT>
  18. struct parse_match_result
  19. {
  20. parse_match_result() :
  21. match_depth(0),
  22. current_match(PARSE_ERROR)
  23. {}
  24. typedef std::basic_string<charT> string_type;
  25. string_type remaining() const
  26. {
  27. if (match_depth == cache.size()) {
  28. return string_type();
  29. }
  30. if (current_match == PARSE_ERROR) {
  31. return cache;
  32. }
  33. //some of the cache was used return the rest
  34. return string_type(cache, match_depth);
  35. }
  36. charT last_char() const
  37. {
  38. return cache[cache.size()-1];
  39. }
  40. //! Returns true if more characters were parsed than was necessary
  41. /*! Should be used in conjunction with last_char()
  42. * to get the remaining character.
  43. */
  44. bool has_remaining() const
  45. {
  46. return (cache.size() > match_depth);
  47. }
  48. // cache will hold characters that have been read from the stream
  49. string_type cache;
  50. unsigned short match_depth;
  51. short current_match;
  52. enum PARSE_STATE { PARSE_ERROR = -1 };
  53. };
  54. //for debug -- really only char streams...
  55. template<typename charT>
  56. std::basic_ostream<charT>&
  57. operator<<(std::basic_ostream<charT>& os, parse_match_result<charT>& mr)
  58. {
  59. os << "cm: " << mr.current_match
  60. << " C: '" << mr.cache
  61. << "' md: " << mr.match_depth
  62. << " R: " << mr.remaining();
  63. return os;
  64. }
  65. //! Recursive data structure to allow efficient parsing of various strings
  66. /*! This class provides a quick lookup by building what amounts to a
  67. * tree data structure. It also features a match function which can
  68. * can handle nasty input interators by caching values as it recurses
  69. * the tree so that it can backtrack as needed.
  70. */
  71. template<typename charT>
  72. struct string_parse_tree
  73. {
  74. #if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x581) )
  75. typedef std::multimap<charT, string_parse_tree< charT> > ptree_coll;
  76. #else
  77. typedef std::multimap<charT, string_parse_tree > ptree_coll;
  78. #endif
  79. typedef typename ptree_coll::value_type value_type;
  80. typedef typename ptree_coll::iterator iterator;
  81. typedef typename ptree_coll::const_iterator const_iterator;
  82. typedef std::basic_string<charT> string_type;
  83. typedef std::vector<std::basic_string<charT> > collection_type;
  84. typedef parse_match_result<charT> parse_match_result_type;
  85. /*! Parameter "starting_point" designates where the numbering begins.
  86. * A starting_point of zero will start the numbering at zero
  87. * (Sun=0, Mon=1, ...) were a starting_point of one starts the
  88. * numbering at one (Jan=1, Feb=2, ...). The default is zero,
  89. * negative vaules are not allowed */
  90. string_parse_tree(collection_type names, unsigned int starting_point=0) :
  91. m_value(parse_match_result_type::PARSE_ERROR)
  92. {
  93. // iterate thru all the elements and build the tree
  94. unsigned short index = 0;
  95. while (index != names.size() ) {
  96. string_type s = boost::algorithm::to_lower_copy(names[index]);
  97. insert(s, static_cast<unsigned short>(index + starting_point));
  98. index++;
  99. }
  100. //set the last tree node = index+1 indicating a value
  101. index++;
  102. }
  103. string_parse_tree(short value = parse_match_result_type::PARSE_ERROR) :
  104. m_value(value)
  105. {}
  106. ptree_coll m_next_chars;
  107. short m_value;
  108. void insert(const string_type& s, unsigned short value)
  109. {
  110. unsigned int i = 0;
  111. iterator ti;
  112. while(i < s.size()) {
  113. if (i==0) {
  114. if (i == (s.size()-1)) {
  115. ti = m_next_chars.insert(value_type(s[i],
  116. string_parse_tree<charT>(value)));
  117. }
  118. else {
  119. ti = m_next_chars.insert(value_type(s[i],
  120. string_parse_tree<charT>()));
  121. }
  122. }
  123. else {
  124. if (i == (s.size()-1)) {
  125. ti = ti->second.m_next_chars.insert(value_type(s[i],
  126. string_parse_tree<charT>(value)));
  127. }
  128. else {
  129. ti = ti->second.m_next_chars.insert(value_type(s[i],
  130. string_parse_tree<charT>()));
  131. }
  132. }
  133. i++;
  134. }
  135. }
  136. //! Recursive function that finds a matching string in the tree.
  137. /*! Must check match_results::has_remaining() after match() is
  138. * called. This is required so the user can determine if
  139. * stream iterator is already pointing to the expected
  140. * character or not (match() might advance sitr to next char in stream).
  141. *
  142. * A parse_match_result that has been returned from a failed match
  143. * attempt can be sent in to the match function of a different
  144. * string_parse_tree to attempt a match there. Use the iterators
  145. * for the partially consumed stream, the parse_match_result object,
  146. * and '0' for the level parameter. */
  147. short
  148. match(std::istreambuf_iterator<charT>& sitr,
  149. std::istreambuf_iterator<charT>& stream_end,
  150. parse_match_result_type& result,
  151. unsigned int& level) const
  152. {
  153. level++;
  154. charT c;
  155. // if we conditionally advance sitr, we won't have
  156. // to consume the next character past the input
  157. bool adv_itr = true;
  158. if (level > result.cache.size()) {
  159. if (sitr == stream_end) return 0; //bail - input exhausted
  160. c = static_cast<charT>(std::tolower(*sitr));
  161. //result.cache += c;
  162. //sitr++;
  163. }
  164. else {
  165. // if we're looking for characters from the cache,
  166. // we don't want to increment sitr
  167. adv_itr = false;
  168. c = static_cast<charT>(std::tolower(result.cache[level-1]));
  169. }
  170. const_iterator litr = m_next_chars.lower_bound(c);
  171. const_iterator uitr = m_next_chars.upper_bound(c);
  172. while (litr != uitr) { // equal if not found
  173. if(adv_itr) {
  174. sitr++;
  175. result.cache += c;
  176. }
  177. if (litr->second.m_value != -1) { // -1 is default value
  178. if (result.match_depth < level) {
  179. result.current_match = litr->second.m_value;
  180. result.match_depth = static_cast<unsigned short>(level);
  181. }
  182. litr->second.match(sitr, stream_end,
  183. result, level);
  184. level--;
  185. }
  186. else {
  187. litr->second.match(sitr, stream_end,
  188. result, level);
  189. level--;
  190. }
  191. if(level <= result.cache.size()) {
  192. adv_itr = false;
  193. }
  194. litr++;
  195. }
  196. return result.current_match;
  197. }
  198. /*! Must check match_results::has_remaining() after match() is
  199. * called. This is required so the user can determine if
  200. * stream iterator is already pointing to the expected
  201. * character or not (match() might advance sitr to next char in stream).
  202. */
  203. parse_match_result_type
  204. match(std::istreambuf_iterator<charT>& sitr,
  205. std::istreambuf_iterator<charT>& stream_end) const
  206. {
  207. // lookup to_lower of char in tree.
  208. unsigned int level = 0;
  209. // string_type cache;
  210. parse_match_result_type result;
  211. match(sitr, stream_end, result, level);
  212. return result;
  213. }
  214. void printme(std::ostream& os, int& level)
  215. {
  216. level++;
  217. iterator itr = m_next_chars.begin();
  218. iterator end = m_next_chars.end();
  219. // os << "starting level: " << level << std::endl;
  220. while (itr != end) {
  221. os << "level: " << level
  222. << " node: " << itr->first
  223. << " value: " << itr->second.m_value
  224. << std::endl;
  225. itr->second.printme(os, level);
  226. itr++;
  227. }
  228. level--;
  229. }
  230. void print(std::ostream& os)
  231. {
  232. int level = 0;
  233. printme(os, level);
  234. }
  235. void printmatch(std::ostream& os, charT c)
  236. {
  237. iterator litr = m_next_chars.lower_bound(c);
  238. iterator uitr = m_next_chars.upper_bound(c);
  239. os << "matches for: " << c << std::endl;
  240. while (litr != uitr) {
  241. os << " node: " << litr->first
  242. << " value: " << litr->second.m_value
  243. << std::endl;
  244. litr++;
  245. }
  246. }
  247. };
  248. } } //namespace
  249. #endif