2021-01-23 17:47:20 +03:00
|
|
|
/*
|
2022-09-14 13:24:13 +03:00
|
|
|
* Copyright (c) 2021-2022, the SerenityOS developers.
|
2021-01-23 17:47:20 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-23 17:47:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 05:02:46 +03:00
|
|
|
#include <LibCore/File.h>
|
2021-01-23 17:47:20 +03:00
|
|
|
#include <LibCpp/Parser.h>
|
2021-11-27 23:17:19 +03:00
|
|
|
#include <LibMain/Main.h>
|
2021-01-23 17:47:20 +03:00
|
|
|
|
2021-11-27 23:17:19 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-01-23 17:47:20 +03:00
|
|
|
{
|
|
|
|
Core::ArgsParser args_parser;
|
2022-09-14 13:24:13 +03:00
|
|
|
StringView path;
|
2021-01-23 17:47:20 +03:00
|
|
|
bool tokens_mode = false;
|
|
|
|
args_parser.add_option(tokens_mode, "Print Tokens", "tokens", 'T');
|
|
|
|
args_parser.add_positional_argument(path, "Cpp File", "cpp-file", Core::ArgsParser::Required::No);
|
2021-11-27 23:17:19 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-01-23 17:47:20 +03:00
|
|
|
|
2022-09-14 13:24:13 +03:00
|
|
|
if (path.is_empty())
|
|
|
|
path = "Source/little/main.cpp"sv;
|
2023-02-09 05:02:46 +03:00
|
|
|
auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
|
2022-12-11 19:49:00 +03:00
|
|
|
auto content = TRY(file->read_until_eof());
|
2021-01-23 17:47:20 +03:00
|
|
|
StringView content_view(content);
|
2021-07-28 03:34:39 +03:00
|
|
|
|
|
|
|
::Cpp::Preprocessor processor(path, content_view);
|
2021-08-06 10:29:57 +03:00
|
|
|
auto tokens = processor.process_and_lex();
|
2021-07-28 03:34:39 +03:00
|
|
|
|
2021-08-06 10:29:57 +03:00
|
|
|
::Cpp::Parser parser(tokens, path);
|
2021-01-23 17:47:20 +03:00
|
|
|
if (tokens_mode) {
|
|
|
|
parser.print_tokens();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto root = parser.parse();
|
|
|
|
|
|
|
|
dbgln("Parser errors:");
|
|
|
|
for (auto& error : parser.errors()) {
|
|
|
|
dbgln("{}", error);
|
|
|
|
}
|
|
|
|
|
2021-05-17 13:04:15 +03:00
|
|
|
root->dump();
|
2021-11-27 23:17:19 +03:00
|
|
|
|
|
|
|
return 0;
|
2021-01-23 17:47:20 +03:00
|
|
|
}
|