Files
carbon-lang/toolchain/format/format.cpp
T
Jon Ross-Perkins 1338f9e0ad Add tracking of lexed comments, with skeletal formatting. (#4385)
In order to format comments, it's helpful if they're tracked. This
tracks them separately from tokens in order to avoid interfering with
parse; it'd be inconvenient if comment tokens could show up in arbitrary
locations, albeit possible to support.

This additionally extracts out the TokenIterator support into a template
in order to generally have it available for IndexBase types. I'm only
adding it for CommentInfo, not sure if we'll want it elsewhere, but this
structure still felt like a good fit.
2024-10-09 21:05:53 +00:00

49 lines
1.3 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/format/format.h"
#include "toolchain/lex/token_index.h"
#include "toolchain/lex/tokenized_buffer.h"
namespace Carbon::Format {
// TODO: Add support for formatting line ranges (will need flags too).
auto Format(const Lex::TokenizedBuffer& tokens, llvm::raw_ostream& out)
-> bool {
if (tokens.has_errors()) {
// TODO: Error recovery.
return false;
}
auto comments = tokens.comments();
auto comment_it = comments.begin();
llvm::ListSeparator sep(" ");
for (auto token : tokens.tokens()) {
while (comment_it != comments.end() &&
tokens.IsAfterComment(token, *comment_it)) {
// TODO: Fix newlines and indent.
out << "\n" << tokens.GetCommentText(*comment_it) << "\n";
++comment_it;
}
switch (tokens.GetKind(token)) {
case Lex::TokenKind::FileStart:
break;
case Lex::TokenKind::FileEnd:
out << "\n";
break;
default:
// TODO: More dependent formatting.
out << sep << tokens.GetTokenText(token);
break;
}
}
return true;
}
} // namespace Carbon::Format