2021-02-06 10:51:04 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-06 10:51:04 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-08-08 15:13:02 -03:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-02-06 10:51:04 -03:00
|
|
|
#include <LibCore/File.h>
|
|
|
|
|
#include <LibCpp/Preprocessor.h>
|
2021-11-27 17:18:01 -03:00
|
|
|
#include <LibMain/Main.h>
|
2021-02-06 10:51:04 -03:00
|
|
|
|
2021-11-27 17:18:01 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-02-06 10:51:04 -03:00
|
|
|
{
|
2021-08-08 15:13:02 -03:00
|
|
|
Core::ArgsParser args_parser;
|
2022-04-01 14:58:27 -03:00
|
|
|
char const* path = nullptr;
|
2021-08-08 15:13:02 -03:00
|
|
|
bool print_definitions = false;
|
|
|
|
|
args_parser.add_positional_argument(path, "File", "file", Core::ArgsParser::Required::Yes);
|
|
|
|
|
args_parser.add_option(print_definitions, "Print preprocessor definitions", "definitions", 'D');
|
2021-11-27 17:18:01 -03:00
|
|
|
args_parser.parse(arguments);
|
2021-08-08 15:13:02 -03:00
|
|
|
auto file = Core::File::construct(path);
|
2021-05-12 06:26:43 -03:00
|
|
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
2021-08-21 18:10:59 -03:00
|
|
|
warnln("Failed to open {}: {}", path, file->error_string());
|
2021-02-06 10:51:04 -03:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
auto content = file->read_all();
|
2021-08-08 15:13:02 -03:00
|
|
|
String name = LexicalPath::basename(path);
|
|
|
|
|
Cpp::Preprocessor cpp(name, StringView { content });
|
2021-08-06 04:29:57 -03:00
|
|
|
auto tokens = cpp.process_and_lex();
|
2021-08-11 16:31:43 -03:00
|
|
|
|
2021-08-08 15:13:02 -03:00
|
|
|
if (print_definitions) {
|
|
|
|
|
outln("Definitions:");
|
|
|
|
|
for (auto& definition : cpp.definitions()) {
|
|
|
|
|
if (definition.value.parameters.is_empty())
|
|
|
|
|
outln("{}: {}", definition.key, definition.value.value);
|
|
|
|
|
else
|
2022-07-11 17:10:18 -03:00
|
|
|
outln("{}({}): {}", definition.key, String::join(',', definition.value.parameters), definition.value.value);
|
2021-08-08 15:13:02 -03:00
|
|
|
}
|
|
|
|
|
outln("");
|
2021-08-11 16:31:43 -03:00
|
|
|
}
|
|
|
|
|
|
2021-08-06 04:29:57 -03:00
|
|
|
for (auto& token : tokens) {
|
2021-08-08 15:13:02 -03:00
|
|
|
outln("{}", token.to_string());
|
2021-08-06 04:29:57 -03:00
|
|
|
}
|
2021-11-27 17:18:01 -03:00
|
|
|
|
|
|
|
|
return 0;
|
2021-02-06 10:51:04 -03:00
|
|
|
}
|