1use crate::table::{Language, SYM_ERROR, SYM_ERROR_REPEAT};
4
5pub const ERROR_COST_PER_RECOVERY: u32 = 500;
6pub const ERROR_COST_PER_MISSING_TREE: u32 = 110;
7pub const ERROR_COST_PER_SKIPPED_TREE: u32 = 100;
8pub const ERROR_COST_PER_SKIPPED_LINE: u32 = 30;
9pub const ERROR_COST_PER_SKIPPED_CHAR: u32 = 1;
10
11#[repr(C)]
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
14pub struct Len {
15 pub bytes: u32,
16 pub rows: u32,
17}
18
19impl Len {
20 #[inline(always)]
21 #[allow(clippy::should_implement_trait)]
22 pub fn add(self, o: Len) -> Len {
23 Len {
24 bytes: self.bytes.wrapping_add(o.bytes),
25 rows: self.rows.wrapping_add(o.rows),
26 }
27 }
28 #[inline(always)]
29 #[allow(clippy::should_implement_trait)]
30 pub fn sub(self, o: Len) -> Len {
31 Len {
32 bytes: self.bytes.wrapping_sub(o.bytes),
33 rows: self.rows.wrapping_sub(o.rows),
34 }
35 }
36}
37
38pub const NONE: u32 = u32::MAX;
39
40pub const F_VISIBLE: u8 = 1;
41pub const F_NAMED: u8 = 2;
42pub const F_EXTRA: u8 = 4;
43pub const F_MISSING: u8 = 8;
44pub const F_KEYWORD: u8 = 16;
45
46const F_COLD_BIT: u32 = 1 << 5;
47const FLAGS_MASK: u32 = 0x1F;
48const MAX_INLINE_VIS: u32 = (1 << 26) - 1;
49const CHILD_COUNT_MASK: u32 = 0x00FF_FFFF;
50pub(crate) const EXT_COLD_U8: u8 = 254;
51pub(crate) const EXT_NONE_U8: u8 = 255;
52const EXT_NONE_HI: u32 = (EXT_NONE_U8 as u32) << 24;
53
54#[repr(C)]
61#[derive(Clone, Copy, Debug)]
62pub struct Node {
63 pub padding: Len,
64 pub size: Len,
65 pub first_child: u32,
67 pub child_count_ext: u32,
68 pub sym: u16,
69 pub prod: u16,
70 pub packed: u32,
71}
72
73const _: () = assert!(core::mem::size_of::<Node>() == 32);
74
75#[repr(C)]
77#[derive(Clone, Copy, Debug)]
78pub struct ColdNode {
79 pub error_cost: u32,
80 pub dprec: i32,
81 pub visible_descendants: u32,
82 pub ext: u32,
83}
84
85impl Node {
86 #[inline(always)]
87 pub fn child_count(&self) -> u32 {
88 self.child_count_ext & CHILD_COUNT_MASK
89 }
90 #[inline(always)]
91 pub(crate) fn ext_inline(&self) -> u8 {
92 (self.child_count_ext >> 24) as u8
93 }
94 #[inline(always)]
95 pub fn flags(&self) -> u8 {
96 (self.packed & FLAGS_MASK) as u8
97 }
98 #[inline(always)]
99 pub(crate) fn has_cold(&self) -> bool {
100 (self.packed & F_COLD_BIT) != 0
101 }
102 #[inline(always)]
103 pub(crate) fn cold_idx(&self) -> usize {
104 (self.packed >> 6) as usize
105 }
106 #[inline(always)]
107 pub(crate) fn inline_vis(&self) -> u32 {
108 self.packed >> 6
109 }
110 #[inline(always)]
111 pub fn set_flag(&mut self, f: u8) {
112 self.packed |= f as u32;
113 }
114 #[inline(always)]
115 pub fn clear_flag(&mut self, f: u8) {
116 self.packed &= !(f as u32);
117 }
118 #[inline(always)]
119 pub fn lchar(&self) -> u32 {
120 if self.child_count() == 0 {
121 self.first_child
122 } else {
123 0
124 }
125 }
126 #[inline(always)]
127 pub fn set_lchar(&mut self, ch: u32) {
128 if self.child_count() == 0 {
129 self.first_child = ch;
130 }
131 }
132 #[inline(always)]
133 pub fn extra(&self) -> bool {
134 (self.packed & F_EXTRA as u32) != 0
135 }
136 #[inline(always)]
137 pub fn visible(&self) -> bool {
138 (self.packed & F_VISIBLE as u32) != 0
139 }
140 #[inline(always)]
141 pub fn named(&self) -> bool {
142 (self.packed & F_NAMED as u32) != 0
143 }
144 #[inline(always)]
145 pub fn missing(&self) -> bool {
146 (self.packed & F_MISSING as u32) != 0
147 }
148 #[inline(always)]
149 pub fn is_keyword(&self) -> bool {
150 (self.packed & F_KEYWORD as u32) != 0
151 }
152 #[inline(always)]
153 pub fn is_error(&self) -> bool {
154 self.sym == SYM_ERROR
155 }
156 #[inline(always)]
157 pub fn total(&self) -> Len {
158 self.padding.add(self.size)
159 }
160}
161
162#[derive(Default)]
163pub struct Arena {
164 pub nodes: Vec<Node>,
165 pub kids: Vec<u32>,
166 pub(crate) cold: Vec<ColdNode>,
167 pub ext: Vec<Vec<u8>>,
169 pub(crate) ext_hashes: Vec<u64>,
170 pub(crate) ext_table: Vec<u32>,
171 pub ext_total: usize,
172 pub(crate) vc_cache: Vec<u32>,
174}
175
176#[inline(always)]
177fn extent_cost(size: Len) -> u32 {
178 ERROR_COST_PER_RECOVERY
179 .wrapping_add(ERROR_COST_PER_SKIPPED_CHAR.wrapping_mul(size.bytes))
180 .wrapping_add(ERROR_COST_PER_SKIPPED_LINE.wrapping_mul(size.rows))
181}
182
183#[inline(always)]
184fn hash_bytes(bytes: &[u8]) -> u64 {
185 let mut h = 0xcbf29ce484222325u64 ^ (bytes.len() as u64);
186 for &b in bytes {
187 h ^= b as u64;
188 h = h.wrapping_mul(0x100000001b3);
189 }
190 h ^ (h >> 33)
191}
192
193impl Arena {
194 #[inline(always)]
195 pub fn n(&self, i: u32) -> &Node {
196 &self.nodes[i as usize]
197 }
198 #[inline(always)]
199 pub fn n_mut(&mut self, i: u32) -> &mut Node {
200 &mut self.nodes[i as usize]
201 }
202
203 #[inline(always)]
206 pub(crate) unsafe fn n_unchecked(&self, i: u32) -> &Node {
207 debug_assert!((i as usize) < self.nodes.len());
208 unsafe { self.nodes.get_unchecked(i as usize) }
210 }
211
212 #[inline(always)]
215 pub(crate) unsafe fn n_mut_unchecked(&mut self, i: u32) -> &mut Node {
216 debug_assert!((i as usize) < self.nodes.len());
217 unsafe { self.nodes.get_unchecked_mut(i as usize) }
219 }
220
221 #[inline(always)]
224 pub(crate) unsafe fn cost_unchecked(&self, id: u32) -> u32 {
225 let n = unsafe { self.n_unchecked(id) };
227 if !n.has_cold() {
228 0
229 } else {
230 unsafe { self.cold.get_unchecked(n.cold_idx()).error_cost }
232 }
233 }
234
235 #[inline(always)]
238 pub(crate) unsafe fn dprec_unchecked(&self, id: u32) -> i32 {
239 let n = unsafe { self.n_unchecked(id) };
241 if !n.has_cold() {
242 0
243 } else {
244 unsafe { self.cold.get_unchecked(n.cold_idx()).dprec }
246 }
247 }
248
249 #[inline(always)]
252 pub(crate) unsafe fn ext_id_unchecked(&self, id: u32) -> u32 {
253 let n = unsafe { self.n_unchecked(id) };
255 let e = n.ext_inline();
256 if e == EXT_NONE_U8 {
257 NONE
258 } else if e < EXT_COLD_U8 {
259 e as u32
260 } else {
261 unsafe { self.cold.get_unchecked(n.cold_idx()).ext }
263 }
264 }
265
266 #[inline(always)]
269 pub(crate) unsafe fn vis_desc_unchecked(&self, id: u32) -> u32 {
270 let n = unsafe { self.n_unchecked(id) };
272 if !n.has_cold() {
273 n.inline_vis()
274 } else {
275 unsafe { self.cold.get_unchecked(n.cold_idx()).visible_descendants }
277 }
278 }
279
280 #[inline(always)]
281 pub fn cost(&self, id: u32) -> u32 {
282 let n = self.n(id);
283 if !n.has_cold() {
284 0
285 } else {
286 self.cold[n.cold_idx()].error_cost
287 }
288 }
289
290 #[inline(always)]
291 pub fn dprec(&self, id: u32) -> i32 {
292 let n = self.n(id);
293 if !n.has_cold() {
294 0
295 } else {
296 self.cold[n.cold_idx()].dprec
297 }
298 }
299
300 #[inline(always)]
301 pub fn ext_id(&self, id: u32) -> u32 {
302 let n = self.n(id);
303 let e = n.ext_inline();
304 if e == EXT_NONE_U8 {
305 NONE
306 } else if e < EXT_COLD_U8 {
307 e as u32
308 } else {
309 self.cold[n.cold_idx()].ext
310 }
311 }
312
313 #[inline(always)]
314 pub fn vis_desc(&self, id: u32) -> u32 {
315 let n = self.n(id);
316 if !n.has_cold() {
317 n.inline_vis()
318 } else {
319 self.cold[n.cold_idx()].visible_descendants
320 }
321 }
322
323 #[inline]
324 fn ensure_cold_mut(&mut self, id: u32) -> &mut ColdNode {
325 let n = self.nodes[id as usize];
326 if n.has_cold() {
327 let idx = n.cold_idx();
328 &mut self.cold[idx]
329 } else {
330 let e = n.ext_inline();
331 let ext = if e == EXT_NONE_U8 { NONE } else { e as u32 };
332 let idx = self.cold.len() as u32;
333 self.cold.push(ColdNode {
334 error_cost: 0,
335 dprec: 0,
336 visible_descendants: n.inline_vis(),
337 ext,
338 });
339 self.nodes[id as usize].packed = (n.packed & FLAGS_MASK) | F_COLD_BIT | (idx << 6);
340 &mut self.cold[idx as usize]
341 }
342 }
343
344 #[inline]
345 pub fn set_error_cost(&mut self, id: u32, cost: u32) {
346 if cost != 0 || self.n(id).has_cold() {
347 self.ensure_cold_mut(id).error_cost = cost;
348 }
349 }
350
351 #[inline]
352 pub fn add_dprec(&mut self, id: u32, delta: i32) {
353 if delta != 0 {
354 self.ensure_cold_mut(id).dprec += delta;
355 }
356 }
357
358 #[inline(always)]
359 pub fn set_ext(&mut self, id: u32, ext: u32) {
360 if ext == NONE {
361 return;
362 }
363 if ext < EXT_COLD_U8 as u32 {
364 let n = self.n_mut(id);
365 n.child_count_ext = (n.child_count_ext & CHILD_COUNT_MASK) | (ext << 24);
366 if n.has_cold() {
367 let idx = n.cold_idx();
368 self.cold[idx].ext = ext;
369 }
370 } else {
371 let n = self.n_mut(id);
372 n.child_count_ext =
373 (n.child_count_ext & CHILD_COUNT_MASK) | ((EXT_COLD_U8 as u32) << 24);
374 self.ensure_cold_mut(id).ext = ext;
375 }
376 }
377
378 #[inline]
379 fn alloc_cold(&mut self, c: ColdNode) -> u32 {
380 let idx = self.cold.len() as u32;
381 self.cold.push(c);
382 idx
383 }
384
385 pub fn intern_ext(&mut self, bytes: &[u8]) -> u32 {
387 if bytes.is_empty() {
388 return 0;
389 }
390 let h = hash_bytes(bytes);
391 if self.ext_table.is_empty() {
392 self.ext_table.resize(32, 0);
393 } else if (self.ext.len() + 1) * 2 >= self.ext_table.len() {
394 let new_cap = self.ext_table.len() * 2;
395 let mut nt = vec![0u32; new_cap];
396 let mask = new_cap - 1;
397 for (idx0, &eh) in self.ext_hashes.iter().enumerate() {
398 let mut pos = (eh as usize) & mask;
399 while nt[pos] != 0 {
400 pos = (pos + 1) & mask;
401 }
402 nt[pos] = (idx0 + 1) as u32;
403 }
404 self.ext_table = nt;
405 }
406 let mask = self.ext_table.len() - 1;
407 let mut pos = (h as usize) & mask;
408 loop {
409 let slot = self.ext_table[pos];
410 if slot == 0 {
411 self.ext.push(bytes.to_vec());
412 self.ext_hashes.push(h);
413 let id = self.ext.len() as u32;
414 self.ext_table[pos] = id;
415 self.ext_total += bytes.len();
416 return id;
417 }
418 let idx = (slot - 1) as usize;
419 if self.ext_hashes[idx] == h && self.ext[idx].as_slice() == bytes {
420 return slot;
421 }
422 pos = (pos + 1) & mask;
423 }
424 }
425
426 #[inline(always)]
427 pub fn ext_bytes(&self, i: u32) -> &[u8] {
428 if i == 0 || i == NONE {
429 &[]
430 } else {
431 self.ext.get(i as usize - 1).map_or(&[], |v| v.as_slice())
432 }
433 }
434
435 #[inline(always)]
436 pub fn children(&self, i: u32) -> &[u32] {
437 let n = self.n(i);
438 let cc = n.child_count();
439 if cc == 0 {
440 &[]
441 } else {
442 let a = n.first_child as usize;
443 let b = a + cc as usize;
444 &self.kids[a..b]
445 }
446 }
447
448 #[inline(always)]
449 pub fn new_leaf(&mut self, lang: &Language, sym: u16, padding: Len, size: Len) -> u32 {
450 let flags = lang.leaf_flags_fast(sym) as u32;
451 self.push(Node {
452 padding,
453 size,
454 first_child: 0,
455 child_count_ext: EXT_NONE_HI,
456 sym,
457 prod: 0,
458 packed: flags,
459 })
460 }
461
462 #[inline]
463 pub fn new_missing_leaf(&mut self, lang: &Language, sym: u16) -> u32 {
464 let flags = (lang.leaf_flags_fast(sym) | F_MISSING) as u32;
465 let cidx = self.alloc_cold(ColdNode {
466 error_cost: ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY,
467 dprec: 0,
468 visible_descendants: 0,
469 ext: NONE,
470 });
471 self.push(Node {
472 padding: Len::default(),
473 size: Len::default(),
474 first_child: 0,
475 child_count_ext: EXT_NONE_HI,
476 sym,
477 prod: 0,
478 packed: flags | F_COLD_BIT | (cidx << 6),
479 })
480 }
481
482 #[inline(always)]
483 fn push(&mut self, n: Node) -> u32 {
484 let id = self.nodes.len() as u32;
485 self.nodes.push(n);
486 id
487 }
488
489 #[cold]
491 fn count_visible_children(&mut self, lang: &Language, root_id: u32) -> u32 {
492 let r = root_id as usize;
493 if r < self.vc_cache.len() && self.vc_cache[r] != NONE {
494 return self.vc_cache[r];
495 }
496 if self.vc_cache.len() <= r {
497 self.vc_cache.resize(self.nodes.len(), NONE);
498 }
499 let mut stack: Vec<(u32, bool)> = vec![(root_id, false)];
501 while let Some((id, expanded)) = stack.pop() {
502 let idx = id as usize;
503 if idx < self.vc_cache.len() && self.vc_cache[idx] != NONE {
504 continue;
505 }
506 let n = self.nodes[idx];
507 let cc = n.child_count() as usize;
508 let start = n.first_child as usize;
509 let end = start + cc;
510 let alias = lang.alias_sequence(n.prod);
511 if !expanded {
512 stack.push((id, true));
513 let mut structural = 0usize;
514 for k in start..end {
515 let c = self.kids[k];
516 let ch = self.nodes[c as usize];
517 let aliased = !ch.extra()
518 && ch.sym != 0
519 && alias.is_some_and(|a| a.get(structural).copied().unwrap_or(0) != 0);
520 if !aliased && !ch.visible() && ch.child_count() > 0 {
521 let cu = c as usize;
522 if cu >= self.vc_cache.len() || self.vc_cache[cu] == NONE {
523 stack.push((c, false));
524 }
525 }
526 if !ch.extra() {
527 structural += 1;
528 }
529 }
530 } else {
531 let mut count = 0u32;
532 let mut structural = 0usize;
533 for k in start..end {
534 let c = self.kids[k];
535 let ch = self.nodes[c as usize];
536 let aliased = !ch.extra()
537 && ch.sym != 0
538 && alias.is_some_and(|a| a.get(structural).copied().unwrap_or(0) != 0);
539 if aliased || ch.visible() {
540 count += 1;
541 } else if ch.child_count() > 0 {
542 count += self.vc_cache[c as usize];
543 }
544 if !ch.extra() {
545 structural += 1;
546 }
547 }
548 if idx >= self.vc_cache.len() {
549 self.vc_cache.resize(self.nodes.len(), NONE);
550 }
551 self.vc_cache[idx] = count;
552 }
553 }
554 self.vc_cache[r]
555 }
556
557 #[inline]
559 pub fn new_node(&mut self, lang: &Language, sym: u16, children: &[u32], prod: u16) -> u32 {
560 let n_kids = children.len();
561 let is_err_parent = sym == SYM_ERROR || sym == SYM_ERROR_REPEAT;
562
563 if n_kids == 1 && prod == 0 && !is_err_parent {
565 let c = children[0];
566 let ch = *self.n(c);
567 if !ch.has_cold() {
568 let vis = ch.inline_vis() + ch.visible() as u32;
569 if vis <= MAX_INLINE_VIS {
570 let first_child = self.kids.len() as u32;
571 self.kids.push(c);
572 let flags = lang.node_flags_fast(sym) as u32;
573 return self.push(Node {
574 padding: ch.padding,
575 size: ch.size,
576 first_child,
577 child_count_ext: (ch.child_count_ext & 0xFF00_0000) | 1,
578 sym,
579 prod: 0,
580 packed: flags | (vis << 6),
581 });
582 }
583 }
584 }
585
586 if n_kids > 1 && n_kids <= CHILD_COUNT_MASK as usize && prod == 0 && !is_err_parent {
588 let c0 = children[0];
589 let ch0 = *self.n(c0);
590 let padding = ch0.padding;
591 let mut size = ch0.size;
592 let mut or_packed = ch0.packed;
593 let mut vis = ch0.inline_vis().saturating_add(ch0.visible() as u32);
594 let mut ext_hi = ch0.child_count_ext & 0xFF00_0000;
595 for &c in &children[1..] {
596 let ch = *self.n(c);
597 size = size.add(ch.total());
598 or_packed |= ch.packed;
599 vis = vis.saturating_add(ch.inline_vis() + ch.visible() as u32);
600 let ce = ch.child_count_ext & 0xFF00_0000;
601 if ce != EXT_NONE_HI {
602 ext_hi = ce;
603 }
604 }
605 if (or_packed & F_COLD_BIT) == 0 && vis <= MAX_INLINE_VIS {
606 let first_child = self.kids.len() as u32;
607 self.kids.extend_from_slice(children);
608 let flags = lang.node_flags_fast(sym) as u32;
609 return self.push(Node {
610 padding,
611 size,
612 first_child,
613 child_count_ext: ext_hi | (n_kids as u32),
614 sym,
615 prod: 0,
616 packed: flags | (vis << 6),
617 });
618 }
619 }
620
621 self.new_node_general(lang, sym, children, prod, is_err_parent)
622 }
623
624 #[inline]
629 pub(crate) unsafe fn new_node_unchecked(
630 &mut self,
631 lang: &Language,
632 sym: u16,
633 children: &[u32],
634 prod: u16,
635 ) -> u32 {
636 let n_kids = children.len();
637 let is_err_parent = sym == SYM_ERROR || sym == SYM_ERROR_REPEAT;
638
639 if n_kids == 1 && prod == 0 && !is_err_parent {
640 let c = unsafe { *children.get_unchecked(0) };
642 let ch = *unsafe { self.n_unchecked(c) };
643 if !ch.has_cold() {
644 let vis = ch.inline_vis() + ch.visible() as u32;
645 if vis <= MAX_INLINE_VIS {
646 let first_child = self.kids.len() as u32;
647 self.kids.push(c);
648 let flags = lang.node_flags_fast(sym) as u32;
649 return self.push(Node {
650 padding: ch.padding,
651 size: ch.size,
652 first_child,
653 child_count_ext: (ch.child_count_ext & 0xFF00_0000) | 1,
654 sym,
655 prod: 0,
656 packed: flags | (vis << 6),
657 });
658 }
659 }
660 }
661
662 if n_kids > 1 && n_kids <= CHILD_COUNT_MASK as usize && prod == 0 && !is_err_parent {
663 let c0 = unsafe { *children.get_unchecked(0) };
665 let ch0 = *unsafe { self.n_unchecked(c0) };
666 let padding = ch0.padding;
667 let mut size = ch0.size;
668 let mut or_packed = ch0.packed;
669 let mut vis = ch0.inline_vis().saturating_add(ch0.visible() as u32);
670 let mut ext_hi = ch0.child_count_ext & 0xFF00_0000;
671 for &c in unsafe { children.get_unchecked(1..) } {
672 let ch = *unsafe { self.n_unchecked(c) };
673 size = size.add(ch.total());
674 or_packed |= ch.packed;
675 vis = vis.saturating_add(ch.inline_vis() + ch.visible() as u32);
676 let ce = ch.child_count_ext & 0xFF00_0000;
677 if ce != EXT_NONE_HI {
678 ext_hi = ce;
679 }
680 }
681 if (or_packed & F_COLD_BIT) == 0 && vis <= MAX_INLINE_VIS {
682 let first_child = self.kids.len() as u32;
683 self.kids.extend_from_slice(children);
684 let flags = lang.node_flags_fast(sym) as u32;
685 return self.push(Node {
686 padding,
687 size,
688 first_child,
689 child_count_ext: ext_hi | (n_kids as u32),
690 sym,
691 prod: 0,
692 packed: flags | (vis << 6),
693 });
694 }
695 }
696
697 self.new_node_general(lang, sym, children, prod, is_err_parent)
698 }
699
700 fn new_node_general(
701 &mut self,
702 lang: &Language,
703 sym: u16,
704 children: &[u32],
705 prod: u16,
706 is_err_parent: bool,
707 ) -> u32 {
708 let first_child = self.kids.len() as u32;
709 self.kids.extend_from_slice(children);
710 let flags = lang.node_flags_fast(sym) as u32;
711 let mut padding = Len::default();
712 let mut size = Len::default();
713 let mut error_cost = 0u32;
714 let mut dprec = 0i32;
715 let mut vis_desc = 0u32;
716 let mut ext = NONE;
717
718 let alias = lang.alias_sequence(prod);
719 let mut structural = 0usize;
720 for (i, &c) in children.iter().enumerate() {
721 let ch = *self.n(c);
722 if i == 0 {
723 padding = ch.padding;
724 size = ch.size;
725 } else {
726 size = size.add(ch.total());
727 }
728 let (ch_cost, ch_dprec, ch_vis, ch_ext) = if ch.has_cold() {
729 let cn = self.cold[ch.cold_idx()];
730 let ce = {
731 let e = ch.ext_inline();
732 if e == EXT_NONE_U8 {
733 NONE
734 } else if e < EXT_COLD_U8 {
735 e as u32
736 } else {
737 cn.ext
738 }
739 };
740 (cn.error_cost, cn.dprec, cn.visible_descendants, ce)
741 } else {
742 let e = ch.ext_inline();
743 let ce = if e == EXT_NONE_U8 { NONE } else { e as u32 };
744 (0, 0, ch.inline_vis(), ce)
745 };
746 if ch.sym == SYM_ERROR_REPEAT {
747 error_cost = error_cost.wrapping_add(ch_cost.wrapping_sub(extent_cost(ch.size)));
748 } else {
749 error_cost = error_cost.wrapping_add(ch_cost);
750 let gc = ch.child_count();
751 if is_err_parent && !ch.extra() && !(ch.is_error() && gc == 0) {
752 if ch.visible() {
753 error_cost = error_cost.wrapping_add(ERROR_COST_PER_SKIPPED_TREE);
754 } else if gc > 0 {
755 let vc = self.count_visible_children(lang, c);
756 error_cost =
757 error_cost.wrapping_add(ERROR_COST_PER_SKIPPED_TREE.wrapping_mul(vc));
758 }
759 }
760 }
761 dprec += ch_dprec;
762 if ch_ext != NONE {
763 ext = ch_ext;
764 }
765 vis_desc += ch_vis;
766 let aliased = !ch.extra()
767 && ch.sym != 0
768 && alias.is_some_and(|a| a.get(structural).copied().unwrap_or(0) != 0);
769 if aliased || ch.visible() {
770 vis_desc += 1;
771 }
772 if !ch.extra() {
773 structural += 1;
774 }
775 }
776 if is_err_parent {
777 error_cost = error_cost.wrapping_add(extent_cost(size));
778 }
779
780 let (ext_hi, ext_needs_cold) = if ext == NONE {
781 (EXT_NONE_HI, false)
782 } else if ext < EXT_COLD_U8 as u32 {
783 (ext << 24, false)
784 } else {
785 ((EXT_COLD_U8 as u32) << 24, true)
786 };
787
788 let packed =
789 if error_cost == 0 && dprec == 0 && !ext_needs_cold && vis_desc <= MAX_INLINE_VIS {
790 flags | (vis_desc << 6)
791 } else {
792 let cidx = self.alloc_cold(ColdNode {
793 error_cost,
794 dprec,
795 visible_descendants: vis_desc,
796 ext,
797 });
798 flags | F_COLD_BIT | (cidx << 6)
799 };
800 let cc = (children.len() as u32).min(CHILD_COUNT_MASK);
801 self.push(Node {
802 padding,
803 size,
804 first_child,
805 child_count_ext: ext_hi | cc,
806 sym,
807 prod,
808 packed,
809 })
810 }
811
812 pub fn new_error_node(&mut self, lang: &Language, children: &[u32], extra: bool) -> u32 {
813 let i = self.new_node(lang, SYM_ERROR, children, 0);
814 if extra {
815 self.n_mut(i).set_flag(F_EXTRA);
816 }
817 i
818 }
819
820 pub fn compare(&self, a: u32, b: u32) -> i32 {
822 let mut work = vec![(a, b)];
823 while let Some((l, r)) = work.pop() {
824 let (ln, rn) = (self.n(l), self.n(r));
825 let res = if ln.sym < rn.sym {
826 -1
827 } else if rn.sym < ln.sym {
828 1
829 } else if ln.child_count() < rn.child_count() {
830 -1
831 } else if rn.child_count() < ln.child_count() {
832 1
833 } else {
834 0
835 };
836 if res != 0 {
837 return res;
838 }
839 let (lc, rc) = (self.children(l), self.children(r));
840 for i in (0..lc.len()).rev() {
841 work.push((lc[i], rc[i]));
842 }
843 }
844 0
845 }
846}