From 1e691169618675f8a9b88fb7d8c2f0b20ebee5cf Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sat, 14 Nov 2026 11:26:52 +0800 Subject: [PATCH] fix: load-phase errors get their own class; parse errors trim the expected-set (G9) Two usability/clarification fixes from the WO18 parity audit: - py: expander-phase failures (use cycles, unknown rulesets, unknown dependency aliases, a non-rule target asked to join a pass) were routed through IscParseError - semantic failures misreported under the parse class. New IscLoadError (MapFormatError sibling, same position triad); the _Expander raises it; the genuine parse scanner keeps IscParseError. rs/ruby raise MapLogicError for the same failures. - ruby: parsanol's expected-set dumps could run to hundreds of characters ("expected one of [20 items...]"). ParseError now trims the list to the first 6 alternatives plus "(+N more)", keeping the position - the compact line:col shape rs/ts/py already give. TDD: py load pins moved to IscLoadError (collection-error red -> suite exit 0); ruby trim spec red (full dump) -> green; rake 430/0/11. --- interscript-py/src/interscript/errors.py | 19 ++++++++++++++++++- interscript-py/src/interscript/isc.py | 9 +++++++-- interscript-py/tests/test_isc_parser.py | 20 ++++++++++---------- lib/interscript/isc/parser.rb | 18 +++++++++++++++++- spec/interscript/isc/parser_spec.rb | 14 ++++++++++++++ 5 files changed, 66 insertions(+), 14 deletions(-) diff --git a/interscript-py/src/interscript/errors.py b/interscript-py/src/interscript/errors.py index 1ff3c1e4..b24f8b9e 100644 --- a/interscript-py/src/interscript/errors.py +++ b/interscript-py/src/interscript/errors.py @@ -20,7 +20,7 @@ class MapFormatError(ValueError): class IscParseError(MapFormatError): """An .isc source could not be parsed (bad token, unclosed block, - malformed rule, `use` cycle...). + malformed rule, ...). Carries the byte `position` plus 1-based `line`/`col` of the failure, mirroring the TypeScript `IscParseError`. @@ -33,6 +33,23 @@ def __init__(self, message: str, position: int, line: int, col: int) -> None: super().__init__(f"ISC parse error at line {line}:{col}: {message}") +class IscLoadError(MapFormatError): + """A parsed .isc document could not be expanded/loaded (use cycle, + unknown ruleset, a ruleset carrying non-rule items asked to join a + pass, ...). The source parsed; its WF-1 constructs do not resolve. + + WO18 G9: expander-phase failures were routed through IscParseError, + misreporting semantic failures under the parse class (rs/ruby raise + their MapLogicError for the same failures). + """ + + def __init__(self, message: str, position: int, line: int, col: int) -> None: + self.position = position + self.line = line + self.col = col + super().__init__(f"ISC load error at line {line}:{col}: {message}") + + class ArtifactIntegrityError(ValueError): """The compiled artifact failed its SHA256SUMS verification.""" diff --git a/interscript-py/src/interscript/isc.py b/interscript-py/src/interscript/isc.py index 73078746..8a2e2a8a 100644 --- a/interscript-py/src/interscript/isc.py +++ b/interscript-py/src/interscript/isc.py @@ -78,7 +78,7 @@ Stage, SubRule, ) -from .errors import IscParseError, MapFormatError +from .errors import IscLoadError, IscParseError, MapFormatError DependencyResolver = Callable[[str], "IscDocument | None"] @@ -196,7 +196,11 @@ def error(self, msg: str) -> None: line = self._src[:pos].count("\n") + 1 col = pos - self._src.rfind("\n", 0, pos) full_msg = f"{self._filename}: {msg}" if self._filename else msg - raise IscParseError(full_msg, pos, line, col) + # The expander runs post-parse: every failure here is a WF-1 + # construct that does not resolve (cycle, unknown ruleset, a + # non-rule target asked to join a pass) — the load class, not + # the parse class (WO18 G9; rs/ruby raise MapLogicError). + raise IscLoadError(full_msg, pos, line, col) def expand_ruleset(self, name: str, chain: tuple[tuple[str, str], ...] = ()) -> list[Any]: entry = (self._system_code, name) @@ -470,6 +474,7 @@ def _lower_rule(rule: ParsedRule) -> SubRule: __all__ = [ "IscDependency", "IscDocument", + "IscLoadError", "IscParseError", "IscStage", "IscTest", diff --git a/interscript-py/tests/test_isc_parser.py b/interscript-py/tests/test_isc_parser.py index 44fa0b65..19dff2da 100644 --- a/interscript-py/tests/test_isc_parser.py +++ b/interscript-py/tests/test_isc_parser.py @@ -17,7 +17,7 @@ StringItem, SubRule, ) -from interscript.errors import IscParseError +from interscript.errors import IscLoadError, IscParseError from interscript.executor import execute_stage from interscript.isc import ( IscDocument, @@ -296,18 +296,18 @@ def test_forward_reference(self): assert row(bare_rules(doc)[0])[:2] == ("a", "A") def test_cycle_error(self): - with pytest.raises(IscParseError, match="use cycle"): + with pytest.raises(IscLoadError, match="use cycle"): parse( 'system "s" { ruleset a { use b } ruleset b { use a } ' 'stage main { use a } }' ) def test_self_cycle_error(self): - with pytest.raises(IscParseError, match="use cycle"): + with pytest.raises(IscLoadError, match="use cycle"): parse('system "s" { ruleset a { use a } stage main { use a } }') def test_unknown_ruleset_error(self): - with pytest.raises(IscParseError, match="unknown ruleset"): + with pytest.raises(IscLoadError, match="unknown ruleset"): parse('system "s" { stage main { use nope } }') # WO18 G5 — §3.2: items inside a pass block participate in that @@ -353,7 +353,7 @@ def test_override_in_block_merges_per_rule(self): 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"): + with pytest.raises(IscLoadError, match="cannot join the pass"): parse( 'system "s" { ruleset tbl { sub "a" "A" upcase } ' 'stage main { parallel { use tbl } } }' @@ -362,7 +362,7 @@ def test_non_rule_target_in_block_is_named_error(self): 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"): + with pytest.raises(IscLoadError, match="cannot join the pass"): parse( 'system "s" { ruleset tbl { sub "a" "A" upcase } ' 'ruleset wrap { use tbl } ' @@ -380,23 +380,23 @@ def test_cross_dependency_use(self): assert rows == [("a", "A"), ("b", "B")] def test_unknown_dependency_alias(self): - with pytest.raises(IscParseError, match="unknown dependency alias"): + with pytest.raises(IscLoadError, match="unknown dependency alias"): parse('system "s" { stage main { use ghost.tbl } }') def test_unresolvable_dependency(self): - with pytest.raises(IscParseError, match="cannot resolve dependency"): + with pytest.raises(IscLoadError, match="cannot resolve dependency"): parse( 'system "s" { dependency "missing" as m stage main { use m.tbl } }', resolve_dependency=lambda code: None, ) def test_no_resolver(self): - with pytest.raises(IscParseError, match="no resolver"): + with pytest.raises(IscLoadError, match="no resolver"): parse('system "s" { dependency "d" as m stage main { use m.tbl } }') def test_missing_ruleset_in_dependency(self): base = parse('system "base" { ruleset other { sub "a" "A" } }') - with pytest.raises(IscParseError, match="unknown ruleset in dependency"): + with pytest.raises(IscLoadError, match="unknown ruleset in dependency"): parse( 'system "s" { dependency "base" as b stage main { use b.tbl } }', resolve_dependency=lambda code: base, diff --git a/lib/interscript/isc/parser.rb b/lib/interscript/isc/parser.rb index 16105e32..8360b342 100644 --- a/lib/interscript/isc/parser.rb +++ b/lib/interscript/isc/parser.rb @@ -66,12 +66,28 @@ def reset_artifact class ParseError < StandardError attr_reader :filename, :source + # Parsanol's expected-set dumps can run to hundreds of characters + # (WO18 G9); the message keeps the position and the first few + # alternatives — rs/ts/py give compact line:col pointers. + MAX_EXPECTED = 6 + def initialize(message, filename:, source:, cause: nil) @filename = filename @source = source @cause = cause loc = filename ? "#{filename}: " : "" - super("#{loc}#{message}") + super("#{loc}#{self.class.trim_expected(message)}") + end + + def self.trim_expected(message) + message.sub(/expected one of \[([^\]]*)\]/) do + items = Regexp.last_match(1).split(", ") + if items.length > MAX_EXPECTED + "expected one of [#{items.first(MAX_EXPECTED).join(", ")} …(+#{items.length - MAX_EXPECTED} more)]" + else + Regexp.last_match(0) + end + end end end end diff --git a/spec/interscript/isc/parser_spec.rb b/spec/interscript/isc/parser_spec.rb index 2385b44d..23add691 100644 --- a/spec/interscript/isc/parser_spec.rb +++ b/spec/interscript/isc/parser_spec.rb @@ -36,6 +36,20 @@ }.to raise_error(Interscript::Isc::ParseError) end + # WO18 G9 (parity audit): parsanol's expected-set dumps can run to + # hundreds of characters; the message keeps the position and the + # first few alternatives instead of the full list (rs/ts/py give + # compact line:col pointers). + it "trims a long expected-set in the parse error message" do + expect { + described_class.parse("system \"x\" { stage main { sub \"a\" \"b\" ", filename: "cut.isc") + }.to raise_error(Interscript::Isc::ParseError) { |e| + expect(e.message).to match(/expected one of \[[^\]]*\(\+\d+ more\)\]/) + expect(e.message.length).to be < 200 + expect(e.message).to include("line 1") + } + end + it "accepts empty metadata block" do src = <<~ISC system "TEST:eng-Latn:Latn:2026" {