leptris 1.9.428.0-aarch64-linux → 1.9.430.0-aarch64-linux

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc7ca7b623a34074c13fda1ecdbc041cf5dc377642a9bd9d0396a0dc5a2a64d0
4
- data.tar.gz: 9f8d153735d723a8e56f8a1bda9716e14d494edca2368db47d6784cda184450f
3
+ metadata.gz: e9e43e0f23cb268379e16a86131556ecae8ef1ebe580bd56cdb4d53b5c0ac1d1
4
+ data.tar.gz: b43cc1230317ea7e53e1e58e7312d1f7cd9d8c0731ff8cd5fa264cde79edf4a1
5
5
  SHA512:
6
- metadata.gz: 2529c0abf7a3d133858cc0cb8c736d7cfce16981e831306fabf8c4dc77d15c3a36d1f8bba13ebbc8653eb88763d266965af9466d98a52ed8eddbf66b2a789277
7
- data.tar.gz: ab8b59839527723c04f068379a88811e614a00e9c2536af954df6b80ea9d1bbf260b13889207323057564ef9aea200f621e59b35f72b78a4dfd5121ac0a3b726
6
+ metadata.gz: 845cd096897abd192440e25b3874418fed79e7bc74592fba3d27003adb3b67e378d01ed99337f15d9ce1b8282d3d3f56adeff40c563810c884751d55ef08cd62
7
+ data.tar.gz: 8b97e90e71414ce084a54721457fe40c4bdfa2023a3b716d84658ffd1143bf1256881768cd1606aa9c8557bb7defa08cd6a91d6fe399301e1d2f99a218451fa9
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ### Fixed
4
4
 
5
+ - RelaxNG: a `combine="interleave"` define can add an element between
6
+ the members of a base sequence again (regression in 1.9.428.0, fixed
7
+ by engine 1.9.429; leptris/leptris#2094). Regression spec with the
8
+ include-override schema pair from #650.
5
9
  - Rebuild the native object under the Ruby that performs final packaging.
6
10
  Restoring the job Ruby after the per-minor release loop could reuse the
7
11
  last minor's object and overwrite the job minor's bundle with a different
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ RSpec::Core::RakeTask.new(:spec)
8
8
  # Pin for `rake compile` and the platform-gem builds. Keep in lockstep
9
9
  # with .github/workflows/build.yml (which calls `rake compile`) and the
10
10
  # CHANGELOG when libleptris releases.
11
- LIBLEPTRIS_VERSION = "1.9.428"
11
+ LIBLEPTRIS_VERSION = "1.9.430"
12
12
 
13
13
  # THE SSOT for the per-minor native bundle list (ruby#645): one line,
14
14
  # space-separated, consumed by the Rakefile, the per-minor builder's
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Leptris
4
- VERSION = "1.9.428.0"
4
+ VERSION = "1.9.430.0"
5
5
  end
Binary file
Binary file
data/lib/libleptris.so CHANGED
Binary file
data/vendor-src/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Sources vendored in this gem (recompile rights; the ruby
2
2
  variant builds them at install via extconf.rb):
3
3
 
4
- - libleptris v1.9.428
4
+ - libleptris v1.9.430
5
5
  - utf8proc v2.11.0
6
6
 
7
7
  Rebuild by hand:
@@ -1,5 +1,23 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.9.430] - 2026-11-20
4
+
5
+ ### Fixed
6
+
7
+ - **RELAX NG: the OpenMath corpus now validates verdict-identical to Jing — 345/345 documents, zero mismatches.** The attr-choice collection walk treated the cyclic content graph (`omel → OME/notom → omel`) as a tree and collected the same attribute-choice once per lap up to the selection cap while the branch search's depth cap is lower, so every element whose content could reach the cycle rejected with "attribute combination matches no branch of the attribute choice" after seconds of re-walking — the corpus-wide failure since 1.9.427 (#2079), independent of the interleave work. alg1-001: 30s-wrong-verdict → sub-millisecond-valid. The walk now carries a pointer set and visits each node once. leptris#2103, disposition on #2088.
8
+ - **RELAX NG regression (1.9.428–429)**: zero-width group members hold and advance the interleave cursor instead of finishing the group, so the simplest OMATTR attribution document — valid on 1.9.428, rejected on 1.9.429 — validates again; misplaced merged elements find their own element pattern. leptris#2099.
9
+ - RELAX NG: the interleave cursor machine memoizes its search states. An interleave of repeats followed by a sibling — `interleave(a*, b*), c` over `(a b)×N c` — spent the whole 200 000-step budget already at N = 10; it now takes a few hundred steps, polynomial in the child count. The failure diagnosis keeps the remainder of the interleave branch that consumed a child, so a document with an unrelated error no longer also reports `element "flavor" not allowed here` for a correctly placed merged element. By @andrew2net. leptris#2098.
10
+ - Build: drop the stale amalgamation rename of xslt_register_ufn_handlers. leptris#2100.
11
+
12
+ ## [1.9.429] - 2026-11-19
13
+
14
+ ### Fixed
15
+
16
+ - **RELAX NG regression (1.9.428.0)**: `combine="interleave"` merges no longer interleaved the added pattern INTO a base sequence — the added element could not sit between two base elements (RELAX NG §4.17; ruby#650). The interleave walk is now a per-branch cursor machine with full backtracking (branches match non-contiguously; repeatable members hold their cursor and may match again after another branch consumes; the search returns the furthest completion with a step budget for pathological documents). Within-a-body order still holds, per Jing. leptris#2094.
17
+ - Fuzz tooling: gen_xpath_seeds emits per-file corpus entries (the nightly's XPath leg takes corpus directories).
18
+
19
+
20
+
3
21
  ## [1.9.428] - 2026-11-18
4
22
 
5
23
  ### Fixed
@@ -3,7 +3,7 @@
3
3
  cmake_minimum_required(VERSION 3.20)
4
4
 
5
5
  project(leptris
6
- VERSION 1.9.428
6
+ VERSION 1.9.430
7
7
  DESCRIPTION "Fast XML parser and XPath evaluator in pure C"
8
8
  LANGUAGES C CXX
9
9
  )
@@ -176,8 +176,6 @@ if(LEPTRIS_AMALGAMATED)
176
176
  "leptris/common/format_number.c:parse_pattern=fmt_num_parse_pattern"
177
177
  "leptris/sax/iterparse.c:set_error=iterparse_set_error"
178
178
  "leptris/xpath/vm.c:xpath_context_document=vm_xpath_context_document"
179
- "leptris/xpath/xpath_public.c:xslt_register_ufn_handlers=xslt_register_ufn_handlers_impl"
180
- "leptris/xslt/xslt_functions.c:xslt_register_ufn_handlers=xslt_register_ufn_handlers_impl"
181
179
  "leptris/xslt/xslt_exec.c:utf8_encode=xslt_utf8_encode"
182
180
  "leptris/xslt/xslt_exec.c:ws_only=xslt_ws_only"
183
181
  "leptris/rng/validate.c:match_seq=rng_match_seq"
@@ -347,10 +347,6 @@ typedef struct {
347
347
 
348
348
  static int check_required_list_sel(RngVal* v, RngPattern* head,
349
349
  LeptrisElement e, const AttrSel* sel);
350
- static int check_required_list(RngVal* v, RngPattern* head,
351
- LeptrisElement e) {
352
- return check_required_list_sel(v, head, e, NULL);
353
- }
354
350
 
355
351
  static RngPattern* attr_sel_branch(const AttrSel* sel,
356
352
  const RngPattern* choice) {
@@ -496,11 +492,49 @@ static int check_required_node_sel(RngVal* v, RngPattern* c,
496
492
  return 1;
497
493
  }
498
494
 
495
+ /* The content graph is CYCLIC (openmath2.rng: omel -> OME/notom ->
496
+ * omel): a naive tree walk revisits the same nodes once per lap and
497
+ * collected the same attr-choice up to RNG_ATTR_SEL_MAX times, while
498
+ * the selection search's depth cap is lower — every element whose
499
+ * content could reach the cycle rejected, and the re-walk cost
500
+ * seconds per element. The walk carries a pointer set and visits
501
+ * each node once. */
502
+ #define RNG_COLLECT_SEEN_MAX 512
503
+
504
+ typedef struct {
505
+ const RngPattern* slot[RNG_COLLECT_SEEN_MAX];
506
+ } CollectSeen;
507
+
508
+ static int collect_seen_add(CollectSeen* s, const RngPattern* p) {
509
+ size_t h = ((uintptr_t)p >> 4) % RNG_COLLECT_SEEN_MAX;
510
+ for (size_t i = 0; i < RNG_COLLECT_SEEN_MAX; i++) {
511
+ size_t j = (h + i) % RNG_COLLECT_SEEN_MAX;
512
+ if (s->slot[j] == p) return 0;
513
+ if (!s->slot[j]) {
514
+ s->slot[j] = p;
515
+ return 1;
516
+ }
517
+ }
518
+ return 1;
519
+ }
520
+
521
+ static void collect_walk(RngVal* v, RngPattern* p, int depth, AttrSel* out,
522
+ CollectSeen* seen);
523
+
499
524
  /* Collect the attr-consuming CHOICE nodes through the content
500
- * (refs expanded, depth-capped). */
525
+ * (refs expanded, depth-capped, cycle-safe). */
501
526
  static void collect_attr_choices(RngVal* v, RngPattern* p, int depth,
502
527
  AttrSel* out) {
503
528
  if (!p || depth > 24) return;
529
+ CollectSeen seen;
530
+ memset(&seen, 0, sizeof(seen));
531
+ collect_walk(v, p, depth, out, &seen);
532
+ }
533
+
534
+ static void collect_walk(RngVal* v, RngPattern* p, int depth, AttrSel* out,
535
+ CollectSeen* seen) {
536
+ if (!p || depth > 24) return;
537
+ if (!collect_seen_add(seen, p)) return;
504
538
  for (RngPattern* c = p->first_child; c; c = c->next) {
505
539
  if (c->kind == RNG_CHOICE) {
506
540
  int consumes = 0;
@@ -515,10 +549,10 @@ static void collect_attr_choices(RngVal* v, RngPattern* p, int depth,
515
549
  }
516
550
  if (c->kind == RNG_REF) {
517
551
  RngDefine* d = find_define(v->g, c->name);
518
- if (d && d->body) collect_attr_choices(v, d->body, depth + 1, out);
552
+ if (d && d->body) collect_walk(v, d->body, depth + 1, out, seen);
519
553
  continue;
520
554
  }
521
- collect_attr_choices(v, c, depth + 1, out);
555
+ collect_walk(v, c, depth + 1, out, seen);
522
556
  }
523
557
  }
524
558
 
@@ -602,6 +636,232 @@ static size_t fold_list(RngVal* v, RngPattern* p, LeptrisNodeRef* kids,
602
636
  static size_t match_seq(RngVal* v, RngPattern* p, LeptrisNodeRef* kids,
603
637
  size_t n, size_t idx);
604
638
 
639
+ /* The interleave matcher (ruby#650): branches match NON-contiguously
640
+ * — that is what interleaving IS — so a window matcher is wrong (a
641
+ * group(base) branch fails atomically at the frontier even though its
642
+ * second element matches two positions later, after another branch
643
+ * consumes there). Each branch carries a CURSOR into its own child
644
+ * chain; the search advances one branch's head pattern at the shared
645
+ * frontier, recurses, and backtracks on overall failure. Jing does
646
+ * the same search over its interleave automaton. */
647
+ /* leptris#2093: one memo slot per visited search state. The result of
648
+ * rng_interleave_try depends only on (frontier, every cursor, every
649
+ * done flag, held mask), and full enumeration reaches the same state
650
+ * along many paths — interleave(a*, b*) followed by a sibling never
651
+ * hits the best == n early exit, so (a b)x40 exhausted the budget and
652
+ * rejected a valid document. Memoized, the search is polynomial. */
653
+ typedef struct {
654
+ size_t hash; /* 0 = empty slot */
655
+ size_t i;
656
+ unsigned mask;
657
+ size_t off; /* this state's cur[]/done[] in the pool */
658
+ size_t val; /* the memoized result */
659
+ } IlvMemo;
660
+
661
+ typedef struct {
662
+ RngPattern* cur[64]; /* the branch's remaining member pattern */
663
+ int unit[64]; /* the branch is a single non-group pattern */
664
+ int done[64]; /* the branch fully matched */
665
+ size_t cn;
666
+ unsigned long steps; /* search budget: invalid documents explode
667
+ combinatorially under full enumeration;
668
+ past the budget the match fails
669
+ conservatively (the diagnose pass then
670
+ reports the shortfall) */
671
+ IlvMemo* memo; /* open addressing, power-of-two capacity */
672
+ size_t memo_cap, memo_len;
673
+ RngPattern** pool; /* cn cursors + cn done markers per state */
674
+ size_t pool_len, pool_cap;
675
+ } IlvState;
676
+
677
+ /* done[b] is stored in the pool as a non-NULL marker. */
678
+ static RngPattern ilv_done_mark;
679
+
680
+ static size_t ilv_hash(const IlvState* st, size_t i, unsigned mask) {
681
+ size_t h = (size_t)1469598103u ^ i;
682
+ h = h * 1099511u ^ mask;
683
+ for (size_t b = 0; b < st->cn; b++) {
684
+ h = h * 1099511u ^ (size_t)(st->cur[b]);
685
+ h = h * 1099511u ^ (size_t)st->done[b];
686
+ }
687
+ return h ? h : 1;
688
+ }
689
+
690
+ static int ilv_same(const IlvState* st, const IlvMemo* m, size_t i,
691
+ unsigned mask) {
692
+ if (m->i != i || m->mask != mask) return 0;
693
+ RngPattern* const* rec = st->pool + m->off;
694
+ for (size_t b = 0; b < st->cn; b++) {
695
+ if (rec[b] != st->cur[b]) return 0;
696
+ if ((rec[st->cn + b] != NULL) != (st->done[b] != 0)) return 0;
697
+ }
698
+ return 1;
699
+ }
700
+
701
+ static const IlvMemo* ilv_lookup(const IlvState* st, size_t h, size_t i,
702
+ unsigned mask) {
703
+ if (!st->memo) return NULL;
704
+ for (size_t k = h & (st->memo_cap - 1);; k = (k + 1) & (st->memo_cap - 1)) {
705
+ const IlvMemo* m = &st->memo[k];
706
+ if (!m->hash) return NULL;
707
+ if (m->hash == h && ilv_same(st, m, i, mask)) return m;
708
+ }
709
+ }
710
+
711
+ /* Best effort: on an allocation failure the state is simply not
712
+ * memoized (the step budget still bounds the search). */
713
+ static void ilv_store(IlvState* st, size_t h, size_t i, unsigned mask,
714
+ size_t val) {
715
+ if (st->memo_len * 2 >= st->memo_cap) {
716
+ size_t cap = st->memo_cap ? st->memo_cap * 2 : 256;
717
+ if (cap > ((size_t)1 << 22)) return;
718
+ IlvMemo* t = (IlvMemo*)calloc(cap, sizeof(*t));
719
+ if (!t) return;
720
+ for (size_t k = 0; k < st->memo_cap; k++) {
721
+ if (!st->memo[k].hash) continue;
722
+ size_t j = st->memo[k].hash & (cap - 1);
723
+ while (t[j].hash) j = (j + 1) & (cap - 1);
724
+ t[j] = st->memo[k];
725
+ }
726
+ free(st->memo);
727
+ st->memo = t;
728
+ st->memo_cap = cap;
729
+ }
730
+ if (st->pool_len + 2 * st->cn > st->pool_cap) {
731
+ size_t cap = st->pool_cap ? st->pool_cap * 2 : 1024;
732
+ while (cap < st->pool_len + 2 * st->cn) cap *= 2;
733
+ RngPattern** p =
734
+ (RngPattern**)realloc(st->pool, cap * sizeof(*p));
735
+ if (!p) return;
736
+ st->pool = p;
737
+ st->pool_cap = cap;
738
+ }
739
+ size_t off = st->pool_len;
740
+ for (size_t b = 0; b < st->cn; b++) {
741
+ st->pool[off + b] = st->cur[b];
742
+ st->pool[off + st->cn + b] = st->done[b] ? &ilv_done_mark : NULL;
743
+ }
744
+ st->pool_len += 2 * st->cn;
745
+ size_t k = h & (st->memo_cap - 1);
746
+ while (st->memo[k].hash) k = (k + 1) & (st->memo_cap - 1);
747
+ st->memo[k].hash = h;
748
+ st->memo[k].i = i;
749
+ st->memo[k].mask = mask;
750
+ st->memo[k].off = off;
751
+ st->memo[k].val = val;
752
+ st->memo_len++;
753
+ }
754
+
755
+ static size_t rng_interleave_try(RngVal* v, IlvState* st,
756
+ LeptrisNodeRef* kids, size_t n,
757
+ size_t i, unsigned held_mask);
758
+
759
+ /* Advance (or hold) branch b after its head matched r. A REPEATABLE
760
+ * member that CONSUMED stays on its cursor — it may match again after
761
+ * another branch consumes at the frontier (ruby#650: ics on both
762
+ * sides of the merged-in element). */
763
+ static void ilv_advance(IlvState* st, size_t b, RngPattern* h,
764
+ size_t r, size_t i) {
765
+ int repeatable = (h->kind == RNG_ZERO_OR_MORE ||
766
+ h->kind == RNG_ONE_OR_MORE);
767
+ if (st->unit[b]) {
768
+ if (repeatable && r > i) return; /* hold the cursor */
769
+ st->done[b] = 1;
770
+ return;
771
+ }
772
+ if (repeatable && r > i) return; /* hold the cursor */
773
+ st->cur[b] = h->next;
774
+ st->done[b] = (h->next == NULL);
775
+ }
776
+
777
+ static size_t rng_interleave_try(RngVal* v, IlvState* st,
778
+ LeptrisNodeRef* kids, size_t n,
779
+ size_t i, unsigned held_mask) {
780
+ size_t b;
781
+ for (b = 0; b < st->cn; b++) {
782
+ if (!st->done[b]) break;
783
+ }
784
+ if (b == st->cn) return i; /* every branch exhausted */
785
+ size_t h = ilv_hash(st, i, held_mask);
786
+ const IlvMemo* seen = ilv_lookup(st, h, i, held_mask);
787
+ if (seen) return seen->val;
788
+ if (++st->steps > 200000) return (size_t)-1;
789
+ /* The search returns the FURTHEST completion, not the first: a
790
+ * branch finishing early can complete at a position that strands
791
+ * a repeatable member's later match (ruby#650 case 4: out=4 vs
792
+ * the required n=5). */
793
+ size_t best = (size_t)-1;
794
+ for (b = 0; b < st->cn; b++) {
795
+ if (st->done[b]) continue;
796
+ RngPattern* h = st->cur[b];
797
+ int repeatable = (h->kind == RNG_ZERO_OR_MORE ||
798
+ h->kind == RNG_ONE_OR_MORE);
799
+ size_t r = match_seq(v, h, kids, n, i);
800
+ if (r != (size_t)-1 && r > i) {
801
+ /* consuming: advance-or-hold, recurse at the new frontier
802
+ * — the frontier moved, so every hold is forgettable */
803
+ RngPattern* save = st->cur[b];
804
+ int save_done = st->done[b];
805
+ ilv_advance(st, b, h, r, i);
806
+ size_t out = rng_interleave_try(v, st, kids, n, r, 0);
807
+ st->cur[b] = save; /* backtrack */
808
+ st->done[b] = save_done;
809
+ if (out != (size_t)-1 && (best == (size_t)-1 || out > best))
810
+ best = out;
811
+ if (best == n) break; /* complete — cannot beat */
812
+ v->failed = 0;
813
+ v->err[0] = 0;
814
+ }
815
+ if (r != (size_t)-1 && r == i) {
816
+ if (repeatable && !(held_mask & (1u << b))) {
817
+ /* zero-width: the member may still feed at a LATER
818
+ * position — hold it and let the others move first
819
+ * (the mask guards the same-branch same-frontier
820
+ * ping-pong: each branch holds at most once per
821
+ * frontier) */
822
+ size_t out = rng_interleave_try(v, st, kids, n, i,
823
+ held_mask | (1u << b));
824
+ if (out != (size_t)-1 && (best == (size_t)-1 ||
825
+ out > best))
826
+ best = out;
827
+ if (best == n) break;
828
+ }
829
+ /* or the member is skipped. A unit branch (or a group's
830
+ * last member) finishes here; a group branch moves its
831
+ * cursor to the NEXT member - a zero-width optional,
832
+ * attribute or empty repeat in the middle of a group must
833
+ * not finish the whole group (doctype, subdoctype?,
834
+ * flavor: skipping subdoctype still requires flavor, and
835
+ * still admits it). The cursor strictly advances, so the
836
+ * same-frontier recursion terminates. */
837
+ RngPattern* save = st->cur[b];
838
+ int save_done = st->done[b];
839
+ size_t out;
840
+ if (st->unit[b] || h->next == NULL) {
841
+ st->done[b] = 1;
842
+ out = rng_interleave_try(v, st, kids, n, r, -1);
843
+ } else {
844
+ st->cur[b] = h->next;
845
+ out = rng_interleave_try(v, st, kids, n, r, held_mask);
846
+ }
847
+ st->cur[b] = save; /* backtrack */
848
+ st->done[b] = save_done;
849
+ if (out != (size_t)-1 && (best == (size_t)-1 || out > best))
850
+ best = out;
851
+ if (best == n) break;
852
+ v->failed = 0;
853
+ v->err[0] = 0;
854
+ continue;
855
+ }
856
+ if (r == (size_t)-1) {
857
+ v->failed = 0; /* speculative: try the next branch */
858
+ v->err[0] = 0;
859
+ }
860
+ }
861
+ ilv_store(st, h, i, held_mask, best);
862
+ return best;
863
+ }
864
+
605
865
  static int leaf_matches(RngVal* v, RngPattern* p, LeptrisNodeRef node) {
606
866
  LeptrisElement e = (LeptrisElement)node;
607
867
  switch (p->kind) {
@@ -663,55 +923,35 @@ static size_t match_seq(RngVal* v, RngPattern* p, LeptrisNodeRef* kids,
663
923
  return i;
664
924
  }
665
925
  case RNG_INTERLEAVE: {
666
- /* Any order; every branch must be satisfied. A single
667
- * assignment pass is unsound against interleaved
668
- * zeroOrMore branches: a branch whose turn precedes the
669
- * elements it matches zero-widths, gets marked used, and
670
- * strands them (leptris#1709: relaton-ieee's 2nd/3rd
671
- * <ics>). Consume to a fixpoint — branches take turns
672
- * while ANY of them advances — then the remainder must
673
- * be zero-width satisfiable. */
674
- RngPattern* ch[64];
675
- size_t cn = 0;
676
- for (RngPattern* c = p->first_child; c && cn < 64;
677
- c = c->next)
678
- ch[cn++] = c;
679
- int used[64] = {0};
680
- int progressed = 1;
681
- size_t i = idx;
682
- while (progressed) {
683
- progressed = 0;
684
- for (size_t b = 0; b < cn; b++) {
685
- if (used[b]) continue;
686
- size_t r = match_seq(v, ch[b], kids, n, i);
687
- if (r != (size_t)-1 && r > i) {
688
- used[b] = 1;
689
- i = r;
690
- progressed = 1;
691
- } else if (r != (size_t)-1) {
692
- /* zero-width here: leave the branch unused so
693
- * a later position can still feed it. */
694
- v->failed = 0;
695
- v->err[0] = 0;
696
- } else {
697
- v->failed = 0;
698
- v->err[0] = 0;
699
- }
926
+ /* Any order; every branch must be satisfied. The cursor
927
+ * machine above: branches match non-contiguously with
928
+ * full backtracking. The earlier fixpoint walk could not
929
+ * DEFER a branch — combine="interleave" merges wrap each
930
+ * body in a GROUP (#2089), and group(base) as one branch
931
+ * must be able to PAUSE mid-sequence while another branch
932
+ * consumes at the frontier (ruby#650: the added element
933
+ * may sit between any two base elements, §4.17). */
934
+ IlvState st;
935
+ memset(&st, 0, sizeof(st));
936
+ for (RngPattern* c = p->first_child; c && st.cn < 64;
937
+ c = c->next) {
938
+ if (c->kind == RNG_GROUP && c->first_child) {
939
+ st.cur[st.cn] = c->first_child;
940
+ st.unit[st.cn] = 0;
941
+ } else {
942
+ st.cur[st.cn] = c;
943
+ st.unit[st.cn] = 1;
700
944
  }
945
+ st.done[st.cn] = 0;
946
+ st.cn++;
701
947
  }
702
- for (size_t b = 0; b < cn; b++) {
703
- if (used[b]) continue;
704
- size_t r = match_seq(v, ch[b], kids, n, i);
705
- if (r == (size_t)-1 || r != i) {
706
- /* A branch that consumes here contradicts the
707
- * fixpoint above — treat as unsatisfied. */
708
- return (size_t)-1;
709
- }
948
+ size_t out = rng_interleave_try(v, &st, kids, n, idx, 0); free(st.memo);
949
+ free(st.pool);
950
+ if (out == (size_t)-1) {
710
951
  v->failed = 0;
711
952
  v->err[0] = 0;
712
- used[b] = 1;
713
953
  }
714
- return i;
954
+ return out;
715
955
  }
716
956
  case RNG_OPTIONAL: {
717
957
  /* ruby-adjacent leptris#2078: the XML form flattens a
@@ -1402,15 +1642,21 @@ static DiagCell* d_state(RngVal* v, DiagCell* s, const char* x, int* ok) {
1402
1642
  for (RngPattern* a = d->first_child; a; a = a->next) {
1403
1643
  DiagCell tmp; tmp.item = a; tmp.next = NULL;
1404
1644
  int sub = 0;
1405
- d_state(v, &tmp, x, &sub);
1645
+ DiagCell* rest = d_state(v, &tmp, x, &sub);
1406
1646
  if (!sub) continue;
1407
1647
  consumed = 1;
1408
- /* Survivors = other children (this one consumed
1409
- * once; single-use children drop, repeats keep). */
1648
+ /* Survivors = the other children plus the
1649
+ * consuming child's RESIDUAL in its place: a
1650
+ * group branch keeps its tail (leptris#2093), a
1651
+ * repeat stays active; a used-up single drops. */
1410
1652
  RngPattern* kids = NULL;
1411
1653
  for (RngPattern* o = d->first_child; o; o = o->next) {
1412
- if (o == a) continue;
1413
- RngPattern* cp = dpat_copy(v, o);
1654
+ RngPattern* src = o;
1655
+ if (o == a) {
1656
+ if (!rest) continue;
1657
+ src = rest->item;
1658
+ }
1659
+ RngPattern* cp = dpat_copy(v, src);
1414
1660
  if (!cp) { consumed = 0; break; }
1415
1661
  cp->next = kids; kids = cp;
1416
1662
  }
@@ -1519,6 +1765,69 @@ static RngPattern* find_element_pattern(RngVal* v, RngPattern* p,
1519
1765
  return NULL;
1520
1766
  }
1521
1767
 
1768
+ static int collect_attr_patterns(RngVal* v, RngPattern* head,
1769
+ RngPattern** out, int* optional,
1770
+ int n, int cap, int opt,
1771
+ RngPattern** seen, int* nseen);
1772
+
1773
+ /* Element patterns named `x` inside `p`, NOT following refs (every
1774
+ * define is visited by the caller, so refs add nothing but cycles).
1775
+ * Collects up to `cap` hits into `out`. */
1776
+ static int collect_named_elements(RngPattern* p, const char* x,
1777
+ RngPattern** out, int n, int cap,
1778
+ int depth) {
1779
+ for (; p && n < cap; p = p->next) {
1780
+ if (depth > 64) return n;
1781
+ if (p->kind == RNG_REF) continue;
1782
+ if (p->kind == RNG_ELEMENT && !p->any_name && p->name &&
1783
+ strcmp(p->name, x) == 0)
1784
+ out[n++] = p;
1785
+ n = collect_named_elements(p->first_child, x, out, n, cap,
1786
+ depth + 1);
1787
+ }
1788
+ return n;
1789
+ }
1790
+
1791
+ /* Does every attribute on `e` have a same-named attribute pattern in
1792
+ * element pattern `el`? */
1793
+ static int attrs_named_in(RngVal* v, RngPattern* el, LeptrisElement e) {
1794
+ RngPattern* cands[64];
1795
+ int cand_opt[64];
1796
+ RngPattern* seen[128];
1797
+ int nseen = 0;
1798
+ int nc = collect_attr_patterns(v, el->first_child, cands, cand_opt,
1799
+ 0, 64, 0, seen, &nseen);
1800
+ for (struct leptris_attribute* a =
1801
+ leptris_element_get_first_attribute(e);
1802
+ a; a = leptris_attr_next(a)) {
1803
+ const char* name = attr_cname(a);
1804
+ int found = 0;
1805
+ for (int i = 0; i < nc && !found; i++)
1806
+ if (cands[i]->name && strcmp(cands[i]->name, name) == 0)
1807
+ found = 1;
1808
+ if (!found) return 0;
1809
+ }
1810
+ return 1;
1811
+ }
1812
+
1813
+ /* leptris#1704 recovery target for a misplaced element: the element
1814
+ * pattern(s) named `x` anywhere in the grammar, as Jing's recovery
1815
+ * does - grammars name the define after the role, not the element
1816
+ * (basicdoc: <define name="paragraph"><element name="p">). Prefer a
1817
+ * pattern that declares every attribute the instance carries, so the
1818
+ * value check runs against the definition that owns the attribute. */
1819
+ static RngPattern* recovery_element_pattern(RngVal* v, LeptrisElement e,
1820
+ const char* x) {
1821
+ RngPattern* hits[32];
1822
+ int n = collect_named_elements(v->g->start, x, hits, 0, 32, 0);
1823
+ for (RngDefine* d = v->g->defines; d && n < 32; d = d->next)
1824
+ n = collect_named_elements(d->body, x, hits, n, 32, 0);
1825
+ if (n == 0) return NULL;
1826
+ for (int i = 0; i < n; i++)
1827
+ if (attrs_named_in(v, hits[i], e)) return hits[i];
1828
+ return hits[0];
1829
+ }
1830
+
1522
1831
  /* Diagnose a matched child element's own content (quiet verdict,
1523
1832
  * then recurse on failure). */
1524
1833
  static void diagnose_child(RngVal* v, RngPattern* content,
@@ -2000,10 +2309,16 @@ static void diagnose_element(RngVal* v, RngPattern* p, LeptrisElement e) {
2000
2309
  * enums) go invisible exactly where documents are most
2001
2310
  * broken. Jing reports both, position first. */
2002
2311
  {
2003
- RngDefine* own = find_define(v->g, x);
2004
- if (own && own->body)
2005
- diagnose_attrs(v, dpat_new(v, RNG_ELEMENT, own->body),
2006
- kid);
2312
+ RngPattern* own_el = recovery_element_pattern(v, kid, x);
2313
+ if (own_el) {
2314
+ diagnose_attrs(v, own_el, kid);
2315
+ } else {
2316
+ RngDefine* own = find_define(v->g, x);
2317
+ if (own && own->body)
2318
+ diagnose_attrs(v,
2319
+ dpat_new(v, RNG_ELEMENT, own->body),
2320
+ kid);
2321
+ }
2007
2322
  }
2008
2323
  /* Skip the offending child; position unchanged. */
2009
2324
  }