driver.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 TOOLCHAIN_DRIVER_DRIVER_H_
  5. #define TOOLCHAIN_DRIVER_DRIVER_H_
  6. #include <cstdint>
  7. #include "llvm/ADT/ArrayRef.h"
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/Support/Debug.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. namespace Carbon {
  12. // Command line interface driver.
  13. //
  14. // Provides simple API to parse and run command lines for Carbon. It is
  15. // generally expected to be used to implement command line tools for working
  16. // with the language.
  17. class Driver {
  18. public:
  19. // Default constructed driver uses stderr for all error and informational
  20. // output.
  21. Driver() : output_stream_(llvm::outs()), error_stream_(llvm::errs()) {}
  22. // Constructs a driver with any error or informational output directed to a
  23. // specified stream.
  24. Driver(llvm::raw_ostream& output_stream, llvm::raw_ostream& error_stream)
  25. : output_stream_(output_stream), error_stream_(error_stream) {}
  26. // Parses the given arguments into both a subcommand to select the operation
  27. // to perform and any arguments to that subcommand.
  28. //
  29. // Returns true if the operation succeeds. If the operation fails, returns
  30. // false and any information about the failure is printed to the registered
  31. // error stream (stderr by default).
  32. auto RunFullCommand(llvm::ArrayRef<llvm::StringRef> args) -> bool;
  33. // Subcommand that prints available help text to the error stream.
  34. //
  35. // Optionally one positional parameter may be provided to select a particular
  36. // subcommand or detailed section of help to print.
  37. //
  38. // Returns true if appropriate help text was found and printed. If an invalid
  39. // positional parameter (or flag) is provided, returns false.
  40. auto RunHelpSubcommand(llvm::ArrayRef<llvm::StringRef> args) -> bool;
  41. // Subcommand that dumps the token information for the provided source file.
  42. //
  43. // Requires exactly one positional parameter to designate the source file to
  44. // read. May be `-` to read from stdin.
  45. //
  46. // Returns true if the operation succeeds. If the operation fails, this
  47. // returns false and any information about the failure is printed to the
  48. // registered error stream (stderr by default).
  49. auto RunDumpTokensSubcommand(llvm::ArrayRef<llvm::StringRef> args) -> bool;
  50. // Subcommand that dumps the parse tree for the provided source file.
  51. //
  52. // Requires exactly one positional parameter to designate the source file to
  53. // read. May be `-` to read from stdin.
  54. //
  55. // Returns true if the operation succeeds. If the operation fails, this
  56. // returns false and any information about the failure is printed to the
  57. // registered error stream (stderr by default).
  58. auto RunDumpParseTreeSubcommand(llvm::ArrayRef<llvm::StringRef> args) -> bool;
  59. private:
  60. auto ReportExtraArgs(llvm::StringRef subcommand_text,
  61. llvm::ArrayRef<llvm::StringRef> args) -> void;
  62. llvm::raw_ostream& output_stream_;
  63. llvm::raw_ostream& error_stream_;
  64. };
  65. } // namespace Carbon
  66. #endif // TOOLCHAIN_DRIVER_DRIVER_H_