regex_grep.qbk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. [/
  2. Copyright 2006-2007 John Maddock.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:regex_grep regex_grep (Deprecated)]
  8. The algorithm `regex_grep` is deprecated in favor of [regex_iterator]
  9. which provides a more convenient and standard library friendly interface.
  10. The following documentation is taken unchanged from the previous boost
  11. release, and will not be updated in future.
  12. #include <boost/regex.hpp>
  13. `regex_grep` allows you to search through a bidirectional-iterator range and
  14. locate all the (non-overlapping) matches with a given regular expression.
  15. The function is declared as:
  16. template <class Predicate, class iterator, class charT, class traits>
  17. unsigned int regex_grep(Predicate foo,
  18. iterator first,
  19. iterator last,
  20. const basic_regex<charT, traits>& e,
  21. boost::match_flag_type flags = match_default)
  22. The library also defines the following convenience versions, which take
  23. either a `const charT*`, or a `const std::basic_string<>&` in place of a
  24. pair of iterators.
  25. template <class Predicate, class charT, class traits>
  26. unsigned int regex_grep(Predicate foo,
  27. const charT* str,
  28. const basic_regex<charT, traits>& e,
  29. boost::match_flag_type flags = match_default);
  30. template <class Predicate, class ST, class SA, class charT, class traits>
  31. unsigned int regex_grep(Predicate foo,
  32. const std::basic_string<charT, ST, SA>& s,
  33. const basic_regex<charT, traits>& e,
  34. boost::match_flag_type flags = match_default);
  35. The parameters for the primary version of `regex_grep` have the following meanings:
  36. foo: A predicate function object or function pointer, see below for more information.
  37. first: The start of the range to search.
  38. last: The end of the range to search.
  39. e: The regular expression to search for.
  40. flags: The flags that determine how matching is carried out, one of the match_flags enumerators.
  41. The algorithm finds all of the non-overlapping matches of the expression /e/,
  42. for each match it fills a `match_results<iterator>` structure, which
  43. contains information on what matched, and calls the predicate /foo/, passing the
  44. `match_results<iterator>` as a single argument. If the predicate returns
  45. /true/, then the grep operation continues, otherwise it terminates
  46. without searching for further matches. The function returns the number
  47. of matches found.
  48. The general form of the predicate is:
  49. struct grep_predicate
  50. {
  51. bool operator()(const match_results<iterator_type>& m);
  52. };
  53. For example the regular expression "a\*b" would find one match in the string
  54. "aaaaab" and two in the string "aaabb".
  55. Remember this algorithm can be used for a lot more than implementing a
  56. version of grep, the predicate can be and do anything that you want,
  57. grep utilities would output the results to the screen, another program could
  58. index a file based on a regular expression and store a set of bookmarks in a list,
  59. or a text file conversion utility would output to file. The results of one
  60. `regex_grep` can even be chained into another `regex_grep` to create recursive parsers.
  61. The algorithm may throw `std::runtime_error` if the complexity of matching the
  62. expression against an /N/ character string begins to exceed O(N[super 2]), or
  63. if the program runs out of stack space while matching the expression
  64. (if Boost.Regex is configured in recursive mode), or if the matcher
  65. exhausts it's permitted memory allocation (if Boost.Regex is configured in
  66. non-recursive mode).
  67. Example: convert the example from [regex_search] to use `regex_grep` instead:
  68. #include <string>
  69. #include <map>
  70. #include <boost/regex.hpp>
  71. // IndexClasses:
  72. // takes the contents of a file in the form of a string
  73. // and searches for all the C++ class definitions, storing
  74. // their locations in a map of strings/int's
  75. typedef std::map<std::string, int, std::less<std::string> > map_type;
  76. const char* re =
  77. // possibly leading whitespace:
  78. "^[[:space:]]*"
  79. // possible template declaration:
  80. "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
  81. // class or struct:
  82. "(class|struct)[[:space:]]*"
  83. // leading declspec macros etc:
  84. "("
  85. "\\<\\w+\\>"
  86. "("
  87. "[[:blank:]]*\\([^)]*\\)"
  88. ")?"
  89. "[[:space:]]*"
  90. ")*"
  91. // the class name
  92. "(\\<\\w*\\>)[[:space:]]*"
  93. // template specialisation parameters
  94. "(<[^;:{]+>)?[[:space:]]*"
  95. // terminate in { or :
  96. "(\\{|:[^;\\{()]*\\{)";
  97. boost::regex expression(re);
  98. class IndexClassesPred
  99. {
  100. map_type& m;
  101. std::string::const_iterator base;
  102. public:
  103. IndexClassesPred(map_type& a, std::string::const_iterator b) : m(a), base(b) {}
  104. bool operator()(const smatch& what)
  105. {
  106. // what[0] contains the whole string
  107. // what[5] contains the class name.
  108. // what[6] contains the template specialisation if any.
  109. // add class name and position to map:
  110. m[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
  111. what[5].first - base;
  112. return true;
  113. }
  114. };
  115. void IndexClasses(map_type& m, const std::string& file)
  116. {
  117. std::string::const_iterator start, end;
  118. start = file.begin();
  119. end = file.end();
  120. regex_grep(IndexClassesPred(m, start), start, end, expression);
  121. }
  122. Example: Use `regex_grep` to call a global callback function:
  123. #include <string>
  124. #include <map>
  125. #include <boost/regex.hpp>
  126. // purpose:
  127. // takes the contents of a file in the form of a string
  128. // and searches for all the C++ class definitions, storing
  129. // their locations in a map of strings/int's
  130. typedef std::map<std::string, int, std::less<std::string> > map_type;
  131. const char* re =
  132. // possibly leading whitespace:
  133. "^[[:space:]]*"
  134. // possible template declaration:
  135. "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
  136. // class or struct:
  137. "(class|struct)[[:space:]]*"
  138. // leading declspec macros etc:
  139. "("
  140. "\\<\\w+\\>"
  141. "("
  142. "[[:blank:]]*\\([^)]*\\)"
  143. ")?"
  144. "[[:space:]]*"
  145. ")*"
  146. // the class name
  147. "(\\<\\w*\\>)[[:space:]]*"
  148. // template specialisation parameters
  149. "(<[^;:{]+>)?[[:space:]]*"
  150. // terminate in { or :
  151. "(\\{|:[^;\\{()]*\\{)";
  152. boost::regex expression(re);
  153. map_type class_index;
  154. std::string::const_iterator base;
  155. bool grep_callback(const boost::smatch& what)
  156. {
  157. // what[0] contains the whole string
  158. // what[5] contains the class name.
  159. // what[6] contains the template specialisation if any.
  160. // add class name and position to map:
  161. class_index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
  162. what[5].first - base;
  163. return true;
  164. }
  165. void IndexClasses(const std::string& file)
  166. {
  167. std::string::const_iterator start, end;
  168. start = file.begin();
  169. end = file.end();
  170. base = start;
  171. regex_grep(grep_callback, start, end, expression, match_default);
  172. }
  173. Example: use `regex_grep` to call a class member function, use the standard
  174. library adapters `std::mem_fun` and `std::bind1st` to convert the member
  175. function into a predicate:
  176. #include <string>
  177. #include <map>
  178. #include <boost/regex.hpp>
  179. #include <functional>
  180. // purpose:
  181. // takes the contents of a file in the form of a string
  182. // and searches for all the C++ class definitions, storing
  183. // their locations in a map of strings/int's
  184. typedef std::map<std::string, int, std::less<std::string> > map_type;
  185. class class_index
  186. {
  187. boost::regex expression;
  188. map_type index;
  189. std::string::const_iterator base;
  190. bool grep_callback(boost::smatch what);
  191. public:
  192. void IndexClasses(const std::string& file);
  193. class_index()
  194. : index(),
  195. expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
  196. "(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?"
  197. "[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?"
  198. "(\\{|:[^;\\{()]*\\{)"
  199. ){}
  200. };
  201. bool class_index::grep_callback(boost::smatch what)
  202. {
  203. // what[0] contains the whole string
  204. // what[5] contains the class name.
  205. // what[6] contains the template specialisation if any.
  206. // add class name and position to map:
  207. index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
  208. what[5].first - base;
  209. return true;
  210. }
  211. void class_index::IndexClasses(const std::string& file)
  212. {
  213. std::string::const_iterator start, end;
  214. start = file.begin();
  215. end = file.end();
  216. base = start;
  217. regex_grep(std::bind1st(std::mem_fun(&class_index::grep_callback), this),
  218. start,
  219. end,
  220. expression);
  221. }
  222. Finally, C++ Builder users can use C++ Builder's closure type as a callback argument:
  223. #include <string>
  224. #include <map>
  225. #include <boost/regex.hpp>
  226. #include <functional>
  227. // purpose:
  228. // takes the contents of a file in the form of a string
  229. // and searches for all the C++ class definitions, storing
  230. // their locations in a map of strings/int's
  231. typedef std::map<std::string, int, std::less<std::string> > map_type;
  232. class class_index
  233. {
  234. boost::regex expression;
  235. map_type index;
  236. std::string::const_iterator base;
  237. typedef boost::smatch arg_type;
  238. bool grep_callback(const arg_type& what);
  239. public:
  240. typedef bool (__closure* grep_callback_type)(const arg_type&);
  241. void IndexClasses(const std::string& file);
  242. class_index()
  243. : index(),
  244. expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
  245. "(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?"
  246. "[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?"
  247. "(\\{|:[^;\\{()]*\\{)"
  248. ){}
  249. };
  250. bool class_index::grep_callback(const arg_type& what)
  251. {
  252. // what[0] contains the whole string
  253. // what[5] contains the class name.
  254. // what[6] contains the template specialisation if any.
  255. // add class name and position to map:
  256. index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
  257. what[5].first - base;
  258. return true;
  259. }
  260. void class_index::IndexClasses(const std::string& file)
  261. {
  262. std::string::const_iterator start, end;
  263. start = file.begin();
  264. end = file.end();
  265. base = start;
  266. class_index::grep_callback_type cl = &(this->grep_callback);
  267. regex_grep(cl,
  268. start,
  269. end,
  270. expression);
  271. }
  272. [endsect]