You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.2 KiB
63 lines
2.2 KiB
#include <regex>
|
|
#include <Highlighter.hpp>
|
|
#include <iostream>
|
|
|
|
namespace groove {
|
|
std::unordered_map<Syntax, RulesList> Highlighter::Syntaxes = {
|
|
std::make_pair(Syntax::CPP,
|
|
RulesList {
|
|
make_pair("([+-.<>,;=!:&*])", ncurses::Colors::CYAN),
|
|
make_pair("([\\{\\}\\[\\]\\(\\)])", ncurses::Colors::GREEN),
|
|
make_pair("(^|\\s)(while|if|try|catch|void|this|else|using|namespace|private|public|protected|friend|class|char|bool|unsigned|long|short|int|return):?(\\s+|$)\\*?", ncurses::Colors::MAGENTA),
|
|
make_pair("([a-zA-Z_][a-zA-Z_0-9]+)::", ncurses::Colors::GREEN),
|
|
make_pair("::([a-zA-Z_][a-zA-Z_0-9]+)", ncurses::Colors::CYAN),
|
|
make_pair("\\.([a-zA-Z_][a-zA-Z_0-9]+)", ncurses::Colors::CYAN),
|
|
make_pair("([_a-zA-Z][a-zA-Z0-9_-]+)\\(", ncurses::Colors::CYAN),
|
|
make_pair("\\\".*\\\"", ncurses::Colors::ORANGE),
|
|
make_pair("/\\*.*\\*//*", ncurses::Colors::COMMENTS),
|
|
make_pair("(//.*)", ncurses::Colors::ORANGE)
|
|
}),
|
|
std::make_pair(Syntax::DEFAULT,
|
|
RulesList {
|
|
make_pair("([.,;!\\*])", ncurses::Colors::MAGENTA),
|
|
make_pair("([\\{\\}\\[\\]\\(\\)])", ncurses::Colors::GREEN),
|
|
make_pair("\\\".*\\\"", ncurses::Colors::ORANGE),
|
|
make_pair("/\\*.*\\*//*", ncurses::Colors::COMMENTS),
|
|
})
|
|
};
|
|
|
|
|
|
RulesList Highlighter::list = RulesList();
|
|
|
|
std::unordered_map<long, std::pair<long, ncurses::Colors>> groove::Highlighter::get() {
|
|
std::unordered_map<long, std::pair<long, ncurses::Colors>> list;
|
|
|
|
if (this->line.size() > 0 && this->line.at(0) == '#') {
|
|
list.emplace(0, std::make_pair(this->line.size(), ncurses::Colors::COMMENTS));
|
|
|
|
return list;
|
|
}
|
|
|
|
for (auto &keyword : Highlighter::list) {
|
|
try {
|
|
std::sregex_iterator next(this->line.begin(), this->line.end(), keyword.first);
|
|
std::sregex_iterator end;
|
|
while (next != end) {
|
|
std::smatch match = *next;
|
|
for (unsigned i = 0; i < match.size(); ++i) {
|
|
list.emplace(match.position(i), std::make_pair(match.length(i), keyword.second));
|
|
}
|
|
next++;
|
|
}
|
|
} catch (std::regex_error &e) {
|
|
// Syntax error in the regular expression
|
|
}
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|