pubid 2.0.58 → 2.0.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/pubid/errors.rb CHANGED
@@ -52,17 +52,28 @@ module Pubid
52
52
  # @return [String, nil]
53
53
  attr_reader :flavor
54
54
 
55
+ # The byte position where the engine gave up, when it reported one and
56
+ # it is an Integer. Held as a plain number: the failure path never
57
+ # retains the engine's error object (whose cause tree and Source pin
58
+ # the subject string), so this field is the only diagnostic that
59
+ # survives the dispatch (issue #803; the engine-side half is
60
+ # parsanol-ruby#123).
61
+ # @return [Integer, nil]
62
+ attr_reader :position
63
+
55
64
  # @param message [String]
56
65
  # @param parse_failure_cause [Object, nil] the engine's structured
57
66
  # cause, nil for a failure raised without running a grammar
58
67
  # @param input [String, nil]
59
68
  # @param flavor [String, nil]
69
+ # @param position [Integer, nil] the byte position of the failure
60
70
  def initialize(message, parse_failure_cause = nil, input: nil,
61
- flavor: nil)
71
+ flavor: nil, position: nil)
62
72
  super(message)
63
73
  @parse_failure_cause = parse_failure_cause
64
74
  @input = input
65
75
  @flavor = flavor
76
+ @position = position
66
77
  end
67
78
 
68
79
  # @return [Object, nil]
@@ -101,3 +101,7 @@ The lesson BSI paid for: a wrapper's hand-written `#number` delegation makes the
101
101
  - **IEEE parses through the edition-2 artifact's typed model bindings (the last converter wave)**: the three `Backend.parse` sites (`identifiers/base.rb` `parse_single`, `Aiee::Identifier.parse`, `Ire::Identifier.parse`) route through `Pubid::Parg::Model.bound` — the grammar's `bindings` block produces the builder-ready hash at the engine, replacing the tree normalization. The grammar side (pubid-grammar `grammars/{ieee,nesc}.parg` edition 2 + bindings, plus a class-ref inlining pass over the merged `aiee.parg`/`ire.parg`) keeps every capture the builder reads: leaf scalars bind `(string)`; the branch/wrapper captures the builder dispatches on (`draft`, `parameters`, `publishers`, `base`, `relationship_type`, `related_ids`, the aiee/ire/nesc wrappers, …) bind `(raw)` — raw subtrees carry `Parsanol::Slice` leaves whose `to_s`/`==` behave like strings, so `extract_value` and the `.is_a?(Hash)` probes are unchanged. **Load-bearing grammar deviations** (documented in the `.parg` headers): the zero-width markers are re-spelled as real-token captures (`printed_dash_year`/`printed_month_year` capture their branch's leading separator, `iec_label_dash` the dash, `iso_published` the whole joint head choice — every read sits behind a nil-`iso_stage` gate, so the wider presence set is inert exactly where v1's was narrow); the `([dash]) as draft_dash` / `([rev_sep] as rev_sep)` capture-over-OPTIONAL landmines are `[ (x) as y ]` (parsanol-ruby#394); nested-only captures that collide with a top-level name rename with the builder (`draft_date_month/year/day`, `draft_version_dash_*`, `fdraft_month/year`, and `reaffirmed_year` — unbound, because binding it would have surfaced the reaffirmation year v1 kept absent). **Class RULE refs (`digit`/`lower`/`upper`, and multi-char ones like `alnum_char`/`identifier_string`'s `non_nl`) materialize per-token named nodes inside captures when the runtime recompiles the `use`-importing source — the vendored `data/parg/{aiee,ire,nesc}.parg` copies are the live import sources, so they must be re-vendored together with the artifacts** (a stale `.parg` beside a fresh `.json` silently parses through the old grammar). **Builder accommodations** (all dual-key with the v1 spellings as fallback, so HEAD trees and bound maps read alike): the renamed nested keys; and the multi-segment draft-version normalization at the top of `build_single_identifier` — edition-2 trees materialize a "D2.1.1" designator as per-token nodes with no `:draft_version`, where v1 degraded to a whole-clause leaf (reconstructed: the leaf string → `Draft.parse` inverse) or to a date-only hash when a draft date follows (no draft built). **Measured**: fixture-corpus parity (old artifact + tree backend + builder vs new artifact + bound + builder) over all 9,054 pass-fixture inputs → 1 diff, and standalone aiee/ire/nesc sweeps → 0 diffs each. **The one residue is an engine wall, documented not chased**: `IEC/IEEE PC37.60/P62271-111_D6.1` (testsuite case `ieee.project_draft_identifier.0946`, one corpus input, one `draft_compact_convergence` example) — v1's edition-1 tree degraded its whole draft clause ("DP62271-111_D6.1, Aug 2016", two P-numbers plus date) into a single leaf, and edition-2's fully-named trees cannot reproduce that collapse at any binding spelling (isolated rule probes + programmatic branch bisect); the row is one of the two-P-number joint one-offs already recorded as deferred above. The conversion also **fixed** two testsuite rows (`ieee.project_draft_identifier.9263` "ISO/IEC/IEEE P42010.2019/D4" / "P42010.WD4:2019(E)" — the lettered `draft_iso_iteration` capture now binds, so the D= face composes), at the cost of the 0946 residue above.
102
102
 
103
103
  - **The pubid-scheme URN (`#to_pubid_urn`, issue #656) — IEEE wave.** Schema: `lib/pubid/ieee/pubid_urn_schema.rb`; design and rulings: the `[[urn-ieee]]` section of `docs/URN-SCHEMES.adoc`. All 9,049 parseable pass ids emit, round-trip by `to_hash`, are path-safe and collision-free; the contract row is `spec/pubid/pubid_urn_contract_spec.rb`, the per-family examples `spec/pubid/ieee/pubid_urn_spec.rb`. **No `to_hash` change, so no `relaton-data-ieee` re-crawl.** Three things to know before editing: **(1)** the wrappers whose parent slot is not `base` (`ieee_identifier`, `first_identifier`, `primary_identifier` …, see "The parent slot is never called `base` in IEEE" above) write that document as a nested atom, so they do NOT share its slug prefix — renaming a slot to `base` moves every such URN; **(2)** IEEE nests ANSI and IEC ids, so ANSI landed whole with this wave and `lib/pubid/iec/pubid_urn_schema.rb` is a partial schema for the one IEC type the corpus nests — a new nested foreign type (an adopted `IEC TR`, `TS`, `AMD`, `CSV`) makes `#to_pubid_urn` raise until its flavor's schema declares it; **(3)** the schema must place every `to_hash` key, so a new IEEE attribute fails the contract row until its atom is declared — add it in the shared-vocabulary group it belongs to, never at the end out of habit, because atom order is part of every published slug. **The URN inherits two `to_hash` gaps, pinned rather than fixed**: `csa_identifier` is a plain accessor, so the CSA half of a `CsaDualPublished` is lost (4 corpus ids, and two such documents with the same IEEE half share a hash and a URN); and one `separator` serves every part, so `IEEE Std 7-4.3.2-2016` comes back as `7.4.3.2` (14 ids). A third: `SupplementIdentifier#to_hash` drops the delegated `publisher` only on a top-level wrapper, so for a non-IEEE supplement nested in an adoption (`IEEE Std 1234-2010 (ISO 1234/Amd 1:2001)`) `from_hash(to_hash)` is not idempotent and `parse_urn` refuses the emitted URN as not canonical (none in the corpus). Fixing any of the three changes `to_hash`. Separately, `IEC/IEEE P62582-6, FDIS May 2019` and `ISO/IEC/IEEE P21840, DIS-2019` raise `Parsanol::PARG::BindError` from `parse` — the parse-error contract expects `Pubid::Errors::ParseError`.
104
+
105
+ - **A dotted draft with a relaton dash date rendered its tail twice (relaton/relaton-data-ieee#10).** `IEEE P1547/D-6.7.2-2017-05` rendered `IEEE P1547/D6.7.2-2017-05.7.2`, and relaton stored that string as the primary docidentifier, which pubid then could not parse — 20 documents missing from the index. The bound tree carries one draft element per version segment (`"-6"`, `".7.2"`) plus the date element. `Builder#build_single_identifier` writes the joined version plus `-YYYY[-MM]` onto the first element (`host`); it left `".7.2"` on the second, so the version was joined a second time. Three branches embed a date this way — the dash month, the dash year beside a base year (`IEEE Std 1547-2011/D-6.7.2-2017`), and the numeric month (`IEEE P1547/D-6.7.2, 05 2017`, which also used only the host's own `"-6"` and rendered `D6-2017-05.7.2`). All three now take `joined_draft_version(drafts)` and call `drop_joined_draft_segments`, which deletes `:draft_version` from every other element. The IEEE pass fixtures hold no such input (0 `to_s`/`to_hash` changes over 9,105 inputs). The release coupling this fix had (the canonical `IEEE P1547/D6.7.2-2017-05` re-parsed draft-less) is closed by the grammar captures below. The crawler-junk rows of the issue stay with relaton (hand-off `relaton__relaton__ieee-issue10-renderer-junk`).
106
+
107
+ - **The relaton-data-ieee#10 grammar captures (pubid-grammar `f7b2842`, artifact checksum `sha256:c4fa66bd…fa11`).** Three captures, three different amounts of gem work. **(1) Dotted-triple draft** — `draft_version_composite` now holds `"6.7.2"`; no builder change. 23 IEEE corpus drafts that rendered with the draft dropped (`IEEE P1547/D6.7.2, May 2017` → `IEEE P1547`) or cut to one segment (`IEEE Unapproved P754/D1`) now keep it. Two of the 23 were pinned lossy by specs: `catalogue_alias_spec.rb`'s "fabricated D1 for a date-only Draft Std" really was a triple draft the grammar had dropped. **(2) Corrigendum month `cor_month`** — the grammar captures it in the nested `corrigendum:` hash and at top level for `corrigendum_identifier`; the builder reads it at all three corrigendum sites plus `rename_supplement_keys`, onto the `month` that `Corrigendum` already inherits (so `exclude(:year)`, the pubid URN atom `month` and the official URN's `month_day_component` needed no change). The renderer prints `-MM` after the corrigendum year, and `mr_supplement_suffix` writes `cor.1.2019-05` so two monthly corrigenda do not share a slug. Before, `IEEE Std 802.3-2018/Cor 1-2019-05` parsed and silently dropped `05`. **(3) Bare-IEEE colon year** — the grammar rule `ieee_colon_year_identifier` was unreachable: `Parser.normalize_input`'s "add missing Std" rewrite turned `IEEE 8802-1AE:2013` into `IEEE Std 8802-1AE:2013`, which the grammar rejects by design. The rewrite now skips a number followed by `:YYYY`. The rendered face is the canonical dash form, `IEEE Std 8802-1AE-2013/Amd 1-2015` (a ruling: no spelling flag, same as `IEEE Std 24748-3:2012` → `-2012`). A colon year with a corrigendum (`IEEE 8802-1AE:2013/Cor 1-2014`) still fails. Locked by `dotted_draft_dash_date_spec.rb`, `corrigendum_month_spec.rb`, `colon_year_spec.rb`.
@@ -377,7 +377,9 @@ module Pubid
377
377
  if m.to_s.match?(/\A\d{1,2}\z/) && y
378
378
  host = drafts.find { |d| d.is_a?(Hash) && d[:draft_version] }
379
379
  if host
380
- host[:draft_version] = [host[:draft_version], y, m].compact.join("-")
380
+ host[:draft_version] =
381
+ [joined_draft_version(drafts), y, m].compact.join("-")
382
+ drop_joined_draft_segments(drafts, host)
381
383
  parsed_hash.delete(:month)
382
384
  parsed_hash.delete(:year)
383
385
  parsed_hash.delete(:trailing_month)
@@ -390,8 +392,7 @@ module Pubid
390
392
  # is the base's year, the 2015-07 the draft's); only the
391
393
  # root-year assignment waits for an unoccupied year slot.
392
394
  if dash
393
- joined = drafts.filter_map { |d| d.is_a?(Hash) ? d[:draft_version] : nil }
394
- .map { |v| v.is_a?(Hash) ? v[:value] : v.to_s }.join
395
+ joined = joined_draft_version(drafts)
395
396
  # dash is either the top-level spine (dash_year/dash_month) or the
396
397
  # draft's own hash (draft_date_dash_year/month on the edition-2
397
398
  # grammar, dash_year/month on v1) — read both spellings.
@@ -411,6 +412,7 @@ module Pubid
411
412
  # normalizes onto it (pubid ruling 2026-10-13).
412
413
  host[:draft_version] =
413
414
  [joined, dash_y, dash_m].compact.join("-")
415
+ drop_joined_draft_segments(drafts, host)
414
416
  dash.delete(:dash_year)
415
417
  dash.delete(:dash_month)
416
418
  dash.delete(:draft_date_dash_year)
@@ -430,6 +432,7 @@ module Pubid
430
432
  # "D2, 04 2015" comma spelling normalizes onto it.
431
433
  host[:draft_version] =
432
434
  [joined, dash_y, dash_m].compact.join("-")
435
+ drop_joined_draft_segments(drafts, host)
433
436
  dash.delete(:dash_year)
434
437
  dash.delete(:dash_month)
435
438
  dash.delete(:draft_date_dash_year)
@@ -571,6 +574,8 @@ module Pubid
571
574
  year = attributes.delete(:cor_year) || attributes.delete(:amd_year) ||
572
575
  attributes.delete(:conf_year) || attributes.delete(:int_year)
573
576
  attributes[:year] = year if year
577
+ month = attributes.delete(:cor_month)
578
+ attributes[:month] = month if month
574
579
  end
575
580
 
576
581
  # Reconstruct the base identifier string from a parsed `:base` subtree
@@ -663,12 +668,16 @@ module Pubid
663
668
  # identifier stores them as the uniform `number`/`year`).
664
669
  cor_number = extract_value(parsed_hash[:cor_number])
665
670
  cor_year = extract_value(parsed_hash[:cor_year])
671
+ # The numeric month after the corrigendum year ("…/Cor 1-2019-05").
672
+ cor_month = parsed_hash[:cor_month] &&
673
+ extract_value(parsed_hash[:cor_month])
666
674
 
667
675
  # Create Corrigendum with parsed base
668
676
  Identifiers::Corrigendum.new(
669
677
  base: base,
670
678
  number: cor_number,
671
679
  year: cor_year,
680
+ month: cor_month,
672
681
  )
673
682
  end
674
683
 
@@ -683,6 +692,7 @@ module Pubid
683
692
  cor_data = parsed_hash[:corrigendum]
684
693
  cor_number = extract_value(cor_data[:cor_number])
685
694
  cor_year = extract_value(cor_data[:cor_year]) if cor_data[:cor_year]
695
+ cor_month = extract_value(cor_data[:cor_month]) if cor_data[:cor_month]
686
696
 
687
697
  base_hash = parsed_hash.reject { |key, _| key == :corrigendum }
688
698
  base = build_single_identifier(base_hash)
@@ -691,6 +701,7 @@ module Pubid
691
701
  base: base,
692
702
  number: cor_number,
693
703
  year: cor_year,
704
+ month: cor_month,
694
705
  )
695
706
  end
696
707
 
@@ -1628,6 +1639,27 @@ module Pubid
1628
1639
  end
1629
1640
  end
1630
1641
 
1642
+ # The draft version spread over the segment elements, joined back.
1643
+ # @param drafts [Array] the draft elements of the parse tree
1644
+ # @return [String]
1645
+ def joined_draft_version(drafts)
1646
+ drafts.filter_map { |d| d.is_a?(Hash) ? d[:draft_version] : nil }
1647
+ .map { |v| v.is_a?(Hash) ? v[:value] : v.to_s }.join
1648
+ end
1649
+
1650
+ # The grammar splits a dotted draft version into one element per
1651
+ # segment ("/D-6.7.2" -> "-6", ".7.2"). Once the host element holds the
1652
+ # joined version with its embedded dash date, the other segments are
1653
+ # already in it; left in place they are joined a second time and the
1654
+ # version reads "6.7.2-2017-05.7.2" (relaton/relaton-data-ieee#10).
1655
+ # @param drafts [Array] the draft elements of the parse tree
1656
+ # @param host [Hash] the element that now holds the joined version
1657
+ def drop_joined_draft_segments(drafts, host)
1658
+ drafts.each do |d|
1659
+ d.delete(:draft_version) if d.is_a?(Hash) && !d.equal?(host)
1660
+ end
1661
+ end
1662
+
1631
1663
  # Extract and normalize attributes from parsed data
1632
1664
  # @param parsed [Hash] the parsed data
1633
1665
  # @return [Hash] normalized attributes for identifier
@@ -2228,6 +2260,9 @@ extract_optional(parsed, attributes, :rev_sep) unless attributes[:revision] =~ /
2228
2260
  attributes[:cor_year] =
2229
2261
  extract_value(cor_data[:cor_year])
2230
2262
  end
2263
+ if cor_data[:cor_month]
2264
+ attributes[:cor_month] = extract_value(cor_data[:cor_month])
2265
+ end
2231
2266
  elsif parsed[:corrigendum]
2232
2267
  attributes[:corrigendum] = extract_value(parsed[:corrigendum])
2233
2268
  end
@@ -31,7 +31,11 @@ module Pubid
31
31
  def mr_supplement_suffix
32
32
  segments = ["cor"]
33
33
  segments << number.to_s if number
34
- segments << year.to_s if year
34
+ if year
35
+ # The month rides the year ("cor.1.2019-05"), so two monthly
36
+ # corrigenda of one year do not share a slug.
37
+ segments << (month ? "#{year}-#{month}" : year.to_s)
38
+ end
35
39
  segments.join(".")
36
40
  end
37
41
  end
@@ -263,8 +263,12 @@ module Pubid
263
263
  cleaned = cleaned.gsub(/\s+-\s+(\d{4})\b/, '-\1')
264
264
 
265
265
  # 3. Add missing "Std" after IEEE: "IEEE 1070-1995" → "IEEE Std 1070-1995"
266
- # Only at start of string, IEEE + space + digit
267
- cleaned = cleaned.gsub(/^IEEE\s+(?!Std\b)(\d)/, 'IEEE Std \1')
266
+ # Only at start of string, IEEE + space + digit.
267
+ # Not before an ISO-style colon year ("IEEE 8802-1AE:2013/Amd.1-2015"):
268
+ # the grammar's ieee_colon_year_identifier reads only the bare IEEE
269
+ # publisher, and "IEEE Std …:YYYY" stays a grammar negative.
270
+ cleaned = cleaned.gsub(/^IEEE\s+(?!Std\b)(?![\dA-Za-z.-]+:\d{4}\b)(\d)/,
271
+ 'IEEE Std \1')
268
272
 
269
273
  # 3.5. Convert "IEEE No." to "IEEE Std": "IEEE No. 264-1968" → "IEEE Std 264-1968"
270
274
  # NOTE: Do NOT convert AIEE No - AIEE uses "No" as standard format
@@ -386,6 +386,8 @@ module Pubid
386
386
  result += ". " if id.number # Add period and space for formal format
387
387
  result += id.number if id.number
388
388
  result += "-#{id.year}" if id.year
389
+ # The corrigendum's own numeric month ("…/Cor. 1-2019-05").
390
+ result += "-#{id.month}" if id.year && id.month
389
391
  result
390
392
  end
391
393
 
data/lib/pubid/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pubid
4
- VERSION = "2.0.58"
4
+ VERSION = "2.0.59"
5
5
  end
data/lib/pubid.rb CHANGED
@@ -491,6 +491,37 @@ module Pubid
491
491
  # A longest-match-first order matters because prefixes nest: "ISO/IEC 2131"
492
492
  # must not be handed to the flavor that merely owns "ISO".
493
493
  #
494
+ # Pass 2 is pruned to the flavors the input is not known to be incompatible
495
+ # with (issue #803): a flavor whose grammar cannot accept +string+ is
496
+ # dropped from the walk, and each dropped parser is one fewer failed parse
497
+ # whose record the dispatch would otherwise carry to the end. The prune is
498
+ # a FILTER over the existing order — it drops entries and never adds or
499
+ # reorders one, so every surviving flavor is tried exactly where the full
500
+ # dispatch would have tried it. It applies only when +string+ HAS a
501
+ # declared leading prefix ({candidates_by_prefix} non-empty): a string no
502
+ # prefix owns keeps the full walk, because the corpus showed flavors
503
+ # accepting leading tokens their prefix tables never declared there (BSI
504
+ # committee documents, standalone amendments, CEN/EVS stage prefixes, IALA
505
+ # specification numbers — see {dispatch_candidates}). Within a pruned
506
+ # walk, a flavor is dropped only when both:
507
+ #
508
+ # * it declares prefix tokens AND its grammar accepts unprefixed input
509
+ # under no declared prefix (see {accepts_bare?}) — never dropped, and
510
+ # * none of its declared prefix tokens occurs anywhere in +string+ at a
511
+ # token-start position ({prefix_token_occurs?}) — not only as the
512
+ # leading token: a co-published tail counts ("API 579-2 /ASME …" stays
513
+ # compatible with the asme flavor because ASME occurs after the slash,
514
+ # and "ISO/ASTMTR52905-EB" stays compatible with astm because ASTM
515
+ # occurs there even glued to the type token). Per the dual-perspective
516
+ # law each flavor's prefix list already carries the co-publisher faces
517
+ # ({Pubid::JOINT_PREFIXES}), so the occurrence test over that one list
518
+ # covers the joint forms too.
519
+ #
520
+ # Every flavor the prune keeps was tried anyway, so no accepted/rejected
521
+ # row can move (proven over the full testsuite corpus: 0 digest deltas —
522
+ # see PR #804). Ambiguous prefixes, no-owner strings and bare forms walk
523
+ # the survivors of their own prune, still in the full dispatch order.
524
+ #
494
525
  # @param string [String] a human-readable identifier
495
526
  # @return [Identifier]
496
527
  # @raise [Pubid::Errors::ParseError] when no flavor accepts the string
@@ -502,7 +533,9 @@ module Pubid
502
533
  fallback = nil
503
534
 
504
535
  prefix_owners = candidates_by_prefix(string)
505
- (prefix_owners + other_candidates(prefix_owners)).each do |mod|
536
+ candidates = dispatch_candidates(string, prefix_owners)
537
+
538
+ candidates.each do |mod|
506
539
  # An umbrella flavor (ANSI) also parses the joint prefix of a
507
540
  # co-publisher that has its own flavor; that flavor's parse wins.
508
541
  # Only as a prefix owner: in the exhaustive pass the umbrella is one
@@ -524,7 +557,13 @@ module Pubid
524
557
  # IEEE `csa_identifier`) into visible ones. Catching it one layer up
525
558
  # would hand it straight back its disguise: a real builder bug in any
526
559
  # flavor would read as "this string is not an identifier".
527
- first_error ||= e
560
+ #
561
+ # Only the record is kept, never the error object: the engine chains
562
+ # its own failure as the Ruby cause, and that chain — Source, pools,
563
+ # cause tree — pins the subject for as long as the dispatch lives
564
+ # (issue #803). The message/input/flavor strings and the failure
565
+ # position as an Integer are all the raise at the end needs.
566
+ first_error ||= DispatchFailure.from(e, string)
528
567
  next
529
568
  end
530
569
 
@@ -552,13 +591,79 @@ module Pubid
552
591
  end
553
592
 
554
593
  return fallback if fallback
555
- raise first_error if first_error
594
+ raise first_error.to_parse_error if first_error
556
595
 
557
596
  raise Pubid::Errors::ParseError,
558
597
  "no registered flavor could parse #{string.inspect}"
559
598
  end
560
599
  private_class_method :parse_by_prefix
561
600
 
601
+ # One failed flavor parse of the dispatch loop, stripped to the fields the
602
+ # eventual raise needs: the message, input and flavor strings plus the
603
+ # engine's failure position as an Integer (issue #803). The error object is
604
+ # NOT retained — Ruby chains the engine's own failure (a
605
+ # +Parsanol::ParseFailed+) as the raised error's cause, and that chain holds
606
+ # the {Parsanol::Source} with the subject string, a scanner, a regex cache
607
+ # and the engine's slice and position pools; the plateau campaign measured
608
+ # millions of such failures alive across long compiles. The engine-side
609
+ # half of the same fix is parsanol-ruby#123 (position as an Integer, cause
610
+ # chain cut); this record is the dispatcher half.
611
+ #
612
+ # The +parse_failure_cause+ of the reconstructed error stays nil, as it
613
+ # already is for every raise site on this path (the PARG backend passes
614
+ # nil; the synthetic flavor failures pass nil), and the error is built
615
+ # outside any rescue, so no engine error is attached as its cause.
616
+ DispatchFailure = Struct.new(:message, :input, :flavor, :position) do
617
+ # The error raised when the dispatch ends with no flavor able to parse
618
+ # the input. Carries only strings and the Integer position.
619
+ # @return [Errors::ParseError]
620
+ def to_parse_error
621
+ Errors::ParseError.new(message, nil, input: input, flavor: flavor,
622
+ position: position)
623
+ end
624
+
625
+ # The record of one failed candidate parse. +input+ prefers the error's
626
+ # own +input+ — what the flavor actually fed its grammar, which a
627
+ # preprocessing flavor may spell differently from the string the
628
+ # dispatch was asked about — so the raised error reports the same input
629
+ # it always did.
630
+ # @param error [Errors::ParseError]
631
+ # @param string [String]
632
+ # @return [DispatchFailure]
633
+ def self.from(error, string)
634
+ new(error.message, error.input || string, error.flavor,
635
+ engine_position(error))
636
+ end
637
+
638
+ # The engine's failure position, walked off the Ruby cause chain the
639
+ # engine error carries ({Parsanol::ParseFailed#parse_failure_cause} holds
640
+ # an Integer position). A synthetic failure without an engine cause has
641
+ # none.
642
+ # @param error [Errors::ParseError]
643
+ # @return [Integer, nil]
644
+ def self.engine_position(error)
645
+ cause = error.cause
646
+ until cause.nil?
647
+ position = cause_position(cause)
648
+ return position if position
649
+
650
+ cause = cause.cause
651
+ end
652
+ nil
653
+ end
654
+
655
+ # @param cause [Exception, nil] one link of the engine error chain
656
+ # @return [Integer, nil]
657
+ def self.cause_position(cause)
658
+ return unless cause.respond_to?(:parse_failure_cause)
659
+
660
+ failure = cause.parse_failure_cause
661
+ position = failure.position if failure.respond_to?(:position)
662
+ position if position.is_a?(Integer)
663
+ end
664
+ end
665
+ private_constant :DispatchFailure
666
+
562
667
  # Flavor modules that own a prefix +string+ starts with, longest prefix
563
668
  # first. Deduplicated by module identity, so an alias registration is not
564
669
  # tried twice.
@@ -599,11 +704,65 @@ module Pubid
599
704
  end
600
705
  private_class_method :other_candidates
601
706
 
707
+ # The dispatch order for +string+ — prefix owners first, then every other
708
+ # flavor — filtered down to the flavors +string+ is not known to be
709
+ # incompatible with ({flavor_compatible?}). A filter: the survivors keep
710
+ # the exact original order (issue #803).
711
+ #
712
+ # The prune applies only when +string+ HAS a declared leading prefix. A
713
+ # string no prefix owns keeps the full walk: the testsuite corpus showed
714
+ # flavors accepting leading tokens their own {PrefixesSupport} table never
715
+ # declared there (BSI committee documents "14/30300822 DC", standalone
716
+ # amendments "AMD 11015", CEN stage prefixes "FprEN 16114:2011", EVS
717
+ # "prEVS-EN 100:2026", CSA "CAN3-A451.1-M86", IALA "S-201") — where no
718
+ # declared prefix occurs, the prefix data bounds nothing and every parser
719
+ # has to run.
720
+ #
721
+ # @param string [String] a human-readable identifier
722
+ # @param prefix_owners [Array<Module>] see {candidates_by_prefix}
723
+ # @return [Array<Module>]
724
+ # @api private
725
+ def self.dispatch_candidates(string, prefix_owners)
726
+ if prefix_owners.empty?
727
+ return prefix_owners + other_candidates(prefix_owners)
728
+ end
729
+
730
+ lowered = string.downcase
731
+ (prefix_owners + other_candidates(prefix_owners))
732
+ .select { |mod| flavor_compatible?(mod, lowered) }
733
+ end
734
+ private_class_method :dispatch_candidates
735
+
602
736
  # A meaningless string no real identifier scheme should claim. Used to find
603
737
  # the flavors whose grammar accepts anything.
604
738
  ROUTING_PROBE = "zzqx-nonsense-9714"
605
739
  private_constant :ROUTING_PROBE
606
740
 
741
+ # Unprefixed probe shapes for {accepts_bare?}: the bare (publisher-less)
742
+ # forms a grammar can accept, one representative per observed shape family.
743
+ # Measured acceptance over the registered flavors:
744
+ #
745
+ # * "123" — a bare number (astm, iec, ieee, iso, nist, iana, adobe)
746
+ # * "1234-56" — a bare dash-number (ogc)
747
+ # * "abc" — a bare slug (adobe, iana)
748
+ # * "TR 123" — a bare series token + number (ashrae, gb, iec, iso)
749
+ # * "978-1-56619-909-4" — a valid bare ISBN-13 (isbn)
750
+ # * "10.1234/foo" — a bare DOI (doi, doi_pfs)
751
+ # * "AB/CD" — a bare slash-delimited designation (un, whose grammar
752
+ # takes any two-token slash shape far outside its own UN prefix)
753
+ # * "TS 1.1" — a bare series token + dotted version (tgpp, whose TS/TR
754
+ # series designations need no 3GPP prefix)
755
+ #
756
+ # A flavor that parses ANY of these accepts unprefixed input, so the
757
+ # dispatch prune (issue #803) may never drop it — its grammar does not
758
+ # require a leading prefix, and a string carrying none of its prefixes can
759
+ # still be its identifier. Measured like {accepts_anything?}: probed, not
760
+ # listed, so a grammar that gains or loses the property is classified
761
+ # without anyone editing a constant.
762
+ BARE_FORM_PROBES = ["123", "1234-56", "abc", "TR 123", "978-1-56619-909-4",
763
+ "10.1234/foo", "AB/CD", "TS 1.1"].freeze
764
+ private_constant :BARE_FORM_PROBES
765
+
607
766
  # True when +mod+'s grammar accepts an arbitrary slug.
608
767
  #
609
768
  # `adobe` and `iana` do, by design — their identifiers genuinely are free
@@ -632,6 +791,89 @@ module Pubid
632
791
  end
633
792
  private_class_method :accepts_anything?
634
793
 
794
+ # True when +mod+ is a possible reader of +lowered_string+ — the dispatch
795
+ # prune (issue #803) drops a flavor only when this answers false, and the
796
+ # answer is built so a kept flavor is never a wrong keep: the two escape
797
+ # hatches ({accepts_bare?}, any prefix occurrence anywhere) each err
798
+ # toward keeping.
799
+ #
800
+ # @param mod [Module] a flavor module
801
+ # @param lowered_string [String] the input, downcased once by the caller
802
+ # @return [Boolean]
803
+ # @api private
804
+ def self.flavor_compatible?(mod, lowered_string)
805
+ return true if accepts_bare?(mod)
806
+
807
+ mod.prefixes.any? do |prefix|
808
+ prefix_token_occurs?(lowered_string, prefix.downcase)
809
+ end
810
+ end
811
+ private_class_method :flavor_compatible?
812
+
813
+ # True when +lowered_prefix+ occurs in +lowered_string+ at a token-start
814
+ # position: preceded by the start of the string or by a non-alphanumeric
815
+ # character — the leading half of the {prefix_match?} boundary rule,
816
+ # applied at every position instead of only the first.
817
+ #
818
+ # The trailing half is deliberately NOT applied: a flavor's prefix may be
819
+ # glued to what follows it in a form its grammar accepts
820
+ # ("ISO/ASTMTR52905-EB" carries ASTM directly against the TR type token),
821
+ # so requiring a trailing boundary would drop exactly the flavor that
822
+ # parses the string.
823
+ #
824
+ # @param lowered_string [String]
825
+ # @param lowered_prefix [String]
826
+ # @return [Boolean]
827
+ # @api private
828
+ def self.prefix_token_occurs?(lowered_string, lowered_prefix)
829
+ index = -1
830
+ while (index = lowered_string.index(lowered_prefix, index + 1))
831
+ return true if index.zero?
832
+
833
+ boundary = lowered_string[index - 1]
834
+ return true unless /[a-z0-9]/.match?(boundary)
835
+ end
836
+ false
837
+ end
838
+ private_class_method :prefix_token_occurs?
839
+
840
+ # True when +mod+'s grammar accepts an unprefixed input — a bare form none
841
+ # of its prefixes leads. astm reads "123" as "ASTM 123", iec reads "CIE" as
842
+ # "IEC CIE", isbn reads a bare ISBN, doi reads a bare "10.…": such a flavor
843
+ # owns strings that carry no prefix token of its at all, so the dispatch
844
+ # prune (issue #803) may never drop it from the walk.
845
+ #
846
+ # Detected by probing ({BARE_FORM_PROBES}) rather than by a hardcoded list,
847
+ # like {accepts_anything?}: one probe round per flavor, memoised, so a
848
+ # grammar that gains or loses the property is classified without anyone
849
+ # remembering to edit a constant.
850
+ #
851
+ # @param mod [Module] a flavor module
852
+ # @return [Boolean]
853
+ # @api private
854
+ def self.accepts_bare?(mod)
855
+ @accepts_bare ||= {}
856
+ return @accepts_bare[mod] if @accepts_bare.key?(mod)
857
+
858
+ @accepts_bare[mod] = BARE_FORM_PROBES.any? { |p| parses_probe?(mod, p) }
859
+ end
860
+ private_class_method :accepts_bare?
861
+
862
+ # One probe parse of the bare-acceptance calibration: true when +mod+'s
863
+ # grammar accepts +probe+.
864
+ #
865
+ # @param mod [Module] a flavor module
866
+ # @param probe [String] a {BARE_FORM_PROBES} shape
867
+ # @return [Boolean]
868
+ # @api private
869
+ def self.parses_probe?(mod, probe)
870
+ mod.parse(probe)
871
+ true
872
+ rescue StandardError, Pubid::Errors::ParseError
873
+ false
874
+ end
875
+ private_class_method :parses_probe?
876
+
635
877
  # Prefix => flavor modules, longest prefix first, built once.
636
878
  #
637
879
  # {prefix_flavors} rebuilds its index on every call, iterating every
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pubid
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.58
4
+ version: 2.0.59
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-27 00:00:00.000000000 Z
11
+ date: 2026-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lutaml-model