parent
8964308b8f
commit
b13b665913
5 changed files with 123 additions and 6 deletions
@ -0,0 +1,43 @@ |
||||
#include <regex> |
||||
#include <Highlighter.hpp> |
||||
#include <iostream> |
||||
|
||||
namespace groove { |
||||
|
||||
std::vector<std::pair<std::regex, ncurses::Colors>> Highlighter::list = { |
||||
make_pair("([+-.<>,;=!:])", ncurses::Colors::CYAN), |
||||
make_pair("([\\{\\}\\[\\]\\(\\)])", ncurses::Colors::GREEN), |
||||
make_pair("(while|if|try|catch|void|this|char|bool|unsigned|long|short|int|return)\\*?", 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("\\\".*\\\"", ncurses::Colors::ORANGE), |
||||
make_pair("/\\*.*\\*//*", ncurses::Colors::ORANGE), |
||||
make_pair("(//.*)", ncurses::Colors::ORANGE), |
||||
make_pair("(^#.*)", ncurses::Colors::ORANGE) |
||||
}; |
||||
|
||||
std::unordered_map<int, std::pair<int, ncurses::Colors>> groove::Highlighter::get() { |
||||
std::unordered_map<int, std::pair<int, ncurses::Colors>> 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; |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,24 @@ |
||||
#pragma once |
||||
|
||||
#include <string> |
||||
#include <ncurses/ncurses.hpp> |
||||
#include <vector> |
||||
#include <unordered_map> |
||||
#include <regex> |
||||
|
||||
namespace groove { |
||||
|
||||
inline std::pair<std::regex, ncurses::Colors> make_pair(std::string regex, ncurses::Colors color) { |
||||
return std::make_pair(std::regex(regex), color); |
||||
}; |
||||
|
||||
class Highlighter { |
||||
private: |
||||
std::string line; |
||||
static std::vector<std::pair<std::regex, ncurses::Colors>> list; |
||||
public: |
||||
Highlighter(std::string line) : line(line) {} |
||||
std::unordered_map<int, std::pair<int, ncurses::Colors>> get(); |
||||
}; |
||||
|
||||
} |
Loading…
Reference in new issue