Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion interscript-py/src/interscript/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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."""

Expand Down
9 changes: 7 additions & 2 deletions interscript-py/src/interscript/isc.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
Stage,
SubRule,
)
from .errors import IscParseError, MapFormatError
from .errors import IscLoadError, IscParseError, MapFormatError

DependencyResolver = Callable[[str], "IscDocument | None"]

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -470,6 +474,7 @@ def _lower_rule(rule: ParsedRule) -> SubRule:
__all__ = [
"IscDependency",
"IscDocument",
"IscLoadError",
"IscParseError",
"IscStage",
"IscTest",
Expand Down
20 changes: 10 additions & 10 deletions interscript-py/tests/test_isc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 } } }'
Expand All @@ -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 } '
Expand All @@ -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,
Expand Down
18 changes: 17 additions & 1 deletion lib/interscript/isc/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,28 @@
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
Comment on lines +83 to +90
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/interscript/isc/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
Loading