cpp-lexer.cpp 872 B

123456789101112131415161718192021222324252627282930313233
  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. #include <LibMain/Main.h>
  10. ErrorOr<int> serenity_main(Main::Arguments arguments)
  11. {
  12. Core::ArgsParser args_parser;
  13. const char* path = nullptr;
  14. args_parser.add_positional_argument(path, "Cpp File", "cpp-file", Core::ArgsParser::Required::Yes);
  15. args_parser.parse(arguments);
  16. auto file = Core::File::construct(path);
  17. if (!file->open(Core::OpenMode::ReadOnly)) {
  18. warnln("Failed to open {}: {}", path, file->error_string());
  19. exit(1);
  20. }
  21. auto content = file->read_all();
  22. StringView content_view(content);
  23. Cpp::Lexer lexer(content);
  24. lexer.lex_iterable([](auto token) {
  25. outln("{}", token.to_string());
  26. });
  27. return 0;
  28. }