cpp-parser.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. perror("open");
  22. exit(1);
  23. }
  24. auto content = file->read_all();
  25. StringView content_view(content);
  26. ::Cpp::Parser parser(content_view, path);
  27. if (tokens_mode) {
  28. parser.print_tokens();
  29. return 0;
  30. }
  31. auto root = parser.parse();
  32. dbgln("Parser errors:");
  33. for (auto& error : parser.errors()) {
  34. dbgln("{}", error);
  35. }
  36. root->dump();
  37. }