Serenity Operating System
1/*
2 * Copyright (c) 2021-2022, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/LexicalPath.h>
8#include <LibCore/ArgsParser.h>
9#include <LibCore/File.h>
10#include <LibCpp/Preprocessor.h>
11#include <LibMain/Main.h>
12
13ErrorOr<int> serenity_main(Main::Arguments arguments)
14{
15 Core::ArgsParser args_parser;
16 StringView path;
17 bool print_definitions = false;
18 args_parser.add_positional_argument(path, "File", "file", Core::ArgsParser::Required::Yes);
19 args_parser.add_option(print_definitions, "Print preprocessor definitions", "definitions", 'D');
20 args_parser.parse(arguments);
21
22 auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
23 auto content = TRY(file->read_until_eof());
24 DeprecatedString name = LexicalPath::basename(path);
25 Cpp::Preprocessor cpp(name, StringView { content });
26 auto tokens = cpp.process_and_lex();
27
28 if (print_definitions) {
29 outln("Definitions:");
30 for (auto const& definition : cpp.definitions()) {
31 if (definition.value.parameters.is_empty())
32 outln("{}: {}", definition.key, definition.value.value);
33 else
34 outln("{}({}): {}", definition.key, DeprecatedString::join(',', definition.value.parameters), definition.value.value);
35 }
36 outln("");
37 }
38
39 for (auto& token : tokens) {
40 outln("{}", token.to_deprecated_string());
41 }
42
43 return 0;
44}