diff --git a/src/tokens.cpp b/src/tokens.cpp index d6a0642..3b64209 100644 --- a/src/tokens.cpp +++ b/src/tokens.cpp @@ -128,7 +128,9 @@ namespace token { uint32_t line = 0; uint32_t line_start = 0; - for (int i = 0; i < static_cast(text.length());) { + int text_length = static_cast(text.length()); + + for (int i = 0; i < text_length;) { Position position{ line, i - line_start }; Metadata meta{ position, position, filename }; @@ -138,6 +140,7 @@ namespace token { std::string content{}; do { content += c; + if ((i + 1) >= text_length) break; c = text[++i]; } while (std::isdigit(c)); tokens.push_back(token::Token{ token::Type::LiteralInt, content, meta + content.size() }); @@ -147,6 +150,7 @@ namespace token { c = text[++i]; // Skip initial " do { content += c; + if ((i + 1) >= text_length) break; c = text[++i]; } while (c != '\"'); i++; // Skip second " @@ -156,6 +160,7 @@ namespace token { std::string content{}; do { content += c; + if ((i + 1) >= text_length) break; c = text[++i]; } while (std::isalnum(c)); @@ -179,6 +184,7 @@ namespace token { line_start = i + 1; } content += c; + if ((i + 1) >= text_length) break; c = text[++i]; } while (iswhitespace(c)); // tokens.push_back(token::Token{ token::Type::Whitespace, content });