• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ArkScript-lang / Ark / 12991909010

27 Jan 2025 03:04PM UTC coverage: 78.302% (-1.1%) from 79.433%
12991909010

push

github

SuperFola
refactor(repl): move keyword and color per keyword collection outside the REPL class

0 of 38 new or added lines in 1 file covered. (0.0%)

5781 of 7383 relevant lines covered (78.3%)

29645.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

9.24
/src/arkscript/REPL/Utils.cpp
1
#include <CLI/REPL/Utils.hpp>
2

3
#include <regex>
4
#include <algorithm>
5
#include <numeric>
6
#include <ranges>
7

8
#include <Ark/Builtins/Builtins.hpp>
9
#include <Ark/Compiler/Common.hpp>
10

11
namespace Ark::internal
12
{
13
    long countOpenEnclosures(const std::string& line, const char open, const char close)
12✔
14
    {
12✔
15
        return std::ranges::count(line, open) - std::ranges::count(line, close);
12✔
16
    }
17

18
    void trimWhitespace(std::string& line)
8✔
19
    {
8✔
20
        const std::size_t string_begin = line.find_first_not_of(" \t");
8✔
21
        if (std::string::npos != string_begin)
8✔
22
        {
23
            const std::size_t string_end = line.find_last_not_of(" \t");
8✔
24
            line = line.substr(string_begin, string_end - string_begin + 1);
8✔
25
        }
8✔
26
    }
8✔
27

NEW
28
    std::vector<std::string> getAllKeywords()
×
NEW
29
    {
×
NEW
30
        std::vector<std::string> output;
×
NEW
31
        output.reserve(keywords.size() + Language::listInstructions.size() + Language::operators.size() + Builtins::builtins.size() + 2);
×
NEW
32
        for (auto keyword : keywords)
×
NEW
33
            output.emplace_back(keyword);
×
NEW
34
        for (auto inst : Language::listInstructions)
×
NEW
35
            output.emplace_back(inst);
×
NEW
36
        for (auto op : Language::operators)
×
NEW
37
            output.emplace_back(op);
×
NEW
38
        for (const auto& builtin : std::ranges::views::keys(Builtins::builtins))
×
NEW
39
            output.push_back(builtin);
×
NEW
40
        output.emplace_back("and");
×
NEW
41
        output.emplace_back("or");
×
42

NEW
43
        return output;
×
NEW
44
    }
×
45

NEW
46
    std::vector<std::pair<std::string, replxx::Replxx::Color>> getColorPerKeyword()
×
NEW
47
    {
×
48
        using namespace replxx;
49

NEW
50
        std::vector<std::pair<std::string, Replxx::Color>> output;
×
NEW
51
        output.reserve(keywords.size() + Language::listInstructions.size() + Language::operators.size() + Builtins::builtins.size() + 4);
×
NEW
52
        for (auto keyword : keywords)
×
NEW
53
            output.emplace_back(keyword, Replxx::Color::BRIGHTRED);
×
NEW
54
        for (auto inst : Language::listInstructions)
×
NEW
55
            output.emplace_back(inst, Replxx::Color::GREEN);
×
NEW
56
        for (auto op : Language::operators)
×
57
        {
NEW
58
            auto safe_op = std::string(op);
×
NEW
59
            if (const auto it = safe_op.find_first_of(R"(-+=/*<>[]()?")"); it != std::string::npos)
×
NEW
60
                safe_op.insert(it, "\\");
×
NEW
61
            output.emplace_back(safe_op, Replxx::Color::BRIGHTBLUE);
×
NEW
62
        }
×
NEW
63
        for (const auto& builtin : std::ranges::views::keys(Builtins::builtins))
×
NEW
64
            output.emplace_back(builtin, Replxx::Color::GREEN);
×
65

NEW
66
        output.emplace_back("and", Replxx::Color::BRIGHTBLUE);
×
NEW
67
        output.emplace_back("or", Replxx::Color::BRIGHTBLUE);
×
NEW
68
        output.emplace_back("[\\-|+]?[0-9]+(\\.[0-9]+)?", Replxx::Color::YELLOW);
×
NEW
69
        output.emplace_back("\".*\"", Replxx::Color::MAGENTA);
×
70

NEW
71
        return output;
×
NEW
72
    }
×
73

74
    std::size_t codepointLength(const std::string& str)
×
75
    {
×
76
        return std::accumulate(
×
77
            str.begin(),
×
78
            str.end(),
×
79
            std::size_t { 0 },
80
            [](const std::size_t acc, const char c) {
×
81
                return acc + ((c & 0xc0) != 0x80);
×
82
            });
83
    }
84

85
    std::size_t contextLen(const std::string& prefix)
×
86
    {
×
87
        const std::string word_break = " \t\n\r\v\f=+*&^%$#@!,./?<>;`~'\"[]{}()\\|";
×
88
        std::size_t count = 0;
×
89

90
        for (const auto c : std::ranges::views::reverse(prefix))
×
91
        {
92
            if (word_break.find(c) != std::string::npos)
×
93
                break;
×
94
            ++count;
×
95
        }
×
96

97
        return count;
×
98
    }
×
99

100
    replxx::Replxx::completions_t hookCompletion(const std::vector<std::string>& words, const std::string& context, int& length)
×
101
    {
×
102
        replxx::Replxx::completions_t completions;
×
103
        std::size_t utf8_context_len = contextLen(context);
×
104
        std::size_t prefix_len = context.size() - utf8_context_len;
×
105

106
        if (prefix_len > 0 && context[prefix_len - 1] == '\\')
×
107
        {
108
            --prefix_len;
×
109
            ++utf8_context_len;
×
110
        }
×
111

112
        length = static_cast<int>(codepointLength(context.substr(prefix_len, utf8_context_len)));
×
113

114
        const std::string prefix = context.substr(prefix_len);
×
115
        for (const auto& e : words)
×
116
        {
117
            if (e.starts_with(prefix) == 0)
×
118
                completions.emplace_back(e.c_str());
×
119
        }
×
120

121
        return completions;
×
122
    }
×
123

124
    void hookColor(const std::vector<std::pair<std::string, replxx::Replxx::Color>>& words_colors, const std::string& context, replxx::Replxx::colors_t& colors)
×
125
    {
×
126
        // highlight matching regex sequences
127
        for (const auto& [regex, color] : words_colors)
×
128
        {
129
            std::size_t pos = 0;
×
130
            std::string str = context;
×
131
            std::smatch match;
×
132

133
            while (std::regex_search(str, match, std::regex(regex)))
×
134
            {
135
                std::string c = match[0];
×
136
                std::string prefix = match.prefix().str();
×
137
                const std::size_t len = codepointLength(c);
×
138

139
                pos += codepointLength(prefix);
×
140
                for (std::size_t i = 0; i < len; ++i)
×
141
                    colors.at(pos + i) = color;
×
142

143
                pos += len;
×
144
                str = match.suffix();
×
145
            }
×
146
        }
×
147
    }
×
148

149
    replxx::Replxx::hints_t hookHint(const std::vector<std::string>& words, const std::string& context, int& length, replxx::Replxx::Color& color)
×
150
    {
×
151
        replxx::Replxx::hints_t hints;
×
152
        // only show hint if prefix is at least 'n' chars long
153
        // or if prefix begins with a specific character
154
        const std::size_t utf8_context_len = contextLen(context);
×
155
        const std::size_t prefix_len = context.size() - utf8_context_len;
×
156
        length = static_cast<int>(codepointLength(context.substr(prefix_len, utf8_context_len)));
×
157
        const std::string prefix = context.substr(prefix_len);
×
158

159
        if (prefix.size() >= 2 || (!prefix.empty() && prefix.at(0) == '.'))
×
160
        {
161
            for (const auto& e : words)
×
162
            {
163
                if (e.compare(0, prefix.size(), prefix) == 0)
×
164
                    hints.emplace_back(e.c_str());
×
165
            }
×
166
        }
×
167

168
        if (hints.size() == 1)
×
169
            color = replxx::Replxx::Color::GREEN;
×
170

171
        return hints;
×
172
    }
×
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc