1use crate::token::{Tin, Token};
4use crate::value::Value;
5use crate::Lexer;
6use crate::{ActionError, Context, ContextAction};
7use std::cell::RefCell;
8use std::collections::HashMap;
9use std::fmt;
10use std::rc::Rc;
11use std::sync::Arc;
12
13pub type AltCondition = Arc<dyn Fn(&mut Rule, &mut Context) -> bool + Send + Sync>;
16
17pub type AltConditionWithMatch =
20 Arc<dyn Fn(&mut Rule, &mut Context, &mut AltMatch) -> bool + Send + Sync>;
21
22pub type AltConditionWithLexer =
25 Arc<dyn for<'source> Fn(&mut Rule, &mut Context, &mut Lexer<'source>) -> bool + Send + Sync>;
26
27pub type AltConditionWithLexerAndMatch = Arc<
30 dyn for<'source> Fn(&mut Rule, &mut Context, &mut AltMatch, &mut Lexer<'source>) -> bool
31 + Send
32 + Sync,
33>;
34
35pub type AltNext = Arc<dyn Fn(&mut Rule, &mut Context) -> Option<String> + Send + Sync>;
37pub type AltNextWithMatch =
38 Arc<dyn Fn(&mut Rule, &mut Context, &mut AltMatch) -> Option<String> + Send + Sync>;
39
40pub type AltBack = Arc<dyn Fn(&mut Rule, &mut Context) -> usize + Send + Sync>;
42pub type AltBackWithMatch =
43 Arc<dyn Fn(&mut Rule, &mut Context, &mut AltMatch) -> usize + Send + Sync>;
44
45pub type AltError = Arc<dyn Fn(&mut Rule, &mut Context) -> Option<Token> + Send + Sync>;
48pub type AltErrorWithMatch =
49 Arc<dyn Fn(&mut Rule, &mut Context, &mut AltMatch) -> Option<Token> + Send + Sync>;
50
51pub type AltModifier = Arc<dyn Fn(AltSpec, &mut Rule, &mut Context) -> AltSpec + Send + Sync>;
54
55pub type AltModifierWithMatch =
59 Arc<dyn Fn(AltMatch, &mut Rule, &mut Context, Option<&RuleSnapshot>) -> AltMatch + Send + Sync>;
60
61pub type AltAction = Arc<
65 dyn Fn(&mut Rule, &mut Context, &mut AltMatch) -> Result<Option<Token>, ActionError>
66 + Send
67 + Sync,
68>;
69
70#[derive(Clone)]
71pub enum AltActionBinding {
72 Named(String),
73 Context(ContextAction),
74 Matched(AltAction),
75}
76
77#[derive(Clone)]
82pub enum ActionBinding {
83 Named(String),
84 Callback(ContextAction),
85 State(StateAction),
86}
87
88pub type StateAction = Arc<
92 dyn Fn(
93 &mut Rule,
94 &mut Context,
95 Option<&RuleSnapshot>,
96 Option<Token>,
97 ) -> Result<Option<Token>, ActionError>
98 + Send
99 + Sync,
100>;
101
102#[derive(Clone, Default)]
106pub struct AltMatch {
107 pub p: Option<String>,
108 pub r: Option<String>,
109 pub b: usize,
110 pub n: HashMap<String, i32>,
111 pub u: HashMap<String, Value>,
112 pub k: HashMap<String, Value>,
113 pub g: Vec<String>,
114 pub e: Option<Box<Token>>,
120 pub h: Option<AltModifierWithMatch>,
124 pub actions: Vec<AltActionBinding>,
125 pub action_configs: HashMap<String, Value>,
126}
127
128impl AltMatch {
129 pub(crate) fn reset(&mut self) {
142 self.p = None;
143 self.r = None;
144 self.b = 0;
145 self.e = None;
146 self.h = None;
147 if !self.n.is_empty() {
148 self.n.clear();
149 }
150 if !self.u.is_empty() {
151 self.u.clear();
152 }
153 if !self.k.is_empty() {
154 self.k.clear();
155 }
156 if !self.g.is_empty() {
157 self.g.clear();
158 }
159 if !self.actions.is_empty() {
160 self.actions.clear();
161 }
162 if !self.action_configs.is_empty() {
163 self.action_configs.clear();
164 }
165 }
166}
167
168#[derive(Debug, Clone, Copy, PartialEq, Eq)]
169pub enum RuleState {
170 Open,
171 Close,
172}
173
174#[derive(Debug, Clone, PartialEq)]
175pub struct RuleDoneAlt {
176 pub b: usize,
177 pub g: Vec<String>,
178 pub p: String,
179 pub r: String,
180 pub err: Option<Token>,
181}
182
183#[derive(Debug, Clone, PartialEq)]
184pub struct RuleDone {
185 pub state: RuleState,
187 pub alt: Option<RuleDoneAlt>,
189 pub forced: bool,
191 pub probe: bool,
196}
197
198#[derive(Debug, Clone, Copy, PartialEq, Eq)]
199pub enum CompareOp {
200 Eq,
201 Ne,
202 Lt,
203 Lte,
204 Gt,
205 Gte,
206 Exist,
207}
208
209#[derive(Debug, Clone, PartialEq)]
210pub struct Condition {
211 pub path: Vec<String>,
212 pub op: CompareOp,
213 pub value: Value,
214}
215
216#[derive(Clone, Default)]
217pub struct AltSpec {
218 pub s: Vec<Vec<Tin>>,
219 pub s_names: Vec<Vec<String>>,
227 pub s_bound: Vec<Vec<Tin>>,
231 pub p: Option<String>,
232 pub p_fn: Option<AltNext>,
233 pub p_match: Option<AltNextWithMatch>,
234 pub r: Option<String>,
235 pub r_fn: Option<AltNext>,
236 pub r_match: Option<AltNextWithMatch>,
237 pub b: usize,
238 pub b_fn: Option<AltBack>,
239 pub b_match: Option<AltBackWithMatch>,
240 pub a: Vec<String>,
241 pub action_fns: Vec<ContextAction>,
245 pub action_order: Vec<AltActionBinding>,
246 pub matched_action_fns: Vec<AltAction>,
247 pub action_configs: HashMap<String, Value>,
248 pub c: Vec<Condition>,
249 pub c_ref: Option<String>,
250 pub c_fn: Option<AltCondition>,
251 pub c_match: Option<AltConditionWithMatch>,
252 pub c_lex: Option<AltConditionWithLexer>,
253 pub c_lex_match: Option<AltConditionWithLexerAndMatch>,
254 pub n: HashMap<String, i32>,
255 pub u: HashMap<String, Value>,
256 pub k: HashMap<String, Value>,
257 pub g: String,
258 pub h: Option<AltModifier>,
259 pub h_match: Option<AltModifierWithMatch>,
260 pub e: Option<AltError>,
261 pub e_match: Option<AltErrorWithMatch>,
262}
263
264impl AltSpec {
265 pub fn new() -> Self {
266 Self::default()
267 }
268}
269
270#[derive(Clone)]
271pub struct RuleSpec {
272 pub name: String,
273 pub open: Vec<AltSpec>,
274 pub close: Vec<AltSpec>,
275 pub bo: Vec<String>,
276 pub ao: Vec<String>,
277 pub bc: Vec<String>,
278 pub ac: Vec<String>,
279 pub bo_fns: Vec<ContextAction>,
282 pub ao_fns: Vec<ContextAction>,
283 pub bc_fns: Vec<ContextAction>,
284 pub ac_fns: Vec<ContextAction>,
285 pub bo_state_fns: Vec<StateAction>,
286 pub ao_state_fns: Vec<StateAction>,
287 pub bc_state_fns: Vec<StateAction>,
288 pub ac_state_fns: Vec<StateAction>,
289 pub bo_order: Vec<ActionBinding>,
290 pub ao_order: Vec<ActionBinding>,
291 pub bc_order: Vec<ActionBinding>,
292 pub ac_order: Vec<ActionBinding>,
293}
294
295impl fmt::Debug for RuleSpec {
296 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
297 formatter
298 .debug_struct("RuleSpec")
299 .field("name", &self.name)
300 .field("open", &self.open.len())
301 .field("close", &self.close.len())
302 .field("bo", &self.bo.len())
303 .field("ao", &self.ao.len())
304 .field("bc", &self.bc.len())
305 .field("ac", &self.ac.len())
306 .finish_non_exhaustive()
307 }
308}
309
310impl RuleSpec {
311 pub fn new(name: impl Into<String>) -> Self {
312 RuleSpec {
313 name: name.into(),
314 open: Vec::new(),
315 close: Vec::new(),
316 bo: Vec::new(),
317 ao: Vec::new(),
318 bc: Vec::new(),
319 ac: Vec::new(),
320 bo_fns: Vec::new(),
321 ao_fns: Vec::new(),
322 bc_fns: Vec::new(),
323 ac_fns: Vec::new(),
324 bo_state_fns: Vec::new(),
325 ao_state_fns: Vec::new(),
326 bc_state_fns: Vec::new(),
327 ac_state_fns: Vec::new(),
328 bo_order: Vec::new(),
329 ao_order: Vec::new(),
330 bc_order: Vec::new(),
331 ac_order: Vec::new(),
332 }
333 }
334
335 pub fn clear(&mut self) -> &mut Self {
337 self.open.clear();
338 self.close.clear();
339 self.clear_actions(&[]);
340 self
341 }
342
343 pub fn add_open(&mut self, alt: AltSpec) -> &mut Self {
344 self.open.push(alt);
345 self
346 }
347
348 pub fn prepend_open(&mut self, alt: AltSpec) -> &mut Self {
349 self.open.insert(0, alt);
350 self
351 }
352
353 pub fn add_close(&mut self, alt: AltSpec) -> &mut Self {
354 self.close.push(alt);
355 self
356 }
357
358 pub fn prepend_close(&mut self, alt: AltSpec) -> &mut Self {
359 self.close.insert(0, alt);
360 self
361 }
362
363 pub fn clear_open(&mut self) -> &mut Self {
364 self.open.clear();
365 self
366 }
367
368 pub fn clear_close(&mut self) -> &mut Self {
369 self.close.clear();
370 self
371 }
372
373 pub fn modify_open(&mut self, mods: &crate::utility::ListMods<AltSpec>) -> &mut Self {
376 self.open = crate::utility::modlist(std::mem::take(&mut self.open), Some(mods));
377 self
378 }
379
380 pub fn modify_close(&mut self, mods: &crate::utility::ListMods<AltSpec>) -> &mut Self {
381 self.close = crate::utility::modlist(std::mem::take(&mut self.close), Some(mods));
382 self
383 }
384
385 pub fn add_bo(
386 &mut self,
387 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
388 ) -> &mut Self {
389 prepare_order(
390 &self.bo,
391 &self.bo_fns,
392 &self.bo_state_fns,
393 &mut self.bo_order,
394 );
395 let action = infallible_action(action);
396 self.bo_fns.push(action.clone());
397 self.bo_order.push(ActionBinding::Callback(action));
398 self
399 }
400
401 pub fn prepend_bo(
402 &mut self,
403 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
404 ) -> &mut Self {
405 prepare_order(
406 &self.bo,
407 &self.bo_fns,
408 &self.bo_state_fns,
409 &mut self.bo_order,
410 );
411 let action = infallible_action(action);
412 self.bo_fns.insert(0, action.clone());
413 self.bo_order.insert(0, ActionBinding::Callback(action));
414 self
415 }
416
417 pub fn add_ao(
418 &mut self,
419 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
420 ) -> &mut Self {
421 prepare_order(
422 &self.ao,
423 &self.ao_fns,
424 &self.ao_state_fns,
425 &mut self.ao_order,
426 );
427 let action = infallible_action(action);
428 self.ao_fns.push(action.clone());
429 self.ao_order.push(ActionBinding::Callback(action));
430 self
431 }
432
433 pub fn prepend_ao(
434 &mut self,
435 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
436 ) -> &mut Self {
437 prepare_order(
438 &self.ao,
439 &self.ao_fns,
440 &self.ao_state_fns,
441 &mut self.ao_order,
442 );
443 let action = infallible_action(action);
444 self.ao_fns.insert(0, action.clone());
445 self.ao_order.insert(0, ActionBinding::Callback(action));
446 self
447 }
448
449 pub fn add_bc(
450 &mut self,
451 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
452 ) -> &mut Self {
453 prepare_order(
454 &self.bc,
455 &self.bc_fns,
456 &self.bc_state_fns,
457 &mut self.bc_order,
458 );
459 let action = infallible_action(action);
460 self.bc_fns.push(action.clone());
461 self.bc_order.push(ActionBinding::Callback(action));
462 self
463 }
464
465 pub fn prepend_bc(
466 &mut self,
467 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
468 ) -> &mut Self {
469 prepare_order(
470 &self.bc,
471 &self.bc_fns,
472 &self.bc_state_fns,
473 &mut self.bc_order,
474 );
475 let action = infallible_action(action);
476 self.bc_fns.insert(0, action.clone());
477 self.bc_order.insert(0, ActionBinding::Callback(action));
478 self
479 }
480
481 pub fn add_ac(
482 &mut self,
483 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
484 ) -> &mut Self {
485 prepare_order(
486 &self.ac,
487 &self.ac_fns,
488 &self.ac_state_fns,
489 &mut self.ac_order,
490 );
491 let action = infallible_action(action);
492 self.ac_fns.push(action.clone());
493 self.ac_order.push(ActionBinding::Callback(action));
494 self
495 }
496
497 pub fn prepend_ac(
498 &mut self,
499 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
500 ) -> &mut Self {
501 prepare_order(
502 &self.ac,
503 &self.ac_fns,
504 &self.ac_state_fns,
505 &mut self.ac_order,
506 );
507 let action = infallible_action(action);
508 self.ac_fns.insert(0, action.clone());
509 self.ac_order.insert(0, ActionBinding::Callback(action));
510 self
511 }
512
513 pub fn add_bo_result(&mut self, action: ContextAction) -> &mut Self {
514 prepare_order(
515 &self.bo,
516 &self.bo_fns,
517 &self.bo_state_fns,
518 &mut self.bo_order,
519 );
520 self.bo_fns.push(action.clone());
521 self.bo_order.push(ActionBinding::Callback(action));
522 self
523 }
524
525 pub fn add_ao_result(&mut self, action: ContextAction) -> &mut Self {
526 prepare_order(
527 &self.ao,
528 &self.ao_fns,
529 &self.ao_state_fns,
530 &mut self.ao_order,
531 );
532 self.ao_fns.push(action.clone());
533 self.ao_order.push(ActionBinding::Callback(action));
534 self
535 }
536
537 pub fn add_bc_result(&mut self, action: ContextAction) -> &mut Self {
538 prepare_order(
539 &self.bc,
540 &self.bc_fns,
541 &self.bc_state_fns,
542 &mut self.bc_order,
543 );
544 self.bc_fns.push(action.clone());
545 self.bc_order.push(ActionBinding::Callback(action));
546 self
547 }
548
549 pub fn add_ac_result(&mut self, action: ContextAction) -> &mut Self {
550 prepare_order(
551 &self.ac,
552 &self.ac_fns,
553 &self.ac_state_fns,
554 &mut self.ac_order,
555 );
556 self.ac_fns.push(action.clone());
557 self.ac_order.push(ActionBinding::Callback(action));
558 self
559 }
560
561 pub fn add_bo_with_state(&mut self, action: StateAction) -> &mut Self {
562 prepare_order(
563 &self.bo,
564 &self.bo_fns,
565 &self.bo_state_fns,
566 &mut self.bo_order,
567 );
568 self.bo_state_fns.push(action.clone());
569 self.bo_order.push(ActionBinding::State(action));
570 self
571 }
572
573 pub fn prepend_bo_with_state(&mut self, action: StateAction) -> &mut Self {
574 prepare_order(
575 &self.bo,
576 &self.bo_fns,
577 &self.bo_state_fns,
578 &mut self.bo_order,
579 );
580 self.bo_state_fns.insert(0, action.clone());
581 self.bo_order.insert(0, ActionBinding::State(action));
582 self
583 }
584
585 pub fn add_ao_with_state(&mut self, action: StateAction) -> &mut Self {
586 prepare_order(
587 &self.ao,
588 &self.ao_fns,
589 &self.ao_state_fns,
590 &mut self.ao_order,
591 );
592 self.ao_state_fns.push(action.clone());
593 self.ao_order.push(ActionBinding::State(action));
594 self
595 }
596
597 pub fn prepend_ao_with_state(&mut self, action: StateAction) -> &mut Self {
598 prepare_order(
599 &self.ao,
600 &self.ao_fns,
601 &self.ao_state_fns,
602 &mut self.ao_order,
603 );
604 self.ao_state_fns.insert(0, action.clone());
605 self.ao_order.insert(0, ActionBinding::State(action));
606 self
607 }
608
609 pub fn add_bc_with_state(&mut self, action: StateAction) -> &mut Self {
610 prepare_order(
611 &self.bc,
612 &self.bc_fns,
613 &self.bc_state_fns,
614 &mut self.bc_order,
615 );
616 self.bc_state_fns.push(action.clone());
617 self.bc_order.push(ActionBinding::State(action));
618 self
619 }
620
621 pub fn prepend_bc_with_state(&mut self, action: StateAction) -> &mut Self {
622 prepare_order(
623 &self.bc,
624 &self.bc_fns,
625 &self.bc_state_fns,
626 &mut self.bc_order,
627 );
628 self.bc_state_fns.insert(0, action.clone());
629 self.bc_order.insert(0, ActionBinding::State(action));
630 self
631 }
632
633 pub fn add_ac_with_state(&mut self, action: StateAction) -> &mut Self {
634 prepare_order(
635 &self.ac,
636 &self.ac_fns,
637 &self.ac_state_fns,
638 &mut self.ac_order,
639 );
640 self.ac_state_fns.push(action.clone());
641 self.ac_order.push(ActionBinding::State(action));
642 self
643 }
644
645 pub fn prepend_ac_with_state(&mut self, action: StateAction) -> &mut Self {
646 prepare_order(
647 &self.ac,
648 &self.ac_fns,
649 &self.ac_state_fns,
650 &mut self.ac_order,
651 );
652 self.ac_state_fns.insert(0, action.clone());
653 self.ac_order.insert(0, ActionBinding::State(action));
654 self
655 }
656
657 pub fn add_bo_ref(&mut self, action: impl Into<String>) -> &mut Self {
658 add_named(
659 &mut self.bo,
660 &mut self.bo_fns,
661 &mut self.bo_state_fns,
662 &mut self.bo_order,
663 action.into(),
664 false,
665 );
666 self
667 }
668
669 pub fn prepend_bo_ref(&mut self, action: impl Into<String>) -> &mut Self {
670 add_named(
671 &mut self.bo,
672 &mut self.bo_fns,
673 &mut self.bo_state_fns,
674 &mut self.bo_order,
675 action.into(),
676 true,
677 );
678 self
679 }
680
681 pub fn add_ao_ref(&mut self, action: impl Into<String>) -> &mut Self {
682 add_named(
683 &mut self.ao,
684 &mut self.ao_fns,
685 &mut self.ao_state_fns,
686 &mut self.ao_order,
687 action.into(),
688 false,
689 );
690 self
691 }
692
693 pub fn prepend_ao_ref(&mut self, action: impl Into<String>) -> &mut Self {
694 add_named(
695 &mut self.ao,
696 &mut self.ao_fns,
697 &mut self.ao_state_fns,
698 &mut self.ao_order,
699 action.into(),
700 true,
701 );
702 self
703 }
704
705 pub fn add_bc_ref(&mut self, action: impl Into<String>) -> &mut Self {
706 add_named(
707 &mut self.bc,
708 &mut self.bc_fns,
709 &mut self.bc_state_fns,
710 &mut self.bc_order,
711 action.into(),
712 false,
713 );
714 self
715 }
716
717 pub fn prepend_bc_ref(&mut self, action: impl Into<String>) -> &mut Self {
718 add_named(
719 &mut self.bc,
720 &mut self.bc_fns,
721 &mut self.bc_state_fns,
722 &mut self.bc_order,
723 action.into(),
724 true,
725 );
726 self
727 }
728
729 pub fn add_ac_ref(&mut self, action: impl Into<String>) -> &mut Self {
730 add_named(
731 &mut self.ac,
732 &mut self.ac_fns,
733 &mut self.ac_state_fns,
734 &mut self.ac_order,
735 action.into(),
736 false,
737 );
738 self
739 }
740
741 pub fn prepend_ac_ref(&mut self, action: impl Into<String>) -> &mut Self {
742 add_named(
743 &mut self.ac,
744 &mut self.ac_fns,
745 &mut self.ac_state_fns,
746 &mut self.ac_order,
747 action.into(),
748 true,
749 );
750 self
751 }
752
753 pub fn clear_actions(&mut self, phases: &[&str]) -> &mut Self {
757 let clear_all = phases.is_empty();
758 for phase in ["bo", "ao", "bc", "ac"] {
759 if clear_all || phases.contains(&phase) {
760 match phase {
761 "bo" => {
762 self.bo.clear();
763 self.bo_fns.clear();
764 self.bo_state_fns.clear();
765 self.bo_order.clear();
766 }
767 "ao" => {
768 self.ao.clear();
769 self.ao_fns.clear();
770 self.ao_state_fns.clear();
771 self.ao_order.clear();
772 }
773 "bc" => {
774 self.bc.clear();
775 self.bc_fns.clear();
776 self.bc_state_fns.clear();
777 self.bc_order.clear();
778 }
779 "ac" => {
780 self.ac.clear();
781 self.ac_fns.clear();
782 self.ac_state_fns.clear();
783 self.ac_order.clear();
784 }
785 _ => unreachable!(),
786 }
787 }
788 }
789 self
790 }
791}
792
793fn infallible_action(
794 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
795) -> ContextAction {
796 Arc::new(move |rule, context| {
797 action(rule, context);
798 Ok::<(), ActionError>(())
799 })
800}
801
802impl AltSpec {
803 pub fn add_action(
805 &mut self,
806 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
807 ) -> &mut Self {
808 prepare_alt_order(
809 &self.a,
810 &self.action_fns,
811 &self.matched_action_fns,
812 &mut self.action_order,
813 );
814 let action = infallible_action(action);
815 self.action_fns.push(action.clone());
816 self.action_order.push(AltActionBinding::Context(action));
817 self
818 }
819
820 pub fn prepend_action(
823 &mut self,
824 action: impl Fn(&mut Rule, &mut Context) + Send + Sync + 'static,
825 ) -> &mut Self {
826 prepare_alt_order(
827 &self.a,
828 &self.action_fns,
829 &self.matched_action_fns,
830 &mut self.action_order,
831 );
832 let action = infallible_action(action);
833 self.action_fns.insert(0, action.clone());
834 self.action_order
835 .insert(0, AltActionBinding::Context(action));
836 self
837 }
838
839 pub fn add_action_result(&mut self, action: ContextAction) -> &mut Self {
841 prepare_alt_order(
842 &self.a,
843 &self.action_fns,
844 &self.matched_action_fns,
845 &mut self.action_order,
846 );
847 self.action_fns.push(action.clone());
848 self.action_order.push(AltActionBinding::Context(action));
849 self
850 }
851
852 pub fn prepend_action_result(&mut self, action: ContextAction) -> &mut Self {
853 prepare_alt_order(
854 &self.a,
855 &self.action_fns,
856 &self.matched_action_fns,
857 &mut self.action_order,
858 );
859 self.action_fns.insert(0, action.clone());
860 self.action_order
861 .insert(0, AltActionBinding::Context(action));
862 self
863 }
864
865 pub fn add_action_with_match(
867 &mut self,
868 action: impl Fn(&mut Rule, &mut Context, &AltMatch) -> Option<Token> + Send + Sync + 'static,
869 ) -> &mut Self {
870 self.add_action_with_match_result(Arc::new(move |rule, context, matched| {
871 Ok(action(rule, context, matched))
872 }))
873 }
874
875 pub fn prepend_action_with_match(
876 &mut self,
877 action: impl Fn(&mut Rule, &mut Context, &AltMatch) -> Option<Token> + Send + Sync + 'static,
878 ) -> &mut Self {
879 self.prepend_action_with_match_result(Arc::new(move |rule, context, matched| {
880 Ok(action(rule, context, matched))
881 }))
882 }
883
884 pub fn add_action_with_match_result(&mut self, action: AltAction) -> &mut Self {
885 prepare_alt_order(
886 &self.a,
887 &self.action_fns,
888 &self.matched_action_fns,
889 &mut self.action_order,
890 );
891 self.matched_action_fns.push(action.clone());
892 self.action_order.push(AltActionBinding::Matched(action));
893 self
894 }
895
896 pub fn prepend_action_with_match_result(&mut self, action: AltAction) -> &mut Self {
897 prepare_alt_order(
898 &self.a,
899 &self.action_fns,
900 &self.matched_action_fns,
901 &mut self.action_order,
902 );
903 self.matched_action_fns.insert(0, action.clone());
904 self.action_order
905 .insert(0, AltActionBinding::Matched(action));
906 self
907 }
908
909 pub fn add_action_ref(&mut self, action: impl Into<String>) -> &mut Self {
910 add_alt_named(
911 &mut self.a,
912 &mut self.action_fns,
913 &mut self.matched_action_fns,
914 &mut self.action_order,
915 action.into(),
916 false,
917 );
918 self
919 }
920
921 pub fn prepend_action_ref(&mut self, action: impl Into<String>) -> &mut Self {
922 add_alt_named(
923 &mut self.a,
924 &mut self.action_fns,
925 &mut self.matched_action_fns,
926 &mut self.action_order,
927 action.into(),
928 true,
929 );
930 self
931 }
932}
933
934fn alt_order_matches(
935 named: &[String],
936 callbacks: &[ContextAction],
937 matched: &[AltAction],
938 order: &[AltActionBinding],
939) -> bool {
940 if order.len() != named.len() + callbacks.len() + matched.len() {
941 return false;
942 }
943 let (mut next_name, mut next_callback, mut next_matched) = (0, 0, 0);
949 for binding in order {
950 match binding {
951 AltActionBinding::Named(name) => {
952 if named.get(next_name) != Some(name) {
953 return false;
954 }
955 next_name += 1;
956 }
957 AltActionBinding::Context(callback) => {
958 if !callbacks
959 .get(next_callback)
960 .is_some_and(|expected| Arc::ptr_eq(expected, callback))
961 {
962 return false;
963 }
964 next_callback += 1;
965 }
966 AltActionBinding::Matched(callback) => {
967 if !matched
968 .get(next_matched)
969 .is_some_and(|expected| Arc::ptr_eq(expected, callback))
970 {
971 return false;
972 }
973 next_matched += 1;
974 }
975 }
976 }
977 next_name == named.len() && next_callback == callbacks.len() && next_matched == matched.len()
978}
979
980fn prepare_alt_order(
981 named: &[String],
982 callbacks: &[ContextAction],
983 matched: &[AltAction],
984 order: &mut Vec<AltActionBinding>,
985) {
986 if !alt_order_matches(named, callbacks, matched, order) {
987 *order = named
988 .iter()
989 .cloned()
990 .map(AltActionBinding::Named)
991 .chain(callbacks.iter().cloned().map(AltActionBinding::Context))
992 .chain(matched.iter().cloned().map(AltActionBinding::Matched))
993 .collect();
994 }
995}
996
997fn add_alt_named(
998 named: &mut Vec<String>,
999 callbacks: &mut Vec<ContextAction>,
1000 matched: &mut Vec<AltAction>,
1001 order: &mut Vec<AltActionBinding>,
1002 action: String,
1003 prepend: bool,
1004) {
1005 prepare_alt_order(named, callbacks, matched, order);
1006 if prepend {
1007 named.insert(0, action.clone());
1008 order.insert(0, AltActionBinding::Named(action));
1009 } else {
1010 named.push(action.clone());
1011 order.push(AltActionBinding::Named(action));
1012 }
1013}
1014
1015pub(crate) fn resolved_alt_action_order(
1016 named: &[String],
1017 callbacks: &[ContextAction],
1018 matched: &[AltAction],
1019 order: &[AltActionBinding],
1020) -> Vec<AltActionBinding> {
1021 if alt_order_matches(named, callbacks, matched, order) {
1022 order.to_vec()
1023 } else {
1024 named
1025 .iter()
1026 .cloned()
1027 .map(AltActionBinding::Named)
1028 .chain(callbacks.iter().cloned().map(AltActionBinding::Context))
1029 .chain(matched.iter().cloned().map(AltActionBinding::Matched))
1030 .collect()
1031 }
1032}
1033
1034fn order_matches(
1035 named: &[String],
1036 callbacks: &[ContextAction],
1037 states: &[StateAction],
1038 order: &[ActionBinding],
1039) -> bool {
1040 if order.len() != named.len() + callbacks.len() + states.len() {
1041 return false;
1042 }
1043 let (mut next_name, mut next_callback, mut next_state) = (0, 0, 0);
1045 for binding in order {
1046 match binding {
1047 ActionBinding::Named(name) => {
1048 if named.get(next_name) != Some(name) {
1049 return false;
1050 }
1051 next_name += 1;
1052 }
1053 ActionBinding::Callback(callback) => {
1054 if !callbacks
1055 .get(next_callback)
1056 .is_some_and(|expected| Arc::ptr_eq(expected, callback))
1057 {
1058 return false;
1059 }
1060 next_callback += 1;
1061 }
1062 ActionBinding::State(callback) => {
1063 if !states
1064 .get(next_state)
1065 .is_some_and(|expected| Arc::ptr_eq(expected, callback))
1066 {
1067 return false;
1068 }
1069 next_state += 1;
1070 }
1071 }
1072 }
1073 next_name == named.len() && next_callback == callbacks.len() && next_state == states.len()
1074}
1075
1076fn prepare_order(
1077 named: &[String],
1078 callbacks: &[ContextAction],
1079 states: &[StateAction],
1080 order: &mut Vec<ActionBinding>,
1081) {
1082 if !order_matches(named, callbacks, states, order) {
1083 *order = named
1084 .iter()
1085 .cloned()
1086 .map(ActionBinding::Named)
1087 .chain(callbacks.iter().cloned().map(ActionBinding::Callback))
1088 .chain(states.iter().cloned().map(ActionBinding::State))
1089 .collect();
1090 }
1091}
1092
1093fn add_named(
1094 named: &mut Vec<String>,
1095 callbacks: &mut Vec<ContextAction>,
1096 states: &mut Vec<StateAction>,
1097 order: &mut Vec<ActionBinding>,
1098 action: String,
1099 prepend: bool,
1100) {
1101 prepare_order(named, callbacks, states, order);
1102 if prepend {
1103 named.insert(0, action.clone());
1104 order.insert(0, ActionBinding::Named(action));
1105 } else {
1106 named.push(action.clone());
1107 order.push(ActionBinding::Named(action));
1108 }
1109}
1110
1111pub(crate) fn resolved_action_order(
1112 named: &[String],
1113 callbacks: &[ContextAction],
1114 states: &[StateAction],
1115 order: &[ActionBinding],
1116) -> Vec<ActionBinding> {
1117 if order_matches(named, callbacks, states, order) {
1118 order.to_vec()
1119 } else {
1120 named
1121 .iter()
1122 .cloned()
1123 .map(ActionBinding::Named)
1124 .chain(callbacks.iter().cloned().map(ActionBinding::Callback))
1125 .chain(states.iter().cloned().map(ActionBinding::State))
1126 .collect()
1127 }
1128}
1129
1130#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
1138pub struct RuleName(Arc<str>);
1139
1140impl RuleName {
1141 pub fn as_str(&self) -> &str {
1142 &self.0
1143 }
1144}
1145
1146impl std::ops::Deref for RuleName {
1147 type Target = str;
1148
1149 fn deref(&self) -> &str {
1150 &self.0
1151 }
1152}
1153
1154impl AsRef<str> for RuleName {
1155 fn as_ref(&self) -> &str {
1156 &self.0
1157 }
1158}
1159
1160impl std::borrow::Borrow<str> for RuleName {
1163 fn borrow(&self) -> &str {
1164 &self.0
1165 }
1166}
1167
1168impl fmt::Debug for RuleName {
1171 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1172 fmt::Debug::fmt(&*self.0, f)
1173 }
1174}
1175
1176impl fmt::Display for RuleName {
1177 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1178 f.write_str(&self.0)
1179 }
1180}
1181
1182impl PartialEq<str> for RuleName {
1183 fn eq(&self, other: &str) -> bool {
1184 &*self.0 == other
1185 }
1186}
1187
1188impl PartialEq<&str> for RuleName {
1189 fn eq(&self, other: &&str) -> bool {
1190 &*self.0 == *other
1191 }
1192}
1193
1194impl PartialEq<String> for RuleName {
1195 fn eq(&self, other: &String) -> bool {
1196 &*self.0 == other.as_str()
1197 }
1198}
1199
1200impl PartialEq<RuleName> for str {
1201 fn eq(&self, other: &RuleName) -> bool {
1202 self == &*other.0
1203 }
1204}
1205
1206impl PartialEq<RuleName> for &str {
1207 fn eq(&self, other: &RuleName) -> bool {
1208 *self == &*other.0
1209 }
1210}
1211
1212impl PartialEq<RuleName> for String {
1213 fn eq(&self, other: &RuleName) -> bool {
1214 self.as_str() == &*other.0
1215 }
1216}
1217
1218impl From<&str> for RuleName {
1219 fn from(name: &str) -> Self {
1220 RuleName(Arc::from(name))
1221 }
1222}
1223
1224impl From<String> for RuleName {
1225 fn from(name: String) -> Self {
1226 RuleName(Arc::from(name.as_str()))
1227 }
1228}
1229
1230impl From<&String> for RuleName {
1231 fn from(name: &String) -> Self {
1232 RuleName(Arc::from(name.as_str()))
1233 }
1234}
1235
1236impl From<Arc<str>> for RuleName {
1237 fn from(name: Arc<str>) -> Self {
1238 RuleName(name)
1239 }
1240}
1241
1242impl From<RuleName> for String {
1243 fn from(name: RuleName) -> Self {
1244 name.0.to_string()
1245 }
1246}
1247
1248#[derive(Clone)]
1259pub struct Rule {
1260 shared: Rc<RuleSnapshot>,
1261 pub parent_node: Option<Rc<RefCell<Value>>>,
1263 pub child_node: Value,
1274 pub(crate) skip_befores: bool,
1275 pub(crate) child_node_is_self: bool,
1276 pub(crate) child_link_open: bool,
1303 pub(crate) child_cell: Option<Rc<RefCell<Value>>>,
1304 pub(crate) child_record: Option<Rc<RuleSnapshot>>,
1312 pub(crate) slot: usize,
1324}
1325
1326impl Drop for RuleSnapshot {
1327 fn drop(&mut self) {
1353 if self.parent_rule.is_none()
1357 && self.child_rule.is_none()
1358 && self.prev_rule.is_none()
1359 && self.next_rule.is_none()
1360 {
1361 return;
1362 }
1363
1364 fn unlink(
1371 snapshot: &mut RuleSnapshot,
1372 cursor: &mut Option<Rc<RuleSnapshot>>,
1373 pending: &mut Vec<Rc<RuleSnapshot>>,
1374 ) {
1375 let mut hold = |held: Option<Rc<RuleSnapshot>>| {
1376 if let Some(held) = held {
1377 if cursor.is_none() {
1378 *cursor = Some(held);
1379 } else {
1380 pending.push(held);
1381 }
1382 }
1383 };
1384 hold(snapshot.parent_rule.take());
1385 hold(snapshot.child_rule.take());
1386 hold(snapshot.prev_rule.take());
1387 hold(snapshot.next_rule.take());
1388 }
1389
1390 let mut cursor: Option<Rc<RuleSnapshot>> = None;
1391 let mut pending: Vec<Rc<RuleSnapshot>> = Vec::new();
1392 unlink(self, &mut cursor, &mut pending);
1393 while let Some(mut link) = cursor.take().or_else(|| pending.pop()) {
1394 if Rc::weak_count(&link) == 0 {
1395 if let Some(owned) = Rc::get_mut(&mut link) {
1400 unlink(owned, &mut cursor, &mut pending);
1401 }
1402 } else if let Ok(mut owned) = Rc::try_unwrap(link) {
1403 unlink(&mut owned, &mut cursor, &mut pending);
1411 }
1412 }
1415 }
1416}
1417
1418impl std::ops::Deref for Rule {
1419 type Target = RuleSnapshot;
1420
1421 fn deref(&self) -> &RuleSnapshot {
1422 &self.shared
1423 }
1424}
1425
1426impl std::ops::DerefMut for Rule {
1442 fn deref_mut(&mut self) -> &mut RuleSnapshot {
1443 Rc::make_mut(&mut self.shared)
1444 }
1445}
1446
1447#[derive(Debug, Clone)]
1448pub struct RuleSnapshot {
1449 pub i: usize,
1450 pub d: usize,
1451 pub name: RuleName,
1452 pub spec: Arc<RuleSpec>,
1453 pub state: RuleState,
1454 pub bo: bool,
1455 pub ao: bool,
1456 pub bc: bool,
1457 pub ac: bool,
1458 pub need: i32,
1459 pub node: Rc<RefCell<Value>>,
1460 pub parent_rule: Option<Rc<RuleSnapshot>>,
1461 pub child_rule: Option<Rc<RuleSnapshot>>,
1462 pub prev_rule: Option<Rc<RuleSnapshot>>,
1463 pub next_rule: Option<Rc<RuleSnapshot>>,
1464 pub next_rule_name: Option<RuleName>,
1465 pub n: Rc<HashMap<String, i32>>,
1466 pub u: Rc<HashMap<String, Value>>,
1467 pub k: Rc<HashMap<String, Value>>,
1468 pub o: Rc<Vec<Token>>,
1472 pub c: Rc<Vec<Token>>,
1473}
1474
1475fn empty_counters() -> Rc<HashMap<String, i32>> {
1479 thread_local! {
1480 static EMPTY: Rc<HashMap<String, i32>> = Rc::new(HashMap::new());
1481 }
1482 EMPTY.with(Rc::clone)
1483}
1484
1485fn empty_values() -> Rc<HashMap<String, Value>> {
1486 thread_local! {
1487 static EMPTY: Rc<HashMap<String, Value>> = Rc::new(HashMap::new());
1488 }
1489 EMPTY.with(Rc::clone)
1490}
1491
1492fn empty_tokens() -> Rc<Vec<Token>> {
1496 thread_local! {
1497 static EMPTY: Rc<Vec<Token>> = Rc::new(Vec::new());
1498 }
1499 EMPTY.with(Rc::clone)
1500}
1501
1502impl Rule {
1503 pub fn n_mut(&mut self) -> &mut HashMap<String, i32> {
1506 Rc::make_mut(&mut self.n)
1507 }
1508
1509 pub fn u_mut(&mut self) -> &mut HashMap<String, Value> {
1510 Rc::make_mut(&mut self.u)
1511 }
1512
1513 pub fn k_mut(&mut self) -> &mut HashMap<String, Value> {
1514 Rc::make_mut(&mut self.k)
1515 }
1516
1517 pub fn new(name: impl Into<RuleName>, initial_node: Value) -> Self {
1518 let name: RuleName = name.into();
1519 let spec = Arc::new(RuleSpec::new(name.as_str()));
1520 Rule {
1521 shared: Rc::new(RuleSnapshot {
1522 i: 0,
1523 d: 0,
1524 name,
1525 spec,
1526 state: RuleState::Open,
1527 bo: true,
1528 ao: true,
1529 bc: true,
1530 ac: true,
1531 need: 0,
1532 node: Rc::new(RefCell::new(initial_node)),
1533 parent_rule: None,
1534 child_rule: None,
1535 prev_rule: None,
1536 next_rule: None,
1537 next_rule_name: None,
1538 n: empty_counters(),
1539 u: empty_values(),
1540 k: empty_values(),
1541 o: empty_tokens(),
1542 c: empty_tokens(),
1543 }),
1544 parent_node: None,
1545 child_node: Value::Undefined,
1546 skip_befores: false,
1547 child_node_is_self: false,
1548 child_link_open: false,
1549 child_cell: None,
1550 child_record: None,
1551 slot: usize::MAX,
1552 }
1553 }
1554
1555 pub fn with_shared_node(name: impl Into<RuleName>, node: Rc<RefCell<Value>>) -> Self {
1556 Self::bound(name.into(), node, None, usize::MAX)
1557 }
1558
1559 pub(crate) fn bound(
1569 name: RuleName,
1570 node: Rc<RefCell<Value>>,
1571 installed: Option<&Arc<RuleSpec>>,
1572 slot: usize,
1573 ) -> Self {
1574 let spec = match installed {
1575 Some(spec) => Arc::clone(spec),
1576 None => Arc::new(RuleSpec::new(name.as_str())),
1577 };
1578 Rule {
1579 shared: Rc::new(RuleSnapshot {
1580 i: 0,
1581 d: 0,
1582 name,
1583 spec,
1584 state: RuleState::Open,
1585 bo: true,
1586 ao: true,
1587 bc: true,
1588 ac: true,
1589 need: 0,
1590 node,
1591 parent_rule: None,
1592 child_rule: None,
1593 prev_rule: None,
1594 next_rule: None,
1595 next_rule_name: None,
1596 n: empty_counters(),
1597 u: empty_values(),
1598 k: empty_values(),
1599 o: empty_tokens(),
1600 c: empty_tokens(),
1601 }),
1602 parent_node: None,
1603 child_node: Value::Undefined,
1604 skip_befores: false,
1605 child_node_is_self: false,
1606 child_link_open: false,
1607 child_cell: None,
1608 child_record: None,
1609 slot,
1610 }
1611 }
1612
1613 pub(crate) fn bind_spec(&mut self, spec: &Arc<RuleSpec>, name: RuleName, slot: usize) {
1619 self.name = name;
1620 self.spec = Arc::clone(spec);
1621 self.slot = slot;
1622 self.bo = true;
1625 self.ao = true;
1626 self.bc = true;
1627 self.ac = true;
1628 }
1629
1630 pub fn o0(&self) -> Option<&Token> {
1631 self.o.first()
1632 }
1633
1634 pub fn o1(&self) -> Option<&Token> {
1635 self.o.get(1)
1636 }
1637
1638 pub fn c0(&self) -> Option<&Token> {
1639 self.c.first()
1640 }
1641
1642 pub fn c1(&self) -> Option<&Token> {
1643 self.c.get(1)
1644 }
1645
1646 pub fn os(&self) -> usize {
1647 self.o.len()
1648 }
1649
1650 pub fn cs(&self) -> usize {
1651 self.c.len()
1652 }
1653
1654 pub fn resolve_open_value(&mut self, index: usize, context: &mut Context) -> Value {
1657 self.o
1658 .get(index)
1659 .cloned()
1660 .map_or(Value::Undefined, |token| token.resolve_val(self, context))
1661 }
1662
1663 pub fn resolve_close_value(&mut self, index: usize, context: &mut Context) -> Value {
1665 self.c
1666 .get(index)
1667 .cloned()
1668 .map_or(Value::Undefined, |token| token.resolve_val(self, context))
1669 }
1670
1671 pub fn eq(&self, counter: &str, limit: i32) -> bool {
1674 self.n.get(counter).copied().unwrap_or(0) == limit
1675 }
1676
1677 pub fn lt(&self, counter: &str, limit: i32) -> bool {
1678 self.n.get(counter).copied().unwrap_or(0) < limit
1679 }
1680
1681 pub fn gt(&self, counter: &str, limit: i32) -> bool {
1682 self.n.get(counter).copied().unwrap_or(0) > limit
1683 }
1684
1685 pub fn lte(&self, counter: &str, limit: i32) -> bool {
1686 self.n.get(counter).copied().unwrap_or(0) <= limit
1687 }
1688
1689 pub fn gte(&self, counter: &str, limit: i32) -> bool {
1690 self.n.get(counter).copied().unwrap_or(0) >= limit
1691 }
1692
1693 pub fn exist(&self, counter: &str) -> bool {
1694 self.n.contains_key(counter)
1695 }
1696
1697 pub fn snapshot(&self) -> Rc<RuleSnapshot> {
1700 Rc::clone(&self.shared)
1701 }
1702
1703 pub(crate) fn note_child_push(&mut self, child: &Rule) {
1706 self.child_link_open = true;
1707 self.child_cell = Some(Rc::clone(&child.node));
1708 self.child_record = Some(child.snapshot());
1709 }
1710
1711 pub(crate) fn relink_child(&mut self, child: &Rule) {
1722 let snapshot = child.snapshot();
1723 self.child_record = Some(Rc::clone(&snapshot));
1724 self.next_rule = Some(Rc::clone(&snapshot));
1725 self.child_rule = Some(snapshot);
1726 }
1727
1728 pub(crate) fn links_unchanged(&self, linked: &Option<Rc<RuleSnapshot>>) -> bool {
1734 let same = |link: &Option<Rc<RuleSnapshot>>| match (link, linked) {
1735 (Some(now), Some(made)) => Rc::ptr_eq(now, made),
1736 _ => false,
1737 };
1738 same(&self.child_rule) && same(&self.next_rule)
1739 }
1740
1741 pub(crate) fn freeze_child(&mut self, child: &Rule) {
1758 if self.child_link_open {
1759 self.child_cell = Some(Rc::clone(&child.node));
1760 self.child_record = Some(child.snapshot());
1761 self.child_link_open = false;
1762 }
1763 }
1764
1765 pub(crate) fn accept_child_node(&mut self, child: &Rule) {
1766 self.freeze_child(child);
1767 let cell = self
1768 .child_cell
1769 .clone()
1770 .unwrap_or_else(|| Rc::clone(&child.node));
1771 self.child_node_is_self = Rc::ptr_eq(&self.node, &cell);
1772 self.child_node = if self.child_node_is_self {
1784 Value::Undefined
1785 } else {
1786 cell.borrow().clone()
1787 };
1788 }
1789
1790 pub fn child_value(&self) -> Value {
1797 if self.child_node_is_self {
1798 self.node.borrow().clone()
1799 } else {
1800 self.child_node.clone()
1801 }
1802 }
1803
1804 pub fn has_child_value(&self) -> bool {
1806 if self.child_node_is_self {
1807 !self.node.borrow().is_undefined()
1808 } else {
1809 !self.child_node.is_undefined()
1810 }
1811 }
1812
1813 pub(crate) fn accept_child(&mut self, child: &Rule) {
1826 self.accept_child_node(child);
1827 self.child_rule = self.child_record.clone().or_else(|| Some(child.snapshot()));
1833 self.next_rule = self.child_rule.clone();
1834 }
1835
1836 pub(crate) fn park_child_node(&mut self) {
1872 if self.child_node_is_self {
1873 self.child_node = Value::Undefined;
1874 }
1875 }
1876}
1877
1878impl fmt::Display for Rule {
1879 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1880 write!(formatter, "[Rule {}~{}]", self.name, self.i)
1881 }
1882}
1883
1884#[cfg(test)]
1885mod tests {
1886 use super::*;
1887
1888 fn cell(text: &str) -> Rc<RefCell<Value>> {
1889 Rc::new(RefCell::new(Value::String(text.into())))
1890 }
1891
1892 #[test]
1900 fn freeze_child_takes_the_cell_the_child_ended_on() {
1901 let mut parent = Rule::new("parent", Value::Undefined);
1902 let mut child = Rule::new("child", Value::Undefined);
1903 parent.note_child_push(&child);
1904
1905 child.node = cell("done");
1906 parent.freeze_child(&child);
1907
1908 assert_eq!(
1909 *parent
1910 .child_cell
1911 .as_ref()
1912 .expect("a frozen child cell")
1913 .borrow(),
1914 Value::String("done".into()),
1915 "the freeze kept the push-time cell instead of the one the child ended on"
1916 );
1917 }
1918
1919 #[test]
1925 fn freeze_child_ignores_every_link_after_the_first() {
1926 let mut parent = Rule::new("parent", Value::Undefined);
1927 let mut pushed = Rule::new("pushed", Value::Undefined);
1928 parent.note_child_push(&pushed);
1929
1930 pushed.node = cell("pushed rule");
1931 parent.freeze_child(&pushed);
1932
1933 let mut tail = Rule::new("tail", Value::Undefined);
1936 tail.node = cell("chain tail");
1937 parent.freeze_child(&tail);
1938
1939 assert_eq!(
1940 *parent
1941 .child_cell
1942 .as_ref()
1943 .expect("a frozen child cell")
1944 .borrow(),
1945 Value::String("pushed rule".into()),
1946 "a later link of the replacement chain overwrote the child link"
1947 );
1948 }
1949
1950 #[test]
1961 fn a_callback_replacing_the_whole_child_rule_does_not_break_the_link() {
1962 let mut parent = Rule::new("parent", Value::Undefined);
1963 let mut child = Rule::new("child", Value::Undefined);
1964 parent.note_child_push(&child);
1965
1966 child = Rule::new("child", Value::Undefined);
1968 child.node = cell("done");
1969
1970 parent.freeze_child(&child);
1971
1972 assert_eq!(
1973 *parent
1974 .child_cell
1975 .as_ref()
1976 .expect("a frozen child cell")
1977 .borrow(),
1978 Value::String("done".into()),
1979 "replacing the rule value cost the parent its child link"
1980 );
1981 }
1982}