#include #include #include namespace groove { std::unordered_map 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> groove::Highlighter::get() { std::unordered_map> 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; } }