tree_node_location_translator.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. #ifndef CARBON_TOOLCHAIN_PARSE_TREE_NODE_LOCATION_TRANSLATOR_H_
  5. #define CARBON_TOOLCHAIN_PARSE_TREE_NODE_LOCATION_TRANSLATOR_H_
  6. #include "toolchain/parse/tree.h"
  7. namespace Carbon::Parse {
  8. class NodeLocationTranslator : public DiagnosticLocationTranslator<Node> {
  9. public:
  10. explicit NodeLocationTranslator(const Lex::TokenizedBuffer* tokens,
  11. llvm::StringRef filename,
  12. const Tree* parse_tree)
  13. : token_translator_(tokens),
  14. filename_(filename),
  15. parse_tree_(parse_tree) {}
  16. // Map the given token into a diagnostic location.
  17. auto GetLocation(Node node) -> DiagnosticLocation override {
  18. // Support the invalid token as a way to emit only the filename, when there
  19. // is no line association.
  20. if (!node.is_valid()) {
  21. return {.file_name = filename_};
  22. }
  23. return token_translator_.GetLocation(parse_tree_->node_token(node));
  24. }
  25. private:
  26. Lex::TokenLocationTranslator token_translator_;
  27. llvm::StringRef filename_;
  28. const Tree* parse_tree_;
  29. };
  30. } // namespace Carbon::Parse
  31. #endif // CARBON_TOOLCHAIN_PARSE_TREE_NODE_LOCATION_TRANSLATOR_H_