Add custom syntax highlighter and optimize
This commit is contained in:
97
include/syntax/decl.h
Normal file
97
include/syntax/decl.h
Normal file
@@ -0,0 +1,97 @@
|
||||
#ifndef SYNTAX_DECL_H
|
||||
#define SYNTAX_DECL_H
|
||||
|
||||
#include "io/knot.h"
|
||||
#include "io/sysio.h"
|
||||
|
||||
struct Trie {
|
||||
struct TrieNode {
|
||||
bool is_word = false;
|
||||
std::array<TrieNode *, 128> children{};
|
||||
TrieNode() { children.fill(nullptr); }
|
||||
};
|
||||
|
||||
Trie() : root(new TrieNode()) {}
|
||||
~Trie() { clear_trie(root); }
|
||||
|
||||
void build(const std::vector<std::string> &words) {
|
||||
for (const auto &word : words) {
|
||||
TrieNode *node = root;
|
||||
for (char c : word) {
|
||||
unsigned char uc = static_cast<unsigned char>(c);
|
||||
if (!node->children[uc])
|
||||
node->children[uc] = new TrieNode();
|
||||
node = node->children[uc];
|
||||
}
|
||||
node->is_word = true;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t match(const char *text, uint32_t pos, uint32_t len,
|
||||
bool (*is_word_char)(char c)) const {
|
||||
const TrieNode *node = root;
|
||||
uint32_t max_len = 0;
|
||||
for (uint32_t i = pos; i < len; ++i) {
|
||||
unsigned char uc = static_cast<unsigned char>(text[i]);
|
||||
if (uc >= 128)
|
||||
return 0;
|
||||
if (!node->children[uc]) {
|
||||
if (node->is_word && !is_word_char(text[i]))
|
||||
return i - pos;
|
||||
break;
|
||||
}
|
||||
node = node->children[uc];
|
||||
if (node->is_word)
|
||||
max_len = i - pos + 1;
|
||||
}
|
||||
if (max_len > 0)
|
||||
if (pos + max_len < len && is_word_char(text[pos + max_len]))
|
||||
return 0;
|
||||
return max_len;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
clear_trie(root);
|
||||
root = new TrieNode();
|
||||
}
|
||||
|
||||
private:
|
||||
TrieNode *root;
|
||||
|
||||
void clear_trie(TrieNode *node) {
|
||||
if (!node)
|
||||
return;
|
||||
for (auto *child : node->children)
|
||||
clear_trie(child);
|
||||
delete node;
|
||||
}
|
||||
};
|
||||
|
||||
struct Highlight {
|
||||
uint32_t fg;
|
||||
uint32_t bg;
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
inline static const std::unordered_map<uint8_t, Highlight> highlight_map = {
|
||||
{0, {0xFFFFFF, 0, 0}}, {1, {0xAAAAAA, 0, CF_ITALIC}},
|
||||
{2, {0xAAD94C, 0, 0}}, {3, {0xFFFFFF, 0, CF_ITALIC}},
|
||||
{4, {0xFF8F40, 0, 0}}, {5, {0xFFB454, 0, 0}},
|
||||
{6, {0xD2A6FF, 0, 0}}, {7, {0x95E6CB, 0, 0}},
|
||||
{8, {0xF07178, 0, 0}}, {9, {0xE6C08A, 0, 0}},
|
||||
{10, {0x7dcfff, 0, 0}},
|
||||
};
|
||||
|
||||
struct Token {
|
||||
uint32_t start;
|
||||
uint32_t end;
|
||||
uint8_t type;
|
||||
};
|
||||
|
||||
struct LineData {
|
||||
std::shared_ptr<void> in_state{nullptr};
|
||||
std::vector<Token> tokens;
|
||||
std::shared_ptr<void> out_state{nullptr};
|
||||
};
|
||||
|
||||
#endif
|
||||
28
include/syntax/langs.h
Normal file
28
include/syntax/langs.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef SYNTAX_LANGS_H
|
||||
#define SYNTAX_LANGS_H
|
||||
|
||||
#include "syntax/decl.h"
|
||||
|
||||
#define DEF_LANG(name) \
|
||||
std::shared_ptr<void> name##_parse(std::vector<Token> *tokens, \
|
||||
std::shared_ptr<void> in_state, \
|
||||
const char *text, uint32_t len); \
|
||||
bool name##_state_match(std::shared_ptr<void> state_1, \
|
||||
std::shared_ptr<void> state_2);
|
||||
|
||||
#define LANG_A(name) {name##_parse, name##_state_match}
|
||||
|
||||
DEF_LANG(ruby);
|
||||
|
||||
inline static const std::unordered_map<
|
||||
std::string,
|
||||
std::tuple<std::shared_ptr<void> (*)(std::vector<Token> *tokens,
|
||||
std::shared_ptr<void> in_state,
|
||||
const char *text, uint32_t len),
|
||||
bool (*)(std::shared_ptr<void> state_1,
|
||||
std::shared_ptr<void> state_2)>>
|
||||
parsers = {
|
||||
{"ruby", LANG_A(ruby)},
|
||||
};
|
||||
|
||||
#endif
|
||||
212
include/syntax/line_tree.h
Normal file
212
include/syntax/line_tree.h
Normal file
@@ -0,0 +1,212 @@
|
||||
// #include "syntax/decl.h"
|
||||
//
|
||||
// struct LineTree {
|
||||
// void clear() {
|
||||
// clear_node(root);
|
||||
// root = nullptr;
|
||||
// stack_size = 0;
|
||||
// }
|
||||
// void build(uint32_t x) { root = build_node(x); }
|
||||
// LineData *at(uint32_t x) {
|
||||
// LineNode *n = root;
|
||||
// while (n) {
|
||||
// uint32_t left_size = n->left ? n->left->size : 0;
|
||||
// if (x < left_size) {
|
||||
// n = n->left;
|
||||
// } else if (x < left_size + n->data.size()) {
|
||||
// return &n->data[x - left_size];
|
||||
// } else {
|
||||
// x -= left_size + n->data.size();
|
||||
// n = n->right;
|
||||
// }
|
||||
// }
|
||||
// return nullptr;
|
||||
// }
|
||||
// LineData *start_iter(uint32_t x) {
|
||||
// stack_size = 0;
|
||||
// LineNode *n = root;
|
||||
// while (n) {
|
||||
// uint32_t left_size = n->left ? n->left->size : 0;
|
||||
// if (x < left_size) {
|
||||
// push(n, 0);
|
||||
// n = n->left;
|
||||
// } else if (x < left_size + n->data.size()) {
|
||||
// push(n, x - left_size + 1);
|
||||
// return &n->data[x - left_size];
|
||||
// } else {
|
||||
// x -= left_size + n->data.size();
|
||||
// push(n, UINT32_MAX);
|
||||
// n = n->right;
|
||||
// }
|
||||
// }
|
||||
// return nullptr;
|
||||
// }
|
||||
// void end_iter() { stack_size = 0; }
|
||||
// LineData *next() {
|
||||
// while (stack_size) {
|
||||
// auto &f = stack[stack_size - 1];
|
||||
// LineNode *n = f.node;
|
||||
// if (f.index < n->data.size())
|
||||
// return &n->data[f.index++];
|
||||
// stack_size--;
|
||||
// if (n->right) {
|
||||
// n = n->right;
|
||||
// while (n) {
|
||||
// push(n, 0);
|
||||
// if (!n->left)
|
||||
// break;
|
||||
// n = n->left;
|
||||
// }
|
||||
// return &stack[stack_size - 1].node->data[0];
|
||||
// }
|
||||
// }
|
||||
// return nullptr;
|
||||
// }
|
||||
// void insert(uint32_t x, uint32_t y) { root = insert_node(root, x, y); }
|
||||
// void erase(uint32_t x, uint32_t y) { root = erase_node(root, x, y); }
|
||||
// uint32_t count() { return subtree_size(root); }
|
||||
// ~LineTree() { clear(); }
|
||||
//
|
||||
// private:
|
||||
// struct LineNode {
|
||||
// LineNode *left = nullptr;
|
||||
// LineNode *right = nullptr;
|
||||
// uint8_t depth = 1;
|
||||
// uint32_t size = 0;
|
||||
// std::vector<LineData> data;
|
||||
// };
|
||||
// struct Frame {
|
||||
// LineNode *node;
|
||||
// uint32_t index;
|
||||
// };
|
||||
// void push(LineNode *n, uint32_t x) {
|
||||
// stack[stack_size].node = n;
|
||||
// stack[stack_size].index = x;
|
||||
// stack_size++;
|
||||
// }
|
||||
// static void clear_node(LineNode *n) {
|
||||
// if (!n)
|
||||
// return;
|
||||
// clear_node(n->left);
|
||||
// clear_node(n->right);
|
||||
// delete n;
|
||||
// }
|
||||
// LineNode *root = nullptr;
|
||||
// Frame stack[32];
|
||||
// uint8_t stack_size = 0;
|
||||
// static constexpr uint32_t LEAF_TARGET = 256;
|
||||
// LineTree::LineNode *erase_node(LineNode *n, uint32_t x, uint32_t y) {
|
||||
// if (!n)
|
||||
// return nullptr;
|
||||
// if (!n->left && !n->right) {
|
||||
// n->data.erase(n->data.begin() + x, n->data.begin() + x + y);
|
||||
// fix(n);
|
||||
// return n;
|
||||
// }
|
||||
// uint32_t left_size = subtree_size(n->left);
|
||||
// if (x < left_size)
|
||||
// n->left = erase_node(n->left, x, y);
|
||||
// else
|
||||
// n->right = erase_node(n->right, x - left_size - n->data.size(), y);
|
||||
// if (n->left && n->right &&
|
||||
// subtree_size(n->left) + subtree_size(n->right) < 256) {
|
||||
// return merge(n->left, n->right);
|
||||
// }
|
||||
// return rebalance(n);
|
||||
// }
|
||||
// LineTree::LineNode *insert_node(LineNode *n, uint32_t x, uint32_t y) {
|
||||
// if (!n) {
|
||||
// auto *leaf = new LineNode();
|
||||
// leaf->data.resize(y);
|
||||
// leaf->size = y;
|
||||
// return leaf;
|
||||
// }
|
||||
// if (!n->left && !n->right) {
|
||||
// n->data.insert(n->data.begin() + x, y, LineData{});
|
||||
// fix(n);
|
||||
// if (n->data.size() > 512)
|
||||
// return split_leaf(n);
|
||||
// return n;
|
||||
// }
|
||||
// uint32_t left_size = subtree_size(n->left);
|
||||
// if (x <= left_size)
|
||||
// n->left = insert_node(n->left, x, y);
|
||||
// else
|
||||
// n->right = insert_node(n->right, x - left_size - n->data.size(), y);
|
||||
// return rebalance(n);
|
||||
// }
|
||||
// LineNode *build_node(uint32_t count) {
|
||||
// if (count <= LEAF_TARGET) {
|
||||
// auto *n = new LineNode();
|
||||
// n->data.resize(count);
|
||||
// n->size = count;
|
||||
// return n;
|
||||
// }
|
||||
// uint32_t left_count = count / 2;
|
||||
// uint32_t right_count = count - left_count;
|
||||
// auto *n = new LineNode();
|
||||
// n->left = build_node(left_count);
|
||||
// n->right = build_node(right_count);
|
||||
// fix(n);
|
||||
// return n;
|
||||
// }
|
||||
// static LineNode *split_leaf(LineNode *n) {
|
||||
// auto *right = new LineNode();
|
||||
// size_t mid = n->data.size() / 2;
|
||||
// right->data.assign(n->data.begin() + mid, n->data.end());
|
||||
// n->data.resize(mid);
|
||||
// fix(n);
|
||||
// fix(right);
|
||||
// auto *parent = new LineNode();
|
||||
// parent->left = n;
|
||||
// parent->right = right;
|
||||
// fix(parent);
|
||||
// return parent;
|
||||
// }
|
||||
// static LineNode *merge(LineNode *a, LineNode *b) {
|
||||
// a->data.insert(a->data.end(), b->data.begin(), b->data.end());
|
||||
// delete b;
|
||||
// fix(a);
|
||||
// return a;
|
||||
// }
|
||||
// static void fix(LineNode *n) {
|
||||
// n->depth = 1 + MAX(height(n->left), height(n->right));
|
||||
// n->size = subtree_size(n->left) + n->data.size() +
|
||||
// subtree_size(n->right);
|
||||
// }
|
||||
// static LineNode *rotate_right(LineNode *y) {
|
||||
// LineNode *x = y->left;
|
||||
// LineNode *T2 = x->right;
|
||||
// x->right = y;
|
||||
// y->left = T2;
|
||||
// fix(y);
|
||||
// fix(x);
|
||||
// return x;
|
||||
// }
|
||||
// static LineNode *rotate_left(LineNode *x) {
|
||||
// LineNode *y = x->right;
|
||||
// LineNode *T2 = y->left;
|
||||
// y->left = x;
|
||||
// x->right = T2;
|
||||
// fix(x);
|
||||
// fix(y);
|
||||
// return y;
|
||||
// }
|
||||
// static LineNode *rebalance(LineNode *n) {
|
||||
// fix(n);
|
||||
// int balance = int(height(n->left)) - int(height(n->right));
|
||||
// if (balance > 1) {
|
||||
// if (height(n->left->left) < height(n->left->right))
|
||||
// n->left = rotate_left(n->left);
|
||||
// return rotate_right(n);
|
||||
// }
|
||||
// if (balance < -1) {
|
||||
// if (height(n->right->right) < height(n->right->left))
|
||||
// n->right = rotate_right(n->right);
|
||||
// return rotate_left(n);
|
||||
// }
|
||||
// return n;
|
||||
// }
|
||||
// static uint8_t height(LineNode *n) { return n ? n->depth : 0; }
|
||||
// static uint32_t subtree_size(LineNode *n) { return n ? n->size : 0; }
|
||||
// };
|
||||
33
include/syntax/parser.h
Normal file
33
include/syntax/parser.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "syntax/decl.h"
|
||||
|
||||
struct Parser {
|
||||
Knot *root;
|
||||
std::shared_mutex *knot_mutex;
|
||||
std::string lang;
|
||||
std::shared_ptr<void> (*parse_func)(std::vector<Token> *tokens,
|
||||
std::shared_ptr<void> in_state,
|
||||
const char *text, uint32_t len);
|
||||
bool (*state_match_func)(std::shared_ptr<void> state_1,
|
||||
std::shared_ptr<void> state_2);
|
||||
std::atomic<uint32_t> scroll_max{UINT32_MAX - 2048};
|
||||
std::mutex mutex;
|
||||
std::mutex data_mutex;
|
||||
std::vector<LineData> line_data;
|
||||
std::set<uint32_t> dirty_lines;
|
||||
|
||||
Parser(Knot *n_root, std::shared_mutex *n_knot_mutex, std::string n_lang,
|
||||
uint32_t n_scroll_max);
|
||||
void edit(Knot *n_root, uint32_t start_line, uint32_t old_end_line,
|
||||
uint32_t new_end_line);
|
||||
void work();
|
||||
void scroll(uint32_t line);
|
||||
uint8_t get_type(Coord c) {
|
||||
if (c.row >= line_data.size())
|
||||
return 0;
|
||||
const LineData &line = line_data[c.row];
|
||||
for (const Token &t : line.tokens)
|
||||
if (t.start <= c.col && c.col < t.end)
|
||||
return t.type;
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user