Lexer, Parser: Reorder constructors and use ssize_t for signed indices

This commit is contained in:
apio 2022-11-01 22:28:21 +01:00
parent 06f24e770b
commit 2cb477447e
4 changed files with 13 additions and 13 deletions

View File

@ -7,7 +7,7 @@
#define IDENTIFIERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ_0123456789" #define IDENTIFIERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ_0123456789"
#define DIGITS "0123456789" #define DIGITS "0123456789"
Lexer::Lexer(const std::string& fname) : location(1, 0, fname), index(-1), previous_location(1, 0, fname) Lexer::Lexer(const std::string& fname) : location(1, 0, fname), previous_location(1, 0, fname), index(-1)
{ {
} }
@ -17,10 +17,10 @@ Lexer::~Lexer()
int Lexer::advance() int Lexer::advance()
{ {
previous_location = location; previous_location = Location{ location };
++index; ++index;
location.advance(); location.advance();
if (index >= current_lexed_text.size()) return 0; if (index >= (ssize_t)current_lexed_text.size()) return 0;
current_char = current_lexed_text[index]; current_char = current_lexed_text[index];
location.pos_from_char(current_char); location.pos_from_char(current_char);
if (current_char == '\n') if (current_char == '\n')
@ -46,10 +46,10 @@ int Lexer::rewind()
std::string Lexer::recalculate_current_line(const std::string& text) std::string Lexer::recalculate_current_line(const std::string& text)
{ {
int idx = index; ssize_t idx = index;
std::string final_str; std::string final_str;
++idx; ++idx;
while (idx != text.size() && text[idx] != '\n') while (idx != (ssize_t)text.size() && text[idx] != '\n')
{ {
final_str += text[idx]; final_str += text[idx];
++idx; ++idx;
@ -106,7 +106,7 @@ TokenStream Lexer::lex(const std::string& text)
switch (current_char) switch (current_char)
{ {
case '/': case '/':
if (index + 1 != current_lexed_text.size()) if (index + 1 != (ssize_t)current_lexed_text.size())
{ {
if (current_lexed_text[index + 1] == '/') if (current_lexed_text[index + 1] == '/')
{ {
@ -172,7 +172,7 @@ TokenStream Lexer::lex(const std::string& text)
break; break;
case '\377': case '\377':
result.push_back(Token(TT_EOF, location)); result.push_back(Token(TT_EOF, location));
return std::move(result); return result;
default: default:
Error::throw_error(location, current_line_text, "unknown character"); Error::throw_error(location, current_line_text, "unknown character");
} }
@ -180,7 +180,7 @@ TokenStream Lexer::lex(const std::string& text)
result.push_back(Token(TT_EOF, location)); result.push_back(Token(TT_EOF, location));
return std::move(result); return result;
} }
Token Lexer::create_identifier() Token Lexer::create_identifier()
@ -303,7 +303,7 @@ Token Lexer::create_string()
} }
if (current_char == '\\') if (current_char == '\\')
{ {
if (index + 1 == current_lexed_text.size()) if (index + 1 == (ssize_t)current_lexed_text.size())
{ {
Error::throw_error(location, current_line_text, "unfinished escape sequence"); Error::throw_error(location, current_line_text, "unfinished escape sequence");
} }

View File

@ -19,7 +19,7 @@ class Lexer
int advance(); int advance();
int rewind(); int rewind();
char current_char; char current_char;
int index; ssize_t index;
Lexer(const std::string& filename); Lexer(const std::string& filename);

View File

@ -37,7 +37,7 @@ std::shared_ptr<ProgramNode> Parser::parse()
int Parser::advance() int Parser::advance()
{ {
++index; ++index;
if (index < tokens.size()) if (index < (ssize_t)tokens.size())
{ {
current_token = &tokens[index]; current_token = &tokens[index];
} }
@ -136,7 +136,7 @@ Result<TopLevelNode> Parser::function()
{ {
proto.returnType = m_type_map.at(current_token->string_value.value()); proto.returnType = m_type_map.at(current_token->string_value.value());
} }
catch (std::out_of_range) catch (const std::out_of_range&)
{ {
return Err<TopLevelNode>("Expected type name", current_token); return Err<TopLevelNode>("Expected type name", current_token);
} }

View File

@ -14,7 +14,7 @@ class Parser
private: private:
Parser(const TokenStream& tokens); Parser(const TokenStream& tokens);
TokenStream tokens; TokenStream tokens;
int index = -1; ssize_t index = -1;
int advance(); int advance();
Token* current_token; Token* current_token;