Line data Source code
1 : // Author(s): Wieger Wesselink 2 : // Copyright: see the accompanying file COPYING or copy at 3 : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING 4 : // 5 : // Distributed under the Boost Software License, Version 1.0. 6 : // (See accompanying file LICENSE_1_0.txt or copy at 7 : // http://www.boost.org/LICENSE_1_0.txt) 8 : // 9 : /// \file mcrl2/utilities/detail/separate_keyword_section.h 10 : /// \brief add your file description here. 11 : 12 : #ifndef MCRL2_UTILITIES_DETAIL_SEPARATE_KEYWORD_SECTION_H 13 : #define MCRL2_UTILITIES_DETAIL_SEPARATE_KEYWORD_SECTION_H 14 : 15 : #include <utility> 16 : #include <boost/algorithm/string/join.hpp> 17 : #include <boost/algorithm/string/trim.hpp> 18 : #include "mcrl2/utilities/text_utility.h" 19 : 20 : namespace mcrl2 { 21 : 22 : namespace utilities { 23 : 24 : namespace detail { 25 : 26 : // Separates all sections with a keyword from the other keyword sections 27 : // Returns a pair containing consisiting of the keyword section and the other keyword sections 28 : inline 29 57 : std::pair<std::string, std::string> separate_keyword_section(const std::string& text1, const std::string& keyword, const std::vector<std::string>& all_keywords, bool repeat_keyword = false) 30 : { 31 57 : std::string text = boost::trim_copy(text1); 32 57 : std::ostringstream out1; // will contain the keyword sections 33 57 : std::ostringstream out2; // will contain the other keyword sections 34 : 35 57 : std::string regex_keyword = "\\b" + keyword + "\\b"; 36 : // create a regex that looks like this: "(\\beqn\\b)|(\\bcons\\b)|(\\bmap\\b)|(\\bvar\\b)" 37 57 : std::vector<std::string> v = all_keywords; 38 57 : v.erase(std::remove(v.begin(), v.end(), keyword), v.end()); // erase keyword from v 39 134 : for (std::string& s: v) 40 : { 41 77 : s = "(\\b" + s + "\\b)"; 42 : } 43 57 : std::string regex_other_keywords = boost::algorithm::join(v, "|"); 44 : 45 57 : std::vector<std::string> specs = utilities::regex_split(text, regex_keyword); 46 57 : if (text.find(keyword) != 0 && !specs.empty()) 47 : { 48 6 : out2 << specs.front() << std::endl; 49 6 : specs.erase(specs.begin()); 50 : } 51 114 : for (const std::string& spec: specs) 52 : { 53 : // strip trailing map/cons/var/eqn declarations 54 57 : std::vector<std::string> v = utilities::regex_split(spec, regex_other_keywords); 55 57 : if (!v.empty()) 56 : { 57 57 : if (repeat_keyword) 58 : { 59 0 : out1 << "\n" << keyword; 60 : } 61 57 : out1 << " " << v.front(); 62 57 : out2 << spec.substr(v.front().size()); 63 : } 64 57 : } 65 57 : std::string s1 = out1.str(); 66 57 : if (!s1.empty()) 67 : { 68 57 : if (!repeat_keyword) 69 : { 70 57 : s1 = keyword + "\n" + s1; 71 : } 72 : } 73 171 : return std::make_pair(s1 + "\n", out2.str() + "\n"); 74 57 : } 75 : 76 : } // namespace detail 77 : 78 : } // namespace utilities 79 : 80 : } // namespace mcrl2 81 : 82 : #endif // MCRL2_UTILITIES_DETAIL_SEPARATE_KEYWORD_SECTION_H