From ef8767ef026eaf8f35eaccc7f8a1606d0c3e5e34 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sat, 14 Nov 2026 10:05:24 +0800 Subject: [PATCH 1/2] fix(WF-1): use inside sequence/parallel blocks per spec 3.2 (ruby + py) The canonical PG grammar's rule_line now accepts the use directive inside sequence/parallel blocks (sha 52949cb3... - previously b0f7a7ca...), and both consumers implement the rs-canonical semantics: - ruby: the concerns grammar + document_builder/node_adapter flatten a used ruleset's rules into the enclosing collection in position; an override block merges per-rule (child-wins on exact from-equality); a non-rule target (funcalls/stage runs inside the used ruleset) is a named load error - non-rule items cannot join a pass. - py: grammar regenerated via sync_isc_grammar.py (sha pin bumped, pin test enforces); the containment rejection in isc.py is removed and replaced by the same flatten + per-rule-merge + named-error path. rs is the reference implementation and is unchanged. No corpus map uses the constructs (428 maps parse unchanged). Gates: rake 428/0/11; py suite green (the one test_api failure is a pre-existing stale un-bul vector, fixed separately); py-vs-py parity 435/435 canonical + raw vs the hand parser; cross-grammar harness 412/435 identical - same three known divergence kinds as the clean-main baseline, no new divergences. --- .../src/interscript/grammar/actions.py | 4 +- .../src/interscript/grammar/grammar_source.py | 6 +- .../src/interscript/grammar/isc_parser.py | 36 +++---- interscript-py/src/interscript/isc.py | 78 +++++++++++--- interscript-py/tests/test_isc_parser.py | 58 +++++++++- lib/interscript/isc/document_builder.rb | 19 +++- .../isc/grammar/concerns/stages.rb | 5 +- lib/interscript/isc/grammar/isc.parg | 6 +- lib/interscript/isc/node_adapter.rb | 63 ++++++++++- spec/interscript/isc/wf1_grammar_spec.rb | 43 ++++++++ spec/interscript/isc/wf1_semantics_spec.rb | 100 ++++++++++++++++++ 11 files changed, 367 insertions(+), 51 deletions(-) diff --git a/interscript-py/src/interscript/grammar/actions.py b/interscript-py/src/interscript/grammar/actions.py index 3ba37f0f..bdc87f8c 100644 --- a/interscript-py/src/interscript/grammar/actions.py +++ b/interscript-py/src/interscript/grammar/actions.py @@ -393,8 +393,8 @@ def item_list(self, v: Tree | None) -> list | None: def rule_list(self, trees: list) -> list: """rule_items for parallel/sequence blocks: skip comment/noop rows; - `use` directives splice into the pass (GRAMMAR_EXTENSIONS - rule_items — the expansion resolves them in place).""" + `use` directives splice into the pass (canonical grammar + rule_line — the expansion resolves them in place, spec §3.2).""" out: list[Any] = [] for t in trees: h = _hash(t) diff --git a/interscript-py/src/interscript/grammar/grammar_source.py b/interscript-py/src/interscript/grammar/grammar_source.py index a0ef33aa..cd649a55 100644 --- a/interscript-py/src/interscript/grammar/grammar_source.py +++ b/interscript-py/src/interscript/grammar/grammar_source.py @@ -4,7 +4,7 @@ interscript-ruby lib/interscript/isc/grammar/isc.parg The .parg file is the single semantic source of the ISC syntax; this -module embeds it verbatim (sha256 b0f7a7caf462a1671a80ef974a59425c636f1d32cb3e405280991b21a6edd712) so the Python parser compiles +module embeds it verbatim (sha256 52949cb3170554071c8085bc5388553fde3d9a7feb590109d4543478d3561bb7) so the Python parser compiles the same grammar text the Ruby runtime ships. Update procedure: python scripts/sync_isc_grammar.py @@ -21,7 +21,7 @@ from __future__ import annotations # sha256 of the grammar text embedded below (pin for drift detection). -ISC_PARG_SHA256 = "b0f7a7caf462a1671a80ef974a59425c636f1d32cb3e405280991b21a6edd712" +ISC_PARG_SHA256 = "52949cb3170554071c8085bc5388553fde3d9a7feb590109d4543478d3561bb7" # The grammar text itself, byte-identical to the ruby checkout copy. -ISC_PARG_SOURCE = "# Interscript ISC grammar \u2014 parsanol grammar language (PG).\n#\n# Line-oriented port of lib/interscript/isc/grammar/concerns/*.rb.\n# The capture tree must reproduce the Ruby-DSL parser's tree exactly:\n# - repetitions are ABNF prefixes (1*x, *x)\n# - negation is !x, positive lookahead is &x\n# - captures at use sites (rule_ref as name) nest inner captures\n# the same way parslet .as does\n#\n# Consumed by the interscript gem (artifact parse) and parsanol npm.\n\ngrammar InterscriptIsc version \"0.1.0\" {\n\n # -- lexical primitives -------------------------------------------------\n\n chr = %x00-10FFFF\n space_char = %x20 / %x09 / %x0A / %x0B / %x0C / %x0D\n newline = %x0D.0A / %x0A / %x0D\n line_comment = \"#\" *(!(newline) chr)\n whitespace = 1*(space_char / line_comment)\n ws = *(space_char / line_comment)\n inline_space = 1*(%x20 / %x09)\n inline_ws = *(inline_space / line_comment)\n hexd = %x30-39 / %x41-46 / %x61-66\n digit = %x30-39\n\n ident_first = %x41-5A / %x61-7A / %x5F\n ident_rest = %x41-5A / %x61-7A / %x30-39 / %x5F\n identifier = (ident_first 0*ident_rest) as identifier\n\n keyword = \"parallel\" / \"sequence\" / \"stage\" / \"compose\" / \"separate\" / \"system\" / \"metadata\" / \"aliases\" / \"tests\" / \"notes\" / \"description\" / \"any_character\" / \"authority\" / \"dependency\" / \"run\" / \"sub\" / \"before\" / \"after\" / \"not_before\" / \"not_after\" / \"any\" / \"none\" / \"boundary\" / \"line_start\" / \"line_end\" / \"word_boundary\" / \"downcase\" / \"upcase\" / \"title_case\" / \"capture\" / \"maybe\" / \"some\" / \"ref\" / \"name\" / \"ruleset\" / \"use\" / \"override\" / \"languages\" / \"when\"\n\n # -- string literals ----------------------------------------------------\n\n esc_nl = (\"n\") as newline\n esc_cr = (\"r\") as carriage_return\n esc_tab = (\"t\") as tab\n esc_dq = \"\\\"\" as dquote\n esc_bs = \"\\\\\" as backslash\n esc_uni4 = \"u\" (4hexd) as unicode\n esc_uni8 = \"U\" (4*8hexd) as unicode\n esc_u_lone = \"U\" as u_lone\n escape_sequence = \"\\\\\" (esc_nl / esc_cr / esc_tab / esc_dq / esc_bs / esc_uni4 / esc_uni8 / esc_u_lone)\n\n raw_run = 1*(!(\"\\\\\" / \"\\\"\") chr)\n dq_parts = *(escape_sequence / (raw_run as run))\n double_quoted = \"\\\"\" (dq_parts as string) \"\\\"\"\n sq_body = *(!\"'\" chr)\n single_quoted = \"'\" (sq_body as string) \"'\"\n quoted_string = double_quoted / single_quoted\n\n arrow = [whitespace] \"->\" [whitespace]\n comma_ws = \",\" [whitespace]\n\n # -- metadata -----------------------------------------------------------\n\n md_authority = \"authority\" whitespace ((quoted_string) as authority)\n md_source_spelling = \"source_spelling\" whitespace ((quoted_string) as source_spelling)\n md_target_spelling = \"target_spelling\" whitespace ((quoted_string) as target_spelling)\n md_identifying = \"identifying\" whitespace ((quoted_string) as identifying)\n md_name = \"name\" whitespace ((quoted_string) as name)\n md_spec_value = quoted_string as specification\n md_specification = \"specification\" whitespace ((md_spec_value *(comma_ws md_spec_value)) as specification)\n md_description = \"description\" [whitespace] \"{\" [ws] (raw_text as description) [ws] \"}\"\n md_system_status = \"system_status\" whitespace ((\"current\" / \"former\" / \"inactive\") as system_status)\n md_code_status = \"code_status\" whitespace ((\"preferred\" / \"proposed\" / \"deprecated\") as code_status)\n\n relation_type = \"supersedes\" / \"superseded_by\" / \"based_on\" / \"basis_for\" / \"alias_of\" / \"adopted_from\" / \"related_to\"\n relation_note = whitespace \"note\" whitespace (quoted_string as note)\n relation_item = [whitespace] (relation_type as type) whitespace (quoted_string as system) [(relation_note)] [whitespace]\n md_relations = \"relations\" [whitespace] \"{\" [ws] ((*(relation_item)) as relations) [ws] \"}\"\n\n note_item = [whitespace] \"note\" [whitespace] (quoted_string as note) [whitespace]\n md_notes = \"notes\" [whitespace] \"{\" [ws] ((*(note_item)) as notes) [ws] \"}\"\n\n md_provenance_value = quoted_string as provenance\n md_provenance = \"provenance\" [whitespace] ((md_provenance_value *(comma_ws md_provenance_value)) as provenance)\n\n md_empty_field = &(newline)\n md_raw_field_value = 1*(!%x0A !\"}\" !\"{\" chr)\n md_field_value = quoted_string / (md_raw_field_value as raw)\n md_field_block = \"{\" [ws] (raw_text as field_block) [ws] \"}\"\n md_generic_field = (identifier as field_name) inline_ws ((md_field_value as field_value) / md_field_block / md_empty_field)\n\n metadata_field = [whitespace] (md_description / md_relations / md_system_status / md_code_status / md_specification / md_notes / md_provenance / md_generic_field) [whitespace]\n md_items = *(metadata_field)\n metadata_block = \"metadata\" [whitespace] \"{\" [ws] (md_items as metadata) [ws] \"}\"\n\n raw_text = *(\"\\\\\" chr / (!\"}\" chr))\n\n # -- aliases ------------------------------------------------------------\n\n alias_decl = [whitespace] (identifier as name) [whitespace] \"=\" [whitespace] (item as value) [whitespace]\n alias_items = *(alias_decl)\n aliases_block = \"aliases\" [whitespace] \"{\" [ws] (alias_items as aliases) [ws] \"}\"\n\n # -- dependencies -------------------------------------------------------\n\n dep_alias = whitespace \"as\" whitespace (identifier as alias)\n dependency_decl = \"dependency\" whitespace (quoted_string as target) [(dep_alias)]\n\n # -- item expressions ---------------------------------------------------\n\n zw_primitive = ((\"boundary\") / (\"line_start\") / (\"line_end\") / (\"word_boundary\") / (\"space\") / (\"non_boundary\")) as primitive\n fn_lit = ((\"upcase\") / (\"downcase\") / (\"title_case\") / (\"reverse\") / (\"strip\") / (\"swapcase\")) as function\n any_char_lit = (\"any_character\") as any_char\n none_lit = (\"none\") as none\n\n digit_cap = digit as digit\n capture_reference_body = \"ref\" \"(\" [ws] digit_cap [ws] \")\"\n capture_reference = capture_reference_body as ref\n\n item_ref = identifier \".\" (identifier as qualified_name)\n alias_reference = (!(keyword) identifier [(\".\" (identifier as qualified_name))] !\"{\" ) as alias\n\n item_seq = item_atom\n item_sep = ([ws] \"+\" [ws]) / whitespace\n item_cont = &item_atom\n\n range_arg = (quoted_string as lo) [ws] \"..\" [ws] (quoted_string as hi)\n list_item = item\n list_items = (list_item *((comma_ws / whitespace) list_item))\n set_arg = ((quoted_string as single)) / (\"[\" [ws] ((list_item *((comma_ws / whitespace) list_item)) as list) [ws] \"]\")\n alias_arg = (!zw_primitive !(keyword) identifier) as alias_ref\n any_arg = range_arg / set_arg / alias_arg\n any_ctor = \"any\" \"(\" [ws] (any_arg as any) [ws] \")\"\n capture_ctor = \"capture\" \"(\" [ws] (item as capture_inner) [ws] \")\"\n maybe_ctor = \"maybe\" \"(\" [ws] (item as maybe_inner) [ws] \")\"\n some_ctor = \"some\" \"(\" [ws] (item as some_inner) [ws] \")\"\n\n item_atom_start = \"\\\"\" / \"'\" / ident_first\n cont_stop = \"to\" / \"before\" / \"after\" / \"not_before\" / \"not_after\" / \"}\"\n alias_decl_stop = identifier [ws] \"=\"\n item_atom = quoted_string / none_lit / zw_primitive / any_char_lit / any_ctor / capture_ctor / maybe_ctor / some_ctor / fn_lit / capture_reference / alias_reference\n item = (item_atom 0*(item_sep !cont_stop !(alias_decl_stop) &item_atom_start item_atom)) as concatenation\n\n # -- rules and stages ---------------------------------------------------\n\n constraint_after = \"after\" whitespace (item as after)\n constraint_before = \"before\" whitespace (item as before)\n constraint_not_before = \"not_before\" whitespace (item as not_before)\n constraint_not_after = \"not_after\" whitespace (item as not_after)\n constraint = constraint_after / constraint_before / constraint_not_before / constraint_not_after\n constraints = *(whitespace constraint)\n\n compact_rule = \"sub\" whitespace (item_atom as from) whitespace (item_atom as to) ((*(whitespace constraint)) as constraints) [(when_guard)]\n\n block_rule = \"sub\" [ws] \"{\" [ws] \"from\" whitespace (item as from) [ws] \"to\" whitespace (item as to) ((*(whitespace constraint)) as constraints) [(when_guard)] [ws] \"}\"\n\n when_lang = (!(keyword) identifier) as when_lang\n when_guard = (whitespace \"when\" whitespace (when_lang) *(((comma_ws / whitespace) (when_lang)))) as when\n\n rule = block_rule / compact_rule\n\n comment_item = ((\"#\" *(!%x0A chr)) as comment) / ((!(keyword) identifier) as noop)\n rule_line = [ws] (rule / comment_item) [ws]\n rule_items = *(rule_line)\n sequence_block = \"sequence\" [ws] \"{\" [ws] (rule_items as sequence) [ws] \"}\"\n parallel_block = \"parallel\" [ws] \"{\" [ws] (rule_items as parallel) [ws] \"}\"\n\n separator_part = whitespace \"separator\" whitespace (item_atom as separator)\n separate_directive = (\"separate\") as separate [(separator_part)]\n case_directive = ((\"downcase\") / (\"upcase\")) as case\n case_kwargs_part = whitespace (kwarg_list as case_kwargs)\n title_case_directive = (\"title_case\") as case [(case_kwargs_part)]\n string_case_directive = case_directive / title_case_directive\n compose_directive = ((\"compose\") / (\"decompose\")) as compose\n kwarg = (identifier as kwarg_name) [ws] \":\" [ws] ((quoted_string) as kwarg_value)\n kwarg_item = (kwarg) as kwarg\n kwarg_list = (kwarg_item *(comma_ws kwarg_item))\n funcall_kwargs_part = whitespace (kwarg_list as funcall_kwargs)\n rababa_directive = (\"rababa\") as funcall_name [(funcall_kwargs_part)]\n run_dep = \"map.\" (identifier as dep) \".stage.\" (identifier as stage)\n run_stage_only = (\"stage.\" (identifier as stage)) as run_stage_only\n run_rule = \"run\" whitespace ((run_dep) / run_stage_only)\n\n bare_rule = (rule) as bare_rule\n\n use_alias = (identifier) as use_alias\n use_qualified = \".\" (identifier as qualified_name)\n use_overrides_part = whitespace \"override\" [ws] \"{\" [ws] ((*(rule_line)) as use_overrides) [ws] \"}\"\n use_directive = ((\"use\" whitespace (use_alias) [(use_qualified)] [(use_overrides_part)]) as use)\n\n stage_item = [ws] (sequence_block / parallel_block / run_rule / separate_directive / string_case_directive / compose_directive / rababa_directive / use_directive / bare_rule / comment_item) [ws]\n stage_items = *(stage_item)\n stage_name_paren = \"(\" (identifier as stage_name) \")\"\n stage_name_ws = whitespace (identifier as stage_name)\n stage_block = \"stage\" (stage_name_paren / stage_name_ws) [ws] \"{\" [ws] (stage_items as stage) [ws] \"}\"\n\n ruleset_name_ws = whitespace (identifier as ruleset_name)\n ruleset_block = \"ruleset\" (ruleset_name_ws) [ws] \"{\" [ws] (stage_items as ruleset) [ws] \"}\"\n\n # -- tests ---------------------------------------------------------------\n\n test_note = whitespace \"note\" whitespace (quoted_string as note)\n test_line = [whitespace] (quoted_string as input) arrow (quoted_string as expected) [(test_note)] [whitespace]\n test_items = *(test_line)\n tests_block = \"tests\" [whitespace] \"{\" [ws] (test_items as tests) [ws] \"}\"\n\n # -- system --------------------------------------------------------------\n\n language_ident = (!(keyword) identifier) as language\n languages_decl = (\"languages\" whitespace ((language_ident *(((comma_ws / whitespace) (language_ident))))) as languages)\n\n block_item = [whitespace] (metadata_block / aliases_block / tests_block / languages_decl / ruleset_block / stage_block / dependency_decl) [whitespace]\n body_items = *(block_item)\n system_block = \"system\" whitespace ((quoted_string) as system_code) [whitespace] \"{\" [ws] ((*(block_item)) as body) [ws] \"}\" [whitespace]\n isc_source = [whitespace] ((system_block) as system) [whitespace]\n\n entry isc: isc_source\n}\n" +ISC_PARG_SOURCE = "# Interscript ISC grammar \u2014 parsanol grammar language (PG).\n#\n# Line-oriented port of lib/interscript/isc/grammar/concerns/*.rb.\n# The capture tree must reproduce the Ruby-DSL parser's tree exactly:\n# - repetitions are ABNF prefixes (1*x, *x)\n# - negation is !x, positive lookahead is &x\n# - captures at use sites (rule_ref as name) nest inner captures\n# the same way parslet .as does\n#\n# Consumed by the interscript gem (artifact parse) and parsanol npm.\n\ngrammar InterscriptIsc version \"0.1.0\" {\n\n # -- lexical primitives -------------------------------------------------\n\n chr = %x00-10FFFF\n space_char = %x20 / %x09 / %x0A / %x0B / %x0C / %x0D\n newline = %x0D.0A / %x0A / %x0D\n line_comment = \"#\" *(!(newline) chr)\n whitespace = 1*(space_char / line_comment)\n ws = *(space_char / line_comment)\n inline_space = 1*(%x20 / %x09)\n inline_ws = *(inline_space / line_comment)\n hexd = %x30-39 / %x41-46 / %x61-66\n digit = %x30-39\n\n ident_first = %x41-5A / %x61-7A / %x5F\n ident_rest = %x41-5A / %x61-7A / %x30-39 / %x5F\n identifier = (ident_first 0*ident_rest) as identifier\n\n keyword = \"parallel\" / \"sequence\" / \"stage\" / \"compose\" / \"separate\" / \"system\" / \"metadata\" / \"aliases\" / \"tests\" / \"notes\" / \"description\" / \"any_character\" / \"authority\" / \"dependency\" / \"run\" / \"sub\" / \"before\" / \"after\" / \"not_before\" / \"not_after\" / \"any\" / \"none\" / \"boundary\" / \"line_start\" / \"line_end\" / \"word_boundary\" / \"downcase\" / \"upcase\" / \"title_case\" / \"capture\" / \"maybe\" / \"some\" / \"ref\" / \"name\" / \"ruleset\" / \"use\" / \"override\" / \"languages\" / \"when\"\n\n # -- string literals ----------------------------------------------------\n\n esc_nl = (\"n\") as newline\n esc_cr = (\"r\") as carriage_return\n esc_tab = (\"t\") as tab\n esc_dq = \"\\\"\" as dquote\n esc_bs = \"\\\\\" as backslash\n esc_uni4 = \"u\" (4hexd) as unicode\n esc_uni8 = \"U\" (4*8hexd) as unicode\n esc_u_lone = \"U\" as u_lone\n escape_sequence = \"\\\\\" (esc_nl / esc_cr / esc_tab / esc_dq / esc_bs / esc_uni4 / esc_uni8 / esc_u_lone)\n\n raw_run = 1*(!(\"\\\\\" / \"\\\"\") chr)\n dq_parts = *(escape_sequence / (raw_run as run))\n double_quoted = \"\\\"\" (dq_parts as string) \"\\\"\"\n sq_body = *(!\"'\" chr)\n single_quoted = \"'\" (sq_body as string) \"'\"\n quoted_string = double_quoted / single_quoted\n\n arrow = [whitespace] \"->\" [whitespace]\n comma_ws = \",\" [whitespace]\n\n # -- metadata -----------------------------------------------------------\n\n md_authority = \"authority\" whitespace ((quoted_string) as authority)\n md_source_spelling = \"source_spelling\" whitespace ((quoted_string) as source_spelling)\n md_target_spelling = \"target_spelling\" whitespace ((quoted_string) as target_spelling)\n md_identifying = \"identifying\" whitespace ((quoted_string) as identifying)\n md_name = \"name\" whitespace ((quoted_string) as name)\n md_spec_value = quoted_string as specification\n md_specification = \"specification\" whitespace ((md_spec_value *(comma_ws md_spec_value)) as specification)\n md_description = \"description\" [whitespace] \"{\" [ws] (raw_text as description) [ws] \"}\"\n md_system_status = \"system_status\" whitespace ((\"current\" / \"former\" / \"inactive\") as system_status)\n md_code_status = \"code_status\" whitespace ((\"preferred\" / \"proposed\" / \"deprecated\") as code_status)\n\n relation_type = \"supersedes\" / \"superseded_by\" / \"based_on\" / \"basis_for\" / \"alias_of\" / \"adopted_from\" / \"related_to\"\n relation_note = whitespace \"note\" whitespace (quoted_string as note)\n relation_item = [whitespace] (relation_type as type) whitespace (quoted_string as system) [(relation_note)] [whitespace]\n md_relations = \"relations\" [whitespace] \"{\" [ws] ((*(relation_item)) as relations) [ws] \"}\"\n\n note_item = [whitespace] \"note\" [whitespace] (quoted_string as note) [whitespace]\n md_notes = \"notes\" [whitespace] \"{\" [ws] ((*(note_item)) as notes) [ws] \"}\"\n\n md_provenance_value = quoted_string as provenance\n md_provenance = \"provenance\" [whitespace] ((md_provenance_value *(comma_ws md_provenance_value)) as provenance)\n\n md_empty_field = &(newline)\n md_raw_field_value = 1*(!%x0A !\"}\" !\"{\" chr)\n md_field_value = quoted_string / (md_raw_field_value as raw)\n md_field_block = \"{\" [ws] (raw_text as field_block) [ws] \"}\"\n md_generic_field = (identifier as field_name) inline_ws ((md_field_value as field_value) / md_field_block / md_empty_field)\n\n metadata_field = [whitespace] (md_description / md_relations / md_system_status / md_code_status / md_specification / md_notes / md_provenance / md_generic_field) [whitespace]\n md_items = *(metadata_field)\n metadata_block = \"metadata\" [whitespace] \"{\" [ws] (md_items as metadata) [ws] \"}\"\n\n raw_text = *(\"\\\\\" chr / (!\"}\" chr))\n\n # -- aliases ------------------------------------------------------------\n\n alias_decl = [whitespace] (identifier as name) [whitespace] \"=\" [whitespace] (item as value) [whitespace]\n alias_items = *(alias_decl)\n aliases_block = \"aliases\" [whitespace] \"{\" [ws] (alias_items as aliases) [ws] \"}\"\n\n # -- dependencies -------------------------------------------------------\n\n dep_alias = whitespace \"as\" whitespace (identifier as alias)\n dependency_decl = \"dependency\" whitespace (quoted_string as target) [(dep_alias)]\n\n # -- item expressions ---------------------------------------------------\n\n zw_primitive = ((\"boundary\") / (\"line_start\") / (\"line_end\") / (\"word_boundary\") / (\"space\") / (\"non_boundary\")) as primitive\n fn_lit = ((\"upcase\") / (\"downcase\") / (\"title_case\") / (\"reverse\") / (\"strip\") / (\"swapcase\")) as function\n any_char_lit = (\"any_character\") as any_char\n none_lit = (\"none\") as none\n\n digit_cap = digit as digit\n capture_reference_body = \"ref\" \"(\" [ws] digit_cap [ws] \")\"\n capture_reference = capture_reference_body as ref\n\n item_ref = identifier \".\" (identifier as qualified_name)\n alias_reference = (!(keyword) identifier [(\".\" (identifier as qualified_name))] !\"{\" ) as alias\n\n item_seq = item_atom\n item_sep = ([ws] \"+\" [ws]) / whitespace\n item_cont = &item_atom\n\n range_arg = (quoted_string as lo) [ws] \"..\" [ws] (quoted_string as hi)\n list_item = item\n list_items = (list_item *((comma_ws / whitespace) list_item))\n set_arg = ((quoted_string as single)) / (\"[\" [ws] ((list_item *((comma_ws / whitespace) list_item)) as list) [ws] \"]\")\n alias_arg = (!zw_primitive !(keyword) identifier) as alias_ref\n any_arg = range_arg / set_arg / alias_arg\n any_ctor = \"any\" \"(\" [ws] (any_arg as any) [ws] \")\"\n capture_ctor = \"capture\" \"(\" [ws] (item as capture_inner) [ws] \")\"\n maybe_ctor = \"maybe\" \"(\" [ws] (item as maybe_inner) [ws] \")\"\n some_ctor = \"some\" \"(\" [ws] (item as some_inner) [ws] \")\"\n\n item_atom_start = \"\\\"\" / \"'\" / ident_first\n cont_stop = \"to\" / \"before\" / \"after\" / \"not_before\" / \"not_after\" / \"}\"\n alias_decl_stop = identifier [ws] \"=\"\n item_atom = quoted_string / none_lit / zw_primitive / any_char_lit / any_ctor / capture_ctor / maybe_ctor / some_ctor / fn_lit / capture_reference / alias_reference\n item = (item_atom 0*(item_sep !cont_stop !(alias_decl_stop) &item_atom_start item_atom)) as concatenation\n\n # -- rules and stages ---------------------------------------------------\n\n constraint_after = \"after\" whitespace (item as after)\n constraint_before = \"before\" whitespace (item as before)\n constraint_not_before = \"not_before\" whitespace (item as not_before)\n constraint_not_after = \"not_after\" whitespace (item as not_after)\n constraint = constraint_after / constraint_before / constraint_not_before / constraint_not_after\n constraints = *(whitespace constraint)\n\n compact_rule = \"sub\" whitespace (item_atom as from) whitespace (item_atom as to) ((*(whitespace constraint)) as constraints) [(when_guard)]\n\n block_rule = \"sub\" [ws] \"{\" [ws] \"from\" whitespace (item as from) [ws] \"to\" whitespace (item as to) ((*(whitespace constraint)) as constraints) [(when_guard)] [ws] \"}\"\n\n when_lang = (!(keyword) identifier) as when_lang\n when_guard = (whitespace \"when\" whitespace (when_lang) *(((comma_ws / whitespace) (when_lang)))) as when\n\n rule = block_rule / compact_rule\n\n comment_item = ((\"#\" *(!%x0A chr)) as comment) / ((!(keyword) identifier) as noop)\n # ISC-WF-1 \u00a73.2: a `use` directive is a rule-line item \u2014 items inside a\n # parallel/sequence block participate in that block's single pass, and\n # the expansion flattens the referenced ruleset's rules at this\n # position (mirrors interscript-rs grammar/isc.parg rule_line).\n rule_line = [ws] (rule / use_directive / comment_item) [ws]\n rule_items = *(rule_line)\n sequence_block = \"sequence\" [ws] \"{\" [ws] (rule_items as sequence) [ws] \"}\"\n parallel_block = \"parallel\" [ws] \"{\" [ws] (rule_items as parallel) [ws] \"}\"\n\n separator_part = whitespace \"separator\" whitespace (item_atom as separator)\n separate_directive = (\"separate\") as separate [(separator_part)]\n case_directive = ((\"downcase\") / (\"upcase\")) as case\n case_kwargs_part = whitespace (kwarg_list as case_kwargs)\n title_case_directive = (\"title_case\") as case [(case_kwargs_part)]\n string_case_directive = case_directive / title_case_directive\n compose_directive = ((\"compose\") / (\"decompose\")) as compose\n kwarg = (identifier as kwarg_name) [ws] \":\" [ws] ((quoted_string) as kwarg_value)\n kwarg_item = (kwarg) as kwarg\n kwarg_list = (kwarg_item *(comma_ws kwarg_item))\n funcall_kwargs_part = whitespace (kwarg_list as funcall_kwargs)\n rababa_directive = (\"rababa\") as funcall_name [(funcall_kwargs_part)]\n run_dep = \"map.\" (identifier as dep) \".stage.\" (identifier as stage)\n run_stage_only = (\"stage.\" (identifier as stage)) as run_stage_only\n run_rule = \"run\" whitespace ((run_dep) / run_stage_only)\n\n bare_rule = (rule) as bare_rule\n\n use_alias = (identifier) as use_alias\n use_qualified = \".\" (identifier as qualified_name)\n use_overrides_part = whitespace \"override\" [ws] \"{\" [ws] ((*(rule_line)) as use_overrides) [ws] \"}\"\n use_directive = ((\"use\" whitespace (use_alias) [(use_qualified)] [(use_overrides_part)]) as use)\n\n stage_item = [ws] (sequence_block / parallel_block / run_rule / separate_directive / string_case_directive / compose_directive / rababa_directive / use_directive / bare_rule / comment_item) [ws]\n stage_items = *(stage_item)\n stage_name_paren = \"(\" (identifier as stage_name) \")\"\n stage_name_ws = whitespace (identifier as stage_name)\n stage_block = \"stage\" (stage_name_paren / stage_name_ws) [ws] \"{\" [ws] (stage_items as stage) [ws] \"}\"\n\n ruleset_name_ws = whitespace (identifier as ruleset_name)\n ruleset_block = \"ruleset\" (ruleset_name_ws) [ws] \"{\" [ws] (stage_items as ruleset) [ws] \"}\"\n\n # -- tests ---------------------------------------------------------------\n\n test_note = whitespace \"note\" whitespace (quoted_string as note)\n test_line = [whitespace] (quoted_string as input) arrow (quoted_string as expected) [(test_note)] [whitespace]\n test_items = *(test_line)\n tests_block = \"tests\" [whitespace] \"{\" [ws] (test_items as tests) [ws] \"}\"\n\n # -- system --------------------------------------------------------------\n\n language_ident = (!(keyword) identifier) as language\n languages_decl = (\"languages\" whitespace ((language_ident *(((comma_ws / whitespace) (language_ident))))) as languages)\n\n block_item = [whitespace] (metadata_block / aliases_block / tests_block / languages_decl / ruleset_block / stage_block / dependency_decl) [whitespace]\n body_items = *(block_item)\n system_block = \"system\" whitespace ((quoted_string) as system_code) [whitespace] \"{\" [ws] ((*(block_item)) as body) [ws] \"}\" [whitespace]\n isc_source = [whitespace] ((system_block) as system) [whitespace]\n\n entry isc: isc_source\n}\n" diff --git a/interscript-py/src/interscript/grammar/isc_parser.py b/interscript-py/src/interscript/grammar/isc_parser.py index a1cb2e3a..214d3aa2 100644 --- a/interscript-py/src/interscript/grammar/isc_parser.py +++ b/interscript-py/src/interscript/grammar/isc_parser.py @@ -12,7 +12,7 @@ Single-sourcing contract: the grammar TEXT embedded in grammar_source.py is the semantic source of truth (synced from interscript-ruby's lib/interscript/isc/grammar/isc.parg via scripts/sync_isc_grammar.py). -The four documented grammar-table extensions below (GRAMMAR_EXTENSIONS) +The three documented grammar-table extensions below (GRAMMAR_EXTENSIONS) restore hand-parser behaviors the canonical .parg rejects — each is spelled in the grammar language, named, and corpus-proven dormant. """ @@ -69,7 +69,7 @@ def compile_grammar() -> CompiledGrammar: # Shape-guarded merge: if a future .parg edit renames any of the three # rules, the merge raises instead of silently misparsing. ext = parse_parg(GRAMMAR_EXTENSIONS) - for name in ("any_arg", "block_rule", "run_dep", "rule_items"): + for name in ("any_arg", "block_rule", "run_dep"): if name not in rules or name not in ext.rules: raise RuntimeError( f"grammar extension target {name} missing from isc.parg — " @@ -141,7 +141,6 @@ def _hand_style_failure(source, c, interp, peg_error) -> tuple[str, int]: any_arg = range_arg / set_arg / alias_arg / ((item) as any_item) block_rule = ("sub" [ws] "{" [ws] "from" whitespace ((item) as from) [ws] "to" whitespace ((item) as to) ((*(whitespace constraint)) as constraints) [ws] "}" [(when_guard)]) / ("sub" [ws] "{" [ws] "from" whitespace ((item) as from) [ws] "to" whitespace ((item) as to) ((*(whitespace constraint)) as constraints) [(when_guard)] [ws] "}") run_dep = "map." (((identifier *("-" identifier)) / identifier) as dep) ".stage." (identifier as stage) - rule_items = *(([ws] ((use_directive / rule / comment_item)) [ws])) } """ @@ -149,8 +148,8 @@ def _hand_style_failure(source, c, interp, peg_error) -> tuple[str, int]: # syntax the canonical .parg rejects. Each is spelled in the grammar # language itself and merged into the rule table, so the .parg remains # the only semantic source and every extension is one visible, named -# block (the first three are the same extensions the ts generated -# parser ships): +# block (all three are the same extensions the ts generated parser +# ships): # # 1. `any_arg` — restore the ruby DSL's final `item as :any_item` # fallback (concerns/items.rb any_constructor), which the canonical @@ -167,20 +166,15 @@ def _hand_style_failure(source, c, interp, peg_error) -> tuple[str, int]: # parser's identifier admitted `-` in structural positions. Only the # dependency slot is widened; the corpus is hyphen-free in every # identifier position (proven by the corpus parity driver). -# 4. `rule_items` — accept `use` directives inside parallel/sequence -# blocks (ISC-WF-1 §3: a ruleset can be spliced into one pass). The -# canonical .parg only feeds rule_line (sub-rows/comments) to pass -# blocks; the hand parser accepted `use` there and the py suite pins -# it (test_isc_parser.py::test_use_inside_parallel_participates_in_pass). -# `use` shadows nothing in rule position (rule always starts with -# "sub", comment with "#"). The override block's rule_line is left -# canonical — the hand parser silently DROPPED non-sub rows there, -# so a `use` inside `override {}` is a generated-parser rejection of -# a construct the corpus never uses. +# The former fourth extension (`rule_items` accepting `use` inside +# parallel/sequence blocks) was RETIRED by WO18 G5: the canonical .parg +# now carries use_directive in rule_line (spec §3.2, mirroring the rs +# grammar), so the extension is redundant and its merge-guard slot is +# gone. The override block's rule_line accepts `use` rows for free — +# the expansion flattens them (isc._Expander._expand_override_rows). # -# Ruby rejects (1)'s absence aside, the outside-when, hyphen-dep, and -# use-in-pass forms are cross-runtime divergences (ruby parses none); -# aligning ruby/ts/py on one placement is an owner decision. None of the -# four extensions fires on any corpus map (proven: the corpus py-vs-py -# document-diff driver passes byte-identical with them in place, and a -# corpus-wide scan finds no `use` inside a pass block). +# Ruby rejects (1)'s absence aside, the outside-when and hyphen-dep +# forms are cross-runtime divergences (ruby parses none); aligning +# ruby/ts/py on one placement is an owner decision. None of the three +# extensions fires on any corpus map (proven: the corpus py-vs-py +# document-diff driver passes byte-identical with them in place). diff --git a/interscript-py/src/interscript/isc.py b/interscript-py/src/interscript/isc.py index c8189e89..73078746 100644 --- a/interscript-py/src/interscript/isc.py +++ b/interscript-py/src/interscript/isc.py @@ -45,9 +45,10 @@ corpus-dormant; see grammar/actions.py for the full deviation list. - `languages` is declared once per map; a second declaration is a parse error. `use` may appear in stage bodies, ruleset bodies and inside - `parallel`/`sequence` blocks; a ruleset whose expansion contains a - pass block may not itself be used inside a pass block (a nested pass - has no meaning in the one-simultaneous-pass model). + `parallel`/`sequence` blocks (WO18 G5, spec §3.2): inside a pass block + the expansion flattens the referenced ruleset's rules into the + enclosing collection, and a referenced ruleset carrying non-rule + items (funcalls/stage runs) is a named load error. Performance note (measured, same machine): the generated parser parses the full 434-file corpus in ~86s where the hand parser took ~7s — the @@ -219,11 +220,6 @@ def expand_items( if isinstance(it, RawUse): out.extend(self.expand_use(it, chain, in_block)) continue - if in_block and isinstance(it, (ParallelBlock, SequenceBlock)): - self.error( - "a ruleset containing a parallel/sequence block cannot be " - "used inside a parallel/sequence block" - ) if isinstance(it, (ParallelBlock, SequenceBlock)): it = type(it)(rules=tuple(self.expand_items(list(it.rules), chain, True))) out.append(it) @@ -237,16 +233,72 @@ def expand_use( else: expanded = self.expand_ruleset(use.name, chain) - if in_block and any(isinstance(x, (ParallelBlock, SequenceBlock)) for x in expanded): - self.error( - "a ruleset containing a parallel/sequence block cannot be used " - "inside a parallel/sequence block" - ) + if in_block: + # WO18 G5 — spec §3.2: items inside a parallel/sequence block + # participate in that block's single pass; the expansion + # inherits the enclosing pass structure — the target's rule + # content flattens into the enclosing collection (interscript-rs + # resolve_use_rules). Overrides merge per-rule on the flat list. + rules = self._flatten_pass_items(expanded, chain) + if use.override is not None: + rules = self._merge_override_rules( + rules, self._expand_override_rows(use.override, chain) + ) + return rules if use.override is not None: expanded = self._apply_override(expanded, list(use.override)) return list(expanded) + def _flatten_pass_items( + self, items: list[Any], chain: tuple[tuple[str, str], ...] + ) -> list[Any]: + """WO18 G5: flatten a structural expansion into the ordered rule + list of an enclosing pass (§3.2). Pass blocks contribute their + (already-expanded) rules in position; raw uses inside them were + flattened during expand_items. Non-rule items (funcalls, stage + runs) cannot join a pass — named load error.""" + out: list[Any] = [] + for it in items: + if isinstance(it, (ParallelBlock, SequenceBlock)): + out.extend(it.rules) + elif isinstance(it, ParsedRule): + out.append(it) + elif isinstance(it, RawUse): + out.extend(self.expand_use(it, chain, True)) + else: + self.error( + "use inside a parallel/sequence block: the referenced ruleset " + "carries non-rule items (funcalls/stage runs) that cannot join " + "the pass" + ) + return out + + def _expand_override_rows( + self, rows: tuple[ParsedRule, ...], chain: tuple[tuple[str, str], ...] + ) -> list[ParsedRule]: + """WO18 G5: an override block is a rule_line list, so its rows may + themselves be `use` directives (rs extract_rule_list recursion); + their content flattens into the override row list.""" + out: list[ParsedRule] = [] + for row in rows: + if isinstance(row, RawUse): + out.extend(self.expand_use(row, chain, True)) + else: + out.append(row) + return out + + def _merge_override_rules( + self, rules: list[Any], overrides: list[ParsedRule] + ) -> list[Any]: + """§4.2 child-wins merge over a FLAT rule list (the block-position + use): matching rules drop wherever they sit; the override rows land + at the expansion tail. No trailing-parallel join here — the caller + re-wraps the list into the enclosing block's structure.""" + o_keys = {self._from_key(o) for o in overrides} + kept = [r for r in rules if self._from_key(r) not in o_keys] + return kept + list(overrides) + def expand_via_dependency( self, use: RawUse, chain: tuple[tuple[str, str], ...] ) -> list[Any]: diff --git a/interscript-py/tests/test_isc_parser.py b/interscript-py/tests/test_isc_parser.py index 087c4f2f..44fa0b65 100644 --- a/interscript-py/tests/test_isc_parser.py +++ b/interscript-py/tests/test_isc_parser.py @@ -310,13 +310,65 @@ def test_unknown_ruleset_error(self): with pytest.raises(IscParseError, match="unknown ruleset"): parse('system "s" { stage main { use nope } }') - def test_block_in_block_error(self): - with pytest.raises(IscParseError, match="parallel/sequence block"): + # WO18 G5 — §3.2: items inside a pass block participate in that + # block's single pass; a ruleset containing a parallel block + # contributes that block as one pass at the expansion position. + # The expansion flattens the referenced ruleset's rules into the + # enclosing collection; non-rule targets are a named load error. + + def test_use_in_sequence_flattens_in_position(self): + # mirrors maps probe tst-use-inseq (rs expects "12") + doc = parse( + 'system "T:tst-use-inseq:1" { ruleset r1 { parallel { sub "a" "1" } } ' + 'stage main { sequence { use r1 sub "b" "2" } } }' + ) + cm = isc_to_compiled_map(doc) + assert execute_stage(cm, "main", "ab") == "12" + + def test_use_in_parallel_joins_the_pass(self): + doc = parse( + 'system "T:tst-use-inpar:1" { ruleset r1 { parallel { sub "a" "1" } } ' + 'stage main { parallel { use r1 sub "b" "2" } } }' + ) + cm = isc_to_compiled_map(doc) + # one simultaneous pass: a→1 and b→2 together + assert execute_stage(cm, "main", "ab") == "12" + + def test_ruleset_with_parallel_block_is_one_pass_in_sequence(self): + doc = parse( + 'system "s" { ruleset tbl { parallel { sub "a" "b" } sub "c" "d" } ' + 'stage main { sequence { use tbl sub "e" "E" } } }' + ) + # the used ruleset's rules splice flat, in position (spec §3.2) + seq = doc.stages[0].items[0] + assert [row(r)[:2] for r in seq.rules] == [("a", "b"), ("c", "d"), ("e", "E")] + + def test_override_in_block_merges_per_rule(self): + doc = parse( + 'system "s" { ruleset tbl { parallel { sub "a" "A1" sub "b" "B" } } ' + 'stage main { parallel { use tbl override { sub "a" "A2" } } } }' + ) + cm = isc_to_compiled_map(doc) + # child-wins replacement on the flat row list, all inside one pass + assert execute_stage(cm, "main", "xa xb") == "xA2 xB" + + def test_non_rule_target_in_block_is_named_error(self): + with pytest.raises(IscParseError, match="cannot join the pass"): parse( - 'system "s" { ruleset tbl { parallel { sub "a" "b" } } ' + 'system "s" { ruleset tbl { sub "a" "A" upcase } ' 'stage main { parallel { use tbl } } }' ) + def test_non_rule_target_through_nested_use_is_named_error(self): + # the funcall sits in the indirectly referenced ruleset; the + # flattening reaches it through one use-of-a-use hop + with pytest.raises(IscParseError, match="cannot join the pass"): + parse( + 'system "s" { ruleset tbl { sub "a" "A" upcase } ' + 'ruleset wrap { use tbl } ' + 'stage main { sequence { use wrap } } }' + ) + def test_cross_dependency_use(self): base = parse('system "base" { ruleset tbl { sub "a" "A" sub "b" "B" } }') doc = parse( diff --git a/lib/interscript/isc/document_builder.rb b/lib/interscript/isc/document_builder.rb index 9d889aca..2b04b0ad 100644 --- a/lib/interscript/isc/document_builder.rb +++ b/lib/interscript/isc/document_builder.rb @@ -279,9 +279,9 @@ def extract_ruleset(item) def extract_stage_items(n) return [] unless n.is_a?(Hash) if n[:sequence] - [{kind: :sequence, rules: filter_noop(Array(n[:sequence]).map { |r| extract_rule(r) })}] + [{kind: :sequence, rules: filter_noop(Array(n[:sequence]).map { |r| extract_stage_row(r) })}] elsif n[:parallel] - [{kind: :parallel, rules: filter_noop(Array(n[:parallel]).map { |r| extract_rule(r) })}] + [{kind: :parallel, rules: filter_noop(Array(n[:parallel]).map { |r| extract_stage_row(r) })}] elsif n[:separate] [{kind: :separate, separator: n[:separator] ? materialize(n[:separator]) : nil}] elsif n[:compose] @@ -314,15 +314,26 @@ def extract_stage_items(n) end end + # WO18 G5: one rule-line entry of a parallel/sequence block — a + # rule def, or (since §3.2) a `use` directive splicing into the + # block's rule list. + def extract_stage_row(r) + return extract_use(r[:use]) if r.is_a?(Hash) && r.key?(:use) + extract_rule(r) + end + # `use ` (same-map ruleset) or `use .` (dependency- # declared ruleset), plus the optional override rows (W2). def extract_use(u) dep = u[:qualified_name] ? ident(u[:use_alias]) : nil name = ident(u[:qualified_name] || u[:use_alias]) item = {kind: :use, ruleset: name, dependency: dep} + # WO18 G5: override rows are rule_line items, so they may + # themselves be `use` directives — carried as nested use defs and + # flattened at expansion time (rs extract_rule_list recursion). overrides = Array(u[:use_overrides]) - .select { |r| r.is_a?(Hash) && r.key?(:from) } - .map { |r| extract_rule(r) } + .select { |r| r.is_a?(Hash) && (r.key?(:from) || r.key?(:use)) } + .map { |r| r.key?(:use) ? extract_use(r[:use]) : extract_rule(r) } item[:overrides] = overrides if overrides.any? item end diff --git a/lib/interscript/isc/grammar/concerns/stages.rb b/lib/interscript/isc/grammar/concerns/stages.rb index b0487fce..489c2856 100644 --- a/lib/interscript/isc/grammar/concerns/stages.rb +++ b/lib/interscript/isc/grammar/concerns/stages.rb @@ -121,7 +121,10 @@ module Stages end rule(:rule_line) do - whitespace? >> (rule | comment_item) >> whitespace? + # ISC-WF-1 #3.2: a `use` directive is a rule-line item - items + # inside a parallel/sequence block participate in that block's + # single pass (mirrors rs grammar/isc.parg rule_line). + whitespace? >> (rule | use_directive | comment_item) >> whitespace? end # A rule is either compact form (single line) or block form diff --git a/lib/interscript/isc/grammar/isc.parg b/lib/interscript/isc/grammar/isc.parg index cf790377..4cb51977 100644 --- a/lib/interscript/isc/grammar/isc.parg +++ b/lib/interscript/isc/grammar/isc.parg @@ -153,7 +153,11 @@ grammar InterscriptIsc version "0.1.0" { rule = block_rule / compact_rule comment_item = (("#" *(!%x0A chr)) as comment) / ((!(keyword) identifier) as noop) - rule_line = [ws] (rule / comment_item) [ws] + # ISC-WF-1 §3.2: a `use` directive is a rule-line item — items inside a + # parallel/sequence block participate in that block's single pass, and + # the expansion flattens the referenced ruleset's rules at this + # position (mirrors interscript-rs grammar/isc.parg rule_line). + rule_line = [ws] (rule / use_directive / comment_item) [ws] rule_items = *(rule_line) sequence_block = "sequence" [ws] "{" [ws] (rule_items as sequence) [ws] "}" parallel_block = "parallel" [ws] "{" [ws] (rule_items as parallel) [ws] "}" diff --git a/lib/interscript/isc/node_adapter.rb b/lib/interscript/isc/node_adapter.rb index fb4b10d3..cb113cb3 100644 --- a/lib/interscript/isc/node_adapter.rb +++ b/lib/interscript/isc/node_adapter.rb @@ -163,10 +163,10 @@ def build_stage_item_nodes(item) case item[:kind] when :parallel group = Interscript::Node::Group::Parallel.new - item[:rules].each { |r| group.children << build_rule(r) } + item[:rules].each { |r| group.children.push(*build_stage_row(r)) } [group] when :sequence - item[:rules].map { |r| build_rule(r) } + item[:rules].flat_map { |r| build_stage_row(r) } when :bare_rule [build_rule(item[:rule])] when :run @@ -213,6 +213,61 @@ def expand_use(item) apply_overrides(deep_copy_children(rs.children), item[:overrides]) end + # WO18 G5 — `use` inside a parallel/sequence block (spec §3.2): + # the expansion inherits the enclosing pass structure — the target's + # rule content flattens into the enclosing collection. Non-rule + # items (funcalls, stage runs) cannot join a pass — named error + # (interscript-rs resolve_use_rules). + def expand_use_flat(item) + rs = resolve_ruleset(item[:dependency], item[:ruleset]) + rules = flatten_rules(rs.children) + if item[:overrides] + o_rules = item[:overrides].flat_map { |r| build_override_row(r) } + rules = merge_override_rules(rules, o_rules) + end + rules + end + + # One flattened row of a parallel/sequence block (§3.2): `use` rows + # splice the referenced ruleset's rules; rule rows build directly. + # Both builders return ARRAYS: a use row expands to possibly + # several flattened rules (spec §3.2), a rule row to itself. + def build_stage_row(row) + row.is_a?(Hash) && row[:kind] == :use ? expand_use_flat(row) : [build_rule(row)] + end + + def build_override_row(row) + row.is_a?(Hash) && row[:kind] == :use ? expand_use_flat(row) : [build_rule(row)] + end + + def flatten_rules(children) + children.flat_map do |node| + case node + when Interscript::Node::Group + node.children + when Interscript::Node::Rule::Sub + node + else + raise(Interscript::MapLogicError, + "use inside a parallel/sequence block: the referenced ruleset carries " \ + "non-rule items (funcalls/stage runs) that cannot join the pass") + end + end + end + + # §4.2 merge over a FLAT rule list (the block-position use): + # matching rules drop wherever they sit (child-wins on exact + # from-equality); the override rules land at the expansion tail. + # No trailing-parallel join here — the caller re-wraps the list + # into the enclosing block's structure. + def merge_override_rules(rules, o_rules) + o_keys = o_rules.map { |r| from_key(r) } + kept = rules.reject do |r| + r.is_a?(Interscript::Node::Rule::Sub) && o_keys.include?(from_key(r)) + end + kept + o_rules + end + # The fully-expanded Node::Ruleset for a reference, memoized. # Same-map rulesets build here; dependency rulesets arrive already # built through the recursive adapter of the dependency document. @@ -293,7 +348,9 @@ def deep_copy_node(node) def apply_overrides(children, overrides) return children if overrides.nil? || overrides.empty? - o_rules = overrides.map { |r| build_rule(r) } + # WO18 G5: an override row may itself be a `use` directive; its + # content flattens into the override list (rs extract_rule_list). + o_rules = overrides.flat_map { |r| build_override_row(r) } o_keys = o_rules.map { |r| from_key(r) } kept = children.filter_map do |node| diff --git a/spec/interscript/isc/wf1_grammar_spec.rb b/spec/interscript/isc/wf1_grammar_spec.rb index 99861b23..b4981d60 100644 --- a/spec/interscript/isc/wf1_grammar_spec.rb +++ b/spec/interscript/isc/wf1_grammar_spec.rb @@ -146,6 +146,49 @@ def parse_doc(src, filename = "wf1.isc") src = %(system "T:e-L:Latn:1" {\nstage main {\n override {\n sub "a" "b"\n }\n}\n}\n) expect { Interscript::Isc::Parser.parse(src) }.to raise_error(Interscript::Isc::ParseError) end + + # WO18 G5 — §3.2: `use` inside a parallel/sequence block is a + # rule-line position (rs grammar: rule_line carries use_directive). + it "parses a use directive inside a sequence block" do + doc = parse_doc(<<~ISC) + system "T:tst-use-inseq:1" { + ruleset r1 { + parallel { sub "a" "1" } + } + stage main { + sequence { + use r1 + sub "b" "2" + } + } + } + ISC + item = doc[:stages].first[:body].first + expect(item[:kind]).to eq(:sequence) + expect(item[:rules].first[:kind]).to eq(:use) + expect(item[:rules].first[:ruleset]).to eq("r1") + expect(item[:rules].last[:from]).to eq(Interscript::Isc::Items::StringValue.new("b")) + end + + it "parses a use directive inside a parallel block" do + doc = parse_doc(<<~ISC) + system "T:tst-use-inpar:1" { + ruleset r1 { + parallel { sub "a" "1" } + } + stage main { + parallel { + use r1 + sub "b" "2" + } + } + } + ISC + item = doc[:stages].first[:body].first + expect(item[:kind]).to eq(:parallel) + expect(item[:rules].first[:kind]).to eq(:use) + expect(item[:rules].last[:from]).to eq(Interscript::Isc::Items::StringValue.new("b")) + end end describe "W3 when guard" do diff --git a/spec/interscript/isc/wf1_semantics_spec.rb b/spec/interscript/isc/wf1_semantics_spec.rb index 0ba27dfc..6c297181 100644 --- a/spec/interscript/isc/wf1_semantics_spec.rb +++ b/spec/interscript/isc/wf1_semantics_spec.rb @@ -434,4 +434,104 @@ def iso9_interp expect(rule.when_languages).to be_nil end end + + # WO18 G5 — spec §3.2: items inside a parallel/sequence block participate + # in that block's single pass; a ruleset containing a parallel block + # contributes that block as one pass at the expansion position. The + # expansion flattens the referenced ruleset's rules into the enclosing + # collection; non-rule targets (funcalls/stage runs) are a named + # MapLogicError — the interscript-rs behavior (probe tst-use-inseq). + describe "use inside blocks" do + it "splices the used ruleset's rules into a sequence block, in position" do + i = interpreter_for(<<~ISC) + system "T:tst-use-inseq:1" { + ruleset r1 { + parallel { sub "a" "1" } + } + stage main { + sequence { + use r1 + sub "b" "2" + } + } + } + ISC + expect(i.call("ab")).to eq("12") + end + + it "joins the pass: the used ruleset's rules flatten into the enclosing parallel" do + i = interpreter_for(<<~ISC) + system "T:tst-use-inpar:1" { + ruleset r1 { + parallel { sub "a" "1" } + } + stage main { + parallel { + use r1 + sub "b" "2" + } + } + } + ISC + # one simultaneous pass: a→1 and b→2 together + expect(i.call("ab")).to eq("12") + end + + it "merges an override block per-rule on the flattened list (child-wins)" do + i = interpreter_for(<<~ISC) + system "T:e-L:Latn:1" { + ruleset tbl { + parallel { + sub "a" "A1" + sub "b" "B" + } + } + stage main { + parallel { + use tbl override { + sub "a" "A2" + } + } + } + } + ISC + # the replacement row joins the same single pass, after the kept rows + expect(i.call("xa xb")).to eq("xA2 xB") + end + + it "rejects a non-rule target (funcall in the used ruleset) with a named error" do + expect { + build_node_doc(<<~ISC) + system "T:e-L:Latn:1" { + ruleset tbl { + sub "a" "A" + upcase + } + stage main { + parallel { use tbl } + } + } + ISC + }.to raise_error(Interscript::MapLogicError, /cannot join the pass/) + end + + it "rejects a non-rule target reached through nested use" do + expect { + build_node_doc(<<~ISC) + system "T:e-L:Latn:1" { + ruleset tbl { + sub "a" "A" + upcase + } + ruleset wrap { + use tbl + } + stage main { + sequence { use wrap } + } + } + ISC + }.to raise_error(Interscript::MapLogicError, /cannot join the pass/) + end + end end From 218d5658d9b5279180d213ab4ea36b9d44c4c8ef Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sat, 14 Nov 2026 10:14:15 +0800 Subject: [PATCH 2/2] style: parenthesize the complex ternary conditions (StandardRB) --- lib/interscript/isc/node_adapter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/interscript/isc/node_adapter.rb b/lib/interscript/isc/node_adapter.rb index cb113cb3..8af239b9 100644 --- a/lib/interscript/isc/node_adapter.rb +++ b/lib/interscript/isc/node_adapter.rb @@ -233,11 +233,11 @@ def expand_use_flat(item) # Both builders return ARRAYS: a use row expands to possibly # several flattened rules (spec §3.2), a rule row to itself. def build_stage_row(row) - row.is_a?(Hash) && row[:kind] == :use ? expand_use_flat(row) : [build_rule(row)] + (row.is_a?(Hash) && row[:kind] == :use) ? expand_use_flat(row) : [build_rule(row)] end def build_override_row(row) - row.is_a?(Hash) && row[:kind] == :use ? expand_use_flat(row) : [build_rule(row)] + (row.is_a?(Hash) && row[:kind] == :use) ? expand_use_flat(row) : [build_rule(row)] end def flatten_rules(children)