#include #include #include #include "./tsl_keywords.hpp" namespace tsl { std::vector TslKeywords::keywords_; std::unordered_set TslKeywords::keyword_set_; bool TslKeywords::initialized_ = false; const std::vector& TslKeywords::GetAllKeywords() { InitKeywords(); return keywords_; } std::vector TslKeywords::GetKeywordsByCategory(KeywordCategory category) { InitKeywords(); std::vector result; for (const auto& keyword : keywords_) if (keyword.category == category) result.push_back(keyword); return result; } std::vector TslKeywords::GetCompletionItems(const std::string& prefix) { InitKeywords(); std::vector result; std::string lower_prefix = ToLowerCase(prefix); for (const auto& keyword : keywords_) { if (prefix.empty() || StartsWith(ToLowerCase(keyword.keyword), lower_prefix)) { lsp::CompletionItem item; item.label = keyword.keyword; item.kind = keyword.completion_kind; item.detail = keyword.description; item.insert_text = keyword.keyword; result.push_back(item); } } // 按字母顺序排序 std::sort(result.begin(), result.end(), [](const lsp::CompletionItem& a, const lsp::CompletionItem& b) { return a.label < b.label; }); return result; } KeywordCategory TslKeywords::GetKeywordCategory(const std::string& word) { InitKeywords(); std::string lower_word = ToLowerCase(word); for (const auto& keyword : keywords_) { if (ToLowerCase(keyword.keyword) == lower_word) { return keyword.category; } } // 如果没找到,返回一个默认值(可以考虑抛出异常) return KeywordCategory::kProgramStructure; } void TslKeywords::InitKeywords() { if (initialized_) return; keywords_ = { // Program Structure { "program", KeywordCategory::kProgramStructure, "Program declaration", lsp::CompletionItemKind::kKeyword }, { "function", KeywordCategory::kProgramStructure, "Function declaration", lsp::CompletionItemKind::kFunction }, { "procedure", KeywordCategory::kProgramStructure, "Procedure declaration", lsp::CompletionItemKind::kFunction }, { "unit", KeywordCategory::kProgramStructure, "Unit declaration", lsp::CompletionItemKind::kModule }, { "uses", KeywordCategory::kProgramStructure, "Uses clause", lsp::CompletionItemKind::kKeyword }, { "implementation", KeywordCategory::kProgramStructure, "Implementation section", lsp::CompletionItemKind::kKeyword }, { "interface", KeywordCategory::kProgramStructure, "Interface section", lsp::CompletionItemKind::kInterface }, { "initialization", KeywordCategory::kProgramStructure, "Initialization section", lsp::CompletionItemKind::kKeyword }, { "finalization", KeywordCategory::kProgramStructure, "Finalization section", lsp::CompletionItemKind::kKeyword }, // Data Types { "string", KeywordCategory::kDataTypes, "String data type", lsp::CompletionItemKind::kClass }, { "integer", KeywordCategory::kDataTypes, "Integer data type", lsp::CompletionItemKind::kClass }, { "boolean", KeywordCategory::kDataTypes, "Boolean data type", lsp::CompletionItemKind::kClass }, { "int64", KeywordCategory::kDataTypes, "64-bit integer data type", lsp::CompletionItemKind::kClass }, { "real", KeywordCategory::kDataTypes, "Real number data type", lsp::CompletionItemKind::kClass }, { "array", KeywordCategory::kDataTypes, "Array data type", lsp::CompletionItemKind::kClass }, // Class Types { "type", KeywordCategory::kClassTypes, "Type declaration", lsp::CompletionItemKind::kKeyword }, { "class", KeywordCategory::kClassTypes, "Class declaration", lsp::CompletionItemKind::kClass }, { "fakeclass", KeywordCategory::kClassTypes, "Fake class declaration", lsp::CompletionItemKind::kClass }, { "new", KeywordCategory::kClassTypes, "Object instantiation", lsp::CompletionItemKind::kKeyword }, // Class Modifiers { "override", KeywordCategory::kClassModifiers, "Override method", lsp::CompletionItemKind::kKeyword }, { "overload", KeywordCategory::kClassModifiers, "Overload method", lsp::CompletionItemKind::kKeyword }, { "virtual", KeywordCategory::kClassModifiers, "Virtual method", lsp::CompletionItemKind::kKeyword }, { "property", KeywordCategory::kClassModifiers, "Property declaration", lsp::CompletionItemKind::kProperty }, { "self", KeywordCategory::kClassModifiers, "Self reference", lsp::CompletionItemKind::kVariable }, { "inherited", KeywordCategory::kClassModifiers, "Inherited method call", lsp::CompletionItemKind::kKeyword }, // Access Modifiers { "public", KeywordCategory::kAccessModifiers, "Public access modifier", lsp::CompletionItemKind::kKeyword }, { "protected", KeywordCategory::kAccessModifiers, "Protected access modifier", lsp::CompletionItemKind::kKeyword }, { "private", KeywordCategory::kAccessModifiers, "Private access modifier", lsp::CompletionItemKind::kKeyword }, { "published", KeywordCategory::kAccessModifiers, "Published access modifier", lsp::CompletionItemKind::kKeyword }, // Property Accessors { "read", KeywordCategory::kPropertyAccessors, "Property read accessor", lsp::CompletionItemKind::kKeyword }, { "write", KeywordCategory::kPropertyAccessors, "Property write accessor", lsp::CompletionItemKind::kKeyword }, // Constructors { "create", KeywordCategory::kConstructors, "Constructor method", lsp::CompletionItemKind::kConstructor }, { "destroy", KeywordCategory::kConstructors, "Destructor method", lsp::CompletionItemKind::kMethod }, { "operator", KeywordCategory::kConstructors, "Operator overload", lsp::CompletionItemKind::kOperator }, // Variable Modifiers { "external", KeywordCategory::kVariableModifiers, "External declaration", lsp::CompletionItemKind::kKeyword }, { "const", KeywordCategory::kVariableModifiers, "Constant declaration", lsp::CompletionItemKind::kConstant }, { "out", KeywordCategory::kVariableModifiers, "Output parameter", lsp::CompletionItemKind::kKeyword }, { "var", KeywordCategory::kVariableModifiers, "Variable declaration", lsp::CompletionItemKind::kVariable }, { "global", KeywordCategory::kVariableModifiers, "Global variable", lsp::CompletionItemKind::kVariable }, { "static", KeywordCategory::kVariableModifiers, "Static variable", lsp::CompletionItemKind::kVariable }, // Conditionals { "if", KeywordCategory::kConditionals, "If statement", lsp::CompletionItemKind::kKeyword }, { "else", KeywordCategory::kConditionals, "Else clause", lsp::CompletionItemKind::kKeyword }, { "then", KeywordCategory::kConditionals, "Then clause", lsp::CompletionItemKind::kKeyword }, { "case", KeywordCategory::kConditionals, "Case statement", lsp::CompletionItemKind::kKeyword }, { "of", KeywordCategory::kConditionals, "Of clause", lsp::CompletionItemKind::kKeyword }, // Loops { "for", KeywordCategory::kLoops, "For loop", lsp::CompletionItemKind::kKeyword }, { "while", KeywordCategory::kLoops, "While loop", lsp::CompletionItemKind::kKeyword }, { "do", KeywordCategory::kLoops, "Do clause", lsp::CompletionItemKind::kKeyword }, { "downto", KeywordCategory::kLoops, "Downto clause", lsp::CompletionItemKind::kKeyword }, { "step", KeywordCategory::kLoops, "Step clause", lsp::CompletionItemKind::kKeyword }, { "until", KeywordCategory::kLoops, "Until clause", lsp::CompletionItemKind::kKeyword }, { "repeat", KeywordCategory::kLoops, "Repeat loop", lsp::CompletionItemKind::kKeyword }, { "to", KeywordCategory::kLoops, "To clause", lsp::CompletionItemKind::kKeyword }, // Branch Control { "break", KeywordCategory::kBranchControl, "Break statement", lsp::CompletionItemKind::kKeyword }, { "continue", KeywordCategory::kBranchControl, "Continue statement", lsp::CompletionItemKind::kKeyword }, { "goto", KeywordCategory::kBranchControl, "Goto statement", lsp::CompletionItemKind::kKeyword }, { "label", KeywordCategory::kBranchControl, "Label declaration", lsp::CompletionItemKind::kKeyword }, { "exit", KeywordCategory::kBranchControl, "Exit statement", lsp::CompletionItemKind::kKeyword }, // Return Control { "return", KeywordCategory::kReturnControl, "Return statement", lsp::CompletionItemKind::kKeyword }, { "debugreturn", KeywordCategory::kReturnControl, "Debug return statement", lsp::CompletionItemKind::kKeyword }, { "debugrunenv", KeywordCategory::kReturnControl, "Debug run environment", lsp::CompletionItemKind::kKeyword }, { "debugrunenvdo", KeywordCategory::kReturnControl, "Debug run environment do", lsp::CompletionItemKind::kKeyword }, // Block Control { "begin", KeywordCategory::kBlockControl, "Begin block", lsp::CompletionItemKind::kKeyword }, { "end", KeywordCategory::kBlockControl, "End block", lsp::CompletionItemKind::kKeyword }, { "with", KeywordCategory::kBlockControl, "With statement", lsp::CompletionItemKind::kKeyword }, // References { "weakref", KeywordCategory::kReferences, "Weak reference", lsp::CompletionItemKind::kKeyword }, { "autoref", KeywordCategory::kReferences, "Auto reference", lsp::CompletionItemKind::kKeyword }, // Namespace { "namespace", KeywordCategory::kNamespace, "Namespace declaration", lsp::CompletionItemKind::kModule }, // Exceptions { "except", KeywordCategory::kExceptions, "Exception handling", lsp::CompletionItemKind::kKeyword }, { "raise", KeywordCategory::kExceptions, "Raise exception", lsp::CompletionItemKind::kKeyword }, { "try", KeywordCategory::kExceptions, "Try block", lsp::CompletionItemKind::kKeyword }, { "finally", KeywordCategory::kExceptions, "Finally block", lsp::CompletionItemKind::kKeyword }, { "exceptobject", KeywordCategory::kExceptions, "Exception object", lsp::CompletionItemKind::kKeyword }, // Logical Operators { "and", KeywordCategory::kLogicalOperators, "Logical AND", lsp::CompletionItemKind::kOperator }, { "in", KeywordCategory::kLogicalOperators, "In operator", lsp::CompletionItemKind::kOperator }, { "is", KeywordCategory::kLogicalOperators, "Is operator", lsp::CompletionItemKind::kOperator }, { "not", KeywordCategory::kLogicalOperators, "Logical NOT", lsp::CompletionItemKind::kOperator }, { "or", KeywordCategory::kLogicalOperators, "Logical OR", lsp::CompletionItemKind::kOperator }, // Arithmetic Operators { "div", KeywordCategory::kArithmeticOperators, "Integer division", lsp::CompletionItemKind::kOperator }, { "mod", KeywordCategory::kArithmeticOperators, "Modulo operation", lsp::CompletionItemKind::kOperator }, // Bitwise Operators { "ror", KeywordCategory::kBitwiseOperators, "Rotate right", lsp::CompletionItemKind::kOperator }, { "rol", KeywordCategory::kBitwiseOperators, "Rotate left", lsp::CompletionItemKind::kOperator }, { "shr", KeywordCategory::kBitwiseOperators, "Shift right", lsp::CompletionItemKind::kOperator }, { "shl", KeywordCategory::kBitwiseOperators, "Shift left", lsp::CompletionItemKind::kOperator }, // Set Operators { "union", KeywordCategory::kSetOperators, "Set union", lsp::CompletionItemKind::kOperator }, { "minus", KeywordCategory::kSetOperators, "Set difference", lsp::CompletionItemKind::kOperator }, { "union2", KeywordCategory::kSetOperators, "Set union (alternative)", lsp::CompletionItemKind::kOperator }, // SQL Control { "select", KeywordCategory::kSqlControl, "SQL SELECT", lsp::CompletionItemKind::kKeyword }, { "vselect", KeywordCategory::kSqlControl, "Virtual SELECT", lsp::CompletionItemKind::kKeyword }, { "sselect", KeywordCategory::kSqlControl, "Special SELECT", lsp::CompletionItemKind::kKeyword }, { "update", KeywordCategory::kSqlControl, "SQL UPDATE", lsp::CompletionItemKind::kKeyword }, { "delete", KeywordCategory::kSqlControl, "SQL DELETE", lsp::CompletionItemKind::kKeyword }, { "mselect", KeywordCategory::kSqlControl, "Multiple SELECT", lsp::CompletionItemKind::kKeyword }, // SQL Keywords { "sqlin", KeywordCategory::kSqlKeywords, "SQL IN", lsp::CompletionItemKind::kKeyword }, { "from", KeywordCategory::kSqlKeywords, "SQL FROM", lsp::CompletionItemKind::kKeyword }, { "where", KeywordCategory::kSqlKeywords, "SQL WHERE", lsp::CompletionItemKind::kKeyword }, { "group", KeywordCategory::kSqlKeywords, "SQL GROUP", lsp::CompletionItemKind::kKeyword }, { "by", KeywordCategory::kSqlKeywords, "SQL BY", lsp::CompletionItemKind::kKeyword }, { "order", KeywordCategory::kSqlKeywords, "SQL ORDER", lsp::CompletionItemKind::kKeyword }, { "distinct", KeywordCategory::kSqlKeywords, "SQL DISTINCT", lsp::CompletionItemKind::kKeyword }, { "join", KeywordCategory::kSqlKeywords, "SQL JOIN", lsp::CompletionItemKind::kKeyword }, // SQL Operators (note: some overlap with logical operators) { "on", KeywordCategory::kSqlOperators, "SQL ON", lsp::CompletionItemKind::kOperator }, { "like", KeywordCategory::kSqlOperators, "SQL LIKE", lsp::CompletionItemKind::kOperator }, // Calling Conventions { "cdecl", KeywordCategory::kCallingConventions, "C calling convention", lsp::CompletionItemKind::kKeyword }, { "pascal", KeywordCategory::kCallingConventions, "Pascal calling convention", lsp::CompletionItemKind::kKeyword }, { "stdcall", KeywordCategory::kCallingConventions, "Standard calling convention", lsp::CompletionItemKind::kKeyword }, { "safecall", KeywordCategory::kCallingConventions, "Safe calling convention", lsp::CompletionItemKind::kKeyword }, { "fastcall", KeywordCategory::kCallingConventions, "Fast calling convention", lsp::CompletionItemKind::kKeyword }, { "register", KeywordCategory::kCallingConventions, "Register calling convention", lsp::CompletionItemKind::kKeyword }, // System Keywords { "setuid", KeywordCategory::kSystemKeywords, "Set user ID", lsp::CompletionItemKind::kKeyword }, { "sudo", KeywordCategory::kSystemKeywords, "Super user do", lsp::CompletionItemKind::kKeyword }, // Builtin Variables { "paramcount", KeywordCategory::kBuiltinVariables, "Parameter count", lsp::CompletionItemKind::kVariable }, { "realparamcount", KeywordCategory::kBuiltinVariables, "Real parameter count", lsp::CompletionItemKind::kVariable }, { "params", KeywordCategory::kBuiltinVariables, "Parameters array", lsp::CompletionItemKind::kVariable }, { "system", KeywordCategory::kBuiltinVariables, "System variable", lsp::CompletionItemKind::kVariable }, { "tslassigning", KeywordCategory::kBuiltinVariables, "TSL assigning", lsp::CompletionItemKind::kVariable }, { "likeeps", KeywordCategory::kBuiltinVariables, "Like EPS", lsp::CompletionItemKind::kVariable }, { "likeepsrate", KeywordCategory::kBuiltinVariables, "Like EPS rate", lsp::CompletionItemKind::kVariable }, // Builtin Functions { "echo", KeywordCategory::kBuiltinFunctions, "Echo function", lsp::CompletionItemKind::kFunction }, { "mtic", KeywordCategory::kBuiltinFunctions, "MTIC function", lsp::CompletionItemKind::kFunction }, { "mtoc", KeywordCategory::kBuiltinFunctions, "MTOC function", lsp::CompletionItemKind::kFunction }, // Boolean Constants { "false", KeywordCategory::kBooleanConstants, "Boolean false", lsp::CompletionItemKind::kConstant }, { "true", KeywordCategory::kBooleanConstants, "Boolean true", lsp::CompletionItemKind::kConstant }, // Math Constants { "inf", KeywordCategory::kMathConstants, "Infinity", lsp::CompletionItemKind::kConstant }, { "nan", KeywordCategory::kMathConstants, "Not a Number", lsp::CompletionItemKind::kConstant }, // Null Constants { "nil", KeywordCategory::kNullConstants, "Null value", lsp::CompletionItemKind::kConstant } }; keyword_set_.clear(); for (const auto& keyword : keywords_) keyword_set_.insert(ToLowerCase(keyword.keyword)); initialized_ = true; } std::string TslKeywords::ToLowerCase(const std::string& str) { std::string result = str; std::transform(result.begin(), result.end(), result.begin(), ::tolower); return result; } bool TslKeywords::StartsWith(const std::string& str, const std::string& prefix) { if (prefix.length() > str.length()) return false; return str.compare(0, prefix.length(), prefix) == 0; } }