cpp-lexer.cpp 810 B

123456789101112131415161718192021222324252627282930
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/ArgsParser.h>
  7. #include <LibCore/File.h>
  8. #include <LibCpp/Lexer.h>
  9. int main(int argc, char** argv)
  10. {
  11. Core::ArgsParser args_parser;
  12. const char* path = nullptr;
  13. args_parser.add_positional_argument(path, "Cpp File", "cpp-file", Core::ArgsParser::Required::Yes);
  14. args_parser.parse(argc, argv);
  15. auto file = Core::File::construct(path);
  16. if (!file->open(Core::OpenMode::ReadOnly)) {
  17. warnln("Failed to open {}: {}", path, file->error_string());
  18. exit(1);
  19. }
  20. auto content = file->read_all();
  21. StringView content_view(content);
  22. Cpp::Lexer lexer(content);
  23. lexer.lex_iterable([](auto token) {
  24. outln("{}", token.to_string());
  25. });
  26. }