Make metadata actually have start and end separately

This commit is contained in:
Sofia 2026-04-11 00:01:14 +03:00
parent a035d07505
commit 6a315711cf
2 changed files with 25 additions and 11 deletions

View File

@ -46,8 +46,8 @@ namespace token {
std::stringstream out{ "" }; std::stringstream out{ "" };
out << type_name(this->type); out << type_name(this->type);
out << "(" << this->content << ")"; out << "(" << this->content << ")";
out << " at line " << this->metadata.position.line + 1 out << " at line " << this->metadata.start.line + 1
<< " col " << this->metadata.position.col + 1; << " col " << this->metadata.start.col + 1;
return out.str(); return out.str();
} }
@ -57,12 +57,25 @@ namespace token {
return stream; return stream;
} }
Position operator+(Position pos, Position other) { Metadata operator+(Metadata meta, Metadata other) {
return Position{ std::min(pos.line, other.line), std::min(pos.col, other.col) }; return Metadata{
Position {
std::min(meta.start.line, other.start.line),
std::min(meta.start.col, other.start.col),
},
Position {
std::max(meta.start.line, other.start.line),
std::max(meta.start.col, other.start.col),
},
meta.filename };
} }
Metadata operator+(Metadata meta, Metadata other) { Metadata operator+(Metadata& meta, int length) {
return Metadata{ meta.position + other.position, meta.filename }; return Metadata{
meta.start,
Position {meta.end.line, meta.end.col + length},
meta.filename,
};
} }
TokenStream::TokenStream(std::vector<Token>& tokens) TokenStream::TokenStream(std::vector<Token>& tokens)
@ -115,7 +128,7 @@ namespace token {
for (int i = 0; i < static_cast<int>(text.length());) { for (int i = 0; i < static_cast<int>(text.length());) {
Position position{ line, i - line_start }; Position position{ line, i - line_start };
Metadata meta{ position, filename }; Metadata meta{ position, position, filename };
char c = text[i]; char c = text[i];
@ -125,7 +138,7 @@ namespace token {
content += c; content += c;
c = text[++i]; c = text[++i];
} while (std::isdigit(c)); } while (std::isdigit(c));
tokens.push_back(token::Token{ token::Type::LiteralInt, content, meta }); tokens.push_back(token::Token{ token::Type::LiteralInt, content, meta + content.size() });
} }
else if (std::isalpha(c)) { else if (std::isalpha(c)) {
std::string content{}; std::string content{};
@ -144,7 +157,7 @@ namespace token {
else if (content == "else") { else if (content == "else") {
type = token::Type::ElseKeyword; type = token::Type::ElseKeyword;
} }
tokens.push_back(token::Token{ type, content, meta }); tokens.push_back(token::Token{ type, content, meta + content.size() });
} }
else if (iswhitespace(c)) { else if (iswhitespace(c)) {
std::string content{}; std::string content{};
@ -166,7 +179,7 @@ namespace token {
} }
Position position{ line, static_cast<uint32_t>(text.length()) - line_start }; Position position{ line, static_cast<uint32_t>(text.length()) - line_start };
Metadata meta{ position, filename }; Metadata meta{ position, position, filename };
tokens.push_back(token::Token{ token::Type::Eof, {}, meta }); tokens.push_back(token::Token{ token::Type::Eof, {}, meta });

View File

@ -29,7 +29,8 @@ namespace token {
}; };
struct Metadata { struct Metadata {
Position position; Position start;
Position end;
std::string filename; std::string filename;
}; };