|
1 | | -#include <regex> |
2 | | -#include <string> |
3 | 1 | #include <QFontMetrics> |
4 | 2 | #include <QPaintEvent> |
5 | 3 | #include <QFontDatabase> |
6 | 4 | #include <QPainter> |
7 | 5 | #include <QStack> |
8 | 6 |
|
9 | 7 | #include "msg_box_preview.h" |
| 8 | +#include "msg_macros.h" |
10 | 9 |
|
11 | 10 |
|
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 | | - |
32 | 11 | QString MsgBoxPreview::runPreProcessor() |
33 | 12 | { |
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; |
50 | 13 | 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); |
134 | 15 | return QString::fromStdString(ret); |
135 | 16 | } |
136 | 17 |
|
|
0 commit comments