Update syntax parser system.
This commit is contained in:
@@ -7,7 +7,7 @@ namespace bed::internal::syntax {
|
||||
struct Token {
|
||||
uint32_t start;
|
||||
uint32_t end;
|
||||
enum : uint8_t {
|
||||
enum Kind : uint8_t {
|
||||
Data,
|
||||
Shebang,
|
||||
Comment,
|
||||
@@ -68,9 +68,20 @@ struct Token {
|
||||
} type;
|
||||
};
|
||||
|
||||
struct ParseEvent {
|
||||
std::string_view name;
|
||||
enum : uint8_t {
|
||||
Opening,
|
||||
Closing,
|
||||
SymbolDef,
|
||||
Symbol
|
||||
} ev_type;
|
||||
uint8_t type; // type.
|
||||
};
|
||||
|
||||
struct Language {
|
||||
std::function<void *()> none_state;
|
||||
std::function<void(void **, std::string_view, bool, std::vector<Token> *)> parse;
|
||||
std::function<void(void **, std::string_view, bool, std::vector<Token> *, std::vector<ParseEvent> *)> parse;
|
||||
std::function<void *(void *)> copy;
|
||||
std::function<bool(void *, void *)> equal;
|
||||
std::function<void(void *)> destroy;
|
||||
@@ -102,81 +113,40 @@ struct TreeCursor {
|
||||
ParseStateBranch *stack[64];
|
||||
uint8_t depth = 0;
|
||||
bool went_left[64];
|
||||
TreeCursor(ParseState *root, uint64_t target_line, uint64_t *relative) {
|
||||
ParseState *node = root;
|
||||
while (node->is_branch()) {
|
||||
auto *branch = (ParseStateBranch *)node;
|
||||
auto *left = branch->left;
|
||||
stack[depth] = branch;
|
||||
if (target_line < left->lines()) {
|
||||
went_left[depth] = true;
|
||||
++depth;
|
||||
node = left;
|
||||
} else {
|
||||
target_line -= left->lines();
|
||||
went_left[depth] = false;
|
||||
++depth;
|
||||
node = branch->right;
|
||||
}
|
||||
}
|
||||
*relative = target_line;
|
||||
leaf = (ParseStateLeaf *)node;
|
||||
TreeCursor(ParseState *root, uint64_t target_line, uint64_t *relative);
|
||||
void next();
|
||||
void prev();
|
||||
};
|
||||
|
||||
struct ScopeNode {
|
||||
static constexpr uint64_t SCOPE_BIT = 1ull << 63;
|
||||
static constexpr uint64_t LINES_MASK = ~SCOPE_BIT;
|
||||
uint64_t header;
|
||||
bool is_scope() const {
|
||||
return header & SCOPE_BIT;
|
||||
}
|
||||
void next() {
|
||||
while (depth > 0) {
|
||||
auto *branch = stack[depth - 1];
|
||||
bool from_left = went_left[depth - 1];
|
||||
--depth;
|
||||
if (!from_left)
|
||||
continue;
|
||||
ParseState *node = branch->right;
|
||||
while (node->is_branch()) {
|
||||
auto *b = (ParseStateBranch *)node;
|
||||
stack[depth] = b;
|
||||
went_left[depth] = true;
|
||||
++depth;
|
||||
node = b->left;
|
||||
}
|
||||
leaf = (ParseStateLeaf *)node;
|
||||
return;
|
||||
}
|
||||
leaf = nullptr;
|
||||
}
|
||||
void prev() {
|
||||
while (depth > 0) {
|
||||
auto *branch = stack[depth - 1];
|
||||
bool from_left = went_left[depth - 1];
|
||||
--depth;
|
||||
if (from_left)
|
||||
continue;
|
||||
ParseState *node = branch->left;
|
||||
while (node->is_branch()) {
|
||||
auto *b = (ParseStateBranch *)node;
|
||||
stack[depth] = b;
|
||||
went_left[depth] = false;
|
||||
++depth;
|
||||
node = b->right;
|
||||
}
|
||||
leaf = (ParseStateLeaf *)node;
|
||||
return;
|
||||
}
|
||||
leaf = nullptr;
|
||||
uint64_t lines() const {
|
||||
return header & LINES_MASK;
|
||||
}
|
||||
};
|
||||
|
||||
struct Symbol {
|
||||
uint64_t definition;
|
||||
std::vector<uint64_t> references;
|
||||
uint32_t definition;
|
||||
uint16_t reference_count;
|
||||
uint8_t type;
|
||||
uint8_t len;
|
||||
// first chars of count len, padded to 4 bytes.
|
||||
// then a set of references. 32bit
|
||||
// references later as modifying at end is faster than shifting the name.
|
||||
};
|
||||
|
||||
struct Space {
|
||||
uint64_t len;
|
||||
};
|
||||
|
||||
struct Scope {
|
||||
uint64_t len;
|
||||
uint32_t type;
|
||||
trie::Trie<Symbol> symbols;
|
||||
std::vector<std::variant<Space, Scope>> children;
|
||||
struct alignas(8) Scope : ScopeNode {
|
||||
uint16_t children_count;
|
||||
uint16_t symbol_count;
|
||||
uint8_t type;
|
||||
uint8_t len;
|
||||
uint32_t : 32; // if more stuff is needed use the padding first.
|
||||
// first chars of count len, padded to 8 bytes.
|
||||
// followed by that many number of pointers.
|
||||
};
|
||||
} // namespace bed::internal::syntax
|
||||
|
||||
Reference in New Issue
Block a user