2021-02-06 15:51:04 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-06 15:51:04 +02:00
|
|
|
*/
|
|
|
|
|
|
2021-08-08 21:13:02 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-02-06 15:51:04 +02:00
|
|
|
#include <LibCore/File.h>
|
|
|
|
|
#include <LibCpp/Preprocessor.h>
|
2021-11-27 21:18:01 +01:00
|
|
|
#include <LibMain/Main.h>
|
2021-02-06 15:51:04 +02:00
|
|
|
|
2021-11-27 21:18:01 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-02-06 15:51:04 +02:00
|
|
|
{
|
2021-08-08 21:13:02 +03:00
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
|
const char* path = nullptr;
|
|
|
|
|
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 21:18:01 +01:00
|
|
|
args_parser.parse(arguments);
|
2021-08-08 21:13:02 +03:00
|
|
|
auto file = Core::File::construct(path);
|
2021-05-12 13:56:43 +04:30
|
|
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
2021-08-21 23:10:59 +02:00
|
|
|
warnln("Failed to open {}: {}", path, file->error_string());
|
2021-02-06 15:51:04 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
auto content = file->read_all();
|
2021-08-08 21:13:02 +03:00
|
|
|
String name = LexicalPath::basename(path);
|
|
|
|
|
Cpp::Preprocessor cpp(name, StringView { content });
|
2021-08-06 10:29:57 +03:00
|
|
|
auto tokens = cpp.process_and_lex();
|
2021-08-11 22:31:43 +03:00
|
|
|
|
2021-08-08 21: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
|
|
|
|
|
outln("{}({}): {}", definition.key, String::join(",", definition.value.parameters), definition.value.value);
|
|
|
|
|
}
|
|
|
|
|
outln("");
|
2021-08-11 22:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
2021-08-06 10:29:57 +03:00
|
|
|
for (auto& token : tokens) {
|
2021-08-08 21:13:02 +03:00
|
|
|
outln("{}", token.to_string());
|
2021-08-06 10:29:57 +03:00
|
|
|
}
|
2021-11-27 21:18:01 +01:00
|
|
|
|
|
|
|
|
return 0;
|
2021-02-06 15:51:04 +02:00
|
|
|
}
|