-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cpp
121 lines (106 loc) · 2.62 KB
/
Parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include"Parser.h"
using namespace Parser;
std::string Parser::prepare(const std::string& line)
{
using namespace std;
regex reg_right(">");
regex reg_left("<");
string updated = line;
if (regex_search(updated.c_str(), reg_right))
updated = regex_replace(updated, reg_right, "> ");
if (regex_search(updated.c_str(), reg_left))
updated = regex_replace(updated, reg_left, " <");
return updated;
}
tags_str Parser::get_tag(const std::string& line)
{
using namespace std;
regex reg("[<][^<>]*[>]");
vector<string> result;
string text;
function<void(const string&, vector<string>&)> _gt = [&](const string& _line,
vector<string>& result)
{
auto line = put_new_line_ch(prepare(_line));
cmatch m;
regex_search(line.c_str(), m, reg);
if (m.empty())
{
text = line;
return;
}
else
{
result.push_back(*m.begin());
auto updated = regex_replace(line, regex((*m.begin()).str()), DELIM);
_gt(updated, result);
}
};
_gt(line, result);
return make_pair(result,text);
}
std::string Parser::put_new_line_ch(const std::string& line)
{
auto reg = std::regex("\n");
return std::regex_replace(line.c_str(), reg, "<br>");
}
std::vector<std::string> Parser::split(std::string s, std::string delimiter)
{
using namespace std;
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
string token;
vector<string> res;
while ((pos_end = s.find(delimiter, pos_start)) != string::npos)
{
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
res.push_back(delimiter);
}
res.push_back(s.substr(pos_start));
return res;
}
std::vector<Node*> Parser::parse(const std::string& line)
{
using namespace std;
auto tag = get_tag(line);
auto splitted_text = split(tag.second, DELIM);
vector<Node*> result;
int tag_counter = 0;
for (auto token : splitted_text)
{
auto _ = regex_replace(token.c_str(), regex("_"), " ");
if (!is_spaceful(_))
{
bool is_tag = regex_search(_.c_str(), regex(".*[#$#].*"));
if (is_tag)
{
result.push_back(make_tag(tag.first[tag_counter]));
tag_counter++;
}
else
{
result.push_back(make_text(_));
}
}
}
return result;
}
bool Parser::is_spaceful(const std::string& line)
{
using namespace std;
regex space("[ \t\r\n\f]{"+to_string(line.size())+"}");
if (regex_match(line.c_str(), space)) return true;
else return false;
}
std::vector<Node*> Parser::parse_script(const std::vector<std::string>& script)
{
using namespace std;
vector<Node*> result;
for (auto& n : script)
{
auto parsed_line = parse(n);
copy(parsed_line.begin(), parsed_line.end(), back_inserter(result));
}
return result;
}