mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 08:54:47 +01:00
Add skeletal format subcommand. (#4383)
This extracts out the SourceBuffer handling of `-` in order to trivially share it. Note this still has a number of TODOs, it's just setting up the essential subcommand infrastructure, with some tests demonstrating that it at least does something.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
// 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;
|
||||
}
|
||||
llvm::ListSeparator sep(" ");
|
||||
for (auto token : tokens.tokens()) {
|
||||
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
|
||||
Reference in New Issue
Block a user