cpp-parser.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2021-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/ArgsParser.h>
  7. #include <LibCore/Stream.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. StringView path;
  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.is_empty())
  19. path = "Source/little/main.cpp"sv;
  20. auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read));
  21. auto content = TRY(file->read_all());
  22. StringView content_view(content);
  23. ::Cpp::Preprocessor processor(path, content_view);
  24. auto tokens = processor.process_and_lex();
  25. ::Cpp::Parser parser(tokens, path);
  26. if (tokens_mode) {
  27. parser.print_tokens();
  28. return 0;
  29. }
  30. auto root = parser.parse();
  31. dbgln("Parser errors:");
  32. for (auto& error : parser.errors()) {
  33. dbgln("{}", error);
  34. }
  35. root->dump();
  36. return 0;
  37. }