format.cpp 996 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/format/format.h"
  5. #include "toolchain/lex/token_index.h"
  6. #include "toolchain/lex/tokenized_buffer.h"
  7. namespace Carbon::Format {
  8. // TODO: Add support for formatting line ranges (will need flags too).
  9. auto Format(const Lex::TokenizedBuffer& tokens, llvm::raw_ostream& out)
  10. -> bool {
  11. if (tokens.has_errors()) {
  12. // TODO: Error recovery.
  13. return false;
  14. }
  15. llvm::ListSeparator sep(" ");
  16. for (auto token : tokens.tokens()) {
  17. switch (tokens.GetKind(token)) {
  18. case Lex::TokenKind::FileStart:
  19. break;
  20. case Lex::TokenKind::FileEnd:
  21. out << "\n";
  22. break;
  23. default:
  24. // TODO: More dependent formatting.
  25. out << sep << tokens.GetTokenText(token);
  26. break;
  27. }
  28. }
  29. return true;
  30. }
  31. } // namespace Carbon::Format