Serenity Operating System
1/*
2 * Copyright (c) 2021-2022, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibCore/ArgsParser.h>
8#include <LibCore/File.h>
9#include <LibCpp/Parser.h>
10#include <LibMain/Main.h>
11
12ErrorOr<int> serenity_main(Main::Arguments arguments)
13{
14 Core::ArgsParser args_parser;
15 StringView path;
16 bool tokens_mode = false;
17 args_parser.add_option(tokens_mode, "Print Tokens", "tokens", 'T');
18 args_parser.add_positional_argument(path, "Cpp File", "cpp-file", Core::ArgsParser::Required::No);
19 args_parser.parse(arguments);
20
21 if (path.is_empty())
22 path = "Source/little/main.cpp"sv;
23 auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
24 auto content = TRY(file->read_until_eof());
25 StringView content_view(content);
26
27 ::Cpp::Preprocessor processor(path, content_view);
28 auto tokens = processor.process_and_lex();
29
30 ::Cpp::Parser parser(tokens, path);
31 if (tokens_mode) {
32 parser.print_tokens();
33 return 0;
34 }
35 auto root = parser.parse();
36
37 dbgln("Parser errors:");
38 for (auto& error : parser.errors()) {
39 dbgln("{}", error);
40 }
41
42 root->dump();
43
44 return 0;
45}