2023-08-17 23:29:05 -03:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
|
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2024-01-16 22:47:08 -03:00
|
|
|
|
#include <LibXML/Parser/Parser.h>
|
2023-08-17 23:29:05 -03:00
|
|
|
|
|
|
|
|
|
|
#include "Parser/Lexer.h"
|
2024-01-16 22:47:08 -03:00
|
|
|
|
#include "Parser/SpecParser.h"
|
2023-08-17 23:29:05 -03:00
|
|
|
|
#include "Parser/XMLUtils.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace JSSpecCompiler {
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2024-01-16 22:47:08 -03:00
|
|
|
|
Optional<Token> consume_number(XML::LineTrackingLexer& lexer, XML::Node const* node, Location& location)
|
2023-08-17 23:29:05 -03:00
|
|
|
|
{
|
|
|
|
|
|
u64 start = lexer.tell();
|
|
|
|
|
|
|
|
|
|
|
|
if (lexer.next_is('-'))
|
|
|
|
|
|
lexer.consume(1);
|
|
|
|
|
|
|
|
|
|
|
|
if (!lexer.next_is(is_ascii_digit)) {
|
|
|
|
|
|
lexer.retreat(lexer.tell() - start);
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lexer.consume_while(is_ascii_digit);
|
|
|
|
|
|
|
|
|
|
|
|
if (lexer.next_is('.')) {
|
|
|
|
|
|
lexer.consume(1);
|
|
|
|
|
|
if (lexer.consume_while(is_ascii_digit).length() == 0)
|
|
|
|
|
|
lexer.retreat(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto length = lexer.tell() - start;
|
|
|
|
|
|
lexer.retreat(length);
|
2024-01-16 22:47:08 -03:00
|
|
|
|
return { Token { TokenType::Number, lexer.consume(length), node, move(location) } };
|
2023-08-17 23:29:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool can_end_word_token(char c)
|
|
|
|
|
|
{
|
|
|
|
|
|
return is_ascii_space(c) || ".,"sv.contains(c);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-16 22:47:08 -03:00
|
|
|
|
ParseErrorOr<void> tokenize_string(SpecificationParsingContext& ctx, XML::Node const* node, StringView view, Vector<Token>& tokens)
|
2023-08-17 23:29:05 -03:00
|
|
|
|
{
|
2024-01-16 22:47:08 -03:00
|
|
|
|
static constexpr struct {
|
|
|
|
|
|
StringView text_to_match;
|
|
|
|
|
|
TokenType token_type;
|
|
|
|
|
|
} choices[] = {
|
|
|
|
|
|
{ "-"sv, TokenType::AmbiguousMinus },
|
|
|
|
|
|
{ "}"sv, TokenType::BraceClose },
|
|
|
|
|
|
{ "{"sv, TokenType::BraceOpen },
|
|
|
|
|
|
{ ":"sv, TokenType::Colon },
|
|
|
|
|
|
{ ","sv, TokenType::Comma },
|
|
|
|
|
|
{ "/"sv, TokenType::Division },
|
|
|
|
|
|
{ ". "sv, TokenType::Dot },
|
|
|
|
|
|
{ ".\n"sv, TokenType::Dot },
|
|
|
|
|
|
{ "="sv, TokenType::Equals },
|
|
|
|
|
|
{ "is equal to"sv, TokenType::Equals },
|
|
|
|
|
|
{ "!"sv, TokenType::ExclamationMark },
|
|
|
|
|
|
{ ">"sv, TokenType::Greater },
|
|
|
|
|
|
{ "is"sv, TokenType::Is },
|
|
|
|
|
|
{ "<"sv, TokenType::Less },
|
|
|
|
|
|
{ "."sv, TokenType::MemberAccess },
|
|
|
|
|
|
{ "×"sv, TokenType::Multiplication },
|
|
|
|
|
|
{ "is not equal to"sv, TokenType::NotEquals },
|
|
|
|
|
|
{ "≠"sv, TokenType::NotEquals },
|
|
|
|
|
|
{ ")"sv, TokenType::ParenClose },
|
|
|
|
|
|
{ "("sv, TokenType::ParenOpen },
|
|
|
|
|
|
{ "+"sv, TokenType::Plus },
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
XML::LineTrackingLexer lexer(view, node->offset);
|
2023-08-17 23:29:05 -03:00
|
|
|
|
|
|
|
|
|
|
while (!lexer.is_eof()) {
|
|
|
|
|
|
lexer.ignore_while(is_ascii_space);
|
|
|
|
|
|
|
2024-01-16 22:47:08 -03:00
|
|
|
|
// FIXME: This is incorrect since we count text offset after XML reference resolution. To do
|
|
|
|
|
|
// this properly, we need support from XML::Parser.
|
|
|
|
|
|
Location token_location = ctx.location_from_xml_offset(lexer.offset_for(lexer.tell()));
|
|
|
|
|
|
|
|
|
|
|
|
if (auto result = consume_number(lexer, node, token_location); result.has_value()) {
|
2023-08-17 23:29:05 -03:00
|
|
|
|
tokens.append(result.release_value());
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-16 22:47:08 -03:00
|
|
|
|
bool matched = false;
|
|
|
|
|
|
for (auto const& [text_to_match, token_type] : choices) {
|
|
|
|
|
|
if (lexer.consume_specific(text_to_match)) {
|
|
|
|
|
|
tokens.append({ token_type, ""sv, node, move(token_location) });
|
|
|
|
|
|
matched = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (matched)
|
|
|
|
|
|
continue;
|
2023-08-17 23:29:05 -03:00
|
|
|
|
|
|
|
|
|
|
StringView word = lexer.consume_until(can_end_word_token);
|
|
|
|
|
|
if (word.length())
|
2024-01-16 22:47:08 -03:00
|
|
|
|
tokens.append({ TokenType::Word, word, node, move(token_location) });
|
2023-08-17 23:29:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-16 22:47:08 -03:00
|
|
|
|
ParseErrorOr<TokenizeTreeResult> tokenize_tree(SpecificationParsingContext& ctx, XML::Node const* node, bool allow_substeps)
|
2023-08-17 23:29:05 -03:00
|
|
|
|
{
|
|
|
|
|
|
TokenizeTreeResult result;
|
|
|
|
|
|
auto& tokens = result.tokens;
|
|
|
|
|
|
|
|
|
|
|
|
for (auto const& child : node->as_element().children) {
|
|
|
|
|
|
TRY(child->content.visit(
|
|
|
|
|
|
[&](XML::Node::Element const& element) -> ParseErrorOr<void> {
|
|
|
|
|
|
if (result.substeps != nullptr)
|
|
|
|
|
|
return ParseError::create("Substeps list must be the last non-empty child"sv, child);
|
|
|
|
|
|
|
2024-01-16 22:47:08 -03:00
|
|
|
|
Location child_location = ctx.location_from_xml_offset(child->offset);
|
|
|
|
|
|
|
2023-08-17 23:29:05 -03:00
|
|
|
|
if (element.name == tag_var) {
|
2024-01-16 22:47:08 -03:00
|
|
|
|
tokens.append({ TokenType::Identifier, TRY(get_text_contents(child)), child, move(child_location) });
|
2023-08-17 23:29:05 -03:00
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (element.name == tag_span) {
|
2024-01-16 23:24:55 -03:00
|
|
|
|
auto element_class = TRY(deprecated_get_attribute_by_name(child, attribute_class));
|
2023-08-17 23:29:05 -03:00
|
|
|
|
if (element_class != class_secnum)
|
|
|
|
|
|
return ParseError::create(String::formatted("Expected 'secnum' as a class name of <span>, but found '{}'", element_class), child);
|
2024-01-16 22:47:08 -03:00
|
|
|
|
tokens.append({ TokenType::SectionNumber, TRY(get_text_contents(child)), child, move(child_location) });
|
2023-08-17 23:29:05 -03:00
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (element.name == tag_emu_val) {
|
|
|
|
|
|
auto contents = TRY(get_text_contents(child));
|
|
|
|
|
|
if (contents.length() >= 2 && contents.starts_with('"') && contents.ends_with('"'))
|
2024-01-16 22:47:08 -03:00
|
|
|
|
tokens.append({ TokenType::String, contents.substring_view(1, contents.length() - 2), child, move(child_location) });
|
2023-08-17 23:29:05 -03:00
|
|
|
|
else if (contents == "undefined")
|
2024-01-16 22:47:08 -03:00
|
|
|
|
tokens.append({ TokenType::Undefined, contents, child, move(child_location) });
|
2023-08-17 23:29:05 -03:00
|
|
|
|
else
|
2024-01-16 22:47:08 -03:00
|
|
|
|
tokens.append({ TokenType::Identifier, contents, child, move(child_location) });
|
2023-08-17 23:29:05 -03:00
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (element.name == tag_emu_xref) {
|
|
|
|
|
|
auto contents = TRY(get_text_contents(TRY(get_only_child(child, "a"sv))));
|
2024-01-16 22:47:08 -03:00
|
|
|
|
tokens.append({ TokenType::Identifier, contents, child, move(child_location) });
|
2023-08-17 23:29:05 -03:00
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (element.name == tag_ol) {
|
|
|
|
|
|
if (!allow_substeps)
|
|
|
|
|
|
return ParseError::create("Found nested list but substeps are not allowed"sv, child);
|
|
|
|
|
|
result.substeps = child;
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ParseError::create(String::formatted("Unexpected child element with tag {}", element.name), child);
|
|
|
|
|
|
},
|
|
|
|
|
|
[&](XML::Node::Text const& text) -> ParseErrorOr<void> {
|
|
|
|
|
|
auto view = text.builder.string_view();
|
|
|
|
|
|
if (result.substeps && !contains_empty_text(child))
|
|
|
|
|
|
return ParseError::create("Substeps list must be the last non-empty child"sv, child);
|
2024-01-16 22:47:08 -03:00
|
|
|
|
return tokenize_string(ctx, child, view, tokens);
|
2023-08-17 23:29:05 -03:00
|
|
|
|
},
|
|
|
|
|
|
move(ignore_comments)));
|
|
|
|
|
|
}
|
2024-01-16 19:44:59 -03:00
|
|
|
|
|
|
|
|
|
|
if (tokens.size() && tokens.last().type == TokenType::MemberAccess)
|
|
|
|
|
|
tokens.last().type = TokenType::Dot;
|
|
|
|
|
|
|
2023-08-17 23:29:05 -03:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|