string_parse_tree.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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: 2008-11-12 14:37:53 -0500 (Wed, 12 Nov 2008) $
  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(-1)// -1 is match_not-found value
  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 == -1) {
  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. {
  92. // iterate thru all the elements and build the tree
  93. unsigned short index = 0;
  94. while (index != names.size() ) {
  95. string_type s = boost::algorithm::to_lower_copy(names[index]);
  96. insert(s, static_cast<unsigned short>(index + starting_point));
  97. index++;
  98. }
  99. //set the last tree node = index+1 indicating a value
  100. index++;
  101. }
  102. string_parse_tree(short value = -1) :
  103. m_value(value)
  104. {}
  105. ptree_coll m_next_chars;
  106. short m_value;
  107. void insert(const string_type& s, unsigned short value)
  108. {
  109. unsigned int i = 0;
  110. iterator ti;
  111. while(i < s.size()) {
  112. if (i==0) {
  113. if (i == (s.size()-1)) {
  114. ti = m_next_chars.insert(value_type(s[i],
  115. string_parse_tree<charT>(value)));
  116. }
  117. else {
  118. ti = m_next_chars.insert(value_type(s[i],
  119. string_parse_tree<charT>()));
  120. }
  121. }
  122. else {
  123. if (i == (s.size()-1)) {
  124. ti = ti->second.m_next_chars.insert(value_type(s[i],
  125. string_parse_tree<charT>(value)));
  126. }
  127. else {
  128. ti = ti->second.m_next_chars.insert(value_type(s[i],
  129. string_parse_tree<charT>()));
  130. }
  131. }
  132. i++;
  133. }
  134. }
  135. //! Recursive function that finds a matching string in the tree.
  136. /*! Must check match_results::has_remaining() after match() is
  137. * called. This is required so the user can determine if
  138. * stream iterator is already pointing to the expected
  139. * character or not (match() might advance sitr to next char in stream).
  140. *
  141. * A parse_match_result that has been returned from a failed match
  142. * attempt can be sent in to the match function of a different
  143. * string_parse_tree to attempt a match there. Use the iterators
  144. * for the partially consumed stream, the parse_match_result object,
  145. * and '0' for the level parameter. */
  146. short
  147. match(std::istreambuf_iterator<charT>& sitr,
  148. std::istreambuf_iterator<charT>& stream_end,
  149. parse_match_result_type& result,
  150. unsigned int& level) const
  151. {
  152. level++;
  153. charT c;
  154. // if we conditionally advance sitr, we won't have
  155. // to consume the next character past the input
  156. bool adv_itr = true;
  157. if (level > result.cache.size()) {
  158. if (sitr == stream_end) return 0; //bail - input exhausted
  159. c = static_cast<charT>(std::tolower(*sitr));
  160. //result.cache += c;
  161. //sitr++;
  162. }
  163. else {
  164. // if we're looking for characters from the cache,
  165. // we don't want to increment sitr
  166. adv_itr = false;
  167. c = static_cast<charT>(std::tolower(result.cache[level-1]));
  168. }
  169. const_iterator litr = m_next_chars.lower_bound(c);
  170. const_iterator uitr = m_next_chars.upper_bound(c);
  171. while (litr != uitr) { // equal if not found
  172. if(adv_itr) {
  173. sitr++;
  174. result.cache += c;
  175. }
  176. if (litr->second.m_value != -1) { // -1 is default value
  177. if (result.match_depth < level) {
  178. result.current_match = litr->second.m_value;
  179. result.match_depth = static_cast<unsigned short>(level);
  180. }
  181. litr->second.match(sitr, stream_end,
  182. result, level);
  183. level--;
  184. }
  185. else {
  186. litr->second.match(sitr, stream_end,
  187. result, level);
  188. level--;
  189. }
  190. if(level <= result.cache.size()) {
  191. adv_itr = false;
  192. }
  193. litr++;
  194. }
  195. return result.current_match;
  196. }
  197. /*! Must check match_results::has_remaining() after match() is
  198. * called. This is required so the user can determine if
  199. * stream iterator is already pointing to the expected
  200. * character or not (match() might advance sitr to next char in stream).
  201. */
  202. parse_match_result_type
  203. match(std::istreambuf_iterator<charT>& sitr,
  204. std::istreambuf_iterator<charT>& stream_end) const
  205. {
  206. // lookup to_lower of char in tree.
  207. unsigned int level = 0;
  208. // string_type cache;
  209. parse_match_result_type result;
  210. match(sitr, stream_end, result, level);
  211. return result;
  212. }
  213. void printme(std::ostream& os, int& level)
  214. {
  215. level++;
  216. iterator itr = m_next_chars.begin();
  217. iterator end = m_next_chars.end();
  218. // os << "starting level: " << level << std::endl;
  219. while (itr != end) {
  220. os << "level: " << level
  221. << " node: " << itr->first
  222. << " value: " << itr->second.m_value
  223. << std::endl;
  224. itr->second.printme(os, level);
  225. itr++;
  226. }
  227. level--;
  228. }
  229. void print(std::ostream& os)
  230. {
  231. int level = 0;
  232. printme(os, level);
  233. }
  234. void printmatch(std::ostream& os, charT c)
  235. {
  236. iterator litr = m_next_chars.lower_bound(c);
  237. iterator uitr = m_next_chars.upper_bound(c);
  238. os << "matches for: " << c << std::endl;
  239. while (litr != uitr) {
  240. os << " node: " << litr->first
  241. << " value: " << litr->second.m_value
  242. << std::endl;
  243. litr++;
  244. }
  245. }
  246. };
  247. } } //namespace
  248. #endif