1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:07:45 +00:00

JSSpecCompiler: Parse lists in xspec mode

This commit is contained in:
Dan Klishch 2024-01-20 22:48:05 -05:00 committed by Andrew Kaster
parent d14bb7e91e
commit e1a1f4ed1a
9 changed files with 102 additions and 0 deletions

View file

@ -64,6 +64,8 @@ void tokenize_string(SpecificationParsingContext& ctx, XML::Node const* node, St
{ ">"sv, TokenType::Greater },
{ "is"sv, TokenType::Is },
{ "<"sv, TokenType::Less },
{ "»"sv, TokenType::ListEnd },
{ "«"sv, TokenType::ListStart },
{ "."sv, TokenType::MemberAccess },
{ "×"sv, TokenType::Multiplication },
{ "is not equal to"sv, TokenType::NotEquals },

View file

@ -170,6 +170,30 @@ TextParseErrorOr<Vector<Tree>> TextParser::parse_function_arguments()
return arguments;
}
// <list_initialization> :== « (<expr> (, <expr>)*)? »
TextParseErrorOr<Tree> TextParser::parse_list_initialization()
{
auto rollback = rollback_point();
TRY(consume_token_with_type(TokenType::ListStart));
if (!consume_token_with_type(TokenType::ListEnd).is_error()) {
rollback.disarm();
return make_ref_counted<List>(Vector<Tree> {});
}
Vector<Tree> elements;
while (true) {
elements.append(TRY(parse_expression()));
auto token = TRY(consume_token_with_one_of_types({ TokenType::ListEnd, TokenType::Comma }));
if (token.type == TokenType::ListEnd)
break;
}
rollback.disarm();
return make_ref_counted<List>(move(elements));
}
// <expr>
TextParseErrorOr<Tree> TextParser::parse_expression()
{
@ -296,6 +320,9 @@ TextParseErrorOr<Tree> TextParser::parse_expression()
// This is just an opening '(' in expression.
stack.append(token);
}
} else if (token.type == TokenType::ListStart) {
stack.append(TRY(parse_list_initialization()));
is_consumed = true;
} else if (token.is_pre_merged_binary_operator()) {
THROW_PARSE_ERROR_IF(last_element_type != ExpressionType);
stack.append(token);

View file

@ -73,6 +73,7 @@ private:
TextParseErrorOr<Tree> parse_record_direct_list_initialization();
TextParseErrorOr<Vector<Tree>> parse_function_arguments();
TextParseErrorOr<Tree> parse_list_initialization();
TextParseErrorOr<Tree> parse_expression();
TextParseErrorOr<Tree> parse_condition();
TextParseErrorOr<Tree> parse_return_statement();

View file

@ -37,6 +37,8 @@ constexpr i32 closing_bracket_precedence = 18;
F(Identifier, -1, Invalid, Invalid, Invalid, "identifier") \
F(Is, -1, Invalid, Invalid, Invalid, "operator is") \
F(Less, 9, Invalid, CompareLess, Invalid, "less than") \
F(ListEnd, -1, Invalid, Invalid, Invalid, "»") \
F(ListStart, -1, Invalid, Invalid, Invalid, "«") \
F(MemberAccess, 2, Invalid, MemberAccess, Invalid, "member access operator '.'") \
F(Multiplication, 5, Invalid, Multiplication, Invalid, "multiplication") \
F(NotEquals, 10, Invalid, CompareNotEqual, Invalid, "not equals") \