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

ArkScript-lang / Ark / 24308984941

12 Apr 2026 02:31PM UTC coverage: 93.813% (-0.1%) from 93.945%
24308984941

Pull #675

github

web-flow
Merge f2cc3512f into 2f53e79e3
Pull Request #675: chore: put force inline attributes so that critical methods of the VM are inlined and can be easily optimized by the compiler

70 of 70 new or added lines in 9 files covered. (100.0%)

12 existing lines in 6 files now uncovered.

9749 of 10392 relevant lines covered (93.81%)

591525.06 hits per line

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

95.76
/src/arkreactor/Compiler/AST/BaseParser.cpp
1
#include <Ark/Compiler/AST/BaseParser.hpp>
2
#include <Ark/Error/Exceptions.hpp>
3

4
#include <utility>
5
#include <algorithm>
6

7
#include <fmt/core.h>
8

9
namespace Ark::internal
10
{
11
    void BaseParser::registerNewLine(std::string::iterator it, std::size_t row)
82,489✔
12
    {
82,489✔
13
        // search for an existing new line position
14
        if (std::ranges::find_if(m_it_to_row, [it](const auto& pair) {
29,155,263✔
15
                return pair.first == it;
29,072,774✔
16
            }) != m_it_to_row.end())
82,489✔
17
            return;
5,180✔
18

19
        // if the mapping is empty, the loop while never hit, and we'll never insert anything
20
        if (m_it_to_row.empty())
77,309✔
21
        {
22
            m_it_to_row.emplace_back(it, row);
742✔
23
            return;
742✔
24
        }
25

26
        for (std::size_t i = 0, end = m_it_to_row.size(); i < end; ++i)
26,420,488✔
27
        {
28
            auto current_it = m_it_to_row[i].first;
26,420,488✔
29
            auto next_it = i + 1 < end ? m_it_to_row[i + 1].first : m_str.end();
26,420,488✔
30
            if (current_it < it && it < next_it)
26,420,488✔
31
            {
32
                m_it_to_row.insert(
153,134✔
33
                    m_it_to_row.begin() + static_cast<decltype(m_it_to_row)::difference_type>(i) + 1,
76,567✔
34
                    std::make_pair(it, row));
76,567✔
35
                break;
76,567✔
36
            }
37
        }
26,343,921✔
38
    }
82,489✔
39

40
    void BaseParser::next()
4,409,410✔
41
    {
4,409,410✔
42
        m_it = m_next_it;
4,409,410✔
43
        if (isEOF())
4,409,410✔
44
        {
45
            m_sym = utf8_char_t();  // reset sym to EOF
770✔
46
            return;
770✔
47
        }
48

49
        // getting a character from the stream
50
        auto [it, sym] = utf8_char_t::at(m_it, m_str.end());
4,408,640✔
51
        m_next_it = it;
4,408,640✔
52
        m_sym = sym;
4,408,640✔
53

54
        if (*m_it == '\n')
4,408,640✔
55
        {
56
            ++m_filepos.row;
82,489✔
57
            m_filepos.col = 0;
82,489✔
58
            registerNewLine(m_it, m_filepos.row);
82,489✔
59
        }
82,489✔
60
        else if (m_sym.isPrintable())
4,326,151✔
61
            m_filepos.col += m_sym.size();
4,326,143✔
62
    }
4,409,410✔
63

64
    void BaseParser::initParser(const std::string& filename, const std::string& code)
802✔
65
    {
802✔
66
        m_filename = filename;
802✔
67

68
        // if the input string is empty, raise an error
69
        if (code.empty())
802✔
70
        {
71
            m_sym = utf8_char_t();
1✔
72
            error("Expected symbol, got empty string", m_filepos);
1✔
73
        }
×
74

75
        m_str = code;
801✔
76
        m_it = m_next_it = m_str.begin();
801✔
77

78
        // otherwise, get the first symbol
79
        next();
801✔
80
    }
802✔
81

82
    void BaseParser::backtrack(const long n)
1,769,839✔
83
    {
1,769,839✔
84
        if (std::cmp_less(n, m_str.size()))
1,769,839✔
85
            m_it = m_str.begin() + n;
1,769,839✔
86
        else
87
            return;
×
88

89
        auto [it, sym] = utf8_char_t::at(m_it, m_str.end());
682,730,664✔
90
        m_next_it = it;
1,769,839✔
91
        m_sym = sym;
1,769,839✔
92

93
        // search for the nearest it < m_it in the map to know the line number
94
        for (const auto& [at, line] : m_it_to_row)
682,730,664✔
95
        {
96
            if (it <= at)
680,960,825✔
97
            {
98
                m_filepos.row = line - 1;
6,334✔
99
                break;
6,334✔
100
            }
101
        }
102
        // compute the position in the line
103
        const auto it_pos = static_cast<std::size_t>(std::distance(m_str.begin(), m_it));
1,769,839✔
104
        const std::string_view view { m_str.begin(), m_it };
1,769,839✔
105
        const auto nearest_newline_index = view.find_last_of('\n');
1,769,839✔
106
        if (nearest_newline_index != std::string_view::npos)
1,769,839✔
107
            m_filepos.col = it_pos - nearest_newline_index;
1,749,755✔
108
        else
109
            m_filepos.col = it_pos + 1;
20,084✔
110
    }
1,769,839✔
111

112
    FilePosition BaseParser::getCursor() const
2,250,154✔
113
    {
2,250,154✔
114
        return m_filepos;
2,250,154✔
115
    }
116

117
    CodeErrorContext BaseParser::generateErrorContextAtCurrentPosition() const
919,824✔
118
    {
919,824✔
119
        const auto [row, col] = getCursor();
919,824✔
120

121
        return CodeErrorContext(
919,824✔
122
            m_filename,
919,824✔
123
            // for additional contexts, the end position is useless
124
            FileSpan { .start = FilePos { .line = row, .column = col }, .end = std::nullopt });
2,759,472✔
UNCOV
125
    }
×
126

127
    void BaseParser::error(const std::string& error, const FilePosition start_at, const std::optional<CodeErrorContext>& additional_context) const
57✔
128
    {
57✔
129
        const auto [row, col] = getCursor();
171✔
130
        throw CodeError(
171✔
131
            error,
57✔
132
            CodeErrorContext(
57✔
133
                m_filename,
57✔
134
                FileSpan {
57✔
135
                    .start = FilePos { .line = start_at.row, .column = start_at.col },
57✔
136
                    .end = FilePos { .line = row, .column = col } }),
171✔
137
            additional_context);
57✔
138
    }
57✔
139

140
    void BaseParser::errorWithNextToken(const std::string& message, const std::optional<CodeErrorContext>& additional_context)
43✔
141
    {
43✔
142
        const auto filepos = getCursor();
43✔
143

144
        anyUntil(IsEither(IsInlineSpace, IsEither(IsChar('('), IsChar(')'))));
43✔
145
        error(message, filepos, additional_context);
43✔
146
    }
43✔
147

148
    void BaseParser::expectSuffixOrError(const char suffix, const std::string& context, const std::optional<CodeErrorContext>& additional_context)
68,706✔
149
    {
68,706✔
150
        if (!accept(IsChar(suffix)))
68,716✔
151
            errorWithNextToken(fmt::format("Missing '{}' {}", suffix, context), additional_context);
10✔
152
    }
68,706✔
153

154
    bool BaseParser::accept(const CharPred& t, std::string* s)
8,708,129✔
155
    {
8,708,129✔
156
        if (isEOF())
8,708,129✔
157
            return false;
836✔
158

159
        // return false if the predicate couldn't consume the symbol
160
        if (!t(m_sym.codepoint()))
8,707,293✔
161
            return false;
4,305,847✔
162
        // otherwise, add it to the string and go to the next symbol
163
        if (s != nullptr)
4,401,446✔
164
            *s += m_sym.c_str();
3,483,828✔
165

166
        next();
4,401,446✔
167
        return true;
4,401,446✔
168
    }
8,708,129✔
169

170
    bool BaseParser::expect(const CharPred& t, std::string* s)
7,163✔
171
    {
7,163✔
172
        // throw an error if the predicate couldn't consume the symbol
173
        if (!t(m_sym.codepoint()))
7,163✔
174
            error("Expected " + t.name, getCursor());
×
175
        // otherwise, add it to the string and go to the next symbol
176
        if (s != nullptr)
7,163✔
177
            *s += m_sym.c_str();
×
178
        next();
7,163✔
179
        return true;
7,163✔
180
    }
×
181

182
    std::string BaseParser::peek() const
6✔
183
    {
6✔
184
        return m_sym.c_str();
6✔
185
    }
×
186

187
    bool BaseParser::space(std::string* s)
797,273✔
188
    {
797,273✔
189
        if (accept(IsSpace))
797,273✔
190
        {
191
            if (s != nullptr)
222,498✔
192
                s->push_back(' ');
×
193
            // loop while there are still ' ' to consume
194
            while (accept(IsSpace))
369,943✔
195
                ;
196
            return true;
222,498✔
197
        }
198
        return false;
574,775✔
199
    }
797,273✔
200

201
    bool BaseParser::inlineSpace(std::string* s)
33,914✔
202
    {
33,914✔
203
        if (accept(IsInlineSpace))
33,914✔
204
        {
205
            if (s != nullptr)
3,190✔
206
                s->push_back(' ');
×
207
            // loop while there are still ' ' to consume
208
            while (accept(IsInlineSpace))
3,199✔
209
                ;
210
            return true;
3,190✔
211
        }
212
        return false;
30,724✔
213
    }
33,914✔
214

215
    bool BaseParser::comment(std::string* s)
829,927✔
216
    {
829,927✔
217
        if (accept(IsChar('#'), s))
829,927✔
218
        {
219
            while (accept(IsNot(IsChar('\n')), s))
1,250,589✔
220
                ;
221
            accept(IsChar('\n'), s);
36,345✔
222
            return true;
36,345✔
223
        }
224
        return false;
793,582✔
225
    }
829,927✔
226

227
    std::string BaseParser::spaceComment()
33,875✔
228
    {
33,875✔
229
        std::string s;
33,875✔
230

231
        inlineSpace();
33,875✔
232
        while (!isEOF() && comment(&s))
33,914✔
233
            inlineSpace();
39✔
234

235
        return s;
33,875✔
236
    }
33,875✔
237

238
    std::string BaseParser::newlineOrComment()
760,497✔
239
    {
760,497✔
240
        std::string s;
760,497✔
241

242
        space();
760,497✔
243
        while (!isEOF() && comment(&s))
796,803✔
244
            space();
36,306✔
245

246
        return s;
760,497✔
247
    }
760,497✔
248

249
    bool BaseParser::prefix(const char c)
545,554✔
250
    {
545,554✔
251
        if (!accept(IsChar(c)))
545,554✔
252
            return false;
291,232✔
253
        return true;
254,322✔
254
    }
545,554✔
255

256
    bool BaseParser::number(std::string* s)
263,002✔
257
    {
263,002✔
258
        if (accept(IsDigit, s))
263,002✔
259
        {
260
            // consume all the digits available,
261
            // stop when the symbol isn't a digit anymore
262
            while (accept(IsDigit, s))
84,041✔
263
                ;
264
            return true;
82,835✔
265
        }
266
        return false;
180,167✔
267
    }
263,002✔
268

269
    bool BaseParser::signedNumber(std::string* s)
262,830✔
270
    {
262,830✔
271
        accept(IsMinus, s);
262,830✔
272
        if (!number(s))
262,830✔
273
            return false;
180,167✔
274

275
        // (optional) floating part
276
        accept(IsChar('.'), s) && number(s);
82,663✔
277
        // (optional) scientific part
278
        if (accept(IsEither(IsChar('e'), IsChar('E')), s))
82,663✔
279
        {
280
            accept(IsEither(IsMinus, IsChar('+')), s);
42✔
281
            number(s);
42✔
282
        }
42✔
283

284
        return true;
82,663✔
285
    }
262,830✔
286

287
    bool BaseParser::hexNumber(unsigned int length, std::string* s)
11✔
288
    {
11✔
289
        while (length != 0)
67✔
290
        {
291
            if (!accept(IsHex, s))
58✔
292
                return false;
2✔
293
            --length;
56✔
294
        }
295
        return true;
9✔
296
    }
11✔
297

298
    bool BaseParser::name(std::string* s)
745,596✔
299
    {
745,596✔
300
        const auto alpha_symbols = IsEither(IsAlpha, IsSymbol);
745,596✔
301
        const auto alnum_symbols = IsEither(IsAlnum, IsSymbol);
745,596✔
302

303
        if (accept(alpha_symbols, s))
745,596✔
304
        {
305
            while (accept(alnum_symbols, s))
2,066,428✔
306
                ;
307
            return true;
519,019✔
308
        }
309
        return false;
226,577✔
310
    }
745,596✔
311

312
    bool BaseParser::sequence(const std::string& s)
176,916✔
313
    {
176,916✔
314
        return std::ranges::all_of(s, [this](const char c) {
354,128✔
315
            return accept(IsChar(c));
177,212✔
316
        });
×
317
    }
318

319
    bool BaseParser::packageName(std::string* s)
531✔
320
    {
531✔
321
        if (accept(IsAlnum, s))
531✔
322
        {
323
            while (accept(IsEither(IsAlnum, IsEither(IsChar('_'), IsChar('-'))), s))
2,682✔
324
                ;
325
            return true;
528✔
326
        }
327
        return false;
3✔
328
    }
531✔
329

330
    bool BaseParser::anyUntil(const CharPred& delim, std::string* s)
43✔
331
    {
43✔
332
        if (accept(IsNot(delim), s))
43✔
333
        {
334
            while (accept(IsNot(delim), s))
1,270✔
335
                ;
336
            return true;
10✔
337
        }
338
        return false;
33✔
339
    }
43✔
340

341
    bool BaseParser::oneOf(const std::initializer_list<std::string> words, std::string* s)
360,253✔
342
    {
360,253✔
343
        std::string buffer;
360,253✔
344
        if (!name(&buffer))
360,253✔
345
            return false;
9,718✔
346

347
        if (s)
350,535✔
348
            *s = buffer;
62,369✔
349

350
        return std::ranges::any_of(words, [&buffer](const std::string& word) {
804,137✔
351
            return word == buffer;
453,602✔
352
        });
353
    }
360,253✔
354
}
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

© 2026 Coveralls, Inc