Add basic c syntax highlighting.

This commit is contained in:
2026-09-14 11:25:29 +01:00
parent 8f67be87ee
commit 6711ecbaee
8 changed files with 448 additions and 1 deletions
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include "../decl.h"
#include "pch.h"
#include "tries.h"
namespace bed::internal::syntax::c {
struct alignas(2) CState {
struct CInternalState {
uint16_t brace_level;
enum : uint8_t {
NONE,
STRING,
CHAR,
MULTI_COMMENT,
INLINE_COMMENT
} state;
uint8_t flags = 0;
};
uint8_t top;
// the stack (RubyInternalState * top)
inline CInternalState *stack() {
return (CInternalState *)(this + 1);
}
};
Language lang_c();
} // namespace bed::internal::syntax::c
+65
View File
@@ -0,0 +1,65 @@
#pragma once
#include "decl.h"
#include "pch.h"
namespace bed::internal::syntax::c {
struct CParser {
void **v_state;
CState *state;
std::string_view line;
bool still_empty = true;
uint32_t i = 0;
CParser(void **v_state, std::string_view line)
: v_state(v_state), state((CState *)*v_state), line(line) {}
uint32_t len() const {
return line.size();
}
CState::CInternalState &current() {
return state->stack()[state->top - 1];
}
char peek(int32_t offset = 0) {
uint32_t pos = i + offset;
return pos < line.size() ? line[pos] : '\0';
}
std::string_view peek_str(uint32_t len) {
return line.substr(i, len);
}
void advance() {
++i;
}
void advance(uint32_t n) {
i += n;
}
void push_state() {
state->top++;
state = (CState *)realloc(
state,
sizeof(CState)
+ sizeof(CState::CInternalState) * state->top
);
current() = {
.brace_level = 1,
.state = CState::CInternalState::NONE,
.flags = 0
};
}
void pop_state() {
state->top--;
}
};
void c_parse(
void **v_state, std::string_view line, bool first_line,
std::vector<io::Token> *tokens, std::vector<ParseEvent> *events
);
} // namespace bed::internal::syntax::c
+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include "internal/trie/trie.h"
#include "pch.h"
namespace bed::internal::syntax::c {
const static std::vector<std::string> types = {
"int",
"char",
"long",
"void",
"auto"
};
const static std::vector<std::string> type_modifier = {
"signed",
"unsigned",
"long"
};
const static std::vector<std::string> math_operators = {
"+",
"-",
"*"
};
struct CTries {
trie::Trie<void> types_trie;
CTries() {
for (auto &keyword : types)
types_trie.insert(keyword);
}
};
} // namespace bed::internal::syntax::c