Skip to content

Commit

Permalink
Kind of hide token_pos_t::file, since it's about to be removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvander committed Sep 29, 2023
1 parent 4c0153c commit c646997
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiler/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ MessageBuilder::~MessageBuilder()

ErrorReport report;
report.number = number_;
report.fileno = where_.file;
report.fileno = cc.sources()->GetSourceFileIndex(where_);
report.lineno = std::max(where_.line, 1);
if (report.fileno < 0)
report.fileno = cc.lexer()->fcurrent();
Expand Down
12 changes: 7 additions & 5 deletions compiler/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ Lexer::PushSynthesizedToken(TokenKind kind, const token_pos_t& pos)
tok->id = kind;
tok->atom = nullptr;
tok->start.line = state_.tokline;
tok->start.file = state_.inpf->sources_index();
tok->start.file_ = state_.inpf->sources_index();
lexpush();
return tok;
}
Expand Down Expand Up @@ -1397,7 +1397,7 @@ int Lexer::LexNewToken() {

void Lexer::FillTokenPos(token_pos_t* pos) {
pos->line = state_.tokline;
pos->file = state_.inpf->sources_index();
pos->file_ = state_.inpf->sources_index();
}

void Lexer::LexIntoToken(full_token_t* tok) {
Expand Down Expand Up @@ -1933,12 +1933,14 @@ Lexer::peek_same_line()
// We should not call this without having parsed at least one token.
assert(token_buffer_->num_tokens > 0);

auto sm = cc_.sources();

// If there's tokens pushed back, then |fline| is the line of the furthest
// token parsed. If fline == current token's line, we are guaranteed any
// buffered token is still on the same line.
if (token_buffer_->depth > 0 &&
current_token()->start.file == next_token()->start.file &&
current_token()->start.line == state_.fline)
current_token()->start.line == state_.fline &&
sm->IsSameSourceFile(current_token()->start, next_token()->start))
{
return next_token()->id ? next_token()->id : tEOL;
}
Expand All @@ -1953,7 +1955,7 @@ Lexer::peek_same_line()
// If the next token starts on the line the last token ends, then the next
// token is considered on the same line.
if (next.start.line == current_token()->start.line &&
next.start.file == current_token()->start.file)
sm->IsSameSourceFile(current_token()->start, next_token()->start))
{
return next.id;
}
Expand Down
10 changes: 9 additions & 1 deletion compiler/source-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class CompileContext;

struct token_pos_t {
int file = 0;
int file_ = 0;
int line = 0;
};

Expand Down Expand Up @@ -106,6 +106,14 @@ class SourceManager final

LREntry GetLocationRangeEntryForFile(const std::shared_ptr<SourceFile>& file);

// For a given token location, retrieve the nearest source file index it maps to.
uint32_t GetSourceFileIndex(const token_pos_t& pos) {
return pos.file_;
}
bool IsSameSourceFile(const token_pos_t& current, const token_pos_t& next) {
return current.file_ == next.file_;
}

const tr::vector<std::shared_ptr<SourceFile>>& opened_files() const {
return opened_files_;
}
Expand Down

0 comments on commit c646997

Please sign in to comment.