-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight.cpp
More file actions
268 lines (222 loc) · 9.64 KB
/
highlight.cpp
File metadata and controls
268 lines (222 loc) · 9.64 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "highlight.h"
#include <algorithm>
#include <string>
#include <string_view>
#include <type_traits>
#include "Parser.h"
#include "Tokenizer.h"
#include "WordExpander.h"
#include "Global.h"
#include "builtins.h"
#include "utils.h"
#include "replxx.hxx"
#include <sys/stat.h>
namespace highlight {
using replxx::Replxx;
static void highlight_commandlist(Replxx::colors_t &colors, const CommandList &cl);
static void highlight_word(Replxx::colors_t &colors, const Token *token) {
for(int i = token->positionStartUtf8Codepoint; i < token->positionEndUtf8Codepoint; i++) {
colors[i] = Replxx::Color::DEFAULT;
}
}
// check if a potentially signed or unsigned numeric value is less than zero
// without compiler warnings if the number is indeed signed
//
// used for checking if stat.st_mode is not negative
template <typename T, typename std::enable_if<std::is_unsigned<T>::value, bool>::type = true>
static bool is_negative(T) { return false; }
template <typename T, typename std::enable_if<std::is_signed<T>::value, bool>::type = true>
static bool is_negative(T number) { return number < 0; }
static bool command_exists(const std::string &command_name) {
if(g.functions.contains(command_name))
return true;
if(find_builtin(command_name).has_value())
return true;
if(std::optional<std::string> path = g.get_variable("PATH")) {
auto exists = utils::Splitter(path.value()).delim(':').for_each<bool>([&] (const std::string &dir) -> std::optional<bool> {
std::string command_path = (dir.empty() ? std::string{"."} : dir) + "/" + command_name;
struct stat st;
if(stat(command_path.c_str(), &st) != 0) {
return {};
}
if(is_negative(st.st_mode)) {
return {};
}
if(! S_ISREG(st.st_mode)) {
return {};
}
if(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
return { true };
}
return {};
});
if(exists.value_or(false)) {
return true;
}
}
return false;
}
static void highlight_command_simple(Replxx::colors_t &colors, const Command &command) {
const Command::Simple &simple_command = std::get<Command::Simple>(command.value);
for(int i = command.start_token->positionStartUtf8Codepoint; i < command.end_token->positionEndUtf8Codepoint; i++) {
colors[i] = Replxx::Color::DEFAULT;
}
if(! simple_command.argv_tokens.empty()) {
const Token *argv0 = simple_command.argv_tokens.at(0);
std::vector<std::string> expandedArgv;
WordExpander::Options opt;
opt.fieldSplitting = true;
opt.pathnameExpansion = WordExpander::Options::NEVER;
opt.variableAtAsMultipleFields = false;
opt.unsafeExpansions = false;
if(! WordExpander(opt, argv0->value).expand_into(expandedArgv) || expandedArgv.empty()) {
// if word expansion didn't succeed here - just ignore it
expandedArgv = { argv0->value };
}
Replxx::Color colorOfArgv0;
if(command_exists(expandedArgv.at(0))) {
colorOfArgv0 = Replxx::Color::GREEN;
} else {
colorOfArgv0 = Replxx::Color::RED;
}
for(int i = argv0->positionStartUtf8Codepoint; i < argv0->positionEndUtf8Codepoint; i++) {
colors[i] = colorOfArgv0;
}
}
for(std::size_t i = 1; i < simple_command.argv_tokens.size(); i++) {
highlight_word(colors, simple_command.argv_tokens.at(i));
}
}
static void highlight_command_bracegroup(Replxx::colors_t &colors, const Command &command) {
const Command::BraceGroup &bracegroup_command = std::get<Command::BraceGroup>(command.value);
highlight_commandlist(colors, bracegroup_command.command_list);
}
static void highlight_command_if(Replxx::colors_t &colors, const Command &command) {
const Command::If &if_command = std::get<Command::If>(command.value);
highlight_commandlist(colors, if_command.condition);
highlight_commandlist(colors, if_command.then);
if(if_command.opt_else.has_value()) {
highlight_commandlist(colors, if_command.opt_else.value());
}
for(const Command::If::Elif &elif : if_command.elif) {
highlight_commandlist(colors, elif.condition);
highlight_commandlist(colors, elif.then);
}
}
static void highlight_command_while(Replxx::colors_t &colors, const Command &command) {
const Command::While &while_command = std::get<Command::While>(command.value);
highlight_commandlist(colors, while_command.condition);
highlight_commandlist(colors, while_command.body);
}
static void highlight_command_until(Replxx::colors_t &colors, const Command &command) {
const Command::Until &until_command = std::get<Command::Until>(command.value);
highlight_commandlist(colors, until_command.condition);
highlight_commandlist(colors, until_command.body);
}
static void highlight_command_for(Replxx::colors_t &colors, const Command &command) {
const Command::For &for_command = std::get<Command::For>(command.value);
for(const Token *item_token : for_command.items_tokens) {
highlight_word(colors, item_token);
}
highlight_commandlist(colors, for_command.body);
}
static void highlight_command_functiondefinition(Replxx::colors_t &colors, const Command &command) {
const Command::FunctionDefinition &function_definition_command = std::get<Command::FunctionDefinition>(command.value);
highlight_commandlist(colors, function_definition_command.body);
}
static void highlight_command(Replxx::colors_t &colors, const Command &command) {
// unhighlighted pieces of a command - default to magenta
for(int i = command.start_token->positionStartUtf8Codepoint; i < command.end_token->positionEndUtf8Codepoint; i++) {
colors[i] = Replxx::Color::BRIGHTMAGENTA;
}
std::visit(utils::overloaded {
[&] (const Command::Empty) { },
[&] (const Command::Simple) { highlight_command_simple(colors, command); },
[&] (const Command::BraceGroup) { highlight_command_bracegroup(colors, command); },
[&] (const Command::If) { highlight_command_if(colors, command); },
[&] (const Command::While) { highlight_command_while(colors, command); },
[&] (const Command::Until) { highlight_command_until(colors, command); },
[&] (const Command::For) { highlight_command_for(colors, command); },
[&] (const Command::FunctionDefinition) { highlight_command_functiondefinition(colors, command); }
}, command.value);
for(const Redirection &redir : command.redirections) {
if(redir.filename_token.has_value()) {
highlight_word(colors, *redir.filename_token);
}
}
}
static void highlight_pipeline(Replxx::colors_t &colors, const Pipeline &pipeline) {
for(const Command &command : pipeline.commands) {
highlight_command(colors, command);
}
}
static void highlight_and_or_list(Replxx::colors_t &colors, const AndOrList &and_or_list) {
for(const WithFollowingOperator<Pipeline> &pipe_op : and_or_list) {
highlight_pipeline(colors, pipe_op.val);
}
}
static void highlight_commandlist(Replxx::colors_t &colors, const CommandList &cl) {
for(const WithFollowingOperator<AndOrList> &aol_op : cl) {
highlight_and_or_list(colors, aol_op.val);
}
}
static void highlight_everything_by_tokens(std::string const &input, Replxx::colors_t &colors, std::vector<Token> &tokens) {
int utf8_index = -1;
int byte_index = -1;
int token_index = -1;
for(char c : input) {
byte_index += 1;
// utf-8 multi-byte character - front part
if(utils::front_of_multibyte_utf8_codepoint(c)) {
continue;
}
utf8_index += 1;
if(token_index + 1 < static_cast<int>(tokens.size())) {
if(byte_index >= tokens.at(token_index + 1).positionStart) {
token_index += 1;
}
}
if(token_index == -1) {
// empty command line - or a comment followed by nothing or lone whitespace
colors[utf8_index] = Replxx::Color::GRAY;
} else if(tokens.at(token_index).positionEnd < byte_index) {
// a comment after some nonwhitespace text
colors[utf8_index] = Replxx::Color::GRAY;
} else if(tokens.at(token_index).type == Token::Type::OPERATOR) {
// <, or >>, or |, or ...
colors[utf8_index] = Replxx::Color::BRIGHTCYAN;
} else if(tokens.at(token_index).type == Token::Type::WORD) {
// might get everriden by highlight_command_list - but set the default
//colors[utf8_index] = Replxx::Color::DEFAULT;
// no longer do this - tokens go last now for easier implementations
}
}
}
static void highlight_all_red(std::string const &input, Replxx::colors_t &colors) {
// syntax error - make everything red
int i = 0;
for(char c : input) {
// utf-8 multi-byte character - front part
if(utils::front_of_multibyte_utf8_codepoint(c)) {
continue;
}
colors[i++] = Replxx::Color::BRIGHTRED;
}
}
void highlighter_callback(std::string const &input, Replxx::colors_t &colors) {
std::vector<Token> tokens;
CommandList parsed;
try {
tokens = Tokenizer(input).tokenize();
parsed = Parser(tokens).parse();
} catch(const Tokenizer::SyntaxError &) {
highlight_all_red(input, colors);
return;
} catch(const Parser::SyntaxError &) {
highlight_all_red(input, colors);
return;
}
highlight_commandlist(colors, parsed);
highlight_everything_by_tokens(input, colors, tokens);
}
} // namespace highlight