Skip to content

Commit 4b450d4

Browse files
committed
Implemented better message box macros processor
Without use of regexes at all.
1 parent e52e6f0 commit 4b450d4

File tree

8 files changed

+752
-121
lines changed

8 files changed

+752
-121
lines changed

_common/qt-modules/msg_box_preview/msg_box_preview.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ include_directories(${CMAKE_CURRENT_LIST_DIR})
33
set(MSG_BOX_PREVIEW_SRC
44
${CMAKE_CURRENT_LIST_DIR}/msg_box_preview.cpp
55
${CMAKE_CURRENT_LIST_DIR}/msg_box_preview.h
6+
7+
${CMAKE_CURRENT_LIST_DIR}/msg_macros.cpp
8+
${CMAKE_CURRENT_LIST_DIR}/msg_macros.h
69
)

_common/qt-modules/msg_box_preview/msg_box_preview.cpp

Lines changed: 2 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,17 @@
1-
#include <regex>
2-
#include <string>
31
#include <QFontMetrics>
42
#include <QPaintEvent>
53
#include <QFontDatabase>
64
#include <QPainter>
75
#include <QStack>
86

97
#include "msg_box_preview.h"
8+
#include "msg_macros.h"
109

1110

12-
static void s_splitString(std::vector<std::string>& out, const std::string& str, char delimiter)
13-
{
14-
std::string::size_type beg = 0;
15-
std::string::size_type end = 0;
16-
17-
out.clear();
18-
if(str.empty())
19-
return;
20-
21-
do
22-
{
23-
end = str.find(delimiter, beg);
24-
if(end == std::string::npos)
25-
end = str.size();
26-
out.emplace_back(str.substr(beg, end - beg));
27-
beg = end + 1;
28-
}
29-
while(end < str.size() - 1);
30-
}
31-
3211
QString MsgBoxPreview::runPreProcessor()
3312
{
34-
std::regex cond_if = std::regex("^#if *(\\w+\\(.*\\))$");
35-
std::regex cond_elif = std::regex("^#elif *(\\w+\\(.*\\))$");
36-
const std::string cond_endif = "#endif";
37-
const std::string cond_else = "#else";
38-
39-
std::regex reg_op_player = std::regex("^player\\((.*)\\)$");
40-
std::regex reg_op_state = std::regex("^state\\((.*)\\)$");
41-
42-
struct State
43-
{
44-
bool open = false;
45-
bool cond_true = false;
46-
bool skip_to_endif = false;
47-
};
48-
49-
State st;
5013
std::string ret;
51-
std::string tmpS = m_currentText.toStdString();
52-
std::vector<std::string> tmp;
53-
s_splitString(tmp, tmpS, '\n');
54-
55-
for(const auto &t : tmp)
56-
{
57-
std::smatch m_if;
58-
std::smatch m_elif;
59-
60-
if((!st.open && std::regex_search(t, m_if, cond_if)) ||
61-
( st.open && std::regex_search(t, m_elif, cond_elif)))
62-
{
63-
st.cond_true = false;
64-
if(st.open && st.skip_to_endif)
65-
continue;
66-
67-
std::string cond = st.open ? m_elif[1].str() : m_if[1].str();
68-
std::smatch m_op_player;
69-
70-
if(std::regex_search(cond, m_op_player, reg_op_player)) // check whaever player
71-
{
72-
st.open = true;
73-
std::string players = m_op_player[1].str();
74-
std::vector<std::string> nums;
75-
s_splitString(nums, players, ',');
76-
77-
for(auto &i : nums)
78-
{
79-
if(std::atoi(i.c_str()) == m_macro.player)
80-
{
81-
st.cond_true = true;
82-
st.skip_to_endif = true;
83-
break;
84-
}
85-
}
86-
}
87-
else if(std::regex_search(cond, m_op_player, reg_op_state)) // check whaever state
88-
{
89-
st.open = true;
90-
std::string players = m_op_player[1].str();
91-
std::vector<std::string> nums;
92-
s_splitString(nums, players, ',');
93-
94-
for(auto &i : nums)
95-
{
96-
if(std::atoi(i.c_str()) == m_macro.state)
97-
{
98-
st.cond_true = true;
99-
st.skip_to_endif = true;
100-
break;
101-
}
102-
}
103-
}
104-
else // invalid line
105-
{
106-
if(!ret.empty())
107-
ret.push_back('\n');
108-
ret.append(t);
109-
}
110-
}
111-
else if(st.open && t == cond_endif)
112-
{
113-
st.open = false;
114-
st.cond_true = false;
115-
st.skip_to_endif = false;
116-
}
117-
else if(st.open && t == cond_else)
118-
{
119-
st.open = true;
120-
st.cond_true = !st.skip_to_endif;
121-
if(st.open && st.skip_to_endif)
122-
continue;
123-
124-
st.skip_to_endif = false;
125-
}
126-
else if(!st.open || st.cond_true) // ordinary line
127-
{
128-
if(!ret.empty())
129-
ret.push_back('\n');
130-
ret.append(t);
131-
}
132-
}
133-
14+
msgMacroProcess(m_currentText.toStdString(), ret, m_macro.player, m_macro.state);
13415
return QString::fromStdString(ret);
13516
}
13617

0 commit comments

Comments
 (0)