feat: custom languages syntax (WIP)

This commit is contained in:
MihailRis
2025-04-13 00:19:38 +03:00
parent 4360cd408b
commit 5253be6c56
18 changed files with 278 additions and 88 deletions
+29
View File
@@ -0,0 +1,29 @@
#include "Editor.hpp"
#include "engine/Engine.hpp"
#include "io/engine_paths.hpp"
#include "coders/syntax_parser.hpp"
#include "SyntaxProcessor.hpp"
using namespace devtools;
Editor::Editor(Engine& engine)
: engine(engine), syntaxProcessor(std::make_unique<SyntaxProcessor>()) {
}
Editor::~Editor() = default;
void Editor::loadTools() {
const auto& paths = engine.getResPaths();
auto files = paths.listdir("devtools/syntax");
for (const auto& file : files) {
auto config = io::read_object(file);
auto syntax = std::make_unique<Syntax>();
syntax->deserialize(config);
syntaxProcessor->addSyntax(std::move(syntax));
}
}
SyntaxProcessor& Editor::getSyntaxProcessor() {
return *syntaxProcessor;
}
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include <string>
#include <memory>
class Engine;
namespace devtools {
class SyntaxProcessor;
class Editor {
public:
Editor(Engine& engine);
~Editor();
void loadTools();
SyntaxProcessor& getSyntaxProcessor();
private:
Engine& engine;
std::unique_ptr<SyntaxProcessor> syntaxProcessor;
};
}
@@ -1,4 +1,4 @@
#include "syntax_highlighting.hpp"
#include "SyntaxProcessor.hpp"
#include "coders/commons.hpp"
#include "coders/syntax_parser.hpp"
@@ -55,16 +55,28 @@ static std::unique_ptr<FontStylesScheme> build_styles(
return std::make_unique<FontStylesScheme>(std::move(styles));
}
std::unique_ptr<FontStylesScheme> devtools::syntax_highlight(
const std::string& lang, std::wstring_view source
void SyntaxProcessor::addSyntax(
std::unique_ptr<Syntax> syntax
) {
const auto ptr = syntax.get();
langs.emplace_back(std::move(syntax));
for (auto& ext : ptr->extensions) {
langsExtensions[ext] = ptr;
}
}
std::unique_ptr<FontStylesScheme> SyntaxProcessor::highlight(
const std::string& ext, std::wstring_view source
) const {
const auto& found = langsExtensions.find(ext);
if (found == langsExtensions.end()) {
return nullptr;
}
const auto& syntax = *found->second;
try {
if (lang == "lua") {
auto tokens = tokenize("<string>", source);
return build_styles(tokens);
} else {
return nullptr;
}
auto tokens = tokenize(syntax, "<string>", source);
return build_styles(tokens);
} catch (const parsing_error& err) {
return nullptr;
}
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include <set>
#include <string>
#include <memory>
#include <vector>
#include <unordered_map>
struct FontStylesScheme;
namespace devtools {
struct Syntax;
enum SyntaxStyles {
DEFAULT, KEYWORD, LITERAL, COMMENT, ERROR
};
class SyntaxProcessor {
public:
std::unique_ptr<FontStylesScheme> highlight(
const std::string& ext, std::wstring_view source
) const;
void addSyntax(std::unique_ptr<Syntax> syntax);
private:
std::vector<std::unique_ptr<Syntax>> langs;
std::unordered_map<std::string, const Syntax*> langsExtensions;
};
}
-16
View File
@@ -1,16 +0,0 @@
#pragma once
#include <string>
#include <memory>
struct FontStylesScheme;
namespace devtools {
enum SyntaxStyles {
DEFAULT, KEYWORD, LITERAL, COMMENT, ERROR
};
std::unique_ptr<FontStylesScheme> syntax_highlight(
const std::string& lang, std::wstring_view source
);
}