Compare commits

...

12 Commits

Author SHA1 Message Date
csh e75027b80a 🔧 chore(symbol): trace per-library file indexing
- Log each indexed .tsf file at trace level during LoadSystemLibrary.
- Include top-level symbol summary when a file is skipped for stem mismatch.
2025-12-14 14:06:24 +08:00
csh 9a12b1b194 🐛 fix(logging): support --log-stderr and clarify stdin wait
- Make logs go to stderr by default (keep LSP stdout clean).
- Add --log-stderr/--log-stdout flags and wire config.use_stderr into spdlog.
- Log an explicit message when waiting for LSP input on stdin.
2025-12-14 13:43:56 +08:00
csh 241de77152 🐛 fix(symbol): treat .tsl as script, not library
- LoadSystemLibrary now only scans .tsf files.
- LoadWorkspace still indexes .tsl scripts but skips the file-stem/top-level check for them.
2025-12-14 13:28:14 +08:00
csh 566830bdb8 🐛 fix(symbol): emit module symbols for unit
Fix Builder::CreateSymbol to handle SymbolKind::Module (Unit) so system/workspace loading can match file stems.
2025-12-14 13:13:54 +08:00
csh 2c9c0b2d8f 🐛 fix(server): load interpreter funcext symbols
- Wire `--interpreter=...` into server startup.
- Auto-load `<interpreter>/funcext` as a system library via Symbol::LoadSystemLibrary.
2025-12-14 13:08:50 +08:00
csh 7d1db3f3ac 🔧 chore(conan): rename profiles without version
- Rename `linux-x86_64-clang-20` to `linux-x86_64-clang`.
- Rename `windows-x86_64-clang-20-cross` to `windows-x86_64-clang-cross`.
- Remove unused `windows-x86_64-clang-17-cross` profile.
2025-12-14 12:59:32 +08:00
csh ab1dcc00bc 🐛 fix(uses): make file-level uses globally visible
- Preserve top-level `uses` imports and apply them to subsequent unit/class/function/method scopes.
- Extend semantic ResolveFromUses to consult file-level imports (and fall back from unit imports).
2025-12-14 11:10:50 +08:00
csh 171d5e2064 feat(lsp_completion): support unit-qualified constructors
- Support `new UnitA.ClassName` and `new unit(UnitA).ClassName` completion.
- Support `createobject("UnitA.ClassName"` and `createobject("unit(UnitA).ClassName"` completion.
- Apply `uses` ordering when resolving ambiguous ClassName (last wins).
- Improve `obj.` member completion using qualified type strings.
2025-12-14 10:33:12 +08:00
csh 3106ab9ce4 feat(lsp_completion): complete object creation and members
- Add class-name completion for `new` and `createobject("..."`.
- Generate constructor parameter snippets in completionItem/resolve.
- Support `obj.` instance member completion (methods/properties/fields).
- Infer `new`/`createobject` result types in SemanticModel TypeSystem.
2025-12-14 09:54:18 +08:00
csh 476e83beb8 feat(lsp_server): support rdo_expression and normalize call field
- rename tree-sitter fields for inherited/rdo to 'call'
- regenerate parser sources and sync into lsp-server
- add RdoExpression to AST + symbol/semantic visitors
2025-12-14 06:05:47 +08:00
csh d1c35de70c 🔧 chore(conan): add clang-20 profiles
add linux and windows-cross conan profiles for clang 20 toolchains
2025-12-13 20:37:10 +08:00
csh e5782c76fa ♻️ refactor(lsp_server): move entrypoints out of named modules
- export Run() from launcher module and add non-module main TU
- apply same pattern to module-based test executables
- migrate utils/string implementation fully into string.cppm and drop string.cpp
- point clangd to linux build compilation database
2025-12-13 19:55:45 +08:00
48 changed files with 2236783 additions and 2262248 deletions

View File

@ -1,5 +1,9 @@
CompileFlags:
CompilationDatabase: build/clang-linux/Release
---
If: If:
PathMatch: [.*\.hpp, .*\.cpp] PathMatch: [.*\.hpp, .*\.hxx, .*\.cpp, .*\.cc, .*\.cxx, .*\.cppm, .*\.ixx, .*\.mpp]
CompileFlags: CompileFlags:
Add: Add:

View File

@ -41,9 +41,9 @@ endif()
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1) set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
set(SOURCES set(SOURCES
main.cppm cli/launcher.cppm
main.cc
utils/args_parser.cppm utils/args_parser.cppm
utils/string.cpp
utils/text_coordinates.cppm utils/text_coordinates.cppm
core/dispacther.cppm core/dispacther.cppm
core/server.cppm core/server.cppm
@ -86,7 +86,7 @@ target_sources(
bridge/spdlog.cppm bridge/spdlog.cppm
bridge/taskflow.cppm bridge/taskflow.cppm
bridge/tree_sitter.cppm bridge/tree_sitter.cppm
main.cppm cli/launcher.cppm
language/ast/ast.cppm language/ast/ast.cppm
language/ast/types.cppm language/ast/types.cppm
language/ast/deserializer.cppm language/ast/deserializer.cppm

View File

@ -1,14 +1,14 @@
module; module;
export module lsp.cli.launcher;
export module lsp.main;
import spdlog; import spdlog;
import std; import std;
import lsp.core.server; import lsp.core.server;
import lsp.utils.args_parser; import lsp.utils.args_parser;
int main(int argc, char* argv[]) export int Run(int argc, char* argv[])
{ {
lsp::utils::ArgsParser& args_parser = lsp::utils::ArgsParser::Instance(); lsp::utils::ArgsParser& args_parser = lsp::utils::ArgsParser::Instance();
auto& config = args_parser.Parse(argc, argv); auto& config = args_parser.Parse(argc, argv);
@ -24,7 +24,7 @@ int main(int argc, char* argv[])
try try
{ {
spdlog::info("TSL-LSP server starting..."); spdlog::info("TSL-LSP server starting...");
lsp::core::LspServer server(config.thread_count); lsp::core::LspServer server(config.thread_count, config.interpreter_path);
server.Run(); server.Run();
} }
catch (const std::exception& e) catch (const std::exception& e)

View File

@ -15,6 +15,7 @@ import lsp.core.dispacther;
import lsp.protocol; import lsp.protocol;
import lsp.codec.facade; import lsp.codec.facade;
import lsp.manager.manager_hub; import lsp.manager.manager_hub;
import lsp.manager.bootstrap;
import lsp.scheduler.async_executor; import lsp.scheduler.async_executor;
import lsp.provider.base.interface; import lsp.provider.base.interface;
import lsp.provider.base.registry; import lsp.provider.base.registry;
@ -37,7 +38,8 @@ export namespace lsp::core
class LspServer class LspServer
{ {
public: public:
LspServer(std::size_t concurrency = std::thread::hardware_concurrency()); explicit LspServer(std::size_t concurrency = std::thread::hardware_concurrency(),
std::string interpreter_path = "");
~LspServer(); ~LspServer();
void Run(); void Run();
@ -80,6 +82,7 @@ export namespace lsp::core
RequestDispatcher dispatcher_; RequestDispatcher dispatcher_;
scheduler::AsyncExecutor async_executor_; scheduler::AsyncExecutor async_executor_;
manager::ManagerHub manager_hub_; manager::ManagerHub manager_hub_;
std::string interpreter_path_;
std::atomic<bool> is_initialized_ = false; std::atomic<bool> is_initialized_ = false;
std::atomic<bool> is_shutting_down_ = false; std::atomic<bool> is_shutting_down_ = false;
@ -89,7 +92,8 @@ export namespace lsp::core
namespace lsp::core namespace lsp::core
{ {
LspServer::LspServer(std::size_t concurrency) : async_executor_(concurrency) LspServer::LspServer(std::size_t concurrency, std::string interpreter_path) : async_executor_(concurrency),
interpreter_path_(std::move(interpreter_path))
{ {
spdlog::info("Initializing LSP server with {} worker threads", concurrency); spdlog::info("Initializing LSP server with {} worker threads", concurrency);
@ -108,6 +112,7 @@ namespace lsp::core
void LspServer::Run() void LspServer::Run()
{ {
spdlog::info("LSP server starting main loop..."); spdlog::info("LSP server starting main loop...");
spdlog::info("Waiting for LSP messages on stdin...");
// 设置二进制模式 // 设置二进制模式
#ifdef _WIN32 #ifdef _WIN32
@ -378,6 +383,23 @@ namespace lsp::core
void LspServer::InitializeManagerHub() void LspServer::InitializeManagerHub()
{ {
manager_hub_.Initialize(); manager_hub_.Initialize();
if (!interpreter_path_.empty())
{
std::filesystem::path base = interpreter_path_;
std::filesystem::path funcext_path = base / "funcext";
if (std::filesystem::exists(funcext_path))
{
manager::bootstrap::InitializeManagerHub(
manager_hub_,
async_executor_,
{ funcext_path.string() });
}
else
{
spdlog::warn("Interpreter funcext path does not exist: {}", funcext_path.string());
}
}
} }
void LspServer::RegisterProviders() void LspServer::RegisterProviders()

View File

@ -1,6 +1,5 @@
module; module;
module lsp.language.ast:detail; module lsp.language.ast:detail;
import tree_sitter; import tree_sitter;
@ -113,6 +112,7 @@ namespace lsp::language::ast::detail
ExpressionPtr ParseEchoExpression(TSNode node, ParseContext& ctx); ExpressionPtr ParseEchoExpression(TSNode node, ParseContext& ctx);
ExpressionPtr ParseRaiseExpression(TSNode node, ParseContext& ctx); ExpressionPtr ParseRaiseExpression(TSNode node, ParseContext& ctx);
ExpressionPtr ParseInheritedExpression(TSNode node, ParseContext& ctx); ExpressionPtr ParseInheritedExpression(TSNode node, ParseContext& ctx);
ExpressionPtr ParseRdoExpression(TSNode node, ParseContext& ctx);
ExpressionPtr ParseArrayExpression(TSNode node, ParseContext& ctx); ExpressionPtr ParseArrayExpression(TSNode node, ParseContext& ctx);
ExpressionPtr ParseParenthesizedExpression(TSNode node, ParseContext& ctx); ExpressionPtr ParseParenthesizedExpression(TSNode node, ParseContext& ctx);
@ -589,6 +589,8 @@ namespace lsp::language::ast::detail
return ParseRaiseExpression(node, ctx); return ParseRaiseExpression(node, ctx);
if (node_type == "inherited_expression") if (node_type == "inherited_expression")
return ParseInheritedExpression(node, ctx); return ParseInheritedExpression(node, ctx);
if (node_type == "rdo_expression")
return ParseRdoExpression(node, ctx);
if (node_type == "array_expression") if (node_type == "array_expression")
return ParseArrayExpression(node, ctx); return ParseArrayExpression(node, ctx);
@ -751,7 +753,26 @@ namespace lsp::language::ast::detail
auto expr = MakeNode<InheritedExpression>(); auto expr = MakeNode<InheritedExpression>();
expr->span = ts_utils::NodeLocation(node); expr->span = ts_utils::NodeLocation(node);
TSNode call_node = ts_node_child_by_field_name(node, "class", 5); TSNode call_node = ts_node_child_by_field_name(node, "call", 4);
if (!ts_node_is_null(call_node))
{
auto call_expr = ParseExpression(call_node, ctx);
if (call_expr && call_expr->kind == NodeKind::kCallExpression)
{
expr->call = std::unique_ptr<CallExpression>(
static_cast<CallExpression*>(call_expr.release()));
}
}
return expr;
}
ExpressionPtr ParseRdoExpression(TSNode node, ParseContext& ctx)
{
auto expr = MakeNode<RdoExpression>();
expr->span = ts_utils::NodeLocation(node);
TSNode call_node = ts_node_child_by_field_name(node, "call", 4);
if (!ts_node_is_null(call_node)) if (!ts_node_is_null(call_node))
{ {
auto call_expr = ParseExpression(call_node, ctx); auto call_expr = ParseExpression(call_node, ctx);

View File

@ -1,6 +1,5 @@
module; module;
export module lsp.language.ast:types; export module lsp.language.ast:types;
import tree_sitter; import tree_sitter;
@ -213,6 +212,7 @@ export namespace lsp::language::ast
kEchoExpression, kEchoExpression,
kRaiseExpression, kRaiseExpression,
kInheritedExpression, kInheritedExpression,
kRdoExpression,
kTSSQLExpression, kTSSQLExpression,
kColumnReference, kColumnReference,
@ -327,6 +327,7 @@ export namespace lsp::language::ast
class EchoExpression; class EchoExpression;
class RaiseExpression; class RaiseExpression;
class InheritedExpression; class InheritedExpression;
class RdoExpression;
class TSSQLExpression; class TSSQLExpression;
class ColumnReference; class ColumnReference;
@ -473,6 +474,7 @@ export namespace lsp::language::ast
virtual void VisitEchoExpression(EchoExpression& node) = 0; virtual void VisitEchoExpression(EchoExpression& node) = 0;
virtual void VisitRaiseExpression(RaiseExpression& node) = 0; virtual void VisitRaiseExpression(RaiseExpression& node) = 0;
virtual void VisitInheritedExpression(InheritedExpression& node) = 0; virtual void VisitInheritedExpression(InheritedExpression& node) = 0;
virtual void VisitRdoExpression(RdoExpression& node) = 0;
virtual void VisitTSSQLExpression(TSSQLExpression& node) = 0; virtual void VisitTSSQLExpression(TSSQLExpression& node) = 0;
virtual void VisitColumnReference(ColumnReference& node) = 0; virtual void VisitColumnReference(ColumnReference& node) = 0;
@ -895,6 +897,16 @@ export namespace lsp::language::ast
std::optional<std::unique_ptr<CallExpression>> call; std::optional<std::unique_ptr<CallExpression>> call;
}; };
class RdoExpression : public Expression
{
public:
RdoExpression() { kind = NodeKind::kRdoExpression; }
void Accept(ASTVisitor& visitor) override { visitor.VisitRdoExpression(*this); }
public:
std::optional<std::unique_ptr<CallExpression>> call;
};
class TSSQLExpression : public Expression class TSSQLExpression : public Expression
{ {
public: public:

View File

@ -11,6 +11,77 @@ import lsp.utils.string;
namespace lsp::language::semantic namespace lsp::language::semantic
{ {
namespace
{
std::optional<std::string> UnquoteStringLiteral(std::string value)
{
if (value.size() < 2)
{
return std::nullopt;
}
char first = value.front();
char last = value.back();
if ((first == '"' && last == '"') || (first == '\'' && last == '\''))
{
value.erase(value.begin());
value.pop_back();
return value;
}
return std::nullopt;
}
std::optional<std::pair<std::string, std::string>> ParseQualifiedTypeName(const std::string& text)
{
std::string trimmed = utils::Trim(text);
if (trimmed.empty())
{
return std::nullopt;
}
std::string lower = utils::ToLower(trimmed);
if (lower.starts_with("unit("))
{
std::string after_unit = trimmed.substr(5);
std::size_t close_paren = after_unit.find(')');
if (close_paren == std::string::npos)
{
return std::nullopt;
}
std::string unit_name = utils::Trim(after_unit.substr(0, close_paren));
std::size_t dot_pos = after_unit.find('.', close_paren);
if (dot_pos == std::string::npos || dot_pos + 1 >= after_unit.size())
{
return std::nullopt;
}
std::string class_name = utils::Trim(after_unit.substr(dot_pos + 1));
if (unit_name.empty() || class_name.empty())
{
return std::nullopt;
}
return std::make_pair(std::move(unit_name), std::move(class_name));
}
std::size_t dot_pos = trimmed.find_last_of('.');
if (dot_pos == std::string::npos || dot_pos == 0 || dot_pos + 1 >= trimmed.size())
{
return std::nullopt;
}
std::string unit_name = utils::Trim(trimmed.substr(0, dot_pos));
std::string class_name = utils::Trim(trimmed.substr(dot_pos + 1));
if (unit_name.empty() || class_name.empty())
{
return std::nullopt;
}
return std::make_pair(std::move(unit_name), std::move(class_name));
}
}
Analyzer::Analyzer(symbol::SymbolTable& symbol_table, Analyzer::Analyzer(symbol::SymbolTable& symbol_table,
SemanticModel& semantic_model) : symbol_table_(symbol_table), SemanticModel& semantic_model) : symbol_table_(symbol_table),
@ -447,21 +518,28 @@ namespace lsp::language::semantic
void Analyzer::VisitUsesStatement(ast::UsesStatement& node) void Analyzer::VisitUsesStatement(ast::UsesStatement& node)
{ {
auto add_to = [&](std::vector<symbol::UnitImport>& target) {
target.reserve(target.size() + node.units.size());
for (const auto& unit : node.units)
{
symbol::UnitImport import;
import.unit_name = unit.name;
import.location = unit.location;
target.push_back(std::move(import));
}
};
if (!current_unit_context_) if (!current_unit_context_)
{ {
// Top-level `uses` is treated as file-global.
add_to(file_imports_);
return; return;
} }
auto& context = *current_unit_context_; auto& context = *current_unit_context_;
auto* target = current_unit_section_ == symbol::UnitVisibility::kInterface ? &context.interface_imports : &context.implementation_imports; auto* target = current_unit_section_ == symbol::UnitVisibility::kInterface ? &context.interface_imports : &context.implementation_imports;
for (const auto& unit : node.units) add_to(*target);
{
symbol::UnitImport import;
import.unit_name = unit.name;
import.location = unit.location;
target->push_back(std::move(import));
}
} }
void Analyzer::VisitIdentifier(ast::Identifier& node) void Analyzer::VisitIdentifier(ast::Identifier& node)
@ -737,7 +815,74 @@ namespace lsp::language::semantic
} }
else if ([[maybe_unused]] auto* attr = dynamic_cast<ast::AttributeExpression*>(node.target.get())) else if ([[maybe_unused]] auto* attr = dynamic_cast<ast::AttributeExpression*>(node.target.get()))
{ {
// 处理限定名的类型(如 SomeUnit.SomeClass const auto* class_ident = dynamic_cast<ast::Identifier*>(attr->attribute.get());
if (class_ident && !class_ident->name.empty())
{
std::optional<std::string> unit_name;
if (const auto* unit_ident = dynamic_cast<ast::Identifier*>(attr->object.get()))
{
unit_name = unit_ident->name;
}
else if (const auto* unit_call = dynamic_cast<ast::CallExpression*>(attr->object.get()))
{
if (const auto* callee_ident = dynamic_cast<ast::Identifier*>(unit_call->callee.get()))
{
if (utils::IEquals(callee_ident->name, "unit") && !unit_call->arguments.empty() && unit_call->arguments[0].value)
{
if (const auto* arg_ident = dynamic_cast<ast::Identifier*>(unit_call->arguments[0].value.get()))
{
unit_name = arg_ident->name;
}
}
}
}
if (unit_name && !unit_name->empty())
{
auto unit_id = ResolveIdentifier(*unit_name, class_ident->location);
if (unit_id)
{
const auto* unit_symbol = symbol_table_.definition(*unit_id);
if (unit_symbol && unit_symbol->Is<symbol::Unit>())
{
auto scope_id = FindScopeOwnedBy(*unit_id);
if (scope_id)
{
auto member_id = symbol_table_.scopes().FindSymbolInScope(*scope_id, class_ident->name);
if (member_id)
{
const auto* member_symbol = symbol_table_.definition(*member_id);
if (member_symbol && member_symbol->kind() == protocol::SymbolKind::Class)
{
TrackReference(*member_id, class_ident->location, false);
auto class_scope_id = FindScopeOwnedBy(*member_id);
if (class_scope_id)
{
auto ctor_id = symbol_table_.scopes().FindSymbolInScope(*class_scope_id, class_ident->name);
if (ctor_id)
{
TrackCall(*ctor_id, node.span);
}
else
{
TrackCall(*member_id, node.span);
}
}
else
{
TrackCall(*member_id, node.span);
}
return;
}
}
}
}
}
}
}
// fallback
VisitExpression(*node.target); VisitExpression(*node.target);
} }
else else
@ -780,6 +925,15 @@ namespace lsp::language::semantic
// TODO: 处理 inherited 调用 // TODO: 处理 inherited 调用
} }
void Analyzer::VisitRdoExpression(ast::RdoExpression& node)
{
if (node.call && *node.call)
{
(*node.call)->Accept(*this);
}
// TODO: 处理 rdo/rdo2 语义
}
void Analyzer::VisitParenthesizedExpression(ast::ParenthesizedExpression& node) void Analyzer::VisitParenthesizedExpression(ast::ParenthesizedExpression& node)
{ {
for (const auto& element : node.elements) for (const auto& element : node.elements)
@ -1020,64 +1174,78 @@ namespace lsp::language::semantic
std::optional<symbol::SymbolId> Analyzer::ResolveFromUses(const std::string& name) std::optional<symbol::SymbolId> Analyzer::ResolveFromUses(const std::string& name)
{ {
if (!current_unit_context_) auto resolve_from_imports = [&](const std::vector<symbol::UnitImport>& imports) -> std::optional<symbol::SymbolId> {
{ // 从后往前查找(最后导入的优先级最高)
return std::nullopt; for (auto it = imports.rbegin(); it != imports.rend(); ++it)
}
// 获取当前上下文的导入列表
const auto& imports = current_unit_section_ == symbol::UnitVisibility::kInterface ? current_unit_context_->interface_imports : current_unit_context_->implementation_imports;
// 从后往前查找(最后导入的优先级最高)
for (auto it = imports.rbegin(); it != imports.rend(); ++it)
{
const auto& unit_import = *it;
// 1. 查找导入的 unit 符号
auto unit_matches = symbol_table_.FindSymbolsByName(unit_import.unit_name);
if (unit_matches.empty())
{ {
// 如果本地找不到,尝试通过 external_symbol_provider_ 查找 const auto& unit_import = *it;
if (external_symbol_provider_)
// 1. 查找导入的 unit 符号
auto unit_matches = symbol_table_.FindSymbolsByName(unit_import.unit_name);
if (unit_matches.empty())
{ {
auto external_unit = external_symbol_provider_(unit_import.unit_name); // 如果本地找不到,尝试通过 external_symbol_provider_ 查找
if (external_unit) if (external_symbol_provider_)
{ {
auto unit_id = symbol_table_.CreateSymbol(std::move(*external_unit)); auto external_unit = external_symbol_provider_(unit_import.unit_name);
unit_matches.push_back(unit_id); if (external_unit)
{
auto unit_id = symbol_table_.CreateSymbol(std::move(*external_unit));
unit_matches.push_back(unit_id);
}
}
}
// 2. 在每个找到的 unit 中查找目标符号
for (auto unit_id : unit_matches)
{
const auto* unit_symbol = symbol_table_.definition(unit_id);
if (!unit_symbol || !unit_symbol->Is<symbol::Unit>())
{
continue;
}
// 3. 查找 unit 的作用域
auto unit_scope_id = FindScopeOwnedBy(unit_id);
if (!unit_scope_id)
{
continue;
}
// 4. 在 unit 的 Interface 作用域中查找符号
auto symbol_id = symbol_table_.scopes().FindSymbolInScope(*unit_scope_id, name);
if (symbol_id)
{
// 验证符号是否在 Interface 部分(通过 visibility 检查)
const auto* symbol = symbol_table_.definition(*symbol_id);
if (symbol)
{
// 如果符号有 visibility 信息,确保它是 Interface 可见的
// TODO: 需要符号表支持 visibility 标记
return symbol_id;
}
} }
} }
} }
// 2. 在每个找到的 unit 中查找目标符号 return std::nullopt;
for (auto unit_id : unit_matches) };
if (current_unit_context_)
{
// 获取当前上下文的导入列表
const auto& imports = current_unit_section_ == symbol::UnitVisibility::kInterface ? current_unit_context_->interface_imports : current_unit_context_->implementation_imports;
if (auto result = resolve_from_imports(imports))
{ {
const auto* unit_symbol = symbol_table_.definition(unit_id); return result;
if (!unit_symbol || !unit_symbol->Is<symbol::Unit>()) }
{ }
continue;
}
// 3. 查找 unit 的作用域 if (!file_imports_.empty())
auto unit_scope_id = FindScopeOwnedBy(unit_id); {
if (!unit_scope_id) if (auto result = resolve_from_imports(file_imports_))
{ {
continue; return result;
}
// 4. 在 unit 的 Interface 作用域中查找符号
auto symbol_id = symbol_table_.scopes().FindSymbolInScope(*unit_scope_id, name);
if (symbol_id)
{
// 验证符号是否在 Interface 部分(通过 visibility 检查)
const auto* symbol = symbol_table_.definition(*symbol_id);
if (symbol)
{
// 如果符号有 visibility 信息,确保它是 Interface 可见的
// TODO: 需要符号表支持 visibility 标记
return symbol_id;
}
}
} }
} }
@ -1141,6 +1309,56 @@ namespace lsp::language::semantic
return type_system.CreateClassType(*class_id); return type_system.CreateClassType(*class_id);
} }
} }
else if (auto* attr = dynamic_cast<ast::AttributeExpression*>(new_expr->target.get()))
{
const auto* class_ident = dynamic_cast<ast::Identifier*>(attr->attribute.get());
if (class_ident && !class_ident->name.empty())
{
std::optional<std::string> unit_name;
if (const auto* unit_ident = dynamic_cast<ast::Identifier*>(attr->object.get()))
{
unit_name = unit_ident->name;
}
else if (const auto* unit_call = dynamic_cast<ast::CallExpression*>(attr->object.get()))
{
if (const auto* callee_ident = dynamic_cast<ast::Identifier*>(unit_call->callee.get()))
{
if (utils::IEquals(callee_ident->name, "unit") && !unit_call->arguments.empty() && unit_call->arguments[0].value)
{
if (const auto* arg_ident = dynamic_cast<ast::Identifier*>(unit_call->arguments[0].value.get()))
{
unit_name = arg_ident->name;
}
}
}
}
if (unit_name && !unit_name->empty())
{
auto unit_id = ResolveIdentifier(*unit_name, class_ident->location);
if (unit_id)
{
const auto* unit_symbol = symbol_table_.definition(*unit_id);
if (unit_symbol && unit_symbol->Is<symbol::Unit>())
{
auto scope_id = FindScopeOwnedBy(*unit_id);
if (scope_id)
{
auto member_id = symbol_table_.scopes().FindSymbolInScope(*scope_id, class_ident->name);
if (member_id)
{
const auto* member_symbol = symbol_table_.definition(*member_id);
if (member_symbol && member_symbol->kind() == protocol::SymbolKind::Class)
{
return type_system.CreateClassType(*member_id);
}
}
}
}
}
}
}
}
} }
return type_system.GetUnknownType(); return type_system.GetUnknownType();
} }
@ -1149,6 +1367,58 @@ namespace lsp::language::semantic
{ {
if (auto* ident = dynamic_cast<ast::Identifier*>(call->callee.get())) if (auto* ident = dynamic_cast<ast::Identifier*>(call->callee.get()))
{ {
if (utils::IEquals(ident->name, "createobject"))
{
if (!call->arguments.empty() && call->arguments.front().value)
{
if (auto* lit = dynamic_cast<ast::Literal*>(call->arguments.front().value.get()))
{
if (lit->literal_kind == ast::LiteralKind::kString)
{
std::string type_name = lit->value;
if (auto unquoted = UnquoteStringLiteral(type_name))
{
type_name = *unquoted;
}
if (auto qualified = ParseQualifiedTypeName(type_name))
{
auto unit_id = ResolveIdentifier(qualified->first, lit->location);
if (unit_id)
{
const auto* unit_symbol = symbol_table_.definition(*unit_id);
if (unit_symbol && unit_symbol->Is<symbol::Unit>())
{
auto scope_id = FindScopeOwnedBy(*unit_id);
if (scope_id)
{
auto member_id = symbol_table_.scopes().FindSymbolInScope(*scope_id, qualified->second);
if (member_id)
{
const auto* member_symbol = symbol_table_.definition(*member_id);
if (member_symbol && member_symbol->kind() == protocol::SymbolKind::Class)
{
return type_system.CreateClassType(*member_id);
}
}
}
}
}
}
else
{
auto class_id = ResolveClassSymbol(type_name, lit->location);
if (class_id)
{
return type_system.CreateClassType(*class_id);
}
}
}
}
}
return type_system.GetUnknownType();
}
auto callee_id = ResolveIdentifier(ident->name, ident->location); auto callee_id = ResolveIdentifier(ident->name, ident->location);
if (callee_id) if (callee_id)
{ {

View File

@ -1,6 +1,5 @@
module; module;
export module lsp.language.semantic:interface; export module lsp.language.semantic:interface;
import tree_sitter; import tree_sitter;
@ -628,6 +627,7 @@ export namespace lsp::language::semantic
void VisitEchoExpression(ast::EchoExpression& node) override; void VisitEchoExpression(ast::EchoExpression& node) override;
void VisitRaiseExpression(ast::RaiseExpression& node) override; void VisitRaiseExpression(ast::RaiseExpression& node) override;
void VisitInheritedExpression(ast::InheritedExpression& node) override; void VisitInheritedExpression(ast::InheritedExpression& node) override;
void VisitRdoExpression(ast::RdoExpression& node) override;
void VisitParenthesizedExpression(ast::ParenthesizedExpression& node) override; void VisitParenthesizedExpression(ast::ParenthesizedExpression& node) override;
void VisitExpressionStatement(ast::ExpressionStatement& node) override; void VisitExpressionStatement(ast::ExpressionStatement& node) override;
@ -681,6 +681,7 @@ export namespace lsp::language::semantic
std::optional<symbol::SymbolId> current_class_id_; std::optional<symbol::SymbolId> current_class_id_;
std::optional<UnitContext> current_unit_context_; std::optional<UnitContext> current_unit_context_;
std::optional<symbol::UnitVisibility> current_unit_section_; std::optional<symbol::UnitVisibility> current_unit_section_;
std::vector<symbol::UnitImport> file_imports_;
ExternalSymbolProvider external_symbol_provider_; ExternalSymbolProvider external_symbol_provider_;
std::unordered_map<std::string, symbol::SymbolId> imported_symbols_; std::unordered_map<std::string, symbol::SymbolId> imported_symbols_;
}; };

View File

@ -7,9 +7,177 @@ module lsp.language.symbol:internal.builder;
import :types; import :types;
import lsp.language.ast; import lsp.language.ast;
import lsp.protocol.types; import lsp.protocol.types;
import lsp.utils.string;
namespace lsp::language::symbol namespace lsp::language::symbol
{ {
namespace
{
std::optional<std::string> UnquoteStringLiteral(std::string value)
{
if (value.size() < 2)
{
return std::nullopt;
}
char first = value.front();
char last = value.back();
if ((first == '"' && last == '"') || (first == '\'' && last == '\''))
{
value.erase(value.begin());
value.pop_back();
return value;
}
return std::nullopt;
}
std::optional<std::string> ExtractClassNameFromExpression(const ast::Expression* expr)
{
if (!expr)
{
return std::nullopt;
}
if (const auto* ident = dynamic_cast<const ast::Identifier*>(expr))
{
if (!ident->name.empty())
{
return ident->name;
}
return std::nullopt;
}
if (const auto* attr = dynamic_cast<const ast::AttributeExpression*>(expr))
{
// Prefer preserving unit-qualified names like `UnitA.ClassName` / `unit(UnitA).ClassName`,
// fallback to last attribute segment if we can't extract a qualifier.
auto attr_name = ExtractClassNameFromExpression(attr->attribute.get());
if (!attr_name || attr_name->empty())
{
return std::nullopt;
}
std::optional<std::string> qualifier;
if (const auto* obj_ident = dynamic_cast<const ast::Identifier*>(attr->object.get()))
{
if (!obj_ident->name.empty())
{
qualifier = obj_ident->name;
}
}
else if (const auto* obj_call = dynamic_cast<const ast::CallExpression*>(attr->object.get()))
{
if (const auto* callee_ident = dynamic_cast<const ast::Identifier*>(obj_call->callee.get()))
{
if (utils::IEquals(callee_ident->name, "unit") && !obj_call->arguments.empty() && obj_call->arguments[0].value)
{
if (const auto* arg_ident = dynamic_cast<const ast::Identifier*>(obj_call->arguments[0].value.get()))
{
if (!arg_ident->name.empty())
{
qualifier = "unit(" + arg_ident->name + ")";
}
}
}
}
}
if (qualifier && !qualifier->empty())
{
return *qualifier + "." + *attr_name;
}
return attr_name;
}
if (const auto* call = dynamic_cast<const ast::CallExpression*>(expr))
{
return ExtractClassNameFromExpression(call->callee.get());
}
return std::nullopt;
}
std::optional<std::string> InferTypeFromExpression(const ast::Expression* expr)
{
if (!expr)
{
return std::nullopt;
}
if (const auto* new_expr = dynamic_cast<const ast::NewExpression*>(expr))
{
return ExtractClassNameFromExpression(new_expr->target.get());
}
if (const auto* call = dynamic_cast<const ast::CallExpression*>(expr))
{
const auto* callee_ident = dynamic_cast<const ast::Identifier*>(call->callee.get());
if (!callee_ident)
{
return std::nullopt;
}
if (!utils::IEquals(utils::ToLower(callee_ident->name), "createobject"))
{
return std::nullopt;
}
if (call->arguments.empty() || !call->arguments[0].value)
{
return std::nullopt;
}
const auto* literal = dynamic_cast<const ast::Literal*>(call->arguments[0].value.get());
if (!literal || literal->literal_kind != ast::LiteralKind::kString)
{
return std::nullopt;
}
if (auto unquoted = UnquoteStringLiteral(literal->value))
{
if (!unquoted->empty())
{
return *unquoted;
}
}
return std::nullopt;
}
return std::nullopt;
}
void MaybeUpdateSymbolType(SymbolTable& table, ScopeId scope_id, const std::string& name, const std::string& type_name)
{
if (name.empty() || type_name.empty())
{
return;
}
auto id_opt = table.scopes().FindSymbolInScopeChain(scope_id, name);
if (!id_opt)
{
return;
}
Symbol* symbol = const_cast<Symbol*>(table.definition(*id_opt));
if (!symbol)
{
return;
}
if (auto* var = symbol->As<Variable>())
{
var->type = type_name;
}
else if (auto* field = symbol->As<Field>())
{
field->type = type_name;
}
}
}
Builder::Builder(SymbolTable& table) : table_(table), Builder::Builder(SymbolTable& table) : table_(table),
current_scope_id_(kInvalidScopeId), current_scope_id_(kInvalidScopeId),
in_interface_section_(false), in_interface_section_(false),
@ -64,6 +232,14 @@ namespace lsp::language::symbol
Symbol symbol = [&]() -> Symbol { Symbol symbol = [&]() -> Symbol {
switch (kind) switch (kind)
{ {
case protocol::SymbolKind::Module:
{
Unit unit;
unit.name = name;
unit.selection_range = location;
unit.range = location;
return Symbol(std::move(unit));
}
case protocol::SymbolKind::Class: case protocol::SymbolKind::Class:
{ {
Class cls; Class cls;
@ -256,6 +432,30 @@ namespace lsp::language::symbol
Builder::ScopeGuard Builder::EnterScopeWithSymbol(ScopeKind kind, SymbolId symbol_id, const ast::Location& range) Builder::ScopeGuard Builder::EnterScopeWithSymbol(ScopeKind kind, SymbolId symbol_id, const ast::Location& range)
{ {
if (!file_imports_.empty() && file_imports_applied_.insert(symbol_id).second)
{
Symbol* owner_symbol = const_cast<Symbol*>(table_.definition(symbol_id));
if (owner_symbol)
{
if (auto* fn = owner_symbol->As<Function>())
{
fn->imports.insert(fn->imports.begin(), file_imports_.begin(), file_imports_.end());
}
else if (auto* method = owner_symbol->As<Method>())
{
method->imports.insert(method->imports.begin(), file_imports_.begin(), file_imports_.end());
}
else if (auto* cls = owner_symbol->As<Class>())
{
cls->imports.insert(cls->imports.begin(), file_imports_.begin(), file_imports_.end());
}
else if (auto* unit = owner_symbol->As<Unit>())
{
unit->interface_imports.insert(unit->interface_imports.begin(), file_imports_.begin(), file_imports_.end());
unit->implementation_imports.insert(unit->implementation_imports.begin(), file_imports_.begin(), file_imports_.end());
}
}
}
current_scope_id_ = table_.CreateScope(kind, range, current_scope_id_, symbol_id); current_scope_id_ = table_.CreateScope(kind, range, current_scope_id_, symbol_id);
return ScopeGuard(*this, current_scope_id_); return ScopeGuard(*this, current_scope_id_);
} }
@ -544,7 +744,13 @@ namespace lsp::language::symbol
void Builder::VisitVarDeclaration(ast::VarDeclaration& node) void Builder::VisitVarDeclaration(ast::VarDeclaration& node)
{ {
CreateSymbol(node.name, protocol::SymbolKind::Variable, node.location, ExtractTypeName(node.type)); auto type_hint = ExtractTypeName(node.type);
if (!type_hint && node.initializer && *node.initializer)
{
type_hint = InferTypeFromExpression(node.initializer->get());
}
CreateSymbol(node.name, protocol::SymbolKind::Variable, node.location, type_hint);
if (node.initializer && *node.initializer) if (node.initializer && *node.initializer)
{ {
@ -624,6 +830,12 @@ namespace lsp::language::symbol
const auto* scope_info = table_.scopes().scope(current_scope_id_); const auto* scope_info = table_.scopes().scope(current_scope_id_);
if (!scope_info || !scope_info->owner) if (!scope_info || !scope_info->owner)
{ {
// Top-level `uses` (no owner scope) is treated as file-global.
file_imports_.reserve(file_imports_.size() + node.units.size());
for (const auto& unit : node.units)
{
file_imports_.push_back({ unit.name, unit.location });
}
return; return;
} }
@ -702,11 +914,40 @@ namespace lsp::language::symbol
void Builder::VisitAssignmentExpression(ast::AssignmentExpression& node) void Builder::VisitAssignmentExpression(ast::AssignmentExpression& node)
{ {
std::optional<std::string> inferred_type;
if (node.right)
{
inferred_type = InferTypeFromExpression(node.right.get());
}
if (const auto* ident = std::get_if<std::unique_ptr<ast::Identifier>>(&node.left))
{
if (*ident)
{
auto existing = table_.scopes().FindSymbolInScopeChain(current_scope_id_, (*ident)->name);
if (!existing)
{
CreateSymbol((*ident)->name, protocol::SymbolKind::Variable, (*ident)->location, inferred_type);
}
}
}
ProcessLValue(node.left, true); ProcessLValue(node.left, true);
if (node.right) if (node.right)
{ {
VisitExpression(*node.right); VisitExpression(*node.right);
if (inferred_type)
{
if (const auto* ident = std::get_if<std::unique_ptr<ast::Identifier>>(&node.left))
{
if (*ident)
{
MaybeUpdateSymbolType(table_, current_scope_id_, (*ident)->name, *inferred_type);
}
}
}
} }
} }
@ -1158,6 +1399,14 @@ namespace lsp::language::symbol
} }
} }
void Builder::VisitRdoExpression(ast::RdoExpression& node)
{
if (node.call && *node.call)
{
(*node.call)->Accept(*this);
}
}
void Builder::VisitParenthesizedExpression(ast::ParenthesizedExpression& node) void Builder::VisitParenthesizedExpression(ast::ParenthesizedExpression& node)
{ {
for (auto& elem : node.elements) for (auto& elem : node.elements)

View File

@ -508,6 +508,7 @@ export namespace lsp::language::symbol
void VisitEchoExpression(ast::EchoExpression& node) override; void VisitEchoExpression(ast::EchoExpression& node) override;
void VisitRaiseExpression(ast::RaiseExpression& node) override; void VisitRaiseExpression(ast::RaiseExpression& node) override;
void VisitInheritedExpression(ast::InheritedExpression& node) override; void VisitInheritedExpression(ast::InheritedExpression& node) override;
void VisitRdoExpression(ast::RdoExpression& node) override;
void VisitParenthesizedExpression(ast::ParenthesizedExpression& node) override; void VisitParenthesizedExpression(ast::ParenthesizedExpression& node) override;
void VisitExpressionStatement(ast::ExpressionStatement& node) override; void VisitExpressionStatement(ast::ExpressionStatement& node) override;
@ -607,6 +608,9 @@ export namespace lsp::language::symbol
std::optional<SymbolId> current_parent_symbol_id_; std::optional<SymbolId> current_parent_symbol_id_;
std::optional<SymbolId> current_function_id_; std::optional<SymbolId> current_function_id_;
std::vector<UnitImport> file_imports_;
std::unordered_set<SymbolId> file_imports_applied_;
bool in_interface_section_; bool in_interface_section_;
ast::AccessModifier current_access_modifier_ = ast::AccessModifier::kPublic; ast::AccessModifier current_access_modifier_ = ast::AccessModifier::kPublic;
}; };

6
lsp-server/src/main.cc Normal file
View File

@ -0,0 +1,6 @@
import lsp.cli.launcher;
int main(int argc, char* argv[])
{
return Run(argc, argv);
}

View File

@ -1,6 +1,5 @@
module; module;
export module lsp.manager.symbol; export module lsp.manager.symbol;
import tree_sitter; import tree_sitter;
import spdlog; import spdlog;
@ -136,23 +135,42 @@ namespace lsp::manager
return decoded; return decoded;
} }
bool IsTsfFile(const std::filesystem::path& path) enum class TslFileKind
{
kLibraryTsf,
kScriptTsl,
kOther,
};
TslFileKind GetTslFileKind(const std::filesystem::path& path)
{ {
if (!path.has_extension()) if (!path.has_extension())
return false; {
return TslFileKind::kOther;
}
std::string ext = path.extension().string(); std::string ext = path.extension().string();
std::transform( std::transform(
ext.begin(), ext.begin(),
ext.end(), ext.end(),
ext.begin(), ext.begin(),
[](unsigned char ch) { return static_cast<char>(std::tolower(ch)); }); [](unsigned char ch) { return static_cast<char>(std::tolower(ch)); });
return ext == ".tsf" || ext == ".tsl";
if (ext == ".tsf")
{
return TslFileKind::kLibraryTsf;
}
if (ext == ".tsl")
{
return TslFileKind::kScriptTsl;
}
return TslFileKind::kOther;
} }
std::unique_ptr<language::symbol::SymbolTable> BuildSymbolTableFromFile( std::unique_ptr<language::symbol::SymbolTable> BuildSymbolTableFromFile(
const std::filesystem::path& file_path) const std::filesystem::path& file_path)
{ {
if (!IsTsfFile(file_path)) if (GetTslFileKind(file_path) == TslFileKind::kOther)
return nullptr; return nullptr;
std::ifstream file(file_path, std::ios::binary); std::ifstream file(file_path, std::ios::binary);
@ -230,6 +248,44 @@ namespace lsp::manager
} }
return false; return false;
} }
std::string DescribeTopLevelSymbols(const language::symbol::SymbolTable& table)
{
std::vector<std::string> parts;
for (const auto& wrapper : table.all_definitions())
{
const auto& symbol = wrapper.get();
switch (symbol.kind())
{
case protocol::SymbolKind::Function:
parts.push_back("function:" + symbol.name());
break;
case protocol::SymbolKind::Class:
parts.push_back("class:" + symbol.name());
break;
case protocol::SymbolKind::Module:
parts.push_back("unit:" + symbol.name());
break;
default:
break;
}
}
if (parts.empty())
{
return "<none>";
}
std::string result;
for (std::size_t i = 0; i < parts.size(); ++i)
{
if (i > 0)
{
result += ", ";
}
result += parts[i];
}
return result;
}
} }
Symbol::Symbol(EventBus& event_bus) : event_bus_(event_bus) Symbol::Symbol(EventBus& event_bus) : event_bus_(event_bus)
@ -269,9 +325,18 @@ namespace lsp::manager
if (!entry.is_regular_file()) if (!entry.is_regular_file())
continue; continue;
// System library only accepts `.tsf` as a library unit. `.tsl` is a script and should be ignored here.
if (GetTslFileKind(entry.path()) != TslFileKind::kLibraryTsf)
{
continue;
}
spdlog::trace("Indexing library file: {}", entry.path().string());
auto table = BuildSymbolTableFromFile(entry.path()); auto table = BuildSymbolTableFromFile(entry.path());
if (!table) if (!table)
{ {
spdlog::trace("Failed to build symbol table for: {}", entry.path().string());
++failed; ++failed;
continue; continue;
} }
@ -279,7 +344,10 @@ namespace lsp::manager
auto stem = entry.path().stem().string(); auto stem = entry.path().stem().string();
if (!HasMatchingTopLevelSymbol(*table, stem)) if (!HasMatchingTopLevelSymbol(*table, stem))
{ {
spdlog::warn("Skipping system file {}: top-level symbol does not match file name", entry.path().string()); spdlog::warn("Skipping system file {}: top-level symbol does not match file name (stem='{}', top-level={})",
entry.path().string(),
stem,
DescribeTopLevelSymbols(*table));
++failed; ++failed;
continue; continue;
} }
@ -331,7 +399,9 @@ namespace lsp::manager
{ {
if (!entry.is_regular_file()) if (!entry.is_regular_file())
continue; continue;
if (!IsTsfFile(entry.path()))
auto kind = GetTslFileKind(entry.path());
if (kind == TslFileKind::kOther)
continue; continue;
auto table = BuildSymbolTableFromFile(entry.path()); auto table = BuildSymbolTableFromFile(entry.path());
@ -342,9 +412,11 @@ namespace lsp::manager
} }
auto stem = entry.path().stem().string(); auto stem = entry.path().stem().string();
if (!HasMatchingTopLevelSymbol(*table, stem)) // Only `.tsf` is a library unit that must match the file name.
// `.tsl` is a script and should not be forced to have a top-level symbol.
if (kind == TslFileKind::kLibraryTsf && !HasMatchingTopLevelSymbol(*table, stem))
{ {
spdlog::warn("Skipping system file {}: top-level symbol does not match file name", entry.path().string()); spdlog::warn("Skipping workspace file {}: top-level symbol does not match file name", entry.path().string());
++failed; ++failed;
continue; continue;
} }
@ -497,6 +569,11 @@ namespace lsp::manager
analysis.semantic_model = std::make_unique<language::semantic::SemanticModel>(*analysis.symbol_table); analysis.semantic_model = std::make_unique<language::semantic::SemanticModel>(*analysis.symbol_table);
{
language::semantic::Analyzer analyzer(*analysis.symbol_table, *analysis.semantic_model);
analyzer.Analyze(*analysis.ast);
}
{ {
std::unique_lock<std::shared_mutex> lock(mutex_); std::unique_lock<std::shared_mutex> lock(mutex_);
editing_symbols_[event.item.uri] = std::move(analysis); editing_symbols_[event.item.uri] = std::move(analysis);

View File

@ -1,6 +1,5 @@
module; module;
export module lsp.provider.completion_item.resolve; export module lsp.provider.completion_item.resolve;
import spdlog; import spdlog;
@ -9,6 +8,9 @@ import std;
import lsp.protocol; import lsp.protocol;
import lsp.codec.facade; import lsp.codec.facade;
import lsp.provider.base.interface; import lsp.provider.base.interface;
import lsp.language.symbol;
import lsp.language.ast;
import lsp.utils.string;
namespace transform = lsp::codec; namespace transform = lsp::codec;
@ -27,6 +29,214 @@ export namespace lsp::provider::completion_item
namespace lsp::provider::completion_item namespace lsp::provider::completion_item
{ {
namespace
{
std::optional<std::string> GetStringField(const protocol::LSPObject& obj, const std::string& key)
{
auto it = obj.find(key);
if (it == obj.end() || !it->second.Is<protocol::string>())
{
return std::nullopt;
}
const auto& s = it->second.Get<protocol::string>();
return s;
}
std::optional<bool> GetBoolField(const protocol::LSPObject& obj, const std::string& key)
{
auto it = obj.find(key);
if (it == obj.end() || !it->second.Is<protocol::boolean>())
{
return std::nullopt;
}
return it->second.Get<protocol::boolean>();
}
std::string GetModuleName(const language::symbol::SymbolTable& table)
{
for (const auto& wrapper : table.all_definitions())
{
const auto& symbol = wrapper.get();
if (symbol.kind() == protocol::SymbolKind::Module)
{
return symbol.name();
}
}
return "";
}
std::optional<const language::symbol::Symbol*> FindClassSymbol(
const language::symbol::SymbolTable& table,
const std::string& class_name)
{
auto ids = table.FindSymbolsByName(class_name);
for (auto id : ids)
{
const auto* symbol = table.definition(id);
if (symbol && symbol->kind() == protocol::SymbolKind::Class)
{
return symbol;
}
}
return std::nullopt;
}
std::optional<language::symbol::ScopeId> FindScopeOwnedBy(
const language::symbol::SymbolTable& table,
language::symbol::ScopeKind kind,
language::symbol::SymbolId owner_id)
{
for (const auto& [scope_id, scope] : table.scopes().all_scopes())
{
if (scope.kind == kind && scope.owner && *scope.owner == owner_id)
{
return scope_id;
}
}
return std::nullopt;
}
std::vector<const language::symbol::Method*> CollectConstructors(
const language::symbol::SymbolTable& table,
language::symbol::SymbolId class_id)
{
std::vector<const language::symbol::Method*> result;
auto scope_id = FindScopeOwnedBy(table, language::symbol::ScopeKind::kClass, class_id);
if (!scope_id)
{
return result;
}
const auto* scope = table.scopes().scope(*scope_id);
if (!scope)
{
return result;
}
for (const auto& [_, ids] : scope->symbols)
{
for (auto id : ids)
{
const auto* member = table.definition(id);
if (!member || member->kind() != protocol::SymbolKind::Method)
{
continue;
}
const auto* method = member->As<language::symbol::Method>();
if (!method || method->method_kind != language::ast::MethodKind::kConstructor)
{
continue;
}
result.push_back(method);
}
}
return result;
}
const language::symbol::Method* PickBestConstructor(const std::vector<const language::symbol::Method*>& ctors)
{
const language::symbol::Method* best = nullptr;
std::size_t best_required = std::numeric_limits<std::size_t>::max();
std::size_t best_total = std::numeric_limits<std::size_t>::max();
for (const auto* ctor : ctors)
{
if (!ctor)
{
continue;
}
std::size_t required = 0;
for (const auto& p : ctor->parameters)
{
if (!p.default_value.has_value())
{
++required;
}
}
if (required < best_required || (required == best_required && ctor->parameters.size() < best_total))
{
best = ctor;
best_required = required;
best_total = ctor->parameters.size();
}
}
return best;
}
std::string BuildSignature(const std::vector<language::symbol::Parameter>& params, const std::optional<std::string>& return_type)
{
std::string detail = "(";
for (std::size_t i = 0; i < params.size(); ++i)
{
if (i > 0)
detail += ", ";
detail += params[i].name;
if (params[i].type && !params[i].type->empty())
detail += ": " + *params[i].type;
}
detail += ")";
if (return_type && !return_type->empty())
detail += ": " + *return_type;
return detail;
}
std::string BuildNewSnippet(const std::string& class_name, const language::symbol::Method* ctor)
{
std::string snippet = class_name;
snippet += "(";
if (ctor && !ctor->parameters.empty())
{
for (std::size_t i = 0; i < ctor->parameters.size(); ++i)
{
if (i > 0)
{
snippet += ", ";
}
const auto& p = ctor->parameters[i];
snippet += "${" + std::to_string(i + 1) + ":" + p.name + "}";
}
}
snippet += ")";
snippet += "$0";
return snippet;
}
std::string BuildCreateObjectSnippet(const std::string& class_name,
const language::symbol::Method* ctor,
bool has_open_quote,
char quote_char)
{
std::string snippet;
if (!has_open_quote)
{
snippet.push_back(quote_char);
}
snippet += class_name;
snippet.push_back(quote_char);
if (ctor && !ctor->parameters.empty())
{
for (std::size_t i = 0; i < ctor->parameters.size(); ++i)
{
snippet += ", ";
const auto& p = ctor->parameters[i];
snippet += "${" + std::to_string(i + 1) + ":" + p.name + "}";
}
}
snippet += "$0";
return snippet;
}
}
std::string Resolve::GetMethod() const std::string Resolve::GetMethod() const
{ {
return "completionItem/resolve"; return "completionItem/resolve";
@ -40,8 +250,6 @@ namespace lsp::provider::completion_item
std::string Resolve::ProvideResponse(const protocol::RequestMessage& request, std::string Resolve::ProvideResponse(const protocol::RequestMessage& request,
ExecutionContext& execution_context) ExecutionContext& execution_context)
{ {
static_cast<void>(execution_context);
if (!request.params.has_value()) if (!request.params.has_value())
{ {
spdlog::warn("{}: Missing params in request", GetProviderName()); spdlog::warn("{}: Missing params in request", GetProviderName());
@ -52,7 +260,109 @@ namespace lsp::provider::completion_item
protocol::CompletionItem item = transform::FromLSPAny.template operator()<protocol::CompletionItem>(request.params.value()); protocol::CompletionItem item = transform::FromLSPAny.template operator()<protocol::CompletionItem>(request.params.value());
// 暂未迁移 resolve 逻辑,保持原样返回 if (item.data && item.data->Is<protocol::LSPObject>())
{
const auto& obj = item.data->Get<protocol::LSPObject>();
auto ctx = GetStringField(obj, "ctx");
auto class_name = GetStringField(obj, "class");
auto unit_name = GetStringField(obj, "unit");
auto uri = GetStringField(obj, "uri");
if (ctx && class_name && !class_name->empty() && uri)
{
auto& hub = execution_context.GetManagerHub();
const language::symbol::SymbolTable* editing_table = hub.symbols().GetSymbolTable(*uri);
auto workspace_tables = hub.symbols().GetWorkspaceSymbolTables();
auto system_tables = hub.symbols().GetSystemSymbolTables();
auto try_find = [&](const language::symbol::SymbolTable& table) -> std::optional<const language::symbol::SymbolTable*> {
if (unit_name && !unit_name->empty())
{
auto module = GetModuleName(table);
if (module.empty() || !utils::IEquals(module, *unit_name))
{
return std::nullopt;
}
}
if (FindClassSymbol(table, *class_name))
{
return &table;
}
return std::nullopt;
};
const language::symbol::SymbolTable* table_for_class = nullptr;
if (editing_table)
{
if (auto t = try_find(*editing_table))
table_for_class = *t;
}
if (!table_for_class)
{
for (const auto* t : workspace_tables)
{
if (!t)
continue;
if (auto found = try_find(*t))
{
table_for_class = *found;
break;
}
}
}
if (!table_for_class)
{
for (const auto* t : system_tables)
{
if (!t)
continue;
if (auto found = try_find(*t))
{
table_for_class = *found;
break;
}
}
}
const language::symbol::Method* best_ctor = nullptr;
if (table_for_class)
{
if (auto cls_sym = FindClassSymbol(*table_for_class, *class_name))
{
auto ctors = CollectConstructors(*table_for_class, (*cls_sym)->id());
best_ctor = PickBestConstructor(ctors);
if (!item.labelDetails)
{
item.labelDetails = protocol::CompletionItemLabelDetails{};
}
item.labelDetails->detail = best_ctor ? BuildSignature(best_ctor->parameters, best_ctor->return_type) : "";
}
}
if (*ctx == "new")
{
item.insertText = BuildNewSnippet(*class_name, best_ctor);
item.insertTextFormat = protocol::InsertTextFormat::Snippet;
item.kind = protocol::CompletionItemKind::Constructor;
}
else if (*ctx == "createobject")
{
bool has_open_quote = GetBoolField(obj, "has_open_quote").value_or(false);
char quote_char = '"';
if (auto quote = GetStringField(obj, "quote"); quote && !quote->empty())
{
quote_char = (*quote)[0];
}
item.insertText = BuildCreateObjectSnippet(*class_name, best_ctor, has_open_quote, quote_char);
item.insertTextFormat = protocol::InsertTextFormat::Snippet;
item.kind = protocol::CompletionItemKind::Constructor;
}
}
}
protocol::ResponseMessage response; protocol::ResponseMessage response;
response.id = request.id; response.id = request.id;
response.result = transform::ToLSPAny(item); response.result = transform::ToLSPAny(item);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -12,37 +12,37 @@ extern "C" {
// Allow clients to override allocation functions // Allow clients to override allocation functions
#ifdef TREE_SITTER_REUSE_ALLOCATOR #ifdef TREE_SITTER_REUSE_ALLOCATOR
extern void* (*ts_current_malloc)(size_t size); extern void *(*ts_current_malloc)(size_t size);
extern void* (*ts_current_calloc)(size_t count, size_t size); extern void *(*ts_current_calloc)(size_t count, size_t size);
extern void* (*ts_current_realloc)(void* ptr, size_t size); extern void *(*ts_current_realloc)(void *ptr, size_t size);
extern void (*ts_current_free)(void* ptr); extern void (*ts_current_free)(void *ptr);
#ifndef ts_malloc #ifndef ts_malloc
#define ts_malloc ts_current_malloc #define ts_malloc ts_current_malloc
#endif #endif
#ifndef ts_calloc #ifndef ts_calloc
#define ts_calloc ts_current_calloc #define ts_calloc ts_current_calloc
#endif #endif
#ifndef ts_realloc #ifndef ts_realloc
#define ts_realloc ts_current_realloc #define ts_realloc ts_current_realloc
#endif #endif
#ifndef ts_free #ifndef ts_free
#define ts_free ts_current_free #define ts_free ts_current_free
#endif #endif
#else #else
#ifndef ts_malloc #ifndef ts_malloc
#define ts_malloc malloc #define ts_malloc malloc
#endif #endif
#ifndef ts_calloc #ifndef ts_calloc
#define ts_calloc calloc #define ts_calloc calloc
#endif #endif
#ifndef ts_realloc #ifndef ts_realloc
#define ts_realloc realloc #define ts_realloc realloc
#endif #endif
#ifndef ts_free #ifndef ts_free
#define ts_free free #define ts_free free
#endif #endif
#endif #endif

View File

@ -21,25 +21,24 @@ extern "C" {
#pragma GCC diagnostic ignored "-Wunused-variable" #pragma GCC diagnostic ignored "-Wunused-variable"
#endif #endif
#define Array(T) \ #define Array(T) \
struct \ struct { \
{ \ T *contents; \
T* contents; \ uint32_t size; \
uint32_t size; \ uint32_t capacity; \
uint32_t capacity; \ }
}
/// Initialize an array. /// Initialize an array.
#define array_init(self) \ #define array_init(self) \
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL) ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
/// Create an empty array. /// Create an empty array.
#define array_new() \ #define array_new() \
{ NULL, 0, 0 } { NULL, 0, 0 }
/// Get a pointer to the element at a given `index` in the array. /// Get a pointer to the element at a given `index` in the array.
#define array_get(self, _index) \ #define array_get(self, _index) \
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index]) (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
/// Get a pointer to the first element in the array. /// Get a pointer to the first element in the array.
#define array_front(self) array_get(self, 0) #define array_front(self) array_get(self, 0)
@ -54,64 +53,66 @@ extern "C" {
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is /// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
/// less than the array's current capacity, this function has no effect. /// less than the array's current capacity, this function has no effect.
#define array_reserve(self, new_capacity) \ #define array_reserve(self, new_capacity) \
_array__reserve((Array*)(self), array_elem_size(self), new_capacity) _array__reserve((Array *)(self), array_elem_size(self), new_capacity)
/// Free any memory allocated for this array. Note that this does not free any /// Free any memory allocated for this array. Note that this does not free any
/// memory allocated for the array's contents. /// memory allocated for the array's contents.
#define array_delete(self) _array__delete((Array*)(self)) #define array_delete(self) _array__delete((Array *)(self))
/// Push a new `element` onto the end of the array. /// Push a new `element` onto the end of the array.
#define array_push(self, element) \ #define array_push(self, element) \
(_array__grow((Array*)(self), 1, array_elem_size(self)), \ (_array__grow((Array *)(self), 1, array_elem_size(self)), \
(self)->contents[(self)->size++] = (element)) (self)->contents[(self)->size++] = (element))
/// Increase the array's size by `count` elements. /// Increase the array's size by `count` elements.
/// New elements are zero-initialized. /// New elements are zero-initialized.
#define array_grow_by(self, count) \ #define array_grow_by(self, count) \
do \ do { \
{ \ if ((count) == 0) break; \
if ((count) == 0) \ _array__grow((Array *)(self), count, array_elem_size(self)); \
break; \ memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
_array__grow((Array*)(self), count, array_elem_size(self)); \ (self)->size += (count); \
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \ } while (0)
(self)->size += (count); \
} while (0)
/// Append all elements from one array to the end of another. /// Append all elements from one array to the end of another.
#define array_push_all(self, other) \ #define array_push_all(self, other) \
array_extend((self), (other)->size, (other)->contents) array_extend((self), (other)->size, (other)->contents)
/// Append `count` elements to the end of the array, reading their values from the /// Append `count` elements to the end of the array, reading their values from the
/// `contents` pointer. /// `contents` pointer.
#define array_extend(self, count, contents) \ #define array_extend(self, count, contents) \
_array__splice( \ _array__splice( \
(Array*)(self), array_elem_size(self), (self)->size, 0, count, contents) (Array *)(self), array_elem_size(self), (self)->size, \
0, count, contents \
)
/// Remove `old_count` elements from the array starting at the given `index`. At /// Remove `old_count` elements from the array starting at the given `index`. At
/// the same index, insert `new_count` new elements, reading their values from the /// the same index, insert `new_count` new elements, reading their values from the
/// `new_contents` pointer. /// `new_contents` pointer.
#define array_splice(self, _index, old_count, new_count, new_contents) \ #define array_splice(self, _index, old_count, new_count, new_contents) \
_array__splice( \ _array__splice( \
(Array*)(self), array_elem_size(self), _index, old_count, new_count, new_contents) (Array *)(self), array_elem_size(self), _index, \
old_count, new_count, new_contents \
)
/// Insert one `element` into the array at the given `index`. /// Insert one `element` into the array at the given `index`.
#define array_insert(self, _index, element) \ #define array_insert(self, _index, element) \
_array__splice((Array*)(self), array_elem_size(self), _index, 0, 1, &(element)) _array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
/// Remove one element from the array at the given `index`. /// Remove one element from the array at the given `index`.
#define array_erase(self, _index) \ #define array_erase(self, _index) \
_array__erase((Array*)(self), array_elem_size(self), _index) _array__erase((Array *)(self), array_elem_size(self), _index)
/// Pop the last element off the array, returning the element by value. /// Pop the last element off the array, returning the element by value.
#define array_pop(self) ((self)->contents[--(self)->size]) #define array_pop(self) ((self)->contents[--(self)->size])
/// Assign the contents of one array to another, reallocating if necessary. /// Assign the contents of one array to another, reallocating if necessary.
#define array_assign(self, other) \ #define array_assign(self, other) \
_array__assign((Array*)(self), (const Array*)(other), array_elem_size(self)) _array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
/// Swap one array with another /// Swap one array with another
#define array_swap(self, other) \ #define array_swap(self, other) \
_array__swap((Array*)(self), (Array*)(other)) _array__swap((Array *)(self), (Array *)(other))
/// Get the size of the array contents /// Get the size of the array contents
#define array_elem_size(self) (sizeof *(self)->contents) #define array_elem_size(self) (sizeof *(self)->contents)
@ -125,176 +126,153 @@ extern "C" {
/// `needle` should be inserted in order to preserve the sorting, and `exists` /// `needle` should be inserted in order to preserve the sorting, and `exists`
/// is set to false. /// is set to false.
#define array_search_sorted_with(self, compare, needle, _index, _exists) \ #define array_search_sorted_with(self, compare, needle, _index, _exists) \
_array__search_sorted(self, 0, compare, , needle, _index, _exists) _array__search_sorted(self, 0, compare, , needle, _index, _exists)
/// Search a sorted array for a given `needle` value, using integer comparisons /// Search a sorted array for a given `needle` value, using integer comparisons
/// of a given struct field (specified with a leading dot) to determine the order. /// of a given struct field (specified with a leading dot) to determine the order.
/// ///
/// See also `array_search_sorted_with`. /// See also `array_search_sorted_with`.
#define array_search_sorted_by(self, field, needle, _index, _exists) \ #define array_search_sorted_by(self, field, needle, _index, _exists) \
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists) _array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
/// Insert a given `value` into a sorted array, using the given `compare` /// Insert a given `value` into a sorted array, using the given `compare`
/// callback to determine the order. /// callback to determine the order.
#define array_insert_sorted_with(self, compare, value) \ #define array_insert_sorted_with(self, compare, value) \
do \ do { \
{ \ unsigned _index, _exists; \
unsigned _index, _exists; \ array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \ if (!_exists) array_insert(self, _index, value); \
if (!_exists) \ } while (0)
array_insert(self, _index, value); \
} while (0)
/// Insert a given `value` into a sorted array, using integer comparisons of /// Insert a given `value` into a sorted array, using integer comparisons of
/// a given struct field (specified with a leading dot) to determine the order. /// a given struct field (specified with a leading dot) to determine the order.
/// ///
/// See also `array_search_sorted_by`. /// See also `array_search_sorted_by`.
#define array_insert_sorted_by(self, field, value) \ #define array_insert_sorted_by(self, field, value) \
do \ do { \
{ \ unsigned _index, _exists; \
unsigned _index, _exists; \ array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
array_search_sorted_by(self, field, (value)field, &_index, &_exists); \ if (!_exists) array_insert(self, _index, value); \
if (!_exists) \ } while (0)
array_insert(self, _index, value); \
} while (0)
// Private // Private
typedef Array(void) Array; typedef Array(void) Array;
/// This is not what you're looking for, see `array_delete`. /// This is not what you're looking for, see `array_delete`.
static inline void _array__delete(Array* self) static inline void _array__delete(Array *self) {
{ if (self->contents) {
if (self->contents) ts_free(self->contents);
{ self->contents = NULL;
ts_free(self->contents); self->size = 0;
self->contents = NULL; self->capacity = 0;
self->size = 0; }
self->capacity = 0;
}
} }
/// This is not what you're looking for, see `array_erase`. /// This is not what you're looking for, see `array_erase`.
static inline void _array__erase(Array* self, size_t element_size, uint32_t index) static inline void _array__erase(Array *self, size_t element_size,
{ uint32_t index) {
assert(index < self->size); assert(index < self->size);
char* contents = (char*)self->contents; char *contents = (char *)self->contents;
memmove(contents + index * element_size, contents + (index + 1) * element_size, (self->size - index - 1) * element_size); memmove(contents + index * element_size, contents + (index + 1) * element_size,
self->size--; (self->size - index - 1) * element_size);
self->size--;
} }
/// This is not what you're looking for, see `array_reserve`. /// This is not what you're looking for, see `array_reserve`.
static inline void _array__reserve(Array* self, size_t element_size, uint32_t new_capacity) static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
{ if (new_capacity > self->capacity) {
if (new_capacity > self->capacity) if (self->contents) {
{ self->contents = ts_realloc(self->contents, new_capacity * element_size);
if (self->contents) } else {
{ self->contents = ts_malloc(new_capacity * element_size);
self->contents = ts_realloc(self->contents, new_capacity * element_size);
}
else
{
self->contents = ts_malloc(new_capacity * element_size);
}
self->capacity = new_capacity;
} }
self->capacity = new_capacity;
}
} }
/// This is not what you're looking for, see `array_assign`. /// This is not what you're looking for, see `array_assign`.
static inline void _array__assign(Array* self, const Array* other, size_t element_size) static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
{ _array__reserve(self, element_size, other->size);
_array__reserve(self, element_size, other->size); self->size = other->size;
self->size = other->size; memcpy(self->contents, other->contents, self->size * element_size);
memcpy(self->contents, other->contents, self->size * element_size);
} }
/// This is not what you're looking for, see `array_swap`. /// This is not what you're looking for, see `array_swap`.
static inline void _array__swap(Array* self, Array* other) static inline void _array__swap(Array *self, Array *other) {
{ Array swap = *other;
Array swap = *other; *other = *self;
*other = *self; *self = swap;
*self = swap;
} }
/// This is not what you're looking for, see `array_push` or `array_grow_by`. /// This is not what you're looking for, see `array_push` or `array_grow_by`.
static inline void _array__grow(Array* self, uint32_t count, size_t element_size) static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
{ uint32_t new_size = self->size + count;
uint32_t new_size = self->size + count; if (new_size > self->capacity) {
if (new_size > self->capacity) uint32_t new_capacity = self->capacity * 2;
{ if (new_capacity < 8) new_capacity = 8;
uint32_t new_capacity = self->capacity * 2; if (new_capacity < new_size) new_capacity = new_size;
if (new_capacity < 8) _array__reserve(self, element_size, new_capacity);
new_capacity = 8; }
if (new_capacity < new_size)
new_capacity = new_size;
_array__reserve(self, element_size, new_capacity);
}
} }
/// This is not what you're looking for, see `array_splice`. /// This is not what you're looking for, see `array_splice`.
static inline void _array__splice(Array* self, size_t element_size, uint32_t index, uint32_t old_count, uint32_t new_count, const void* elements) static inline void _array__splice(Array *self, size_t element_size,
{ uint32_t index, uint32_t old_count,
uint32_t new_size = self->size + new_count - old_count; uint32_t new_count, const void *elements) {
uint32_t old_end = index + old_count; uint32_t new_size = self->size + new_count - old_count;
uint32_t new_end = index + new_count; uint32_t old_end = index + old_count;
assert(old_end <= self->size); uint32_t new_end = index + new_count;
assert(old_end <= self->size);
_array__reserve(self, element_size, new_size); _array__reserve(self, element_size, new_size);
char* contents = (char*)self->contents; char *contents = (char *)self->contents;
if (self->size > old_end) if (self->size > old_end) {
{ memmove(
memmove( contents + new_end * element_size,
contents + new_end * element_size, contents + old_end * element_size,
contents + old_end * element_size, (self->size - old_end) * element_size
(self->size - old_end) * element_size); );
}
if (new_count > 0) {
if (elements) {
memcpy(
(contents + index * element_size),
elements,
new_count * element_size
);
} else {
memset(
(contents + index * element_size),
0,
new_count * element_size
);
} }
if (new_count > 0) }
{ self->size += new_count - old_count;
if (elements)
{
memcpy(
(contents + index * element_size),
elements,
new_count * element_size);
}
else
{
memset(
(contents + index * element_size),
0,
new_count * element_size);
}
}
self->size += new_count - old_count;
} }
/// A binary search routine, based on Rust's `std::slice::binary_search_by`. /// A binary search routine, based on Rust's `std::slice::binary_search_by`.
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`. /// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \ #define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
do \ do { \
{ \ *(_index) = start; \
*(_index) = start; \ *(_exists) = false; \
*(_exists) = false; \ uint32_t size = (self)->size - *(_index); \
uint32_t size = (self)->size - *(_index); \ if (size == 0) break; \
if (size == 0) \ int comparison; \
break; \ while (size > 1) { \
int comparison; \ uint32_t half_size = size / 2; \
while (size > 1) \ uint32_t mid_index = *(_index) + half_size; \
{ \ comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
uint32_t half_size = size / 2; \ if (comparison <= 0) *(_index) = mid_index; \
uint32_t mid_index = *(_index) + half_size; \ size -= half_size; \
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \ } \
if (comparison <= 0) \ comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
*(_index) = mid_index; \ if (comparison == 0) *(_exists) = true; \
size -= half_size; \ else if (comparison < 0) *(_index) += 1; \
} \ } while (0)
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
if (comparison == 0) \
*(_exists) = true; \
else if (comparison < 0) \
*(_index) += 1; \
} while (0)
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing) /// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
/// parameter by reference in order to work with the generic sorting function above. /// parameter by reference in order to work with the generic sorting function above.
@ -310,4 +288,4 @@ static inline void _array__splice(Array* self, size_t element_size, uint32_t ind
} }
#endif #endif
#endif // TREE_SITTER_ARRAY_H_ #endif // TREE_SITTER_ARRAY_H_

View File

@ -9,7 +9,7 @@ extern "C" {
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol) - 1) #define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0 #define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 #define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
@ -18,176 +18,155 @@ typedef uint16_t TSStateId;
typedef uint16_t TSSymbol; typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId; typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage; typedef struct TSLanguage TSLanguage;
typedef struct TSLanguageMetadata typedef struct TSLanguageMetadata {
{ uint8_t major_version;
uint8_t major_version; uint8_t minor_version;
uint8_t minor_version; uint8_t patch_version;
uint8_t patch_version;
} TSLanguageMetadata; } TSLanguageMetadata;
#endif #endif
typedef struct typedef struct {
{ TSFieldId field_id;
TSFieldId field_id; uint8_t child_index;
uint8_t child_index; bool inherited;
bool inherited;
} TSFieldMapEntry; } TSFieldMapEntry;
// Used to index the field and supertype maps. // Used to index the field and supertype maps.
typedef struct typedef struct {
{ uint16_t index;
uint16_t index; uint16_t length;
uint16_t length;
} TSMapSlice; } TSMapSlice;
typedef struct typedef struct {
{ bool visible;
bool visible; bool named;
bool named; bool supertype;
bool supertype;
} TSSymbolMetadata; } TSSymbolMetadata;
typedef struct TSLexer TSLexer; typedef struct TSLexer TSLexer;
struct TSLexer struct TSLexer {
{ int32_t lookahead;
int32_t lookahead; TSSymbol result_symbol;
TSSymbol result_symbol; void (*advance)(TSLexer *, bool);
void (*advance)(TSLexer*, bool); void (*mark_end)(TSLexer *);
void (*mark_end)(TSLexer*); uint32_t (*get_column)(TSLexer *);
uint32_t (*get_column)(TSLexer*); bool (*is_at_included_range_start)(const TSLexer *);
bool (*is_at_included_range_start)(const TSLexer*); bool (*eof)(const TSLexer *);
bool (*eof)(const TSLexer*); void (*log)(const TSLexer *, const char *, ...);
void (*log)(const TSLexer*, const char*, ...);
}; };
typedef enum typedef enum {
{ TSParseActionTypeShift,
TSParseActionTypeShift, TSParseActionTypeReduce,
TSParseActionTypeReduce, TSParseActionTypeAccept,
TSParseActionTypeAccept, TSParseActionTypeRecover,
TSParseActionTypeRecover,
} TSParseActionType; } TSParseActionType;
typedef union typedef union {
{ struct {
struct
{
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct
{
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type; uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction; } TSParseAction;
typedef struct typedef struct {
{ uint16_t lex_state;
uint16_t lex_state; uint16_t external_lex_state;
uint16_t external_lex_state;
} TSLexMode; } TSLexMode;
typedef struct typedef struct {
{ uint16_t lex_state;
uint16_t lex_state; uint16_t external_lex_state;
uint16_t external_lex_state; uint16_t reserved_word_set_id;
uint16_t reserved_word_set_id;
} TSLexerMode; } TSLexerMode;
typedef union typedef union {
{ TSParseAction action;
TSParseAction action; struct {
struct uint8_t count;
{ bool reusable;
uint8_t count; } entry;
bool reusable;
} entry;
} TSParseActionEntry; } TSParseActionEntry;
typedef struct typedef struct {
{ int32_t start;
int32_t start; int32_t end;
int32_t end;
} TSCharacterRange; } TSCharacterRange;
struct TSLanguage struct TSLanguage {
{ uint32_t abi_version;
uint32_t abi_version; uint32_t symbol_count;
uint32_t symbol_count; uint32_t alias_count;
uint32_t alias_count; uint32_t token_count;
uint32_t token_count; uint32_t external_token_count;
uint32_t external_token_count; uint32_t state_count;
uint32_t state_count; uint32_t large_state_count;
uint32_t large_state_count; uint32_t production_id_count;
uint32_t production_id_count; uint32_t field_count;
uint32_t field_count; uint16_t max_alias_sequence_length;
uint16_t max_alias_sequence_length; const uint16_t *parse_table;
const uint16_t* parse_table; const uint16_t *small_parse_table;
const uint16_t* small_parse_table; const uint32_t *small_parse_table_map;
const uint32_t* small_parse_table_map; const TSParseActionEntry *parse_actions;
const TSParseActionEntry* parse_actions; const char * const *symbol_names;
const char* const* symbol_names; const char * const *field_names;
const char* const* field_names; const TSMapSlice *field_map_slices;
const TSMapSlice* field_map_slices; const TSFieldMapEntry *field_map_entries;
const TSFieldMapEntry* field_map_entries; const TSSymbolMetadata *symbol_metadata;
const TSSymbolMetadata* symbol_metadata; const TSSymbol *public_symbol_map;
const TSSymbol* public_symbol_map; const uint16_t *alias_map;
const uint16_t* alias_map; const TSSymbol *alias_sequences;
const TSSymbol* alias_sequences; const TSLexerMode *lex_modes;
const TSLexerMode* lex_modes; bool (*lex_fn)(TSLexer *, TSStateId);
bool (*lex_fn)(TSLexer*, TSStateId); bool (*keyword_lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer*, TSStateId); TSSymbol keyword_capture_token;
TSSymbol keyword_capture_token; struct {
struct const bool *states;
{ const TSSymbol *symbol_map;
const bool* states; void *(*create)(void);
const TSSymbol* symbol_map; void (*destroy)(void *);
void* (*create)(void); bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
void (*destroy)(void*); unsigned (*serialize)(void *, char *);
bool (*scan)(void*, TSLexer*, const bool* symbol_whitelist); void (*deserialize)(void *, const char *, unsigned);
unsigned (*serialize)(void*, char*); } external_scanner;
void (*deserialize)(void*, const char*, unsigned); const TSStateId *primary_state_ids;
} external_scanner; const char *name;
const TSStateId* primary_state_ids; const TSSymbol *reserved_words;
const char* name; uint16_t max_reserved_word_set_size;
const TSSymbol* reserved_words; uint32_t supertype_count;
uint16_t max_reserved_word_set_size; const TSSymbol *supertype_symbols;
uint32_t supertype_count; const TSMapSlice *supertype_map_slices;
const TSSymbol* supertype_symbols; const TSSymbol *supertype_map_entries;
const TSMapSlice* supertype_map_slices; TSLanguageMetadata metadata;
const TSSymbol* supertype_map_entries;
TSLanguageMetadata metadata;
}; };
static inline bool set_contains(const TSCharacterRange* ranges, uint32_t len, int32_t lookahead) static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
{ uint32_t index = 0;
uint32_t index = 0; uint32_t size = len - index;
uint32_t size = len - index; while (size > 1) {
while (size > 1) uint32_t half_size = size / 2;
{ uint32_t mid_index = index + half_size;
uint32_t half_size = size / 2; const TSCharacterRange *range = &ranges[mid_index];
uint32_t mid_index = index + half_size; if (lookahead >= range->start && lookahead <= range->end) {
const TSCharacterRange* range = &ranges[mid_index]; return true;
if (lookahead >= range->start && lookahead <= range->end) } else if (lookahead > range->end) {
{ index = mid_index;
return true;
}
else if (lookahead > range->end)
{
index = mid_index;
}
size -= half_size;
} }
const TSCharacterRange* range = &ranges[index]; size -= half_size;
return (lookahead >= range->start && lookahead <= range->end); }
const TSCharacterRange *range = &ranges[index];
return (lookahead >= range->start && lookahead <= range->end);
} }
/* /*
@ -200,49 +179,47 @@ static inline bool set_contains(const TSCharacterRange* ranges, uint32_t len, in
#define UNUSED __attribute__((unused)) #define UNUSED __attribute__((unused))
#endif #endif
#define START_LEXER() \ #define START_LEXER() \
bool result = false; \ bool result = false; \
bool skip = false; \ bool skip = false; \
UNUSED \ UNUSED \
bool eof = false; \ bool eof = false; \
int32_t lookahead; \ int32_t lookahead; \
goto start; \ goto start; \
next_state: \ next_state: \
lexer->advance(lexer, skip); \ lexer->advance(lexer, skip); \
start: \ start: \
skip = false; \ skip = false; \
lookahead = lexer->lookahead; lookahead = lexer->lookahead;
#define ADVANCE(state_value) \ #define ADVANCE(state_value) \
{ \ { \
state = state_value; \ state = state_value; \
goto next_state; \ goto next_state; \
} }
#define ADVANCE_MAP(...) \ #define ADVANCE_MAP(...) \
{ \ { \
static const uint16_t map[] = { __VA_ARGS__ }; \ static const uint16_t map[] = { __VA_ARGS__ }; \
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) \ for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
{ \ if (map[i] == lookahead) { \
if (map[i] == lookahead) \ state = map[i + 1]; \
{ \ goto next_state; \
state = map[i + 1]; \ } \
goto next_state; \ } \
} \ }
} \
}
#define SKIP(state_value) \ #define SKIP(state_value) \
{ \ { \
skip = true; \ skip = true; \
state = state_value; \ state = state_value; \
goto next_state; \ goto next_state; \
} }
#define ACCEPT_TOKEN(symbol_value) \ #define ACCEPT_TOKEN(symbol_value) \
result = true; \ result = true; \
lexer->result_symbol = symbol_value; \ lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer); lexer->mark_end(lexer);
#define END_STATE() return result; #define END_STATE() return result;
@ -256,66 +233,54 @@ static inline bool set_contains(const TSCharacterRange* ranges, uint32_t len, in
#define ACTIONS(id) id #define ACTIONS(id) id
#define SHIFT(state_value) \ #define SHIFT(state_value) \
{ \ {{ \
{ \ .shift = { \
.shift = { \ .type = TSParseActionTypeShift, \
.type = TSParseActionTypeShift, \ .state = (state_value) \
.state = (state_value) \ } \
} \ }}
} \
}
#define SHIFT_REPEAT(state_value) \ #define SHIFT_REPEAT(state_value) \
{ \ {{ \
{ \ .shift = { \
.shift = { \ .type = TSParseActionTypeShift, \
.type = TSParseActionTypeShift, \ .state = (state_value), \
.state = (state_value), \ .repetition = true \
.repetition = true \ } \
} \ }}
} \
}
#define SHIFT_EXTRA() \ #define SHIFT_EXTRA() \
{ \ {{ \
{ \ .shift = { \
.shift = { \ .type = TSParseActionTypeShift, \
.type = TSParseActionTypeShift, \ .extra = true \
.extra = true \ } \
} \ }}
} \
}
#define REDUCE(symbol_name, children, precedence, prod_id) \ #define REDUCE(symbol_name, children, precedence, prod_id) \
{ \ {{ \
{ \ .reduce = { \
.reduce = { \ .type = TSParseActionTypeReduce, \
.type = TSParseActionTypeReduce, \ .symbol = symbol_name, \
.symbol = symbol_name, \ .child_count = children, \
.child_count = children, \ .dynamic_precedence = precedence, \
.dynamic_precedence = precedence, \ .production_id = prod_id \
.production_id = prod_id \ }, \
}, \ }}
} \
}
#define RECOVER() \ #define RECOVER() \
{ \ {{ \
{ \ .type = TSParseActionTypeRecover \
.type = TSParseActionTypeRecover \ }}
} \
}
#define ACCEPT_INPUT() \ #define ACCEPT_INPUT() \
{ \ {{ \
{ \ .type = TSParseActionTypeAccept \
.type = TSParseActionTypeAccept \ }}
} \
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif // TREE_SITTER_PARSER_H_ #endif // TREE_SITTER_PARSER_H_

View File

@ -52,7 +52,9 @@ namespace lsp::utils
const ServerConfig& ArgsParser::Parse(int argc, char* argv[]) const ServerConfig& ArgsParser::Parse(int argc, char* argv[])
{ {
config_ = ServerConfig{}; config_ = ServerConfig{};
bool use_stdio = false; // Default to stderr so LSP stdio (stdout) stays clean.
config_.use_stderr = true;
for (int i = 1; i < argc; ++i) for (int i = 1; i < argc; ++i)
{ {
std::string arg = argv[i]; std::string arg = argv[i];
@ -75,10 +77,14 @@ namespace lsp::utils
config_.log_level = spdlog::level::err; config_.log_level = spdlog::level::err;
else if (arg == "--log=off") else if (arg == "--log=off")
config_.log_level = spdlog::level::off; config_.log_level = spdlog::level::off;
else if (arg == "--log-stderr")
config_.use_stderr = true;
else if (arg == "--log-stdout")
config_.use_stderr = false;
else if (arg.find("--log-file=") == 0) else if (arg.find("--log-file=") == 0)
config_.log_file = arg.substr(std::strlen("--log-file=")); config_.log_file = arg.substr(std::strlen("--log-file="));
else if (arg == "--use-stdio") else if (arg == "--use-stdio")
use_stdio = true; config_.use_stderr = true;
else if (arg.find("--threads=") == 0) else if (arg.find("--threads=") == 0)
{ {
auto value = arg.substr(std::strlen("--threads=")); auto value = arg.substr(std::strlen("--threads="));
@ -90,9 +96,6 @@ namespace lsp::utils
} }
} }
if (!use_stdio)
config_.use_stderr = true;
return config_; return config_;
} }
@ -113,7 +116,7 @@ namespace lsp::utils
} }
else else
{ {
auto console_logger = spdlog::stdout_logger_mt("console_logger"); auto console_logger = config.use_stderr ? spdlog::stderr_logger_mt("console_logger") : spdlog::stdout_logger_mt("console_logger");
console_logger->set_level(config.log_level); console_logger->set_level(config.log_level);
spdlog::set_default_logger(console_logger); spdlog::set_default_logger(console_logger);
} }
@ -127,8 +130,10 @@ namespace lsp::utils
<< "Options:\\n" << "Options:\\n"
<< " --help Show this help message\\n" << " --help Show this help message\\n"
<< " --log=<level> Set log level (trace, debug, info, warn, error, off)\\n" << " --log=<level> Set log level (trace, debug, info, warn, error, off)\\n"
<< " --log-stderr Output logs to stderr (default)\\n"
<< " --log-stdout Output logs to stdout\\n"
<< " --log-file=<path> Output logs to specified file\\n" << " --log-file=<path> Output logs to specified file\\n"
<< " --use-stdio Use stdin/stdout for I/O (default: stderr)\\n" << " --use-stdio Alias for --log-stderr (keep stdout clean for LSP)\\n"
<< " --threads=<count> Number of worker threads\\n" << " --threads=<count> Number of worker threads\\n"
<< " --interpreter=<path> Custom interpreter path\\n"; << " --interpreter=<path> Custom interpreter path\\n";
} }

View File

@ -1,130 +0,0 @@
module;
import std;
module lsp.utils.string;
namespace lsp::utils
{
std::string Trim(const std::string& str)
{
std::size_t first = str.find_first_not_of(" \t\n\r");
if (first == std::string::npos)
return "";
std::size_t last = str.find_last_not_of(" \t\n\r");
return str.substr(first, last - first + 1);
}
std::string ToLower(const std::string& str)
{
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::tolower(c); });
return result;
}
std::string ToUpper(const std::string& str)
{
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::toupper(c); });
return result;
}
bool StartsWith(const std::string& str, const std::string& prefix)
{
if (prefix.size() > str.size())
return false;
return str.compare(0, prefix.size(), prefix) == 0;
}
bool EndsWith(const std::string& str, const std::string& suffix)
{
if (suffix.size() > str.size())
return false;
return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
// ==================== 大小写不敏感比较 ====================
bool IEquals(const std::string& a, const std::string& b)
{
if (a.size() != b.size())
return false;
return std::equal(a.begin(), a.end(), b.begin(), [](unsigned char ca, unsigned char cb) {
return std::tolower(ca) == std::tolower(cb);
});
}
bool IStartsWith(const std::string& str, const std::string& prefix)
{
if (prefix.size() > str.size())
return false;
for (std::size_t i = 0; i < prefix.size(); ++i)
{
if (std::tolower(static_cast<unsigned char>(str[i])) !=
std::tolower(static_cast<unsigned char>(prefix[i])))
return false;
}
return true;
}
bool IEndsWith(const std::string& str, const std::string& suffix)
{
if (suffix.size() > str.size())
return false;
std::size_t offset = str.size() - suffix.size();
for (std::size_t i = 0; i < suffix.size(); ++i)
{
if (std::tolower(static_cast<unsigned char>(str[offset + i])) !=
std::tolower(static_cast<unsigned char>(suffix[i])))
return false;
}
return true;
}
int ICompare(const std::string& a, const std::string& b)
{
std::size_t min_len = std::min(a.size(), b.size());
for (std::size_t i = 0; i < min_len; ++i)
{
int ca = std::tolower(static_cast<unsigned char>(a[i]));
int cb = std::tolower(static_cast<unsigned char>(b[i]));
if (ca != cb)
return ca - cb;
}
// 长度不同
if (a.size() < b.size())
return -1;
if (a.size() > b.size())
return 1;
return 0;
}
std::size_t IHash(const std::string& str)
{
std::size_t hash = 0;
for (unsigned char c : str)
hash = hash * 31 + std::tolower(c);
return hash;
}
std::size_t IHasher::operator()(const std::string& key) const
{
return IHash(key);
}
bool IEqualTo::operator()(const std::string& a, const std::string& b) const
{
return IEquals(a, b);
}
bool ILess::operator()(const std::string& a, const std::string& b) const
{
return ICompare(a, b) < 0;
}
}

View File

@ -38,3 +38,126 @@ export namespace lsp::utils
bool operator()(const std::string& a, const std::string& b) const; bool operator()(const std::string& a, const std::string& b) const;
}; };
} }
namespace lsp::utils
{
std::string Trim(const std::string& str)
{
std::size_t first = str.find_first_not_of(" \t\n\r");
if (first == std::string::npos)
return "";
std::size_t last = str.find_last_not_of(" \t\n\r");
return str.substr(first, last - first + 1);
}
std::string ToLower(const std::string& str)
{
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::tolower(c); });
return result;
}
std::string ToUpper(const std::string& str)
{
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::toupper(c); });
return result;
}
bool StartsWith(const std::string& str, const std::string& prefix)
{
if (prefix.size() > str.size())
return false;
return str.compare(0, prefix.size(), prefix) == 0;
}
bool EndsWith(const std::string& str, const std::string& suffix)
{
if (suffix.size() > str.size())
return false;
return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
bool IEquals(const std::string& a, const std::string& b)
{
if (a.size() != b.size())
return false;
return std::equal(a.begin(), a.end(), b.begin(), [](unsigned char ca, unsigned char cb) {
return std::tolower(ca) == std::tolower(cb);
});
}
bool IStartsWith(const std::string& str, const std::string& prefix)
{
if (prefix.size() > str.size())
return false;
for (std::size_t i = 0; i < prefix.size(); ++i)
{
if (std::tolower(static_cast<unsigned char>(str[i])) !=
std::tolower(static_cast<unsigned char>(prefix[i])))
return false;
}
return true;
}
bool IEndsWith(const std::string& str, const std::string& suffix)
{
if (suffix.size() > str.size())
return false;
std::size_t offset = str.size() - suffix.size();
for (std::size_t i = 0; i < suffix.size(); ++i)
{
if (std::tolower(static_cast<unsigned char>(str[offset + i])) !=
std::tolower(static_cast<unsigned char>(suffix[i])))
return false;
}
return true;
}
int ICompare(const std::string& a, const std::string& b)
{
std::size_t min_len = std::min(a.size(), b.size());
for (std::size_t i = 0; i < min_len; ++i)
{
int ca = std::tolower(static_cast<unsigned char>(a[i]));
int cb = std::tolower(static_cast<unsigned char>(b[i]));
if (ca != cb)
return ca - cb;
}
if (a.size() < b.size())
return -1;
if (a.size() > b.size())
return 1;
return 0;
}
std::size_t IHash(const std::string& str)
{
std::size_t hash = 0;
for (unsigned char c : str)
hash = hash * 31 + std::tolower(c);
return hash;
}
std::size_t IHasher::operator()(const std::string& key) const
{
return IHash(key);
}
bool IEqualTo::operator()(const std::string& a, const std::string& b) const
{
return IEquals(a, b);
}
bool ILess::operator()(const std::string& a, const std::string& b) const
{
return ICompare(a, b) < 0;
}
}

View File

@ -18,10 +18,10 @@ endif()
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1) set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
set(SOURCES set(SOURCES
main.cc
test.cppm test.cppm
debug_printer.cppm debug_printer.cppm
../../src/utils/string.cppm ../../src/utils/string.cppm
../../src/utils/string.cpp
../../src/tree-sitter/scanner.c ../../src/tree-sitter/scanner.c
../../src/tree-sitter/parser.c) ../../src/tree-sitter/parser.c)

View File

@ -135,6 +135,7 @@ export namespace lsp::language::ast::debug
void VisitEchoExpression(EchoExpression& node) override; void VisitEchoExpression(EchoExpression& node) override;
void VisitRaiseExpression(RaiseExpression& node) override; void VisitRaiseExpression(RaiseExpression& node) override;
void VisitInheritedExpression(InheritedExpression& node) override; void VisitInheritedExpression(InheritedExpression& node) override;
void VisitRdoExpression(RdoExpression& node) override;
void VisitTSSQLExpression(TSSQLExpression& node) override; void VisitTSSQLExpression(TSSQLExpression& node) override;
void VisitColumnReference(ColumnReference& node) override; void VisitColumnReference(ColumnReference& node) override;
@ -2082,6 +2083,26 @@ export namespace lsp::language::ast::debug
} }
} }
void DebugPrinter::VisitRdoExpression(RdoExpression& node)
{
PrintIndent();
PrintColored("RdoExpression", Color::BrightCyan);
if (options_.show_location)
{
os_ << " ";
PrintLocation(node.span);
}
os_ << "\n";
if (node.call)
{
IncreaseIndent();
PrintExpression(node.call.value().get(), "Call", true);
DecreaseIndent();
}
}
void DebugPrinter::VisitTSSQLExpression(TSSQLExpression& node) void DebugPrinter::VisitTSSQLExpression(TSSQLExpression& node)
{ {
PrintIndent(); PrintIndent();

View File

@ -0,0 +1,6 @@
import lsp.test.ast.main;
int main(int argc, char* argv[])
{
return Run(argc, argv);
}

View File

@ -120,7 +120,7 @@ void PrintUsage(const char* program_name)
std::cout << " " << program_name << " test.tsf -s -n # Show source without colors\n"; std::cout << " " << program_name << " test.tsf -s -n # Show source without colors\n";
} }
int main(int argc, char* argv[]) export int Run(int argc, char* argv[])
{ {
if (argc < 2) if (argc < 2)
{ {

View File

@ -13,6 +13,7 @@ if(UNIX AND NOT APPLE)
endif() endif()
set(SOURCES set(SOURCES
main.cc
test_main.cppm) test_main.cppm)
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} ${SOURCES})

View File

@ -0,0 +1,6 @@
import lsp.test.lsp_any.main;
int main()
{
return Run();
}

View File

@ -10,7 +10,7 @@ import lsp.test.lsp_any.transformer;
import lsp.test.lsp_any.facade; import lsp.test.lsp_any.facade;
import lsp.test.lsp_any.common; import lsp.test.lsp_any.common;
int main() export int Run()
{ {
lsp::test::TestRunner runner; lsp::test::TestRunner runner;

View File

@ -12,7 +12,7 @@ endif()
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1) set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
add_executable(test_module main.cppm) add_executable(test_module main.cppm main.cc)
target_sources( target_sources(
test_module test_module
PRIVATE PRIVATE

View File

@ -0,0 +1,6 @@
import lsp.test.module_demo.main;
int main()
{
return Run();
}

View File

@ -7,7 +7,7 @@ import std;
import math; import math;
import math2; import math2;
int main() export int Run()
{ {
std::cout << "hello std module\n"; std::cout << "hello std module\n";
std::vector<int> v{ 1, 2, 3 }; std::vector<int> v{ 1, 2, 3 };

View File

@ -15,6 +15,7 @@ if(UNIX AND NOT APPLE)
endif() endif()
set(SOURCES set(SOURCES
main.cc
test_async_executor.cppm) test_async_executor.cppm)
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} ${SOURCES})

View File

@ -0,0 +1,6 @@
import lsp.test.scheduler.async_executor;
int main()
{
return Run();
}

View File

@ -55,7 +55,7 @@ namespace
}; };
} }
int main() export int Run()
{ {
SchedulerTestSuite suite; SchedulerTestSuite suite;

View File

@ -17,9 +17,9 @@ endif()
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1) set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
set(SOURCES set(SOURCES
main.cc
test_semantic.cppm test_semantic.cppm
../../src/utils/string.cppm ../../src/utils/string.cppm
../../src/utils/string.cpp
../../src/language/symbol/internal/builder.cppm ../../src/language/symbol/internal/builder.cppm
../../src/language/symbol/internal/store.cppm ../../src/language/symbol/internal/store.cppm
../../src/language/symbol/internal/table.cppm ../../src/language/symbol/internal/table.cppm

View File

@ -0,0 +1,6 @@
import lsp.test.semantic.main;
int main(int argc, char** argv)
{
return Run(argc, argv);
}

View File

@ -436,7 +436,7 @@ namespace
} }
} }
int main(int argc, char** argv) export int Run(int argc, char** argv)
{ {
try try
{ {

View File

@ -18,9 +18,9 @@ endif()
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1) set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
set(SOURCES set(SOURCES
main.cc
test.cppm test.cppm
../../src/utils/string.cppm ../../src/utils/string.cppm
../../src/utils/string.cpp
../../src/language/symbol/internal/builder.cppm ../../src/language/symbol/internal/builder.cppm
../../src/language/symbol/internal/store.cppm ../../src/language/symbol/internal/store.cppm
../../src/language/symbol/internal/table.cppm ../../src/language/symbol/internal/table.cppm

View File

@ -0,0 +1,6 @@
import lsp.test.symbol.main;
int main(int argc, char* argv[])
{
return Run(argc, argv);
}

View File

@ -1050,7 +1050,7 @@ void AnalyzeFile(const Options& options)
// ==================== 主程序 ==================== // ==================== 主程序 ====================
int main(int argc, char* argv[]) export int Run(int argc, char* argv[])
{ {
Options options; Options options;

View File

@ -1151,6 +1151,7 @@ module.exports = grammar({
$.echo_expression, $.echo_expression,
$.raise_expression, $.raise_expression,
$.inherited_expression, $.inherited_expression,
$.rdo_expression,
), ),
new_expression: ($) => seq(kw("new"), field("constructor", $.expression)), new_expression: ($) => seq(kw("new"), field("constructor", $.expression)),
@ -1162,7 +1163,13 @@ module.exports = grammar({
inherited_expression: ($) => inherited_expression: ($) =>
prec.right( prec.right(
kPrec.kCall, kPrec.kCall,
seq(kw("inherited"), optional(field("class", $.call_expression))), seq(kw("inherited"), optional(field("call", $.call_expression))),
),
rdo_expression: ($) =>
prec.right(
kPrec.kCall,
seq(choice(kw("rdo"), kw("rdo2")), optional(field("call", $.call_expression))),
), ),
array_expression: ($) => array_expression: ($) =>

View File

@ -6754,6 +6754,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "inherited_expression" "name": "inherited_expression"
},
{
"type": "SYMBOL",
"name": "rdo_expression"
} }
] ]
}, },
@ -6864,7 +6868,55 @@
"members": [ "members": [
{ {
"type": "FIELD", "type": "FIELD",
"name": "class", "name": "call",
"content": {
"type": "SYMBOL",
"name": "call_expression"
}
},
{
"type": "BLANK"
}
]
}
]
}
},
"rdo_expression": {
"type": "PREC_RIGHT",
"value": 18,
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "(?i)rdo"
},
"named": false,
"value": "rdo"
},
{
"type": "ALIAS",
"content": {
"type": "PATTERN",
"value": "(?i)rdo2"
},
"named": false,
"value": "rdo2"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "call",
"content": { "content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "call_expression" "name": "call_expression"

View File

@ -2256,6 +2256,10 @@
"type": "raise_expression", "type": "raise_expression",
"named": true "named": true
}, },
{
"type": "rdo_expression",
"named": true
},
{ {
"type": "select_expression", "type": "select_expression",
"named": true "named": true
@ -3600,7 +3604,7 @@
"type": "inherited_expression", "type": "inherited_expression",
"named": true, "named": true,
"fields": { "fields": {
"class": { "call": {
"multiple": false, "multiple": false,
"required": false, "required": false,
"types": [ "types": [
@ -4863,6 +4867,22 @@
} }
} }
}, },
{
"type": "rdo_expression",
"named": true,
"fields": {
"call": {
"multiple": false,
"required": false,
"types": [
{
"type": "call_expression",
"named": true
}
]
}
}
},
{ {
"type": "reference_modifier", "type": "reference_modifier",
"named": true, "named": true,
@ -7001,6 +7021,14 @@
"type": "raise", "type": "raise",
"named": false "named": false
}, },
{
"type": "rdo",
"named": false
},
{
"type": "rdo2",
"named": false
},
{ {
"type": "read", "type": "read",
"named": false "named": false

File diff suppressed because it is too large Load Diff

View File

@ -12,37 +12,37 @@ extern "C" {
// Allow clients to override allocation functions // Allow clients to override allocation functions
#ifdef TREE_SITTER_REUSE_ALLOCATOR #ifdef TREE_SITTER_REUSE_ALLOCATOR
extern void* (*ts_current_malloc)(size_t size); extern void *(*ts_current_malloc)(size_t size);
extern void* (*ts_current_calloc)(size_t count, size_t size); extern void *(*ts_current_calloc)(size_t count, size_t size);
extern void* (*ts_current_realloc)(void* ptr, size_t size); extern void *(*ts_current_realloc)(void *ptr, size_t size);
extern void (*ts_current_free)(void* ptr); extern void (*ts_current_free)(void *ptr);
#ifndef ts_malloc #ifndef ts_malloc
#define ts_malloc ts_current_malloc #define ts_malloc ts_current_malloc
#endif #endif
#ifndef ts_calloc #ifndef ts_calloc
#define ts_calloc ts_current_calloc #define ts_calloc ts_current_calloc
#endif #endif
#ifndef ts_realloc #ifndef ts_realloc
#define ts_realloc ts_current_realloc #define ts_realloc ts_current_realloc
#endif #endif
#ifndef ts_free #ifndef ts_free
#define ts_free ts_current_free #define ts_free ts_current_free
#endif #endif
#else #else
#ifndef ts_malloc #ifndef ts_malloc
#define ts_malloc malloc #define ts_malloc malloc
#endif #endif
#ifndef ts_calloc #ifndef ts_calloc
#define ts_calloc calloc #define ts_calloc calloc
#endif #endif
#ifndef ts_realloc #ifndef ts_realloc
#define ts_realloc realloc #define ts_realloc realloc
#endif #endif
#ifndef ts_free #ifndef ts_free
#define ts_free free #define ts_free free
#endif #endif
#endif #endif

View File

@ -21,25 +21,24 @@ extern "C" {
#pragma GCC diagnostic ignored "-Wunused-variable" #pragma GCC diagnostic ignored "-Wunused-variable"
#endif #endif
#define Array(T) \ #define Array(T) \
struct \ struct { \
{ \ T *contents; \
T* contents; \ uint32_t size; \
uint32_t size; \ uint32_t capacity; \
uint32_t capacity; \ }
}
/// Initialize an array. /// Initialize an array.
#define array_init(self) \ #define array_init(self) \
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL) ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
/// Create an empty array. /// Create an empty array.
#define array_new() \ #define array_new() \
{ NULL, 0, 0 } { NULL, 0, 0 }
/// Get a pointer to the element at a given `index` in the array. /// Get a pointer to the element at a given `index` in the array.
#define array_get(self, _index) \ #define array_get(self, _index) \
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index]) (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
/// Get a pointer to the first element in the array. /// Get a pointer to the first element in the array.
#define array_front(self) array_get(self, 0) #define array_front(self) array_get(self, 0)
@ -54,64 +53,66 @@ extern "C" {
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is /// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
/// less than the array's current capacity, this function has no effect. /// less than the array's current capacity, this function has no effect.
#define array_reserve(self, new_capacity) \ #define array_reserve(self, new_capacity) \
_array__reserve((Array*)(self), array_elem_size(self), new_capacity) _array__reserve((Array *)(self), array_elem_size(self), new_capacity)
/// Free any memory allocated for this array. Note that this does not free any /// Free any memory allocated for this array. Note that this does not free any
/// memory allocated for the array's contents. /// memory allocated for the array's contents.
#define array_delete(self) _array__delete((Array*)(self)) #define array_delete(self) _array__delete((Array *)(self))
/// Push a new `element` onto the end of the array. /// Push a new `element` onto the end of the array.
#define array_push(self, element) \ #define array_push(self, element) \
(_array__grow((Array*)(self), 1, array_elem_size(self)), \ (_array__grow((Array *)(self), 1, array_elem_size(self)), \
(self)->contents[(self)->size++] = (element)) (self)->contents[(self)->size++] = (element))
/// Increase the array's size by `count` elements. /// Increase the array's size by `count` elements.
/// New elements are zero-initialized. /// New elements are zero-initialized.
#define array_grow_by(self, count) \ #define array_grow_by(self, count) \
do \ do { \
{ \ if ((count) == 0) break; \
if ((count) == 0) \ _array__grow((Array *)(self), count, array_elem_size(self)); \
break; \ memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
_array__grow((Array*)(self), count, array_elem_size(self)); \ (self)->size += (count); \
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \ } while (0)
(self)->size += (count); \
} while (0)
/// Append all elements from one array to the end of another. /// Append all elements from one array to the end of another.
#define array_push_all(self, other) \ #define array_push_all(self, other) \
array_extend((self), (other)->size, (other)->contents) array_extend((self), (other)->size, (other)->contents)
/// Append `count` elements to the end of the array, reading their values from the /// Append `count` elements to the end of the array, reading their values from the
/// `contents` pointer. /// `contents` pointer.
#define array_extend(self, count, contents) \ #define array_extend(self, count, contents) \
_array__splice( \ _array__splice( \
(Array*)(self), array_elem_size(self), (self)->size, 0, count, contents) (Array *)(self), array_elem_size(self), (self)->size, \
0, count, contents \
)
/// Remove `old_count` elements from the array starting at the given `index`. At /// Remove `old_count` elements from the array starting at the given `index`. At
/// the same index, insert `new_count` new elements, reading their values from the /// the same index, insert `new_count` new elements, reading their values from the
/// `new_contents` pointer. /// `new_contents` pointer.
#define array_splice(self, _index, old_count, new_count, new_contents) \ #define array_splice(self, _index, old_count, new_count, new_contents) \
_array__splice( \ _array__splice( \
(Array*)(self), array_elem_size(self), _index, old_count, new_count, new_contents) (Array *)(self), array_elem_size(self), _index, \
old_count, new_count, new_contents \
)
/// Insert one `element` into the array at the given `index`. /// Insert one `element` into the array at the given `index`.
#define array_insert(self, _index, element) \ #define array_insert(self, _index, element) \
_array__splice((Array*)(self), array_elem_size(self), _index, 0, 1, &(element)) _array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
/// Remove one element from the array at the given `index`. /// Remove one element from the array at the given `index`.
#define array_erase(self, _index) \ #define array_erase(self, _index) \
_array__erase((Array*)(self), array_elem_size(self), _index) _array__erase((Array *)(self), array_elem_size(self), _index)
/// Pop the last element off the array, returning the element by value. /// Pop the last element off the array, returning the element by value.
#define array_pop(self) ((self)->contents[--(self)->size]) #define array_pop(self) ((self)->contents[--(self)->size])
/// Assign the contents of one array to another, reallocating if necessary. /// Assign the contents of one array to another, reallocating if necessary.
#define array_assign(self, other) \ #define array_assign(self, other) \
_array__assign((Array*)(self), (const Array*)(other), array_elem_size(self)) _array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
/// Swap one array with another /// Swap one array with another
#define array_swap(self, other) \ #define array_swap(self, other) \
_array__swap((Array*)(self), (Array*)(other)) _array__swap((Array *)(self), (Array *)(other))
/// Get the size of the array contents /// Get the size of the array contents
#define array_elem_size(self) (sizeof *(self)->contents) #define array_elem_size(self) (sizeof *(self)->contents)
@ -125,176 +126,153 @@ extern "C" {
/// `needle` should be inserted in order to preserve the sorting, and `exists` /// `needle` should be inserted in order to preserve the sorting, and `exists`
/// is set to false. /// is set to false.
#define array_search_sorted_with(self, compare, needle, _index, _exists) \ #define array_search_sorted_with(self, compare, needle, _index, _exists) \
_array__search_sorted(self, 0, compare, , needle, _index, _exists) _array__search_sorted(self, 0, compare, , needle, _index, _exists)
/// Search a sorted array for a given `needle` value, using integer comparisons /// Search a sorted array for a given `needle` value, using integer comparisons
/// of a given struct field (specified with a leading dot) to determine the order. /// of a given struct field (specified with a leading dot) to determine the order.
/// ///
/// See also `array_search_sorted_with`. /// See also `array_search_sorted_with`.
#define array_search_sorted_by(self, field, needle, _index, _exists) \ #define array_search_sorted_by(self, field, needle, _index, _exists) \
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists) _array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
/// Insert a given `value` into a sorted array, using the given `compare` /// Insert a given `value` into a sorted array, using the given `compare`
/// callback to determine the order. /// callback to determine the order.
#define array_insert_sorted_with(self, compare, value) \ #define array_insert_sorted_with(self, compare, value) \
do \ do { \
{ \ unsigned _index, _exists; \
unsigned _index, _exists; \ array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \ if (!_exists) array_insert(self, _index, value); \
if (!_exists) \ } while (0)
array_insert(self, _index, value); \
} while (0)
/// Insert a given `value` into a sorted array, using integer comparisons of /// Insert a given `value` into a sorted array, using integer comparisons of
/// a given struct field (specified with a leading dot) to determine the order. /// a given struct field (specified with a leading dot) to determine the order.
/// ///
/// See also `array_search_sorted_by`. /// See also `array_search_sorted_by`.
#define array_insert_sorted_by(self, field, value) \ #define array_insert_sorted_by(self, field, value) \
do \ do { \
{ \ unsigned _index, _exists; \
unsigned _index, _exists; \ array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
array_search_sorted_by(self, field, (value)field, &_index, &_exists); \ if (!_exists) array_insert(self, _index, value); \
if (!_exists) \ } while (0)
array_insert(self, _index, value); \
} while (0)
// Private // Private
typedef Array(void) Array; typedef Array(void) Array;
/// This is not what you're looking for, see `array_delete`. /// This is not what you're looking for, see `array_delete`.
static inline void _array__delete(Array* self) static inline void _array__delete(Array *self) {
{ if (self->contents) {
if (self->contents) ts_free(self->contents);
{ self->contents = NULL;
ts_free(self->contents); self->size = 0;
self->contents = NULL; self->capacity = 0;
self->size = 0; }
self->capacity = 0;
}
} }
/// This is not what you're looking for, see `array_erase`. /// This is not what you're looking for, see `array_erase`.
static inline void _array__erase(Array* self, size_t element_size, uint32_t index) static inline void _array__erase(Array *self, size_t element_size,
{ uint32_t index) {
assert(index < self->size); assert(index < self->size);
char* contents = (char*)self->contents; char *contents = (char *)self->contents;
memmove(contents + index * element_size, contents + (index + 1) * element_size, (self->size - index - 1) * element_size); memmove(contents + index * element_size, contents + (index + 1) * element_size,
self->size--; (self->size - index - 1) * element_size);
self->size--;
} }
/// This is not what you're looking for, see `array_reserve`. /// This is not what you're looking for, see `array_reserve`.
static inline void _array__reserve(Array* self, size_t element_size, uint32_t new_capacity) static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
{ if (new_capacity > self->capacity) {
if (new_capacity > self->capacity) if (self->contents) {
{ self->contents = ts_realloc(self->contents, new_capacity * element_size);
if (self->contents) } else {
{ self->contents = ts_malloc(new_capacity * element_size);
self->contents = ts_realloc(self->contents, new_capacity * element_size);
}
else
{
self->contents = ts_malloc(new_capacity * element_size);
}
self->capacity = new_capacity;
} }
self->capacity = new_capacity;
}
} }
/// This is not what you're looking for, see `array_assign`. /// This is not what you're looking for, see `array_assign`.
static inline void _array__assign(Array* self, const Array* other, size_t element_size) static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
{ _array__reserve(self, element_size, other->size);
_array__reserve(self, element_size, other->size); self->size = other->size;
self->size = other->size; memcpy(self->contents, other->contents, self->size * element_size);
memcpy(self->contents, other->contents, self->size * element_size);
} }
/// This is not what you're looking for, see `array_swap`. /// This is not what you're looking for, see `array_swap`.
static inline void _array__swap(Array* self, Array* other) static inline void _array__swap(Array *self, Array *other) {
{ Array swap = *other;
Array swap = *other; *other = *self;
*other = *self; *self = swap;
*self = swap;
} }
/// This is not what you're looking for, see `array_push` or `array_grow_by`. /// This is not what you're looking for, see `array_push` or `array_grow_by`.
static inline void _array__grow(Array* self, uint32_t count, size_t element_size) static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
{ uint32_t new_size = self->size + count;
uint32_t new_size = self->size + count; if (new_size > self->capacity) {
if (new_size > self->capacity) uint32_t new_capacity = self->capacity * 2;
{ if (new_capacity < 8) new_capacity = 8;
uint32_t new_capacity = self->capacity * 2; if (new_capacity < new_size) new_capacity = new_size;
if (new_capacity < 8) _array__reserve(self, element_size, new_capacity);
new_capacity = 8; }
if (new_capacity < new_size)
new_capacity = new_size;
_array__reserve(self, element_size, new_capacity);
}
} }
/// This is not what you're looking for, see `array_splice`. /// This is not what you're looking for, see `array_splice`.
static inline void _array__splice(Array* self, size_t element_size, uint32_t index, uint32_t old_count, uint32_t new_count, const void* elements) static inline void _array__splice(Array *self, size_t element_size,
{ uint32_t index, uint32_t old_count,
uint32_t new_size = self->size + new_count - old_count; uint32_t new_count, const void *elements) {
uint32_t old_end = index + old_count; uint32_t new_size = self->size + new_count - old_count;
uint32_t new_end = index + new_count; uint32_t old_end = index + old_count;
assert(old_end <= self->size); uint32_t new_end = index + new_count;
assert(old_end <= self->size);
_array__reserve(self, element_size, new_size); _array__reserve(self, element_size, new_size);
char* contents = (char*)self->contents; char *contents = (char *)self->contents;
if (self->size > old_end) if (self->size > old_end) {
{ memmove(
memmove( contents + new_end * element_size,
contents + new_end * element_size, contents + old_end * element_size,
contents + old_end * element_size, (self->size - old_end) * element_size
(self->size - old_end) * element_size); );
}
if (new_count > 0) {
if (elements) {
memcpy(
(contents + index * element_size),
elements,
new_count * element_size
);
} else {
memset(
(contents + index * element_size),
0,
new_count * element_size
);
} }
if (new_count > 0) }
{ self->size += new_count - old_count;
if (elements)
{
memcpy(
(contents + index * element_size),
elements,
new_count * element_size);
}
else
{
memset(
(contents + index * element_size),
0,
new_count * element_size);
}
}
self->size += new_count - old_count;
} }
/// A binary search routine, based on Rust's `std::slice::binary_search_by`. /// A binary search routine, based on Rust's `std::slice::binary_search_by`.
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`. /// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \ #define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
do \ do { \
{ \ *(_index) = start; \
*(_index) = start; \ *(_exists) = false; \
*(_exists) = false; \ uint32_t size = (self)->size - *(_index); \
uint32_t size = (self)->size - *(_index); \ if (size == 0) break; \
if (size == 0) \ int comparison; \
break; \ while (size > 1) { \
int comparison; \ uint32_t half_size = size / 2; \
while (size > 1) \ uint32_t mid_index = *(_index) + half_size; \
{ \ comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
uint32_t half_size = size / 2; \ if (comparison <= 0) *(_index) = mid_index; \
uint32_t mid_index = *(_index) + half_size; \ size -= half_size; \
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \ } \
if (comparison <= 0) \ comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
*(_index) = mid_index; \ if (comparison == 0) *(_exists) = true; \
size -= half_size; \ else if (comparison < 0) *(_index) += 1; \
} \ } while (0)
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
if (comparison == 0) \
*(_exists) = true; \
else if (comparison < 0) \
*(_index) += 1; \
} while (0)
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing) /// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
/// parameter by reference in order to work with the generic sorting function above. /// parameter by reference in order to work with the generic sorting function above.
@ -310,4 +288,4 @@ static inline void _array__splice(Array* self, size_t element_size, uint32_t ind
} }
#endif #endif
#endif // TREE_SITTER_ARRAY_H_ #endif // TREE_SITTER_ARRAY_H_

View File

@ -9,7 +9,7 @@ extern "C" {
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol) - 1) #define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0 #define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 #define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
@ -18,176 +18,155 @@ typedef uint16_t TSStateId;
typedef uint16_t TSSymbol; typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId; typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage; typedef struct TSLanguage TSLanguage;
typedef struct TSLanguageMetadata typedef struct TSLanguageMetadata {
{ uint8_t major_version;
uint8_t major_version; uint8_t minor_version;
uint8_t minor_version; uint8_t patch_version;
uint8_t patch_version;
} TSLanguageMetadata; } TSLanguageMetadata;
#endif #endif
typedef struct typedef struct {
{ TSFieldId field_id;
TSFieldId field_id; uint8_t child_index;
uint8_t child_index; bool inherited;
bool inherited;
} TSFieldMapEntry; } TSFieldMapEntry;
// Used to index the field and supertype maps. // Used to index the field and supertype maps.
typedef struct typedef struct {
{ uint16_t index;
uint16_t index; uint16_t length;
uint16_t length;
} TSMapSlice; } TSMapSlice;
typedef struct typedef struct {
{ bool visible;
bool visible; bool named;
bool named; bool supertype;
bool supertype;
} TSSymbolMetadata; } TSSymbolMetadata;
typedef struct TSLexer TSLexer; typedef struct TSLexer TSLexer;
struct TSLexer struct TSLexer {
{ int32_t lookahead;
int32_t lookahead; TSSymbol result_symbol;
TSSymbol result_symbol; void (*advance)(TSLexer *, bool);
void (*advance)(TSLexer*, bool); void (*mark_end)(TSLexer *);
void (*mark_end)(TSLexer*); uint32_t (*get_column)(TSLexer *);
uint32_t (*get_column)(TSLexer*); bool (*is_at_included_range_start)(const TSLexer *);
bool (*is_at_included_range_start)(const TSLexer*); bool (*eof)(const TSLexer *);
bool (*eof)(const TSLexer*); void (*log)(const TSLexer *, const char *, ...);
void (*log)(const TSLexer*, const char*, ...);
}; };
typedef enum typedef enum {
{ TSParseActionTypeShift,
TSParseActionTypeShift, TSParseActionTypeReduce,
TSParseActionTypeReduce, TSParseActionTypeAccept,
TSParseActionTypeAccept, TSParseActionTypeRecover,
TSParseActionTypeRecover,
} TSParseActionType; } TSParseActionType;
typedef union typedef union {
{ struct {
struct
{
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct
{
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type; uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction; } TSParseAction;
typedef struct typedef struct {
{ uint16_t lex_state;
uint16_t lex_state; uint16_t external_lex_state;
uint16_t external_lex_state;
} TSLexMode; } TSLexMode;
typedef struct typedef struct {
{ uint16_t lex_state;
uint16_t lex_state; uint16_t external_lex_state;
uint16_t external_lex_state; uint16_t reserved_word_set_id;
uint16_t reserved_word_set_id;
} TSLexerMode; } TSLexerMode;
typedef union typedef union {
{ TSParseAction action;
TSParseAction action; struct {
struct uint8_t count;
{ bool reusable;
uint8_t count; } entry;
bool reusable;
} entry;
} TSParseActionEntry; } TSParseActionEntry;
typedef struct typedef struct {
{ int32_t start;
int32_t start; int32_t end;
int32_t end;
} TSCharacterRange; } TSCharacterRange;
struct TSLanguage struct TSLanguage {
{ uint32_t abi_version;
uint32_t abi_version; uint32_t symbol_count;
uint32_t symbol_count; uint32_t alias_count;
uint32_t alias_count; uint32_t token_count;
uint32_t token_count; uint32_t external_token_count;
uint32_t external_token_count; uint32_t state_count;
uint32_t state_count; uint32_t large_state_count;
uint32_t large_state_count; uint32_t production_id_count;
uint32_t production_id_count; uint32_t field_count;
uint32_t field_count; uint16_t max_alias_sequence_length;
uint16_t max_alias_sequence_length; const uint16_t *parse_table;
const uint16_t* parse_table; const uint16_t *small_parse_table;
const uint16_t* small_parse_table; const uint32_t *small_parse_table_map;
const uint32_t* small_parse_table_map; const TSParseActionEntry *parse_actions;
const TSParseActionEntry* parse_actions; const char * const *symbol_names;
const char* const* symbol_names; const char * const *field_names;
const char* const* field_names; const TSMapSlice *field_map_slices;
const TSMapSlice* field_map_slices; const TSFieldMapEntry *field_map_entries;
const TSFieldMapEntry* field_map_entries; const TSSymbolMetadata *symbol_metadata;
const TSSymbolMetadata* symbol_metadata; const TSSymbol *public_symbol_map;
const TSSymbol* public_symbol_map; const uint16_t *alias_map;
const uint16_t* alias_map; const TSSymbol *alias_sequences;
const TSSymbol* alias_sequences; const TSLexerMode *lex_modes;
const TSLexerMode* lex_modes; bool (*lex_fn)(TSLexer *, TSStateId);
bool (*lex_fn)(TSLexer*, TSStateId); bool (*keyword_lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer*, TSStateId); TSSymbol keyword_capture_token;
TSSymbol keyword_capture_token; struct {
struct const bool *states;
{ const TSSymbol *symbol_map;
const bool* states; void *(*create)(void);
const TSSymbol* symbol_map; void (*destroy)(void *);
void* (*create)(void); bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
void (*destroy)(void*); unsigned (*serialize)(void *, char *);
bool (*scan)(void*, TSLexer*, const bool* symbol_whitelist); void (*deserialize)(void *, const char *, unsigned);
unsigned (*serialize)(void*, char*); } external_scanner;
void (*deserialize)(void*, const char*, unsigned); const TSStateId *primary_state_ids;
} external_scanner; const char *name;
const TSStateId* primary_state_ids; const TSSymbol *reserved_words;
const char* name; uint16_t max_reserved_word_set_size;
const TSSymbol* reserved_words; uint32_t supertype_count;
uint16_t max_reserved_word_set_size; const TSSymbol *supertype_symbols;
uint32_t supertype_count; const TSMapSlice *supertype_map_slices;
const TSSymbol* supertype_symbols; const TSSymbol *supertype_map_entries;
const TSMapSlice* supertype_map_slices; TSLanguageMetadata metadata;
const TSSymbol* supertype_map_entries;
TSLanguageMetadata metadata;
}; };
static inline bool set_contains(const TSCharacterRange* ranges, uint32_t len, int32_t lookahead) static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
{ uint32_t index = 0;
uint32_t index = 0; uint32_t size = len - index;
uint32_t size = len - index; while (size > 1) {
while (size > 1) uint32_t half_size = size / 2;
{ uint32_t mid_index = index + half_size;
uint32_t half_size = size / 2; const TSCharacterRange *range = &ranges[mid_index];
uint32_t mid_index = index + half_size; if (lookahead >= range->start && lookahead <= range->end) {
const TSCharacterRange* range = &ranges[mid_index]; return true;
if (lookahead >= range->start && lookahead <= range->end) } else if (lookahead > range->end) {
{ index = mid_index;
return true;
}
else if (lookahead > range->end)
{
index = mid_index;
}
size -= half_size;
} }
const TSCharacterRange* range = &ranges[index]; size -= half_size;
return (lookahead >= range->start && lookahead <= range->end); }
const TSCharacterRange *range = &ranges[index];
return (lookahead >= range->start && lookahead <= range->end);
} }
/* /*
@ -200,49 +179,47 @@ static inline bool set_contains(const TSCharacterRange* ranges, uint32_t len, in
#define UNUSED __attribute__((unused)) #define UNUSED __attribute__((unused))
#endif #endif
#define START_LEXER() \ #define START_LEXER() \
bool result = false; \ bool result = false; \
bool skip = false; \ bool skip = false; \
UNUSED \ UNUSED \
bool eof = false; \ bool eof = false; \
int32_t lookahead; \ int32_t lookahead; \
goto start; \ goto start; \
next_state: \ next_state: \
lexer->advance(lexer, skip); \ lexer->advance(lexer, skip); \
start: \ start: \
skip = false; \ skip = false; \
lookahead = lexer->lookahead; lookahead = lexer->lookahead;
#define ADVANCE(state_value) \ #define ADVANCE(state_value) \
{ \ { \
state = state_value; \ state = state_value; \
goto next_state; \ goto next_state; \
} }
#define ADVANCE_MAP(...) \ #define ADVANCE_MAP(...) \
{ \ { \
static const uint16_t map[] = { __VA_ARGS__ }; \ static const uint16_t map[] = { __VA_ARGS__ }; \
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) \ for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
{ \ if (map[i] == lookahead) { \
if (map[i] == lookahead) \ state = map[i + 1]; \
{ \ goto next_state; \
state = map[i + 1]; \ } \
goto next_state; \ } \
} \ }
} \
}
#define SKIP(state_value) \ #define SKIP(state_value) \
{ \ { \
skip = true; \ skip = true; \
state = state_value; \ state = state_value; \
goto next_state; \ goto next_state; \
} }
#define ACCEPT_TOKEN(symbol_value) \ #define ACCEPT_TOKEN(symbol_value) \
result = true; \ result = true; \
lexer->result_symbol = symbol_value; \ lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer); lexer->mark_end(lexer);
#define END_STATE() return result; #define END_STATE() return result;
@ -256,66 +233,54 @@ static inline bool set_contains(const TSCharacterRange* ranges, uint32_t len, in
#define ACTIONS(id) id #define ACTIONS(id) id
#define SHIFT(state_value) \ #define SHIFT(state_value) \
{ \ {{ \
{ \ .shift = { \
.shift = { \ .type = TSParseActionTypeShift, \
.type = TSParseActionTypeShift, \ .state = (state_value) \
.state = (state_value) \ } \
} \ }}
} \
}
#define SHIFT_REPEAT(state_value) \ #define SHIFT_REPEAT(state_value) \
{ \ {{ \
{ \ .shift = { \
.shift = { \ .type = TSParseActionTypeShift, \
.type = TSParseActionTypeShift, \ .state = (state_value), \
.state = (state_value), \ .repetition = true \
.repetition = true \ } \
} \ }}
} \
}
#define SHIFT_EXTRA() \ #define SHIFT_EXTRA() \
{ \ {{ \
{ \ .shift = { \
.shift = { \ .type = TSParseActionTypeShift, \
.type = TSParseActionTypeShift, \ .extra = true \
.extra = true \ } \
} \ }}
} \
}
#define REDUCE(symbol_name, children, precedence, prod_id) \ #define REDUCE(symbol_name, children, precedence, prod_id) \
{ \ {{ \
{ \ .reduce = { \
.reduce = { \ .type = TSParseActionTypeReduce, \
.type = TSParseActionTypeReduce, \ .symbol = symbol_name, \
.symbol = symbol_name, \ .child_count = children, \
.child_count = children, \ .dynamic_precedence = precedence, \
.dynamic_precedence = precedence, \ .production_id = prod_id \
.production_id = prod_id \ }, \
}, \ }}
} \
}
#define RECOVER() \ #define RECOVER() \
{ \ {{ \
{ \ .type = TSParseActionTypeRecover \
.type = TSParseActionTypeRecover \ }}
} \
}
#define ACCEPT_INPUT() \ #define ACCEPT_INPUT() \
{ \ {{ \
{ \ .type = TSParseActionTypeAccept \
.type = TSParseActionTypeAccept \ }}
} \
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif // TREE_SITTER_PARSER_H_ #endif // TREE_SITTER_PARSER_H_