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

ArkScript-lang / Ark / 14956573498

11 May 2025 02:06PM UTC coverage: 86.495% (+0.02%) from 86.474%
14956573498

Pull #535

github

web-flow
Merge 793b28a34 into 3f1c6e9e3
Pull Request #535: Feat/parsing errors context

208 of 230 new or added lines in 10 files covered. (90.43%)

3 existing lines in 2 files now uncovered.

6949 of 8034 relevant lines covered (86.49%)

79310.84 hits per line

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

96.28
/src/arkreactor/Compiler/AST/BaseParser.cpp
1
#include <Ark/Compiler/AST/BaseParser.hpp>
2
#include <Ark/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)
18,291✔
12
    {
18,291✔
13
        // search for an existing new line position
14
        if (std::ranges::find_if(m_it_to_row, [it](const auto& pair) {
2,429,983✔
15
                return pair.first == it;
2,411,692✔
16
            }) != m_it_to_row.end())
18,291✔
17
            return;
1,084✔
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())
17,207✔
21
        {
22
            m_it_to_row.emplace_back(it, row);
404✔
23
            return;
404✔
24
        }
25

26
        for (std::size_t i = 0, end = m_it_to_row.size(); i < end; ++i)
2,315,098✔
27
        {
28
            auto current_it = m_it_to_row[i].first;
2,298,295✔
29
            auto next_it = i + 1 < end ? m_it_to_row[i + 1].first : m_str.end();
2,298,295✔
30
            if (current_it < it && it < next_it)
2,298,295✔
31
            {
32
                m_it_to_row.insert(
33,606✔
33
                    m_it_to_row.begin() + static_cast<decltype(m_it_to_row)::difference_type>(i) + 1,
16,803✔
34
                    std::make_pair(it, row));
16,803✔
35
                break;
16,803✔
36
            }
37
        }
2,298,295✔
38
    }
18,291✔
39

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

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

54
        if (*m_it == '\n')
1,078,450✔
55
        {
56
            ++m_filepos.row;
18,291✔
57
            m_filepos.col = 0;
18,291✔
58
            registerNewLine(m_it, m_filepos.row);
18,291✔
59
        }
18,291✔
60
        else if (m_sym.isPrintable())
1,060,159✔
61
            m_filepos.col += m_sym.size();
1,060,159✔
62
    }
1,078,887✔
63

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

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

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

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

82
    void BaseParser::backtrack(const long n)
411,844✔
83
    {
411,844✔
84
        if (std::cmp_greater_equal(n, m_str.size()))
411,844✔
85
            return;
10✔
86

87
        m_it = m_str.begin() + n;
411,834✔
88
        auto [it, sym] = utf8_char_t::at(m_it, m_str.end());
54,333,372✔
89
        m_next_it = it;
411,834✔
90
        m_sym = sym;
411,834✔
91

92
        // search for the nearest it < m_it in the map to know the line number
93
        for (std::size_t i = 0, end = m_it_to_row.size(); i < end; ++i)
54,333,372✔
94
        {
95
            auto [at, line] = m_it_to_row[i];
53,922,830✔
96
            if (it <= at)
53,921,538✔
97
            {
98
                m_filepos.row = line - 1;
1,292✔
99
                break;
1,292✔
100
            }
101
        }
53,921,538✔
102
        // compute the position in the line
103
        std::string_view view = m_str;
411,834✔
104
        const auto it_pos = static_cast<std::size_t>(std::distance(m_str.begin(), m_it));
411,834✔
105
        view = view.substr(0, it_pos);
411,834✔
106
        const auto nearest_newline_index = view.find_last_of('\n');
411,834✔
107
        if (nearest_newline_index != std::string_view::npos)
411,834✔
108
            m_filepos.col = it_pos - nearest_newline_index;
403,109✔
109
        else
110
            m_filepos.col = it_pos + 1;
8,725✔
111
    }
411,844✔
112

113
    FilePosition BaseParser::getCursor() const
710,996✔
114
    {
710,996✔
115
        return m_filepos;
710,996✔
116
    }
117

118
    CodeErrorContext BaseParser::generateErrorContext(const std::string& expr)
217,444✔
119
    {
217,444✔
120
        const auto [row, col] = getCursor();
217,444✔
121

122
        return CodeErrorContext(
217,444✔
123
            m_filename,
217,444✔
124
            row,
217,444✔
125
            col,
217,444✔
126
            expr,
217,444✔
127
            m_sym);
217,444✔
128
    }
217,444✔
129

130
    void BaseParser::error(const std::string& error, std::string exp, const std::optional<CodeErrorContext>& additional_context)
42✔
131
    {
42✔
132
        const auto [row, col] = getCursor();
126✔
133
        throw CodeError(
126✔
134
            error,
42✔
135
            CodeErrorContext(m_filename, row, col, std::move(exp), m_sym),
42✔
136
            additional_context);
42✔
137
    }
84✔
138

139
    void BaseParser::errorWithNextToken(const std::string& message, const std::optional<CodeErrorContext>& additional_context)
34✔
140
    {
34✔
141
        const auto pos = getCount();
34✔
142
        std::string next_token;
34✔
143

144
        anyUntil(IsEither(IsInlineSpace, IsEither(IsChar('('), IsChar(')'))), &next_token);
34✔
145
        backtrack(pos);
34✔
146

147
        error(message, next_token, additional_context);
34✔
148
    }
34✔
149

150
    void BaseParser::expectSuffixOrError(const char suffix, const std::string& context, const std::optional<CodeErrorContext>& additional_context)
16,255✔
151
    {
16,255✔
152
        if (!accept(IsChar(suffix)))
16,263✔
153
            errorWithNextToken(fmt::format("Missing '{}' {}", suffix, context), additional_context);
8✔
154
    }
16,255✔
155

156
    bool BaseParser::accept(const CharPred& t, std::string* s)
2,396,293✔
157
    {
2,396,293✔
158
        if (isEOF())
2,396,293✔
159
            return false;
504✔
160

161
        // return false if the predicate couldn't consume the symbol
162
        if (!t(m_sym.codepoint()))
2,395,789✔
163
            return false;
1,318,640✔
164
        // otherwise, add it to the string and go to the next symbol
165
        if (s != nullptr)
1,077,149✔
166
            *s += m_sym.c_str();
819,554✔
167

168
        next();
1,077,149✔
169
        return true;
1,077,149✔
170
    }
2,396,293✔
171

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

184
    std::string BaseParser::peek() const
4✔
185
    {
4✔
186
        return m_sym.c_str();
4✔
NEW
187
    }
×
188

189
    bool BaseParser::space(std::string* s)
241,358✔
190
    {
241,358✔
191
        if (accept(IsSpace))
241,358✔
192
        {
193
            if (s != nullptr)
102,043✔
194
                s->push_back(' ');
×
195
            // loop while there are still ' ' to consume
196
            while (accept(IsSpace))
129,863✔
197
                ;
198
            return true;
102,043✔
199
        }
200
        return false;
139,315✔
201
    }
241,358✔
202

203
    bool BaseParser::inlineSpace(std::string* s)
8,168✔
204
    {
8,168✔
205
        if (accept(IsInlineSpace))
8,168✔
206
        {
207
            if (s != nullptr)
653✔
208
                s->push_back(' ');
×
209
            // loop while there are still ' ' to consume
210
            while (accept(IsInlineSpace))
664✔
211
                ;
212
            return true;
653✔
213
        }
214
        return false;
7,515✔
215
    }
8,168✔
216

217
    bool BaseParser::comment(std::string* s)
248,836✔
218
    {
248,836✔
219
        if (accept(IsChar('#'), s))
248,836✔
220
        {
221
            while (accept(IsNot(IsChar('\n')), s))
240,274✔
222
                ;
223
            accept(IsChar('\n'), s);
7,190✔
224
            return true;
7,190✔
225
        }
226
        return false;
241,646✔
227
    }
248,836✔
228

229
    bool BaseParser::spaceComment(std::string* s)
8,127✔
230
    {
8,127✔
231
        bool matched = false;
8,127✔
232

233
        inlineSpace();
8,127✔
234
        while (!isEOF() && comment(s))
8,168✔
235
        {
236
            inlineSpace();
41✔
237
            matched = true;
41✔
238
        }
239

240
        return matched;
8,127✔
241
    }
8,127✔
242

243
    bool BaseParser::newlineOrComment(std::string* s)
233,958✔
244
    {
233,958✔
245
        bool matched = false;
233,958✔
246

247
        space();
233,958✔
248
        while (!isEOF() && comment(s))
241,107✔
249
        {
250
            space();
7,149✔
251
            matched = true;
7,149✔
252
        }
253

254
        return matched;
233,958✔
255
    }
233,958✔
256

257
    bool BaseParser::prefix(const char c)
128,797✔
258
    {
128,797✔
259
        if (!accept(IsChar(c)))
128,797✔
260
            return false;
69,942✔
261
        return true;
58,855✔
262
    }
128,797✔
263

264
    bool BaseParser::number(std::string* s)
110,900✔
265
    {
110,900✔
266
        if (accept(IsDigit, s))
110,900✔
267
        {
268
            // consume all the digits available,
269
            // stop when the symbol isn't a digit anymore
270
            while (accept(IsDigit, s))
69,933✔
271
                ;
272
            return true;
69,393✔
273
        }
274
        return false;
41,507✔
275
    }
110,900✔
276

277
    bool BaseParser::signedNumber(std::string* s)
110,827✔
278
    {
110,827✔
279
        accept(IsMinus, s);
110,827✔
280
        if (!number(s))
110,827✔
281
            return false;
41,507✔
282

283
        // (optional) floating part
284
        accept(IsChar('.'), s) && number(s);
69,320✔
285
        // (optional) scientific part
286
        if (accept(IsEither(IsChar('e'), IsChar('E')), s))
69,320✔
287
        {
288
            accept(IsEither(IsMinus, IsChar('+')), s);
5✔
289
            number(s);
5✔
290
        }
5✔
291

292
        return true;
69,320✔
293
    }
110,827✔
294

295
    bool BaseParser::hexNumber(unsigned int length, std::string* s)
8✔
296
    {
8✔
297
        while (length != 0)
56✔
298
        {
299
            if (!accept(IsHex, s))
48✔
300
                return false;
×
301
            --length;
48✔
302
        }
303
        return true;
8✔
304
    }
8✔
305

306
    bool BaseParser::name(std::string* s)
171,567✔
307
    {
171,567✔
308
        const auto alpha_symbols = IsEither(IsAlpha, IsSymbol);
171,567✔
309
        const auto alnum_symbols = IsEither(IsAlnum, IsSymbol);
171,567✔
310

311
        if (accept(alpha_symbols, s))
171,567✔
312
        {
313
            while (accept(alnum_symbols, s))
484,674✔
314
                ;
315
            return true;
121,011✔
316
        }
317
        return false;
50,556✔
318
    }
171,567✔
319

320
    bool BaseParser::sequence(const std::string& s)
40,476✔
321
    {
40,476✔
322
        return std::ranges::all_of(s, [this](const char c) {
81,098✔
323
            return accept(IsChar(c));
40,622✔
324
        });
×
325
    }
326

327
    bool BaseParser::packageName(std::string* s)
279✔
328
    {
279✔
329
        if (accept(IsAlnum, s))
279✔
330
        {
331
            while (accept(IsEither(IsAlnum, IsEither(IsChar('_'), IsChar('-'))), s))
1,628✔
332
                ;
333
            return true;
276✔
334
        }
335
        return false;
3✔
336
    }
279✔
337

338
    bool BaseParser::anyUntil(const CharPred& delim, std::string* s)
34✔
339
    {
34✔
340
        if (accept(IsNot(delim), s))
34✔
341
        {
342
            while (accept(IsNot(delim), s))
1,224✔
343
                ;
344
            return true;
8✔
345
        }
346
        return false;
26✔
347
    }
34✔
348

349
    bool BaseParser::oneOf(const std::initializer_list<std::string> words, std::string* s)
83,117✔
350
    {
83,117✔
351
        std::string buffer;
83,117✔
352
        if (!name(&buffer))
83,117✔
353
            return false;
216✔
354

355
        if (s)
82,901✔
356
            *s = buffer;
14,292✔
357

358
        return std::ranges::any_of(words, [&buffer](const std::string& word) {
190,003✔
359
            return word == buffer;
107,102✔
360
        });
361
    }
83,117✔
362
}
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