Debian Codegraph @

Source/Core/DolphinQt/Debugger/GekkoSyntaxHighlight.cpp

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/Debugger/GekkoSyntaxHighlight.h"
5
6 #include "Common/Assembler/GekkoParser.h"
7
8 #include <utility>
9
10 #include <QLabel>
11 #include <QPalette>
12
13 namespace
14 {
15 using namespace Common::GekkoAssembler;
16 using namespace Common::GekkoAssembler::detail;
17
18 class HighlightParsePlugin : public ParsePlugin
19 {
20 public:
21 ~HighlightParsePlugin() override = default;
22
23 std::vector<std::pair<int, int>>&& MoveParens() { return std::move(m_matched_parens); }
24 std::vector<std::tuple<int, int, HighlightFormat>>&& MoveFormatting()
25 {
26 return std::move(m_formatting);
27 }
28
29 void OnDirectivePre(GekkoDirective) override { HighlightCurToken(HighlightFormat::Directive); }
30
31 void OnInstructionPre(const ParseInfo&, bool) override
32 {
33 HighlightCurToken(HighlightFormat::Mnemonic);
34 }
35
36 void OnTerminal(Terminal type, const AssemblerToken& val) override
37 {
38 switch (type)
39 {
40 case Terminal::NumLabFwd:
41 case Terminal::NumLabBwd:
42 case Terminal::Id:
43 HighlightCurToken(HighlightFormat::Symbol);
44 break;
45
46 case Terminal::Hex:
47 case Terminal::Dec:
48 case Terminal::Oct:
49 case Terminal::Bin:
50 case Terminal::Flt:
51 HighlightCurToken(HighlightFormat::Immediate);
52 break;
53
54 case Terminal::GPR:
55 HighlightCurToken(HighlightFormat::GPR);
56 break;
57
58 case Terminal::FPR:
59 HighlightCurToken(HighlightFormat::FPR);
60 break;
61
62 case Terminal::SPR:
63 HighlightCurToken(HighlightFormat::SPR);
64 break;
65
66 case Terminal::CRField:
67 HighlightCurToken(HighlightFormat::CRField);
68 break;
69
70 case Terminal::Lt:
71 case Terminal::Gt:
72 case Terminal::Eq:
73 case Terminal::So:
74 HighlightCurToken(HighlightFormat::CRFlag);
75 break;
76
77 case Terminal::Str:
78 HighlightCurToken(HighlightFormat::Str);
79 break;
80
81 default:
82 break;
83 }
84 }
85
86 void OnHiaddr(std::string_view) override
87 {
88 HighlightCurToken(HighlightFormat::Symbol);
89 auto&& [ha_pos, ha_tok] = m_owner->lexer.LookaheadTagRef(2);
90 m_formatting.emplace_back(static_cast<int>(ha_pos.col),
91 static_cast<int>(ha_tok.token_val.length()), HighlightFormat::HaLa);
92 }
93
94 void OnLoaddr(std::string_view id) override { OnHiaddr(id); }
95
96 void OnOpenParen(ParenType type) override
97 {
98 m_paren_stack.push_back(static_cast<int>(m_owner->lexer.ColNumber()));
99 }
100
101 void OnCloseParen(ParenType type) override
102 {
103 if (m_paren_stack.empty())
104 {
105 return;
106 }
107
108 m_matched_parens.emplace_back(m_paren_stack.back(),
109 static_cast<int>(m_owner->lexer.ColNumber()));
110 m_paren_stack.pop_back();
111 }
112
113 void OnError() override
114 {
115 m_formatting.emplace_back(static_cast<int>(m_owner->error->col),
116 static_cast<int>(m_owner->error->len), HighlightFormat::Error);
117 }
118
119 void OnLabelDecl(std::string_view name) override
120 {
121 const int len = static_cast<int>(m_owner->lexer.LookaheadRef().token_val.length());
122 const int off = static_cast<int>(m_owner->lexer.ColNumber());
123 m_formatting.emplace_back(len, off, HighlightFormat::Symbol);
124 }
125
126 void OnNumericLabelDecl(std::string_view name, u32 parse_num) override
127 {
128 const int len = static_cast<int>(m_owner->lexer.LookaheadRef().token_val.length());
129 const int off = static_cast<int>(m_owner->lexer.ColNumber());
130 m_formatting.emplace_back(len, off, HighlightFormat::Symbol);
131 }
132
133 void OnVarDecl(std::string_view name) override { OnLabelDecl(name); }
134
135 private:
136 std::vector<int> m_paren_stack;
137 std::vector<std::pair<int, int>> m_matched_parens;
138 std::vector<std::tuple<int, int, HighlightFormat>> m_formatting;
139
140 void HighlightCurToken(HighlightFormat format)
141 {
142 const int len = static_cast<int>(m_owner->lexer.LookaheadRef().token_val.length());
143 const int off = static_cast<int>(m_owner->lexer.ColNumber());
144 m_formatting.emplace_back(off, len, format);
145 }
146 };
147 } // namespace
148
149 void GekkoSyntaxHighlight::highlightBlock(const QString& text)
150 {
151 BlockInfo* info = static_cast<BlockInfo*>(currentBlockUserData());
152 if (info == nullptr)
153 {
154 info = new BlockInfo;
155 setCurrentBlockUserData(info);
156 }
157
158 qsizetype comment_idx = text.indexOf(QLatin1Char('#'));
159 if (comment_idx != -1)
160 {
161 HighlightSubstr(comment_idx, text.length() - comment_idx, HighlightFormat::Comment);
162 }
163
164 if (m_mode == 0)
165 {
166 HighlightParsePlugin plugin;
167 ParseWithPlugin(&plugin, text.toStdString());
168
169 info->block_format = plugin.MoveFormatting();
170 info->parens = plugin.MoveParens();
171 info->error = std::move(plugin.Error());
172 info->error_at_eol = info->error && info->error->len == 0;
173 }
174 else if (m_mode == 1)
175 {
176 auto paren_it = std::ranges::find_if(info->parens, [this](const std::pair<int, int>& p) {
177 return p.first == m_cursor_loc || p.second == m_cursor_loc;
178 });
179 if (paren_it != info->parens.end())
180 {
181 HighlightSubstr(paren_it->first, 1, HighlightFormat::Paren);
182 HighlightSubstr(paren_it->second, 1, HighlightFormat::Paren);
183 }
184 }
185
186 for (auto&& [off, len, format] : info->block_format)
187 {
188 HighlightSubstr(off, len, format);
189 }
190 }
191
192 GekkoSyntaxHighlight::GekkoSyntaxHighlight(QTextDocument* document, QTextCharFormat base_format,
193 bool dark_scheme)
194 : QSyntaxHighlighter(document), m_base_format(std::move(base_format))
195 {
196 QPalette base_scheme;
197 m_theme_idx = dark_scheme ? 1 : 0;
198 }
199
200 void GekkoSyntaxHighlight::HighlightSubstr(int start, int len, HighlightFormat format)
201 {
202 QTextCharFormat hl_format = m_base_format;
203 const QColor DIRECTIVE_COLOR[2] = {QColor(0x9d, 0x00, 0x06),
204 QColor(0xfb, 0x49, 0x34)}; // Gruvbox darkred
205 const QColor MNEMONIC_COLOR[2] = {QColor(0x79, 0x74, 0x0e),
206 QColor(0xb8, 0xbb, 0x26)}; // Gruvbox darkgreen
207 const QColor IMM_COLOR[2] = {QColor(0xb5, 0x76, 0x14),
208 QColor(0xfa, 0xbd, 0x2f)}; // Gruvbox darkyellow
209 const QColor BUILTIN_COLOR[2] = {QColor(0x07, 0x66, 0x78),
210 QColor(0x83, 0xa5, 0x98)}; // Gruvbox darkblue
211 const QColor HA_LA_COLOR[2] = {QColor(0xaf, 0x3a, 0x03),
212 QColor(0xfe, 0x80, 0x19)}; // Gruvbox darkorange
213 const QColor HOVER_BG_COLOR[2] = {QColor(0xd5, 0xc4, 0xa1),
214 QColor(0x50, 0x49, 0x45)}; // Gruvbox bg2
215 const QColor STRING_COLOR[2] = {QColor(0x98, 0x97, 0x1a),
216 QColor(0x98, 0x97, 0x1a)}; // Gruvbox green
217 const QColor COMMENT_COLOR[2] = {QColor(0x68, 0x9d, 0x6a),
218 QColor(0x68, 0x9d, 0x6a)}; // Gruvbox aqua
219
220 switch (format)
221 {
222 case HighlightFormat::Directive:
223 hl_format.setForeground(DIRECTIVE_COLOR[m_theme_idx]);
224 break;
225 case HighlightFormat::Mnemonic:
226 hl_format.setForeground(MNEMONIC_COLOR[m_theme_idx]);
227 break;
228 case HighlightFormat::Symbol:
229 break;
230 case HighlightFormat::Immediate:
231 hl_format.setForeground(IMM_COLOR[m_theme_idx]);
232 break;
233 case HighlightFormat::GPR:
234 case HighlightFormat::FPR:
235 case HighlightFormat::SPR:
236 case HighlightFormat::CRField:
237 case HighlightFormat::CRFlag:
238 hl_format.setForeground(BUILTIN_COLOR[m_theme_idx]);
239 break;
240 case HighlightFormat::Str:
241 hl_format.setForeground(STRING_COLOR[m_theme_idx]);
242 break;
243 case HighlightFormat::HaLa:
244 hl_format.setForeground(HA_LA_COLOR[m_theme_idx]);
245 break;
246 case HighlightFormat::Paren:
247 hl_format.setBackground(HOVER_BG_COLOR[m_theme_idx]);
248 break;
249 case HighlightFormat::Default:
250 hl_format.clearForeground();
251 hl_format.clearBackground();
252 break;
253 case HighlightFormat::Comment:
254 hl_format.setForeground(COMMENT_COLOR[m_theme_idx]);
255 break;
256 case HighlightFormat::Error:
257 hl_format.setUnderlineColor(Qt::red);
258 hl_format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
259 break;
260 }
261
262 setFormat(start, len, hl_format);
263 }