mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 10:31:03 +01:00
Implement some token-based formatting structure. (#4386)
Here I'm trying to add some simple formatting based on the token kind, aiming mainly to keep the implementation short for now. This approach won't generalize to arbitrary structures (e.g., it doesn't discern between braces for a function body and a struct literal). I think we'll probably want to build a parse tree and associate parse kinds with tokens in order to format, additionally doing something less linear. But my essential goal at present is to just get a proof-of-concept that the basics can yield something that looks okay.
This commit is contained in:
@@ -4,45 +4,13 @@
|
||||
|
||||
#include "toolchain/format/format.h"
|
||||
|
||||
#include "toolchain/lex/token_index.h"
|
||||
#include "toolchain/lex/tokenized_buffer.h"
|
||||
#include "toolchain/format/formatter.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;
|
||||
return Formatter(&tokens, &out).Run();
|
||||
}
|
||||
|
||||
} // namespace Carbon::Format
|
||||
|
||||
Reference in New Issue
Block a user