2026-04-10 08:14:15 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2026, Sam Atkins <sam@ladybird.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/Format.h>
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
|
#include <LibCore/File.h>
|
|
|
|
|
#include <LibMain/Main.h>
|
2026-04-15 07:53:15 -03:00
|
|
|
#include <LibWeb/CSS/Parser/RustTokenizer.h>
|
2026-04-10 08:14:15 -03:00
|
|
|
#include <LibWeb/CSS/Parser/Tokenizer.h>
|
|
|
|
|
|
|
|
|
|
ErrorOr<int> ladybird_main(Main::Arguments arguments)
|
|
|
|
|
{
|
2026-04-15 07:53:15 -03:00
|
|
|
StringView backend = "cpp"sv;
|
2026-04-10 08:14:15 -03:00
|
|
|
StringView encoding = "utf-8"sv;
|
|
|
|
|
StringView input_path;
|
2026-05-03 04:52:45 -03:00
|
|
|
bool silent = false;
|
2026-04-10 08:14:15 -03:00
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2026-04-15 07:53:15 -03:00
|
|
|
args_parser.add_option(backend, "Tokenizer backend to use (cpp or rust)", "backend", 'b', "backend");
|
2026-04-10 08:14:15 -03:00
|
|
|
args_parser.add_option(encoding, "Source encoding label", "encoding", 'e', "encoding");
|
2026-05-03 04:52:45 -03:00
|
|
|
args_parser.add_option(silent, "Don't print the tokens (useful for perf testing)", "silent", 's');
|
2026-04-10 08:14:15 -03:00
|
|
|
args_parser.add_positional_argument(input_path, "Path to the CSS input file", "input", Core::ArgsParser::Required::Yes);
|
|
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
|
2026-04-15 07:53:15 -03:00
|
|
|
if (backend != "cpp"sv && backend != "rust"sv) {
|
|
|
|
|
warnln("Unknown backend '{}'. Expected 'cpp' or 'rust'.", backend);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-10 08:14:15 -03:00
|
|
|
auto file = TRY(Core::File::open(input_path, Core::File::OpenMode::Read));
|
|
|
|
|
auto input = TRY(file->read_until_eof());
|
2026-04-15 07:53:15 -03:00
|
|
|
auto tokens = backend == "rust"sv
|
2026-05-18 07:52:17 -03:00
|
|
|
? Web::CSS::Parser::RustTokenizer::tokenize(input, encoding, Web::CSS::Parser::TokenizerInput::EncodedBytes)
|
|
|
|
|
: Web::CSS::Parser::Tokenizer::tokenize(input, encoding, Web::CSS::Parser::TokenizerInput::EncodedBytes);
|
2026-04-10 08:14:15 -03:00
|
|
|
|
2026-05-03 04:52:45 -03:00
|
|
|
if (!silent) {
|
|
|
|
|
for (auto const& token : tokens)
|
|
|
|
|
outln("{}", token.to_debug_string());
|
|
|
|
|
}
|
2026-04-10 08:14:15 -03:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|