Skip to content

Commit 383c739

Browse files
committed
fix: add a check on the position when backtracking in the parser
1 parent 58293c7 commit 383c739

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

include/Ark/Compiler/AST/BaseParser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ namespace Ark::internal
8484
*
8585
* @return distance in characters from the beginning of the file to the cursor
8686
*/
87-
long getCount() { return std::distance(m_str.begin(), m_it); }
87+
long getCount() { return static_cast<long>(std::distance(m_str.begin(), m_it)); }
8888

8989
/**
9090
*

src/arkreactor/Compiler/AST/BaseParser.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,18 @@ namespace Ark::internal
8484
if (std::cmp_greater_equal(n, m_str.size()))
8585
return;
8686

87-
m_it = m_str.begin() + n;
87+
if (std::cmp_less(n, m_str.size()))
88+
m_it = m_str.begin() + n;
89+
else
90+
m_it = m_str.begin();
91+
8892
auto [it, sym] = utf8_char_t::at(m_it, m_str.end());
8993
m_next_it = it;
9094
m_sym = sym;
9195

9296
// 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)
97+
for (const auto& [at, line] : m_it_to_row)
9498
{
95-
auto [at, line] = m_it_to_row[i];
9699
if (it <= at)
97100
{
98101
m_filepos.row = line - 1;

0 commit comments

Comments
 (0)