Make metadata actually have start and end separately
This commit is contained in:
parent
a035d07505
commit
6a315711cf
@ -46,8 +46,8 @@ namespace token {
|
||||
std::stringstream out{ "" };
|
||||
out << type_name(this->type);
|
||||
out << "(" << this->content << ")";
|
||||
out << " at line " << this->metadata.position.line + 1
|
||||
<< " col " << this->metadata.position.col + 1;
|
||||
out << " at line " << this->metadata.start.line + 1
|
||||
<< " col " << this->metadata.start.col + 1;
|
||||
|
||||
return out.str();
|
||||
}
|
||||
@ -57,12 +57,25 @@ namespace token {
|
||||
return stream;
|
||||
}
|
||||
|
||||
Position operator+(Position pos, Position other) {
|
||||
return Position{ std::min(pos.line, other.line), std::min(pos.col, other.col) };
|
||||
Metadata operator+(Metadata meta, Metadata other) {
|
||||
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) {
|
||||
return Metadata{ meta.position + other.position, meta.filename };
|
||||
Metadata operator+(Metadata& meta, int length) {
|
||||
return Metadata{
|
||||
meta.start,
|
||||
Position {meta.end.line, meta.end.col + length},
|
||||
meta.filename,
|
||||
};
|
||||
}
|
||||
|
||||
TokenStream::TokenStream(std::vector<Token>& tokens)
|
||||
@ -115,7 +128,7 @@ namespace token {
|
||||
|
||||
for (int i = 0; i < static_cast<int>(text.length());) {
|
||||
Position position{ line, i - line_start };
|
||||
Metadata meta{ position, filename };
|
||||
Metadata meta{ position, position, filename };
|
||||
|
||||
char c = text[i];
|
||||
|
||||
@ -125,7 +138,7 @@ namespace token {
|
||||
content += c;
|
||||
c = text[++i];
|
||||
} 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)) {
|
||||
std::string content{};
|
||||
@ -144,7 +157,7 @@ namespace token {
|
||||
else if (content == "else") {
|
||||
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)) {
|
||||
std::string content{};
|
||||
@ -166,7 +179,7 @@ namespace token {
|
||||
}
|
||||
|
||||
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 });
|
||||
|
||||
|
||||
@ -29,7 +29,8 @@ namespace token {
|
||||
};
|
||||
|
||||
struct Metadata {
|
||||
Position position;
|
||||
Position start;
|
||||
Position end;
|
||||
std::string filename;
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user