mCRL2
Loading...
Searching...
No Matches
separate_keyword_section.h
Go to the documentation of this file.
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//
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>
19
20namespace mcrl2 {
21
22namespace utilities {
23
24namespace 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 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 std::string text = boost::trim_copy(text1);
32 std::ostringstream out1; // will contain the keyword sections
33 std::ostringstream out2; // will contain the other keyword sections
34
35 std::string regex_keyword = "\\b" + keyword + "\\b";
36 // create a regex that looks like this: "(\\beqn\\b)|(\\bcons\\b)|(\\bmap\\b)|(\\bvar\\b)"
37 std::vector<std::string> v = all_keywords;
38 v.erase(std::remove(v.begin(), v.end(), keyword), v.end()); // erase keyword from v
39 for (std::string& s: v)
40 {
41 s = "(\\b" + s + "\\b)";
42 }
43 std::string regex_other_keywords = boost::algorithm::join(v, "|");
44
45 std::vector<std::string> specs = utilities::regex_split(text, regex_keyword);
46 if (text.find(keyword) != 0 && !specs.empty())
47 {
48 out2 << specs.front() << std::endl;
49 specs.erase(specs.begin());
50 }
51 for (const std::string& spec: specs)
52 {
53 // strip trailing map/cons/var/eqn declarations
54 std::vector<std::string> v = utilities::regex_split(spec, regex_other_keywords);
55 if (!v.empty())
56 {
57 if (repeat_keyword)
58 {
59 out1 << "\n" << keyword;
60 }
61 out1 << " " << v.front();
62 out2 << spec.substr(v.front().size());
63 }
64 }
65 std::string s1 = out1.str();
66 if (!s1.empty())
67 {
68 if (!repeat_keyword)
69 {
70 s1 = keyword + "\n" + s1;
71 }
72 }
73 return std::make_pair(s1 + "\n", out2.str() + "\n");
74 }
75
76} // namespace detail
77
78} // namespace utilities
79
80} // namespace mcrl2
81
82#endif // MCRL2_UTILITIES_DETAIL_SEPARATE_KEYWORD_SECTION_H
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)
std::vector< std::string > regex_split(const std::string &text, const std::string &sep)
Split a string using a regular expression separator.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
String manipulation functions.