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

ArkScript-lang / Ark / 17100280902

20 Aug 2025 01:45PM UTC coverage: 87.348% (-0.08%) from 87.426%
17100280902

push

github

SuperFola
feat(tests): adding parser tests to improve coverage

4 of 4 new or added lines in 1 file covered. (100.0%)

171 existing lines in 10 files now uncovered.

7553 of 8647 relevant lines covered (87.35%)

129750.62 hits per line

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

96.2
/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)
31,496✔
12
    {
31,496✔
13
        // search for an existing new line position
14
        if (std::ranges::find_if(m_it_to_row, [it](const auto& pair) {
6,189,928✔
15
                return pair.first == it;
6,158,432✔
16
            }) != m_it_to_row.end())
31,496✔
17
            return;
1,414✔
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())
30,082✔
21
        {
22
            m_it_to_row.emplace_back(it, row);
471✔
23
            return;
471✔
24
        }
25

26
        for (std::size_t i = 0, end = m_it_to_row.size(); i < end; ++i)
5,989,877✔
27
        {
28
            auto current_it = m_it_to_row[i].first;
5,960,266✔
29
            auto next_it = i + 1 < end ? m_it_to_row[i + 1].first : m_str.end();
5,960,266✔
30
            if (current_it < it && it < next_it)
5,960,266✔
31
            {
32
                m_it_to_row.insert(
59,222✔
33
                    m_it_to_row.begin() + static_cast<decltype(m_it_to_row)::difference_type>(i) + 1,
29,611✔
34
                    std::make_pair(it, row));
29,611✔
35
                break;
29,611✔
36
            }
37
        }
5,960,266✔
38
    }
31,496✔
39

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

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

54
        if (*m_it == '\n')
1,773,203✔
55
        {
56
            ++m_filepos.row;
31,496✔
57
            m_filepos.col = 0;
31,496✔
58
            registerNewLine(m_it, m_filepos.row);
31,496✔
59
        }
31,496✔
60
        else if (m_sym.isPrintable())
1,741,707✔
61
            m_filepos.col += m_sym.size();
1,741,707✔
62
    }
1,773,697✔
63

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

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

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

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

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

89
        auto [it, sym] = utf8_char_t::at(m_it, m_str.end());
128,937,814✔
90
        m_next_it = it;
622,065✔
91
        m_sym = sym;
622,065✔
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)
128,939,494✔
95
        {
96
            if (it <= at)
128,315,749✔
97
            {
98
                m_filepos.row = line - 1;
1,680✔
99
                break;
1,680✔
100
            }
101
        }
128,315,749✔
102
        // compute the position in the line
103
        const auto it_pos = static_cast<std::size_t>(std::distance(m_str.begin(), m_it));
622,065✔
104
        const std::string_view view { m_str.begin(), m_it };
622,065✔
105
        const auto nearest_newline_index = view.find_last_of('\n');
622,065✔
106
        if (nearest_newline_index != std::string_view::npos)
622,065✔
107
            m_filepos.col = it_pos - nearest_newline_index;
610,655✔
108
        else
109
            m_filepos.col = it_pos + 1;
11,410✔
110
    }
622,065✔
111

112
    FilePosition BaseParser::getCursor() const
881,544✔
113
    {
881,544✔
114
        return m_filepos;
881,544✔
115
    }
116

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

121
        return CodeErrorContext(
327,872✔
122
            m_filename,
327,872✔
123
            // for additional contexts, the end position is useless
124
            FileSpan { .start = FilePos { .line = row, .column = col }, .end = std::nullopt });
983,616✔
125
    }
327,872✔
126

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

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

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

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

154
    bool BaseParser::accept(const CharPred& t, std::string* s)
3,563,573✔
155
    {
3,563,573✔
156
        if (isEOF())
3,563,573✔
157
            return false;
562✔
158

159
        // return false if the predicate couldn't consume the symbol
160
        if (!t(m_sym.codepoint()))
3,563,011✔
161
            return false;
1,792,231✔
162
        // otherwise, add it to the string and go to the next symbol
163
        if (s != nullptr)
1,770,780✔
164
            *s += m_sym.c_str();
1,405,727✔
165

166
        next();
1,770,780✔
167
        return true;
1,770,780✔
168
    }
3,563,573✔
169

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

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

187
    bool BaseParser::space(std::string* s)
331,425✔
188
    {
331,425✔
189
        if (accept(IsSpace))
331,425✔
190
        {
191
            if (s != nullptr)
120,933✔
UNCOV
192
                s->push_back(' ');
×
193
            // loop while there are still ' ' to consume
194
            while (accept(IsSpace))
162,584✔
195
                ;
196
            return true;
120,933✔
197
        }
198
        return false;
210,492✔
199
    }
331,425✔
200

201
    bool BaseParser::inlineSpace(std::string* s)
13,413✔
202
    {
13,413✔
203
        if (accept(IsInlineSpace))
13,413✔
204
        {
205
            if (s != nullptr)
890✔
UNCOV
206
                s->push_back(' ');
×
207
            // loop while there are still ' ' to consume
208
            while (accept(IsInlineSpace))
899✔
209
                ;
210
            return true;
890✔
211
        }
212
        return false;
12,523✔
213
    }
13,413✔
214

215
    bool BaseParser::comment(std::string* s)
344,009✔
216
    {
344,009✔
217
        if (accept(IsChar('#'), s))
344,009✔
218
        {
219
            while (accept(IsNot(IsChar('\n')), s))
470,301✔
220
                ;
221
            accept(IsChar('\n'), s);
14,537✔
222
            return true;
14,537✔
223
        }
224
        return false;
329,472✔
225
    }
344,009✔
226

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

231
        inlineSpace();
13,374✔
232
        while (!isEOF() && comment(&s))
13,413✔
233
            inlineSpace();
39✔
234

235
        return s;
13,374✔
236
    }
13,374✔
237

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

242
        space();
316,608✔
243
        while (!isEOF() && comment(&s))
331,106✔
244
            space();
14,498✔
245

246
        return s;
316,608✔
247
    }
316,608✔
248

249
    bool BaseParser::prefix(const char c)
194,873✔
250
    {
194,873✔
251
        if (!accept(IsChar(c)))
194,873✔
252
            return false;
100,986✔
253
        return true;
93,887✔
254
    }
194,873✔
255

256
    bool BaseParser::number(std::string* s)
133,467✔
257
    {
133,467✔
258
        if (accept(IsDigit, s))
133,467✔
259
        {
260
            // consume all the digits available,
261
            // stop when the symbol isn't a digit anymore
262
            while (accept(IsDigit, s))
71,241✔
263
                ;
264
            return true;
70,594✔
265
        }
266
        return false;
62,873✔
267
    }
133,467✔
268

269
    bool BaseParser::signedNumber(std::string* s)
133,379✔
270
    {
133,379✔
271
        accept(IsMinus, s);
133,379✔
272
        if (!number(s))
133,379✔
273
            return false;
62,873✔
274

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

284
        return true;
70,506✔
285
    }
133,379✔
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)
267,891✔
299
    {
267,891✔
300
        const auto alpha_symbols = IsEither(IsAlpha, IsSymbol);
267,891✔
301
        const auto alnum_symbols = IsEither(IsAlnum, IsSymbol);
267,891✔
302

303
        if (accept(alpha_symbols, s))
267,891✔
304
        {
305
            while (accept(alnum_symbols, s))
826,437✔
306
                ;
307
            return true;
181,633✔
308
        }
309
        return false;
86,258✔
310
    }
267,891✔
311

312
    bool BaseParser::sequence(const std::string& s)
61,380✔
313
    {
61,380✔
314
        return std::ranges::all_of(s, [this](const char c) {
122,908✔
315
            return accept(IsChar(c));
61,528✔
UNCOV
316
        });
×
317
    }
318

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

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

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

347
        if (s)
123,067✔
348
            *s = buffer;
21,977✔
349

350
        return std::ranges::any_of(words, [&buffer](const std::string& word) {
282,344✔
351
            return word == buffer;
159,277✔
352
        });
353
    }
132,515✔
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