15 return std::ranges::count(line, open) - std::ranges::count(line, close);
20 const std::size_t string_begin = line.find_first_not_of(
" \t");
21 if (std::string::npos != string_begin)
23 const std::size_t string_end = line.find_last_not_of(
" \t");
24 line = line.substr(string_begin, string_end - string_begin + 1);
30 std::vector<std::string> output;
33 std::ranges::transform(
keywords, std::back_inserter(output), [](
const auto& string_view) {
34 return std::string(string_view);
37 return std::string(string_view);
39 std::ranges::transform(
Language::operators, std::back_inserter(output), [](
const auto& string_view) {
40 return std::string(string_view);
42 std::ranges::transform(std::ranges::views::keys(
Builtins::builtins), std::back_inserter(output), [](
const auto&
string) {
46 output.emplace_back(
"and");
47 output.emplace_back(
"or");
54 using namespace replxx;
55 using K = std::string;
56 using V = Replxx::Color;
58 std::vector<std::pair<K, V>> output;
61 std::ranges::transform(
keywords, std::back_inserter(output), [](
const auto& string_view) {
62 return std::make_pair(std::string(string_view), Replxx::Color::BRIGHTRED);
65 return std::make_pair(std::string(string_view), Replxx::Color::GREEN);
67 std::ranges::transform(
Language::operators, std::back_inserter(output), [](
const auto& string_view) {
68 auto safe_op = std::string(string_view);
69 if (
const auto it = safe_op.find_first_of(R
"(-+=/*<>[]()?")"); it != std::string::npos)
70 safe_op.insert(it, "\\");
71 return std::make_pair(safe_op, Replxx::Color::BRIGHTBLUE);
73 std::ranges::transform(std::ranges::views::keys(
Builtins::builtins), std::back_inserter(output), [](
const auto&
string) {
74 return std::make_pair(
string, Replxx::Color::GREEN);
77 output.emplace_back(
"and", Replxx::Color::BRIGHTBLUE);
78 output.emplace_back(
"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) == 0)
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)
152 colors.at(pos + i) = color;
155 str = match.suffix();
160 replxx::Replxx::hints_t
hookHint(
const std::vector<std::string>& words,
const std::string& context,
int& length, replxx::Replxx::Color& color)
162 replxx::Replxx::hints_t hints;
165 const std::size_t utf8_context_len = contextLen(context);
166 const std::size_t prefix_len = context.size() - utf8_context_len;
167 length =
static_cast<int>(codepointLength(context.substr(prefix_len, utf8_context_len)));
168 const std::string prefix = context.substr(prefix_len);
170 if (prefix.size() >= 2 || (!prefix.empty() && prefix.at(0) ==
'.'))
172 for (
const auto& e : words)
174 if (e.compare(0, prefix.size(), prefix) == 0)
175 hints.emplace_back(e.c_str());
179 if (hints.size() == 1)
180 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::array< std::string_view, 24 > operators
std::vector< std::string > getAllKeywords()
Compute a list of all the language keywords and builtins.
long countOpenEnclosures(const std::string &line, char open, char close)
Count the open enclosure and its counterpart: (), {}, [].
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)
void trimWhitespace(std::string &line)
Remove whitespaces at the start and end of a string.
constexpr std::array< std::string_view, 9 > keywords
List of available keywords in ArkScript.