cpp-parser.cpp 1.2 KB

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