15 std::vector<std::string> output;
18 std::ranges::transform(
keywords, std::back_inserter(output), [](
const auto& string_view) {
19 return std::string(string_view);
22 return std::string(string_view);
24 std::ranges::transform(
Language::operators, std::back_inserter(output), [](
const auto& string_view) {
25 return std::string(string_view);
27 std::ranges::transform(
28 std::ranges::views::keys(
Builtins::builtins) | std::ranges::views::filter([&output](
const auto& val) ->
bool {
29 return std::ranges::find(output, val) == output.end();
31 std::back_inserter(output), [](
const auto& string) {
44 using namespace replxx;
45 using K = std::string;
46 using V = Replxx::Color;
48 std::vector<std::pair<K, V>> output;
51 std::ranges::transform(
keywords, std::back_inserter(output), [](
const auto& string_view) {
52 return std::make_pair(std::string(string_view), Replxx::Color::BRIGHTRED);
55 return std::make_pair(std::string(string_view), Replxx::Color::GREEN);
57 std::ranges::transform(
Language::operators, std::back_inserter(output), [](
const auto& string_view) {
58 auto safe_op = std::string(string_view);
59 if (
const auto it = safe_op.find_first_of(R
"(-+=/*<>[]()?")"); it != std::string::npos)
60 safe_op.insert(it, "\\");
61 return std::make_pair(safe_op, Replxx::Color::BRIGHTBLUE);
63 std::ranges::transform(
64 std::ranges::views::keys(
Builtins::builtins) | std::ranges::views::filter([&output](
const auto& val) ->
bool {
65 return std::ranges::find_if(output, [&val](
const std::pair<K, V>& pair) ->
bool {
66 return pair.first == val;
69 std::back_inserter(output), [](
const auto& string) {
70 auto safe_op = string;
71 if (
const auto it = safe_op.find_first_of(R
"(-+=/*<>[]()?")"); it != std::string::npos)
72 safe_op.insert(it, "\\");
73 return std::make_pair(safe_op, Replxx::Color::GREEN);
76 output.emplace_back(
Language::And, Replxx::Color::BRIGHTBLUE);
77 output.emplace_back(
Language::Or, Replxx::Color::BRIGHTBLUE);
79 output.emplace_back(
"[\\-|+]?[0-9]+(\\.[0-9]+)?", Replxx::Color::YELLOW);
80 output.emplace_back(
"\".*\"", Replxx::Color::MAGENTA);
85 std::size_t codepointLength(
const std::string& str)
87 return std::accumulate(
91 [](
const std::size_t acc,
const char c) {
92 return acc + ((c & 0xc0) != 0x80);
96 std::size_t contextLen(
const std::string& prefix)
98 const std::string word_break =
" \t\n\r\v\f=+*&^%$#@!,./?<>;`~'\"[]{}()\\|";
99 std::size_t count = 0;
101 for (
const auto c : std::ranges::views::reverse(prefix))
103 if (word_break.find(c) != std::string::npos)
111 replxx::Replxx::completions_t
hookCompletion(
const std::vector<std::string>& words,
const std::string& context,
int& length)
113 replxx::Replxx::completions_t completions;
114 std::size_t utf8_context_len = contextLen(context);
115 std::size_t prefix_len = context.size() - utf8_context_len;
117 if (prefix_len > 0 && context[prefix_len - 1] ==
'\\')
123 length =
static_cast<int>(codepointLength(context.substr(prefix_len, utf8_context_len)));
125 const std::string prefix = context.substr(prefix_len);
126 for (
const auto& e : words)
128 if (e.starts_with(prefix))
129 completions.emplace_back(e.c_str());
135 void hookColor(
const std::vector<std::pair<std::string, replxx::Replxx::Color>>& words_colors,
const std::string& context, replxx::Replxx::colors_t&
colors)
138 for (
const auto& [regex, color] : words_colors)
141 std::string str = context;
144 while (std::regex_search(str, match, std::regex(regex)))
146 std::string c = match[0];
147 std::string prefix = match.prefix().str();
148 const std::size_t len = codepointLength(c);
150 pos += codepointLength(prefix);
151 for (std::size_t i = 0; i < len; ++i)
153 if (
colors.at(pos + i) == replxx::Replxx::Color::DEFAULT)
154 colors.at(pos + i) = color;
158 str = match.suffix();
163 replxx::Replxx::hints_t
hookHint(
const std::vector<std::string>& words,
const std::string& context,
int& length, replxx::Replxx::Color& color)
165 replxx::Replxx::hints_t hints;
168 const std::size_t utf8_context_len = contextLen(context);
169 const std::size_t prefix_len = context.size() - utf8_context_len;
170 length =
static_cast<int>(codepointLength(context.substr(prefix_len, utf8_context_len)));
171 const std::string prefix = context.substr(prefix_len);
173 if (prefix.size() >= 2 || (!prefix.empty() && prefix.at(0) ==
'.'))
175 for (
const auto& e : words)
177 if (e.compare(0, prefix.size(), prefix) == 0)
178 hints.emplace_back(e.c_str());
182 if (hints.size() == 1)
183 color = replxx::Replxx::Color::GREEN;
Host the declaration of all the ArkScript builtins.
Common code for the compiler.
ARK_API const std::vector< std::pair< std::string, Value > > builtins
constexpr std::array< std::string_view, 9 > listInstructions
constexpr std::string_view Apply
constexpr std::array< std::string_view, 24 > operators
constexpr std::string_view And
constexpr std::string_view Or
std::vector< std::string > getAllKeywords()
Compute a list of all the language keywords and builtins.
void hookColor(const std::vector< std::pair< std::string, replxx::Replxx::Color > > &words_colors, const std::string &context, replxx::Replxx::colors_t &colors)
constexpr std::array colors
replxx::Replxx::completions_t hookCompletion(const std::vector< std::string > &words, const std::string &context, int &length)
std::vector< std::pair< std::string, replxx::Replxx::Color > > getColorPerKeyword()
Compute a list of pairs (word -> color) to be used for coloration by the REPL.
replxx::Replxx::hints_t hookHint(const std::vector< std::string > &words, const std::string &context, int &length, replxx::Replxx::Color &color)
constexpr std::array< std::string_view, 9 > keywords
List of available keywords in ArkScript.