Improve diagnosis of mismatched brackets. (#3282)

Move handling of mismatched brackets out of the main lexing loop into a
separate pass that is only run if there are mismatched brackets This is
done in preparation for using both lookahead and lookbehind to work out
how to match brackets, and to get this code far away from the hot lexing
loop.

Fix bracket insertion location to be immediately after the token that
we're inserting the bracket after, rather than potentially at the end of
a comment. When there are open brackets at the end of the file, say that
there are open brackets, not that there's a closing bracket without a
matching opening bracket.
This commit is contained in:
Richard Smith
2023-12-21 08:49:37 +00:00
committed by GitHub
parent 3d260b92f1
commit 0a06fceb5f
13 changed files with 330 additions and 149 deletions
+19
View File
@@ -35,6 +35,25 @@ auto TokenizedBuffer::GetColumnNumber(TokenIndex token) const -> int {
return GetTokenInfo(token).column + 1;
}
auto TokenizedBuffer::GetEndLocation(TokenIndex token) const
-> std::pair<LineIndex, int> {
auto line = GetLine(token);
int column = GetColumnNumber(token);
auto token_text = GetTokenText(token);
if (auto [before_newline, after_newline] = token_text.rsplit('\n');
before_newline.size() == token_text.size()) {
// Token fits on one line, advance the column number.
column += before_newline.size();
} else {
// Token contains newlines.
line.index += before_newline.count('\n') + 1;
column = 1 + after_newline.size();
}
return {line, column};
}
auto TokenizedBuffer::GetTokenText(TokenIndex token) const -> llvm::StringRef {
const auto& token_info = GetTokenInfo(token);
llvm::StringRef fixed_spelling = token_info.kind.fixed_spelling();