Files
crib/src/syntax/parser.cc
T
2026-07-01 18:53:49 +01:00

150 lines
5.0 KiB
C++

#include "syntax/parser.h"
#include "editor/editor.h"
#include "io/knot.h"
#include "main.h"
#include "syntax/decl.h"
#include "syntax/langs.h"
std::array<Highlight, TOKEN_KIND_COUNT> highlights = {};
Parser::Parser(Editor *n_editor, std::string n_lang, uint32_t n_scroll_max) {
editor = n_editor;
scroll_max = n_scroll_max;
lang = n_lang;
auto custom_parser = custom_highlighters.find(n_lang);
if (custom_parser != custom_highlighters.end()) {
parser_block = custom_parser->second.first;
match_block = custom_parser->second.second;
is_custom = true;
} else {
auto pair = parsers.find(n_lang);
if (pair != parsers.end()) {
parse_func = std::get<0>(pair->second);
state_match_func = std::get<1>(pair->second);
is_custom = false;
} else {
assert("unknown lang should be checked by caller" && 0);
}
}
line_map.apply_edit(0, editor->root->line_count + 1);
}
void Parser::edit(uint32_t start_line, uint32_t removed_rows,
uint32_t inserted_rows) {
int64_t delta = (int64_t)inserted_rows - (int64_t)removed_rows;
line_map.apply_edit(start_line, delta);
uint32_t span = MAX(removed_rows, inserted_rows);
uint32_t begin = (start_line > 0) ? start_line - 1 : 0;
uint32_t end = start_line + span;
for (uint32_t line = begin; line <= end + 1; ++line)
if (LineData *ld = line_map.at(line))
ld->out_state = nullptr;
}
void Parser::work() {
if (!editor || !editor->root)
return;
std::vector<uint32_t> batch;
uint32_t line_count = editor->root->line_count + 1;
uint32_t min_line =
scroll_max > MAX_LINES_LOOKAROUND ? scroll_max - MAX_LINES_LOOKAROUND : 0;
uint32_t max_line = MIN(scroll_max + MAX_LINES_LOOKAROUND, line_count - 1);
bool sequential = false;
for (uint32_t i = min_line; i <= max_line; ++i) {
LineData *ld = line_map.at(i);
if ((!ld || !ld->in_state || !ld->out_state) && !sequential) {
batch.push_back(i);
sequential = true;
continue;
}
sequential = false;
}
for (uint32_t c_line : batch) {
if (!running.load(std::memory_order_relaxed))
break;
uint32_t min_line = scroll_max > MAX_LINES_LOOKAROUND
? scroll_max - MAX_LINES_LOOKAROUND
: 0;
uint32_t max_line = scroll_max + MAX_LINES_LOOKAROUND;
if (c_line < min_line || c_line > max_line)
continue;
uint32_t scroll_snapshot = scroll_max;
std::unique_ptr<StateBase> prev_state = nullptr;
if (c_line > 0 && c_line < line_count) {
auto lm = line_map.at(c_line - 1);
if (lm && lm->out_state)
prev_state = lm->out_state ? lm->out_state->clone() : nullptr;
}
LineIterator *it = begin_l_iter(editor->root, c_line);
if (!it)
continue;
uint32_t cur_line = c_line;
while (cur_line < line_count) {
if (!running.load(std::memory_order_relaxed))
break;
if (scroll_snapshot != scroll_max)
break;
if (cur_line < min_line || cur_line > max_line) {
LineData *line_data = line_map.at(cur_line);
if (line_data)
line_data->out_state = nullptr;
break;
}
uint32_t len;
char *line = next_line(it, &len);
if (!line)
break;
LineData *line_data = line_map.create_at(cur_line);
std::unique_ptr<StateBase> new_state;
if (is_custom) {
std::string prev_value = "";
if (prev_state) {
CustomState *prev_custom =
static_cast<CustomState *>(prev_state.get());
prev_value = prev_custom->value;
}
std::string out_value = parse_custom(&line_data->tokens, parser_block,
line, len, prev_value, cur_line);
new_state = std::make_unique<CustomState>(out_value);
} else {
new_state = parse_func(&line_data->tokens, prev_state.get(), line, len,
cur_line);
}
line_data->in_state = std::move(prev_state);
line_data->out_state = std::move(new_state);
bool done = false;
if (cur_line + 1 < line_count) {
LineData *next_line_data = line_map.at(cur_line + 1);
if (next_line_data) {
if (is_custom) {
std::string a = "";
if (new_state) {
CustomState *cs = static_cast<CustomState *>(new_state.get());
a = cs->value;
}
std::string b = "";
if (next_line_data->in_state) {
CustomState *cs =
static_cast<CustomState *>(next_line_data->in_state.get());
b = cs->value;
}
done = custom_compare(match_block, a, b);
} else {
done = state_match_func(new_state.get(),
next_line_data->in_state.get());
}
}
}
prev_state =
line_data->out_state ? line_data->out_state->clone() : nullptr;
cur_line++;
if (done)
break;
}
free(it->buffer);
free(it);
}
}
void Parser::scroll(uint32_t line) { scroll_max = line; }