cpp-parser.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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/Parser.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. bool tokens_mode = false;
  15. args_parser.add_option(tokens_mode, "Print Tokens", "tokens", 'T');
  16. args_parser.add_positional_argument(path, "Cpp File", "cpp-file", Core::ArgsParser::Required::No);
  17. args_parser.parse(arguments);
  18. if (!path)
  19. path = "Source/little/main.cpp";
  20. auto file = Core::File::construct(path);
  21. if (!file->open(Core::OpenMode::ReadOnly)) {
  22. warnln("Failed to open {}: {}", path, file->error_string());
  23. exit(1);
  24. }
  25. auto content = file->read_all();
  26. StringView content_view(content);
  27. ::Cpp::Preprocessor processor(path, content_view);
  28. auto tokens = processor.process_and_lex();
  29. ::Cpp::Parser parser(tokens, path);
  30. if (tokens_mode) {
  31. parser.print_tokens();
  32. return 0;
  33. }
  34. auto root = parser.parse();
  35. dbgln("Parser errors:");
  36. for (auto& error : parser.errors()) {
  37. dbgln("{}", error);
  38. }
  39. root->dump();
  40. return 0;
  41. }