diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.cpp b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.cpp index 0c3e57b67e..4a7779a9f1 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.cpp @@ -72,7 +72,9 @@ Optional AlgorithmStep::create(SpecificationParsingContext& ctx, } auto [tokens, substeps] = tokenization_result.release_value(); - AlgorithmStep result { .m_tokens = move(tokens), .m_node = element }; + AlgorithmStep result(ctx); + result.m_tokens = move(tokens); + result.m_node = element; if (substeps) { // FIXME: Remove this once macOS Lagom CI updates to Clang >= 16. @@ -81,8 +83,7 @@ Optional AlgorithmStep::create(SpecificationParsingContext& ctx, auto step_list = ctx.with_new_step_list_nesting_level([&] { return AlgorithmStepList::create(ctx, substeps_copy); }); - if (step_list.has_value()) - result.m_substeps = step_list->m_expression; + result.m_substeps = step_list.has_value() ? step_list->tree() : error_tree; } auto parse_result = result.parse(); @@ -110,7 +111,6 @@ Optional AlgorithmStepList::create(SpecificationParsingContex VERIFY(element->as_element().name == tag_ol); AlgorithmStepList result; - auto& steps = result.m_steps; Vector step_expressions; bool all_steps_parsed = true; @@ -126,12 +126,10 @@ Optional AlgorithmStepList::create(SpecificationParsingContex update_logical_scope_for_step(ctx, parent_scope, step_number); return AlgorithmStep::create(ctx, child); }); - if (!step_creation_result.has_value()) { + if (!step_creation_result.has_value()) all_steps_parsed = false; - } else { - steps.append(step_creation_result.release_value()); - step_expressions.append(steps.last().m_expression); - } + else + step_expressions.append(step_creation_result.release_value().tree()); ++step_number; return; } @@ -214,8 +212,7 @@ Optional Algorithm::create(SpecificationParsingContext& ctx, XML::Nod auto steps_creation_result = AlgorithmStepList::create(ctx, steps_list[0]); if (steps_creation_result.has_value()) { Algorithm algorithm; - algorithm.m_steps = steps_creation_result.release_value(); - algorithm.m_tree = algorithm.m_steps.m_expression; + algorithm.m_tree = steps_creation_result.release_value().tree(); return algorithm; } return {}; @@ -226,8 +223,8 @@ NonnullOwnPtr SpecificationClause::create(SpecificationPars return ctx.with_new_logical_scope([&] { VERIFY(element->as_element().name == tag_emu_clause); - SpecificationClause specification_clause; - specification_clause.parse(ctx, element); + SpecificationClause specification_clause(ctx); + specification_clause.parse(element); OwnPtr result; @@ -239,7 +236,7 @@ NonnullOwnPtr SpecificationClause::create(SpecificationPars result = make(move(specification_clause)); }); - if (!result->post_initialize(ctx, element)) + if (!result->post_initialize(element)) result = make(move(*result)); return result.release_nonnull(); @@ -262,8 +259,9 @@ ParseErrorOr SpecificationClause::parse_header(XML::Node const* element) return {}; } -void SpecificationClause::parse(SpecificationParsingContext& ctx, XML::Node const* element) +void SpecificationClause::parse(XML::Node const* element) { + auto& ctx = context(); u32 child_index = 0; Optional> header_parse_error; @@ -314,10 +312,12 @@ void SpecificationClause::parse(SpecificationParsingContext& ctx, XML::Node cons } } -bool SpecFunction::post_initialize(SpecificationParsingContext& ctx, XML::Node const* element) +bool SpecFunction::post_initialize(XML::Node const* element) { VERIFY(element->as_element().name == tag_emu_clause); + auto& ctx = context(); + auto maybe_id = get_attribute_by_name(element, attribute_id); if (!maybe_id.has_value()) { ctx.diag().error(ctx.location_from_xml_offset(element->offset), @@ -376,7 +376,7 @@ bool SpecFunction::post_initialize(SpecificationParsingContext& ctx, XML::Node c void SpecFunction::do_collect(TranslationUnitRef translation_unit) { - translation_unit->adopt_function(make_ref_counted(m_name, m_algorithm.m_tree, move(m_arguments))); + translation_unit->adopt_function(make_ref_counted(m_name, m_algorithm.tree(), move(m_arguments))); } Specification Specification::create(SpecificationParsingContext& ctx, XML::Node const* element) diff --git a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.h b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.h index 2a4629b20c..d613cc25ea 100644 --- a/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.h +++ b/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Parser/SpecParser.h @@ -50,30 +50,42 @@ class AlgorithmStepList { public: static Optional create(SpecificationParsingContext& ctx, XML::Node const* element); - Vector m_steps; - Tree m_expression = error_tree; + Tree tree() const { return m_expression; } private: static void update_logical_scope_for_step(SpecificationParsingContext& ctx, LogicalLocation const& parent_scope, int step_number); + + Tree m_expression = error_tree; }; class AlgorithmStep { public: static Optional create(SpecificationParsingContext& ctx, XML::Node const* node); + Tree tree() const { return m_expression; } + +private: + AlgorithmStep(SpecificationParsingContext& ctx) + : m_ctx(ctx) + { + } + ParseErrorOr parse(); - Tree m_expression = error_tree; + SpecificationParsingContext& m_ctx; Vector m_tokens; - NullableTree m_substeps; XML::Node const* m_node; + Tree m_expression = error_tree; + NullableTree m_substeps; }; class Algorithm { public: static Optional create(SpecificationParsingContext& ctx, XML::Node const* element); - AlgorithmStepList m_steps; + Tree tree() const { return m_tree; } + +private: Tree m_tree = error_tree; }; @@ -88,16 +100,23 @@ public: void collect_into(TranslationUnitRef translation_unit); protected: - virtual bool post_initialize(SpecificationParsingContext& /*ctx*/, XML::Node const* /*element*/) { return true; } + virtual bool post_initialize(XML::Node const* /*element*/) { return true; } virtual void do_collect(TranslationUnitRef /*translation_unit*/) { } + SpecificationParsingContext& context() { return *m_ctx_pointer; } + ClauseHeader m_header; private: - SpecificationClause() = default; - ParseErrorOr parse_header(XML::Node const* element); - void parse(SpecificationParsingContext& ctx, XML::Node const* element); + SpecificationClause(SpecificationParsingContext& ctx) + : m_ctx_pointer(&ctx) + { + } + ParseErrorOr parse_header(XML::Node const* element); + void parse(XML::Node const* element); + + SpecificationParsingContext* m_ctx_pointer; Vector> m_subclauses; }; @@ -109,7 +128,7 @@ public: } protected: - bool post_initialize(SpecificationParsingContext& ctx, XML::Node const* element) override; + bool post_initialize(XML::Node const* element) override; void do_collect(TranslationUnitRef translation_unit) override; private: